* fix(ui): open app deep links from chat after confirmation DOMPurify's default URI policy stripped href from anchors with custom application schemes (obsidian://, vscode://, ...), so every app link rendered in chat was dead across web, desktop, VS Code, and mobile. - Classify safe app-link schemes in lib/url.ts (browser-handled, scriptable, webview-internal, network, and self-deep-link schemes stay excluded) and let openExternalUrl accept them - Keep app-link hrefs through the markdown sanitize hook - Intercept app-link clicks in the markdown renderer and route them through a confirmation dialog (Trust and open / Open once, dismiss to cancel) mounted in the desktop/web app root and the mobile shell - Persist per-device trusted schemes in a zustand store; trusted schemes open without asking again * feat(settings): manage trusted app link schemes in General Add an App links section to Settings > General listing the application schemes trusted on this device with a delete action; removing a scheme restores the confirmation dialog for it. Register the section in settings search. * fix(ui): enforce app link confirmation * fix(ui): handle app links by runtime * fix(vscode): keep app links unsupported * fix(settings): clarify trusted app links --------- Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
import { isAppLinkUrl, isExternalHttpUrl } from '@/lib/url';
|
|
|
|
type AppLinkInteractionOptions = {
|
|
allowExternalHttp: boolean;
|
|
openAppLink: (url: string) => void;
|
|
openExternalHttp: (url: string) => void;
|
|
};
|
|
|
|
type LinkInteractionContainer = {
|
|
addEventListener: (type: string, listener: (event: MouseEvent) => void) => void;
|
|
removeEventListener: (type: string, listener: (event: MouseEvent) => void) => void;
|
|
};
|
|
|
|
const findLink = (event: MouseEvent | DragEvent): HTMLAnchorElement | null => {
|
|
const target = event.target;
|
|
if (!(target instanceof Element)) return null;
|
|
const anchor = target.closest('a[href]');
|
|
if (!(anchor instanceof HTMLAnchorElement)) return null;
|
|
if (anchor.getAttribute('data-openchamber-file-link') === 'true') return null;
|
|
return anchor;
|
|
};
|
|
|
|
const interceptAppLink = (
|
|
event: MouseEvent | DragEvent,
|
|
openAppLink?: (url: string) => void,
|
|
): boolean => {
|
|
if (event.defaultPrevented) return false;
|
|
const anchor = findLink(event);
|
|
const href = anchor?.getAttribute('href') ?? '';
|
|
if (!isAppLinkUrl(href)) return false;
|
|
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
openAppLink?.(href);
|
|
return true;
|
|
};
|
|
|
|
const isPlainPrimaryClick = (event: MouseEvent): boolean => (
|
|
event.button === 0
|
|
&& !event.metaKey
|
|
&& !event.ctrlKey
|
|
&& !event.altKey
|
|
&& !event.shiftKey
|
|
);
|
|
|
|
export const attachAppLinkInteractions = (
|
|
container: LinkInteractionContainer,
|
|
options: AppLinkInteractionOptions,
|
|
): (() => void) => {
|
|
const handleClick = (event: MouseEvent) => {
|
|
if (interceptAppLink(event, options.openAppLink)) return;
|
|
if (!options.allowExternalHttp || event.defaultPrevented || !isPlainPrimaryClick(event)) return;
|
|
|
|
const href = findLink(event)?.getAttribute('href') ?? '';
|
|
if (!isExternalHttpUrl(href)) return;
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
options.openExternalHttp(href);
|
|
};
|
|
const handleAuxClick = (event: MouseEvent) => {
|
|
if (event.button === 1) interceptAppLink(event, options.openAppLink);
|
|
};
|
|
const blockAlternateAppLinkActivation = (event: MouseEvent | DragEvent) => {
|
|
interceptAppLink(event);
|
|
};
|
|
|
|
container.addEventListener('click', handleClick);
|
|
container.addEventListener('auxclick', handleAuxClick);
|
|
container.addEventListener('dragstart', blockAlternateAppLinkActivation);
|
|
return () => {
|
|
container.removeEventListener('click', handleClick);
|
|
container.removeEventListener('auxclick', handleAuxClick);
|
|
container.removeEventListener('dragstart', blockAlternateAppLinkActivation);
|
|
};
|
|
};
|