// "Links to site pages" setting: when it is not "same", a plain click on one of the site's own pages // (a post, a profile, a channel, ...) opens that page beside the pane, in a new tab or in the system // browser instead of replacing what you are reading. The click becomes an ordinary new-window // request; native code (`new_window_mode`) decides where it opens. Which clicks count is the site // adapter's `clickedUrl`; everything else (navigation, tabs, menus, buttons) is never touched. export type LinkMode = 'same' | 'pane' | 'tab' | 'browser'; export type LinkRoutingHandle = { configure(value: unknown): void; dispose(): void }; export type LinkSite = { /** The site page a click on `target` would open in place, when it is one to divert; else null. */ clickedUrl(target: Element | null, location: Location): string | null; /** The key of the mode in the native `configure` payload (`{ [configKey]: mode }`). */ configKey: string; }; export const LINK_MODES: readonly LinkMode[] = ['same', 'pane', 'tab', 'browser']; export function installLinkRouting(win: Window, site: LinkSite): LinkRoutingHandle { let mode: LinkMode = 'same'; const onClick = (event: MouseEvent) => { if (mode === 'same' || event.button !== 0 || event.defaultPrevented) return; // Modified clicks keep the site's own meaning (Ctrl/Cmd+click already opens a new tab). if (event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return; if (win.getSelection?.()?.toString().trim()) return; const url = site.clickedUrl(event.target instanceof Element ? event.target : null, win.location); if (!url) return; // Before the site's own handlers (frameworks listen below window): the in-place navigation never starts. event.preventDefault(); event.stopPropagation(); win.open(url, '_blank', 'noopener'); }; win.addEventListener('click', onClick, true); return { configure(value) { const next = (value as Record | null)?.[site.configKey]; if (typeof next === 'string' && (LINK_MODES as readonly string[]).includes(next)) mode = next as LinkMode; }, dispose() { win.removeEventListener('click', onClick, true); }, }; }