waybetter/packages/runtime/src/linkRouting.ts
CRT f19033bcc1 Extract the wayBetter framework core from wayBetter for X
Site-agnostic parts of the X app, behind a small site adapter:

- crates/waybetter-host: webview-label security policy and tab rect
  clamp, account profiles with native-owned accounts (AccountRules),
  plugin store with bundled-plugin upgrades (PluginHost), signed
  data-only hotfix channel, update scheduling and crash handshake, tab
  payloads/link routing/tab-chrome validation, and (feature "tauri")
  webview helpers and the Windows system menu.
- packages/runtime: DOM hub, request hub, plugin API core, OKLab theme
  color model, split-view rail hiding, link routing and account
  reporting, each configured by a SiteAdapter.
- packages/shell: address bar, pane, link-drop, profile, palette and
  update-chip models, and window controls with the Tauri API injected.

Tests moved/adapted from the app: 45 Rust (headless), 40 TypeScript.
2026-09-27 20:01:33 +00:00

41 lines
2.2 KiB
TypeScript

// "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<string, unknown> | null)?.[site.configKey];
if (typeof next === 'string' && (LINK_MODES as readonly string[]).includes(next)) mode = next as LinkMode;
},
dispose() { win.removeEventListener('click', onClick, true); },
};
}