lib/irc: add formatURL

This commit is contained in:
Simon Ser 2023-04-19 11:43:45 +02:00
parent 57809be989
commit 57f64e9cc2
4 changed files with 18 additions and 16 deletions

View file

@ -2,7 +2,7 @@ import { html, Component } from "../lib/index.js";
import linkify from "../lib/linkify.js"; import linkify from "../lib/linkify.js";
import * as irc from "../lib/irc.js"; import * as irc from "../lib/irc.js";
import { strip as stripANSI } from "../lib/ansi.js"; import { strip as stripANSI } from "../lib/ansi.js";
import { BufferType, ServerStatus, BufferEventsDisplayMode, getNickURL, getChannelURL, getMessageURL, isMessageBeforeReceipt, SettingsContext } from "../state.js"; import { BufferType, ServerStatus, BufferEventsDisplayMode, getMessageURL, isMessageBeforeReceipt, SettingsContext } from "../state.js";
import * as store from "../store.js"; import * as store from "../store.js";
import Membership from "./membership.js"; import Membership from "./membership.js";
@ -23,7 +23,7 @@ function Nick(props) {
let colorIndex = djb2(props.nick) % 16 + 1; let colorIndex = djb2(props.nick) % 16 + 1;
return html` return html`
<a href=${getNickURL(props.nick)} class="nick nick-${colorIndex}" onClick=${handleClick}>${props.nick}</a> <a href=${irc.formatURL({ entity: props.nick })} class="nick nick-${colorIndex}" onClick=${handleClick}>${props.nick}</a>
`; `;
} }
@ -103,7 +103,7 @@ class LogLine extends Component {
} }
function createChannel(channel) { function createChannel(channel) {
return html` return html`
<a href=${getChannelURL(channel)} onClick=${onChannelClick}> <a href=${irc.formatURL({ entity: channel })} onClick=${onChannelClick}>
${channel} ${channel}
</a> </a>
`; `;

View file

@ -1,5 +1,4 @@
import { html, Component } from "../lib/index.js"; import { html, Component } from "../lib/index.js";
import { getNickURL } from "../state.js";
import { strip as stripANSI } from "../lib/ansi.js"; import { strip as stripANSI } from "../lib/ansi.js";
import Membership from "./membership.js"; import Membership from "./membership.js";
import * as irc from "../lib/irc.js"; import * as irc from "../lib/irc.js";
@ -73,7 +72,7 @@ class MemberItem extends Component {
return html` return html`
<li> <li>
<a <a
href=${getNickURL(this.props.nick)} href=${irc.formatURL({ entity: this.props.nick, enttype: "user" })}
class=${classes.join(" ")} class=${classes.join(" ")}
title=${title} title=${title}
onClick=${this.handleClick} onClick=${this.handleClick}

View file

@ -839,6 +839,17 @@ export function parseURL(str) {
return { host, enttype, entity }; return { host, enttype, entity };
} }
export function formatURL({ host, enttype, entity } = {}) {
host = host || "";
entity = entity || "";
let s = "irc://" + host + "/" + encodeURIComponent(entity);
if (enttype) {
s += ",is" + enttype;
}
return s;
}
export class CapRegistry { export class CapRegistry {
available = new Map(); available = new Map();
enabled = new Set(); enabled = new Set();

View file

@ -43,22 +43,14 @@ export const BufferEventsDisplayMode = {
export const SettingsContext = createContext("settings"); export const SettingsContext = createContext("settings");
export function getNickURL(nick) {
return "irc:///" + encodeURIComponent(nick) + ",isuser";
}
export function getChannelURL(channel) {
return "irc:///" + encodeURIComponent(channel);
}
export function getBufferURL(buf) { export function getBufferURL(buf) {
switch (buf.type) { switch (buf.type) {
case BufferType.SERVER: case BufferType.SERVER:
return "irc:///"; return irc.formatURL();
case BufferType.CHANNEL: case BufferType.CHANNEL:
return getChannelURL(buf.name); return irc.formatURL({ entity: buf.name });
case BufferType.NICK: case BufferType.NICK:
return getNickURL(buf.name); return irc.formatURL({ entity: buf.name, enttype: "user" });
} }
throw new Error("Unknown buffer type: " + buf.type); throw new Error("Unknown buffer type: " + buf.type);
} }