From f35ebeab6799b2e3a224cc499e36aea8380b84e0 Mon Sep 17 00:00:00 2001 From: brecert Date: Tue, 7 Sep 2021 09:26:12 -0400 Subject: [PATCH] Add `shift+click` to channel links --- src/components/markdown/Renderer.tsx | 49 +++++++++++++++++++++++----- src/lib/links.ts | 9 ++++- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/components/markdown/Renderer.tsx b/src/components/markdown/Renderer.tsx index 01a850e2..c845390c 100644 --- a/src/components/markdown/Renderer.tsx +++ b/src/components/markdown/Renderer.tsx @@ -140,16 +140,33 @@ export default function Renderer({ content, disallowBigEmoji }: MarkdownProps) { (ev: MouseEvent) => { if (ev.currentTarget) { const element = ev.currentTarget as HTMLAnchorElement; - if (element.dataset.type === 'mention' && ev.shiftKey) { - internalEmit( - "MessageBox", - "append", - `<@${element.dataset.mentionId}>`, - "mention", - ); - ev.preventDefault() - } else if (openLink(element.href)) { + if (ev.shiftKey) { + switch (element.dataset.type) { + case "mention": { + internalEmit( + "MessageBox", + "append", + `<@${element.dataset.mentionId}>`, + "mention", + ); + ev.preventDefault() + return + } + case "channel_mention": { + internalEmit( + "MessageBox", + "append", + `<#${element.dataset.mentionId}>`, + "channel_mention", + ); + ev.preventDefault() + return + } + } + } + + if (openLink(element.href)) { ev.preventDefault(); } } @@ -177,6 +194,7 @@ export default function Renderer({ content, disallowBigEmoji }: MarkdownProps) { element.removeAttribute("target"); const link = determineLink(element.href); + console.log(link) switch (link.type) { case "profile": { element.setAttribute( @@ -189,6 +207,19 @@ export default function Renderer({ content, disallowBigEmoji }: MarkdownProps) { ) break; } + case "navigate": { + if (link.navigation_type === 'channel') { + element.setAttribute( + "data-type", + "channel_mention", + ); + element.setAttribute( + "data-mention-id", + link.channel_id + ) + } + break; + } case "external": { element.setAttribute("target", "_blank"); break; diff --git a/src/lib/links.ts b/src/lib/links.ts index d915a43c..c41b5bf5 100644 --- a/src/lib/links.ts +++ b/src/lib/links.ts @@ -1,6 +1,7 @@ type LinkType = | { type: "profile"; id: string } - | { type: "navigate"; path: string } + | { type: "navigate"; path: string; navigation_type?: null } + | { type: "navigate"; path: string; navigation_type: 'channel'; channel_id: string } | { type: "external"; href: string; url: URL } | { type: "none" }; @@ -11,6 +12,8 @@ const ALLOWED_ORIGINS = [ "local.revolt.chat", ]; +const CHANNEL_PATH_RE = /^\/server\/[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}\/channel\/[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$/ + export function determineLink(href?: string): LinkType { let internal, url: URL | null = null; @@ -27,6 +30,10 @@ export function determineLink(href?: string): LinkType { return { type: "profile", id }; } } else { + console.log(path) + if(CHANNEL_PATH_RE.test(path)) { + return { type: 'navigate', path, navigation_type: 'channel', channel_id: path.slice(43) } + } return { type: "navigate", path }; }