feat(webui): add adaptive pointer interactions
Introduce a CSS-driven adaptive cursor that morphs for controls, text editing, dragging, resizing, busy states, and other native pointer semantics on desktop devices. Add a themed context menu for editing, selections, links, images, and page navigation while preserving explicit native-menu escape paths. Store a default-on interaction preference through the normalized appearance model and cover its migration and opt-out behavior.
This commit is contained in:
9 files changed
+1339
-1
No files matched your search
@@ -0,0 +1,304 @@
|
||||
import * as React from 'react';
|
||||
import {
|
||||
motion,
|
||||
useMotionValue,
|
||||
useReducedMotion,
|
||||
useSpring,
|
||||
} from 'motion/react';
|
||||
import { useTheme } from '@/contexts/ThemeContext';
|
||||
|
||||
const POINTER_MEDIA = '(hover: hover) and (pointer: fine)';
|
||||
const MODE_PROPERTY = '--snowluma-cursor-mode';
|
||||
const RESIZE_MODE_PROPERTY = '--snowluma-cursor-resize-mode';
|
||||
const POINTER_SPRING = { stiffness: 1000, damping: 50 } as const;
|
||||
|
||||
const CURSOR_MODES = [
|
||||
'default',
|
||||
'target',
|
||||
'text',
|
||||
'grab',
|
||||
'grabbing',
|
||||
'move',
|
||||
'ew-resize',
|
||||
'ns-resize',
|
||||
'nwse-resize',
|
||||
'nesw-resize',
|
||||
'not-allowed',
|
||||
'wait',
|
||||
'crosshair',
|
||||
'copy',
|
||||
'zoom-in',
|
||||
'zoom-out',
|
||||
'help',
|
||||
'hidden',
|
||||
] as const;
|
||||
|
||||
type CursorMode = (typeof CURSOR_MODES)[number];
|
||||
|
||||
interface CursorContext {
|
||||
mode: CursorMode;
|
||||
anchor: Element | null;
|
||||
tracksPointerRegion: boolean;
|
||||
}
|
||||
|
||||
function useMediaQuery(query: string): boolean {
|
||||
const [matches, setMatches] = React.useState(false);
|
||||
|
||||
React.useEffect(() => {
|
||||
const media = window.matchMedia(query);
|
||||
const update = () => setMatches(media.matches);
|
||||
update();
|
||||
media.addEventListener('change', update);
|
||||
return () => media.removeEventListener('change', update);
|
||||
}, [query]);
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
function readCursorMode(element: Element): CursorMode {
|
||||
const value = getComputedStyle(element).getPropertyValue(MODE_PROPERTY).trim();
|
||||
return (CURSOR_MODES as readonly string[]).includes(value)
|
||||
? value as CursorMode
|
||||
: 'default';
|
||||
}
|
||||
|
||||
/**
|
||||
* CSS owns the semantic mapping. JavaScript only reads the resolved custom
|
||||
* property and finds the outermost element that still belongs to that mode.
|
||||
* This keeps nested SVGs/spans inside a button attached to the whole button.
|
||||
*/
|
||||
function resolveCursorContext(
|
||||
target: EventTarget | null,
|
||||
clientX: number,
|
||||
clientY: number,
|
||||
): CursorContext {
|
||||
if (!(target instanceof Element)) {
|
||||
return { mode: 'default', anchor: null, tracksPointerRegion: false };
|
||||
}
|
||||
|
||||
const styles = getComputedStyle(target);
|
||||
const resizeModeValue = styles.getPropertyValue(RESIZE_MODE_PROPERTY).trim();
|
||||
const resizeMode = (CURSOR_MODES as readonly string[]).includes(resizeModeValue)
|
||||
? resizeModeValue as CursorMode
|
||||
: null;
|
||||
|
||||
if (resizeMode) {
|
||||
const rect = target.getBoundingClientRect();
|
||||
const inResizeCorner = clientX >= rect.right - 18
|
||||
&& clientX <= rect.right
|
||||
&& clientY >= rect.bottom - 18
|
||||
&& clientY <= rect.bottom;
|
||||
if (inResizeCorner) {
|
||||
return { mode: resizeMode, anchor: null, tracksPointerRegion: true };
|
||||
}
|
||||
}
|
||||
|
||||
const modeValue = styles.getPropertyValue(MODE_PROPERTY).trim();
|
||||
const mode = (CURSOR_MODES as readonly string[]).includes(modeValue)
|
||||
? modeValue as CursorMode
|
||||
: 'default';
|
||||
if (mode !== 'target') {
|
||||
return { mode, anchor: null, tracksPointerRegion: Boolean(resizeMode) };
|
||||
}
|
||||
|
||||
let anchor = target;
|
||||
let parent = target.parentElement;
|
||||
while (parent && readCursorMode(parent) === mode) {
|
||||
anchor = parent;
|
||||
parent = parent.parentElement;
|
||||
}
|
||||
return { mode, anchor, tracksPointerRegion: Boolean(resizeMode) };
|
||||
}
|
||||
|
||||
function clamp(value: number, min: number, max: number): number {
|
||||
return Math.min(max, Math.max(min, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Global adaptive cursor for fine-pointer desktop environments.
|
||||
*
|
||||
* All cursor semantics and visual variants live in index.css. The component
|
||||
* only tracks the pointer, reads the CSS-selected mode, and supplies target
|
||||
* geometry for Mikk Martin-style target morphing.
|
||||
*/
|
||||
export function AdaptivePointer() {
|
||||
const { appearance } = useTheme();
|
||||
const systemReducedMotion = useReducedMotion();
|
||||
const finePointer = useMediaQuery(POINTER_MEDIA);
|
||||
const enabled = appearance.customPointerSystem
|
||||
&& finePointer
|
||||
&& !appearance.reduceMotion
|
||||
&& !appearance.disableMotion
|
||||
&& !systemReducedMotion;
|
||||
|
||||
const cursorRef = React.useRef<HTMLDivElement>(null);
|
||||
const rawX = useMotionValue(-100);
|
||||
const rawY = useMotionValue(-100);
|
||||
const x = useSpring(rawX, POINTER_SPRING);
|
||||
const y = useSpring(rawY, POINTER_SPRING);
|
||||
|
||||
React.useEffect(() => {
|
||||
const cursor = cursorRef.current;
|
||||
if (!enabled || !cursor) return;
|
||||
|
||||
const root = document.documentElement;
|
||||
const lastPoint = { x: -100, y: -100 };
|
||||
let context: CursorContext = { mode: 'default', anchor: null, tracksPointerRegion: false };
|
||||
let lastEventTarget: EventTarget | null = null;
|
||||
let targetObserver: ResizeObserver | null = null;
|
||||
|
||||
root.dataset.cursorSystem = 'custom';
|
||||
cursor.dataset.visible = 'false';
|
||||
|
||||
const stopObservingTarget = () => {
|
||||
targetObserver?.disconnect();
|
||||
targetObserver = null;
|
||||
};
|
||||
|
||||
const setMode = (mode: CursorMode) => {
|
||||
cursor.dataset.mode = mode;
|
||||
if (mode !== 'target') {
|
||||
cursor.style.removeProperty('--cursor-target-width');
|
||||
cursor.style.removeProperty('--cursor-target-height');
|
||||
cursor.style.removeProperty('--cursor-target-radius');
|
||||
}
|
||||
};
|
||||
|
||||
const positionTarget = () => {
|
||||
const anchor = context.anchor;
|
||||
if (context.mode !== 'target' || !anchor?.isConnected) return false;
|
||||
|
||||
const rect = anchor.getBoundingClientRect();
|
||||
if (rect.width <= 0 || rect.height <= 0) return false;
|
||||
|
||||
const centerX = rect.left + rect.width / 2;
|
||||
const centerY = rect.top + rect.height / 2;
|
||||
const magneticX = clamp((lastPoint.x - centerX) / Math.max(rect.width / 2, 1), -1, 1) * 10;
|
||||
const magneticY = clamp((lastPoint.y - centerY) / Math.max(rect.height / 2, 1), -1, 1) * 10;
|
||||
const radius = getComputedStyle(anchor).borderRadius || '0.75rem';
|
||||
|
||||
cursor.style.setProperty('--cursor-target-width', `${rect.width}px`);
|
||||
cursor.style.setProperty('--cursor-target-height', `${rect.height}px`);
|
||||
cursor.style.setProperty('--cursor-target-radius', radius);
|
||||
rawX.set(centerX + magneticX);
|
||||
rawY.set(centerY + magneticY);
|
||||
return true;
|
||||
};
|
||||
|
||||
const observeTarget = (anchor: Element | null) => {
|
||||
stopObservingTarget();
|
||||
if (!anchor) return;
|
||||
targetObserver = new ResizeObserver(positionTarget);
|
||||
targetObserver.observe(anchor);
|
||||
};
|
||||
|
||||
const applyContext = (next: CursorContext) => {
|
||||
const targetChanged = next.anchor !== context.anchor;
|
||||
context = next;
|
||||
setMode(next.mode);
|
||||
if (targetChanged) observeTarget(next.mode === 'target' ? next.anchor : null);
|
||||
};
|
||||
|
||||
const positionAtPointer = () => {
|
||||
if (context.mode === 'target' && positionTarget()) return;
|
||||
rawX.set(lastPoint.x);
|
||||
rawY.set(lastPoint.y);
|
||||
};
|
||||
|
||||
const refreshContext = (target: EventTarget | null) => {
|
||||
lastEventTarget = target;
|
||||
applyContext(resolveCursorContext(target, lastPoint.x, lastPoint.y));
|
||||
positionAtPointer();
|
||||
};
|
||||
|
||||
const onPointerMove = (event: PointerEvent) => {
|
||||
if (event.pointerType === 'touch') return;
|
||||
lastPoint.x = event.clientX;
|
||||
lastPoint.y = event.clientY;
|
||||
cursor.dataset.visible = 'true';
|
||||
if (event.target !== lastEventTarget || context.tracksPointerRegion) refreshContext(event.target);
|
||||
else positionAtPointer();
|
||||
};
|
||||
|
||||
const onPointerOver = (event: PointerEvent) => {
|
||||
if (event.pointerType === 'touch') return;
|
||||
lastPoint.x = event.clientX;
|
||||
lastPoint.y = event.clientY;
|
||||
cursor.dataset.visible = 'true';
|
||||
refreshContext(event.target);
|
||||
};
|
||||
|
||||
const onPointerOut = (event: PointerEvent) => {
|
||||
if (event.relatedTarget === null) cursor.dataset.visible = 'false';
|
||||
};
|
||||
|
||||
const onPointerDown = (event: PointerEvent) => {
|
||||
if (event.pointerType === 'touch') return;
|
||||
cursor.dataset.pressed = 'true';
|
||||
refreshContext(event.target);
|
||||
if (context.mode === 'grab') setMode('grabbing');
|
||||
};
|
||||
|
||||
const restorePointerContext = () => {
|
||||
cursor.dataset.pressed = 'false';
|
||||
const target = document.elementFromPoint(lastPoint.x, lastPoint.y);
|
||||
refreshContext(target);
|
||||
};
|
||||
|
||||
const onDragStart = () => {
|
||||
cursor.dataset.pressed = 'true';
|
||||
setMode('grabbing');
|
||||
};
|
||||
|
||||
const onViewportChange = () => positionTarget();
|
||||
const hide = () => { cursor.dataset.visible = 'false'; };
|
||||
const onVisibilityChange = () => {
|
||||
if (document.visibilityState !== 'visible') hide();
|
||||
};
|
||||
|
||||
window.addEventListener('pointermove', onPointerMove, { passive: true });
|
||||
window.addEventListener('pointerover', onPointerOver, { passive: true });
|
||||
window.addEventListener('pointerout', onPointerOut, { passive: true });
|
||||
window.addEventListener('pointerdown', onPointerDown, { capture: true, passive: true });
|
||||
window.addEventListener('pointerup', restorePointerContext, { capture: true, passive: true });
|
||||
window.addEventListener('pointercancel', restorePointerContext, { capture: true, passive: true });
|
||||
window.addEventListener('dragstart', onDragStart, { capture: true });
|
||||
window.addEventListener('dragend', restorePointerContext, { capture: true });
|
||||
window.addEventListener('scroll', onViewportChange, { capture: true, passive: true });
|
||||
window.addEventListener('resize', onViewportChange, { passive: true });
|
||||
window.addEventListener('blur', hide);
|
||||
document.addEventListener('visibilitychange', onVisibilityChange);
|
||||
|
||||
return () => {
|
||||
stopObservingTarget();
|
||||
delete root.dataset.cursorSystem;
|
||||
cursor.dataset.visible = 'false';
|
||||
window.removeEventListener('pointermove', onPointerMove);
|
||||
window.removeEventListener('pointerover', onPointerOver);
|
||||
window.removeEventListener('pointerout', onPointerOut);
|
||||
window.removeEventListener('pointerdown', onPointerDown, { capture: true });
|
||||
window.removeEventListener('pointerup', restorePointerContext, { capture: true });
|
||||
window.removeEventListener('pointercancel', restorePointerContext, { capture: true });
|
||||
window.removeEventListener('dragstart', onDragStart, { capture: true });
|
||||
window.removeEventListener('dragend', restorePointerContext, { capture: true });
|
||||
window.removeEventListener('scroll', onViewportChange, { capture: true });
|
||||
window.removeEventListener('resize', onViewportChange);
|
||||
window.removeEventListener('blur', hide);
|
||||
document.removeEventListener('visibilitychange', onVisibilityChange);
|
||||
};
|
||||
}, [enabled, rawX, rawY]);
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
ref={cursorRef}
|
||||
className="adaptive-cursor"
|
||||
data-mode="default"
|
||||
data-visible="false"
|
||||
data-pressed="false"
|
||||
style={{ x, y }}
|
||||
aria-hidden="true"
|
||||
>
|
||||
<span className="adaptive-cursor-shape" />
|
||||
</motion.div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
import * as React from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { AnimatePresence, motion } from 'motion/react';
|
||||
import {
|
||||
ArrowLeft,
|
||||
ClipboardPaste,
|
||||
Copy,
|
||||
ExternalLink,
|
||||
Image as ImageIcon,
|
||||
Link2,
|
||||
RefreshCw,
|
||||
Scissors,
|
||||
TextSelect,
|
||||
type LucideIcon,
|
||||
} from 'lucide-react';
|
||||
import { useTheme } from '@/contexts/ThemeContext';
|
||||
|
||||
type TextEditor = HTMLInputElement | HTMLTextAreaElement | HTMLElement;
|
||||
|
||||
interface SelectionSnapshot {
|
||||
editor: TextEditor | null;
|
||||
editorStart: number | null;
|
||||
editorEnd: number | null;
|
||||
range: Range | null;
|
||||
selectedText: string;
|
||||
}
|
||||
|
||||
interface ContextSnapshot extends SelectionSnapshot {
|
||||
target: Element;
|
||||
link: HTMLAnchorElement | null;
|
||||
image: HTMLImageElement | null;
|
||||
}
|
||||
|
||||
interface MenuState {
|
||||
id: number;
|
||||
x: number;
|
||||
y: number;
|
||||
keyboard: boolean;
|
||||
snapshot: ContextSnapshot;
|
||||
}
|
||||
|
||||
interface MenuAction {
|
||||
id: string;
|
||||
label: string;
|
||||
icon: LucideIcon;
|
||||
shortcut?: string;
|
||||
disabled?: boolean;
|
||||
disabledReason?: string;
|
||||
run: () => void | Promise<void>;
|
||||
}
|
||||
|
||||
const TEXT_INPUT_SELECTOR = [
|
||||
'input:not([type])',
|
||||
'input[type="text"]',
|
||||
'input[type="search"]',
|
||||
'input[type="url"]',
|
||||
'input[type="tel"]',
|
||||
'input[type="password"]',
|
||||
'input[type="email"]',
|
||||
'textarea',
|
||||
'[contenteditable]:not([contenteditable="false"])',
|
||||
].join(',');
|
||||
|
||||
function clamp(value: number, min: number, max: number): number {
|
||||
return Math.min(max, Math.max(min, value));
|
||||
}
|
||||
|
||||
function findEditor(target: Element): TextEditor | null {
|
||||
const candidate = target.closest(TEXT_INPUT_SELECTOR);
|
||||
if (
|
||||
candidate instanceof HTMLInputElement
|
||||
|| candidate instanceof HTMLTextAreaElement
|
||||
|| (candidate instanceof HTMLElement && candidate.isContentEditable)
|
||||
) return candidate;
|
||||
return null;
|
||||
}
|
||||
|
||||
function captureSelection(target: Element): SelectionSnapshot {
|
||||
const editor = findEditor(target);
|
||||
if (editor instanceof HTMLInputElement || editor instanceof HTMLTextAreaElement) {
|
||||
const start = editor.selectionStart;
|
||||
const end = editor.selectionEnd;
|
||||
return {
|
||||
editor,
|
||||
editorStart: start,
|
||||
editorEnd: end,
|
||||
range: null,
|
||||
selectedText: start !== null && end !== null ? editor.value.slice(start, end) : '',
|
||||
};
|
||||
}
|
||||
|
||||
const selection = window.getSelection();
|
||||
const range = selection?.rangeCount ? selection.getRangeAt(0).cloneRange() : null;
|
||||
const selectionInsideEditor = editor instanceof HTMLElement
|
||||
&& range !== null
|
||||
&& editor.contains(range.commonAncestorContainer);
|
||||
|
||||
return {
|
||||
editor,
|
||||
editorStart: null,
|
||||
editorEnd: null,
|
||||
range: selectionInsideEditor ? range : null,
|
||||
selectedText: editor instanceof HTMLElement && !selectionInsideEditor
|
||||
? ''
|
||||
: selection?.toString() ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
function restoreRange(snapshot: SelectionSnapshot): Range {
|
||||
if (!(snapshot.editor instanceof HTMLElement) || !snapshot.range) {
|
||||
throw new Error('无法恢复文本选区');
|
||||
}
|
||||
snapshot.editor.focus({ preventScroll: true });
|
||||
const range = snapshot.range.cloneRange();
|
||||
const selection = window.getSelection();
|
||||
if (!selection) throw new Error('浏览器未提供文本选区');
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
return range;
|
||||
}
|
||||
|
||||
function emitInput(target: TextEditor, inputType: string, data: string | null) {
|
||||
target.dispatchEvent(new InputEvent('input', {
|
||||
bubbles: true,
|
||||
composed: true,
|
||||
inputType,
|
||||
data,
|
||||
}));
|
||||
}
|
||||
|
||||
function replaceSelection(snapshot: SelectionSnapshot, value: string, inputType: string) {
|
||||
const { editor } = snapshot;
|
||||
if (editor instanceof HTMLInputElement || editor instanceof HTMLTextAreaElement) {
|
||||
if (snapshot.editorStart === null || snapshot.editorEnd === null) {
|
||||
throw new Error('当前输入框不支持选区替换');
|
||||
}
|
||||
editor.focus({ preventScroll: true });
|
||||
editor.setRangeText(value, snapshot.editorStart, snapshot.editorEnd, 'end');
|
||||
emitInput(editor, inputType, value || null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(editor instanceof HTMLElement)) throw new Error('缺少可编辑文本目标');
|
||||
const range = restoreRange(snapshot);
|
||||
range.deleteContents();
|
||||
if (value) {
|
||||
const text = document.createTextNode(value);
|
||||
range.insertNode(text);
|
||||
range.setStartAfter(text);
|
||||
}
|
||||
range.collapse(true);
|
||||
const selection = window.getSelection();
|
||||
selection?.removeAllRanges();
|
||||
selection?.addRange(range);
|
||||
emitInput(editor, inputType, value || null);
|
||||
}
|
||||
|
||||
function selectEditorContents(editor: TextEditor) {
|
||||
editor.focus({ preventScroll: true });
|
||||
if (editor instanceof HTMLInputElement || editor instanceof HTMLTextAreaElement) {
|
||||
editor.select();
|
||||
return;
|
||||
}
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(editor);
|
||||
const selection = window.getSelection();
|
||||
if (!selection) throw new Error('浏览器未提供文本选区');
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
}
|
||||
|
||||
function legacyCopy(value: string): boolean {
|
||||
const active = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
||||
const selection = window.getSelection();
|
||||
const ranges = selection
|
||||
? Array.from({ length: selection.rangeCount }, (_, index) => selection.getRangeAt(index).cloneRange())
|
||||
: [];
|
||||
const textarea = document.createElement('textarea');
|
||||
textarea.value = value;
|
||||
textarea.setAttribute('readonly', '');
|
||||
textarea.style.position = 'fixed';
|
||||
textarea.style.left = '-9999px';
|
||||
textarea.style.opacity = '0';
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
|
||||
try {
|
||||
const exec = (document as unknown as { execCommand(command: string): boolean }).execCommand;
|
||||
return exec.call(document, 'copy');
|
||||
} finally {
|
||||
textarea.remove();
|
||||
active?.focus({ preventScroll: true });
|
||||
if (selection) {
|
||||
selection.removeAllRanges();
|
||||
ranges.forEach((range) => selection.addRange(range));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function writeClipboard(value: string) {
|
||||
let clipboardError: unknown;
|
||||
try {
|
||||
if (!navigator.clipboard?.writeText) throw new Error('Clipboard API unavailable');
|
||||
await navigator.clipboard.writeText(value);
|
||||
return;
|
||||
} catch (error) {
|
||||
clipboardError = error;
|
||||
}
|
||||
|
||||
if (legacyCopy(value)) return;
|
||||
throw clipboardError instanceof Error
|
||||
? clipboardError
|
||||
: new Error('浏览器拒绝写入剪贴板');
|
||||
}
|
||||
|
||||
async function readClipboard(): Promise<string> {
|
||||
if (!navigator.clipboard?.readText) {
|
||||
throw new Error('当前页面没有读取剪贴板的权限');
|
||||
}
|
||||
return navigator.clipboard.readText();
|
||||
}
|
||||
|
||||
function openInNewTab(url: string) {
|
||||
window.open(url, '_blank', 'noopener,noreferrer');
|
||||
}
|
||||
|
||||
function platformShortcut(key: string): string {
|
||||
return /Mac|iPhone|iPad|iPod/i.test(navigator.platform) ? `⌘${key}` : `Ctrl+${key}`;
|
||||
}
|
||||
|
||||
function buildActionGroups(snapshot: ContextSnapshot): MenuAction[][] {
|
||||
const groups: MenuAction[][] = [];
|
||||
const contextual: MenuAction[] = [];
|
||||
|
||||
if (snapshot.link) {
|
||||
const href = snapshot.link.href;
|
||||
contextual.push(
|
||||
{ id: 'open-link', label: '在新标签页打开链接', icon: ExternalLink, run: () => openInNewTab(href) },
|
||||
{ id: 'copy-link', label: '复制链接地址', icon: Link2, run: () => writeClipboard(href) },
|
||||
);
|
||||
}
|
||||
|
||||
if (snapshot.image) {
|
||||
const src = snapshot.image.currentSrc || snapshot.image.src;
|
||||
contextual.push(
|
||||
{ id: 'open-image', label: '在新标签页打开图片', icon: ImageIcon, run: () => openInNewTab(src) },
|
||||
{ id: 'copy-image-url', label: '复制图片地址', icon: Copy, run: () => writeClipboard(src) },
|
||||
);
|
||||
}
|
||||
if (contextual.length) groups.push(contextual);
|
||||
|
||||
if (snapshot.editor) {
|
||||
const hasSelection = snapshot.selectedText.length > 0;
|
||||
groups.push([
|
||||
{
|
||||
id: 'cut',
|
||||
label: '剪切',
|
||||
icon: Scissors,
|
||||
shortcut: platformShortcut('X'),
|
||||
disabled: !hasSelection,
|
||||
run: async () => {
|
||||
await writeClipboard(snapshot.selectedText);
|
||||
replaceSelection(snapshot, '', 'deleteByCut');
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'copy',
|
||||
label: '复制',
|
||||
icon: Copy,
|
||||
shortcut: platformShortcut('C'),
|
||||
disabled: !hasSelection,
|
||||
run: () => writeClipboard(snapshot.selectedText),
|
||||
},
|
||||
{
|
||||
id: 'paste',
|
||||
label: '粘贴',
|
||||
icon: ClipboardPaste,
|
||||
shortcut: platformShortcut('V'),
|
||||
disabled: !navigator.clipboard?.readText,
|
||||
disabledReason: '当前页面没有读取剪贴板的权限',
|
||||
run: async () => replaceSelection(snapshot, await readClipboard(), 'insertFromPaste'),
|
||||
},
|
||||
{
|
||||
id: 'select-all',
|
||||
label: '全选',
|
||||
icon: TextSelect,
|
||||
shortcut: platformShortcut('A'),
|
||||
run: () => selectEditorContents(snapshot.editor!),
|
||||
},
|
||||
]);
|
||||
} else if (snapshot.selectedText) {
|
||||
groups.push([{
|
||||
id: 'copy-selection',
|
||||
label: '复制选中文本',
|
||||
icon: Copy,
|
||||
shortcut: platformShortcut('C'),
|
||||
run: () => writeClipboard(snapshot.selectedText),
|
||||
}]);
|
||||
}
|
||||
|
||||
groups.push([
|
||||
{
|
||||
id: 'back',
|
||||
label: '返回上一页',
|
||||
icon: ArrowLeft,
|
||||
disabled: window.history.length <= 1,
|
||||
run: () => window.history.back(),
|
||||
},
|
||||
{ id: 'reload', label: '刷新页面', icon: RefreshCw, shortcut: platformShortcut('R'), run: () => window.location.reload() },
|
||||
{ id: 'copy-page-url', label: '复制当前页面地址', icon: Link2, run: () => writeClipboard(window.location.href) },
|
||||
]);
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
export function GlobalContextMenu() {
|
||||
const { appearance } = useTheme();
|
||||
const enabled = appearance.customPointerSystem;
|
||||
const [menu, setMenu] = React.useState<MenuState | null>(null);
|
||||
const [position, setPosition] = React.useState({ x: -9999, y: -9999, ready: false });
|
||||
const [busyAction, setBusyAction] = React.useState<string | null>(null);
|
||||
const [error, setError] = React.useState<string | null>(null);
|
||||
const menuRef = React.useRef<HTMLDivElement>(null);
|
||||
const sequence = React.useRef(0);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!enabled) return;
|
||||
|
||||
const onContextMenu = (event: MouseEvent) => {
|
||||
if (event.shiftKey || !window.matchMedia('(hover: hover) and (pointer: fine)').matches) return;
|
||||
if (!(event.target instanceof Element)) return;
|
||||
if (event.target.closest('[data-context-menu="native"]')) return;
|
||||
if (event.target.closest('.global-context-menu')) {
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
const target = event.target;
|
||||
const keyboard = event.clientX === 0 && event.clientY === 0;
|
||||
const rect = target.getBoundingClientRect();
|
||||
const x = keyboard ? rect.left + Math.min(rect.width / 2, 24) : event.clientX;
|
||||
const y = keyboard ? rect.top + Math.min(rect.height, 24) : event.clientY;
|
||||
|
||||
setError(null);
|
||||
setBusyAction(null);
|
||||
setPosition({ x, y, ready: false });
|
||||
setMenu({
|
||||
id: ++sequence.current,
|
||||
x,
|
||||
y,
|
||||
keyboard,
|
||||
snapshot: {
|
||||
target,
|
||||
link: target.closest('a[href]'),
|
||||
image: target.closest('img'),
|
||||
...captureSelection(target),
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
window.addEventListener('contextmenu', onContextMenu, { capture: true });
|
||||
return () => window.removeEventListener('contextmenu', onContextMenu, { capture: true });
|
||||
}, [enabled]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (enabled) return;
|
||||
setMenu(null);
|
||||
setError(null);
|
||||
setBusyAction(null);
|
||||
}, [enabled]);
|
||||
|
||||
React.useLayoutEffect(() => {
|
||||
const node = menuRef.current;
|
||||
if (!menu || !node) return;
|
||||
const rect = node.getBoundingClientRect();
|
||||
const edge = 8;
|
||||
setPosition({
|
||||
x: clamp(menu.x, edge, Math.max(edge, window.innerWidth - rect.width - edge)),
|
||||
y: clamp(menu.y, edge, Math.max(edge, window.innerHeight - rect.height - edge)),
|
||||
ready: true,
|
||||
});
|
||||
}, [error, menu]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!menu) return;
|
||||
const close = () => setMenu(null);
|
||||
const onPointerDown = (event: PointerEvent) => {
|
||||
if (menuRef.current?.contains(event.target as Node)) return;
|
||||
close();
|
||||
};
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.key === 'Escape') {
|
||||
event.preventDefault();
|
||||
close();
|
||||
return;
|
||||
}
|
||||
if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return;
|
||||
const items = Array.from(menuRef.current?.querySelectorAll<HTMLButtonElement>('[role="menuitem"]:not(:disabled)') ?? []);
|
||||
if (!items.length) return;
|
||||
event.preventDefault();
|
||||
const current = items.indexOf(document.activeElement as HTMLButtonElement);
|
||||
let next = 0;
|
||||
if (event.key === 'End') next = items.length - 1;
|
||||
else if (event.key === 'ArrowUp') next = current <= 0 ? items.length - 1 : current - 1;
|
||||
else if (event.key === 'ArrowDown') next = current < 0 || current === items.length - 1 ? 0 : current + 1;
|
||||
items[next]?.focus({ preventScroll: true });
|
||||
};
|
||||
|
||||
window.addEventListener('pointerdown', onPointerDown, { capture: true });
|
||||
window.addEventListener('keydown', onKeyDown, { capture: true });
|
||||
window.addEventListener('scroll', close, { capture: true, passive: true });
|
||||
window.addEventListener('resize', close, { passive: true });
|
||||
window.addEventListener('blur', close);
|
||||
return () => {
|
||||
window.removeEventListener('pointerdown', onPointerDown, { capture: true });
|
||||
window.removeEventListener('keydown', onKeyDown, { capture: true });
|
||||
window.removeEventListener('scroll', close, { capture: true });
|
||||
window.removeEventListener('resize', close);
|
||||
window.removeEventListener('blur', close);
|
||||
};
|
||||
}, [menu]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (menu?.keyboard && position.ready) {
|
||||
menuRef.current?.querySelector<HTMLButtonElement>('[role="menuitem"]:not(:disabled)')?.focus({ preventScroll: true });
|
||||
}
|
||||
}, [menu, position.ready]);
|
||||
|
||||
const groups = enabled && menu ? buildActionGroups(menu.snapshot) : [];
|
||||
|
||||
const runAction = async (action: MenuAction) => {
|
||||
if (action.disabled || busyAction) return;
|
||||
setBusyAction(action.id);
|
||||
setError(null);
|
||||
try {
|
||||
await action.run();
|
||||
if (menu?.keyboard && menu.snapshot.target instanceof HTMLElement) {
|
||||
menu.snapshot.target.focus({ preventScroll: true });
|
||||
}
|
||||
setMenu(null);
|
||||
} catch (cause) {
|
||||
const message = cause instanceof Error ? cause.message : String(cause);
|
||||
console.error(`[context-menu] ${action.id} failed`, cause);
|
||||
setError(message || '操作失败');
|
||||
} finally {
|
||||
setBusyAction(null);
|
||||
}
|
||||
};
|
||||
|
||||
return createPortal(
|
||||
<AnimatePresence initial={false}>
|
||||
{enabled && menu && (
|
||||
<motion.div
|
||||
key={menu.id}
|
||||
ref={menuRef}
|
||||
role="menu"
|
||||
aria-label="右键菜单"
|
||||
className="global-context-menu"
|
||||
style={{
|
||||
left: position.x,
|
||||
top: position.y,
|
||||
visibility: position.ready ? 'visible' : 'hidden',
|
||||
transformOrigin: `${menu.x > window.innerWidth / 2 ? 'right' : 'left'} ${menu.y > window.innerHeight / 2 ? 'bottom' : 'top'}`,
|
||||
}}
|
||||
initial={{ opacity: 0, scale: 0.96, y: -4 }}
|
||||
animate={{ opacity: 1, scale: 1, y: 0 }}
|
||||
exit={{ opacity: 0, scale: 0.98, y: -2, transition: { duration: 0.12, ease: 'easeIn' } }}
|
||||
transition={{ type: 'spring', duration: 0.3, bounce: 0 }}
|
||||
>
|
||||
{groups.map((group, groupIndex) => (
|
||||
<React.Fragment key={group.map((action) => action.id).join(':')}>
|
||||
{groupIndex > 0 && <div className="global-context-menu-separator" role="separator" />}
|
||||
<div className="global-context-menu-group">
|
||||
{group.map((action) => {
|
||||
const Icon = action.icon;
|
||||
const busy = busyAction === action.id;
|
||||
return (
|
||||
<button
|
||||
key={action.id}
|
||||
type="button"
|
||||
role="menuitem"
|
||||
tabIndex={-1}
|
||||
disabled={action.disabled || Boolean(busyAction)}
|
||||
aria-busy={busy || undefined}
|
||||
data-cursor={busy ? 'wait' : undefined}
|
||||
title={action.disabled ? action.disabledReason : undefined}
|
||||
className="global-context-menu-item"
|
||||
onClick={() => void runAction(action)}
|
||||
>
|
||||
<Icon className="global-context-menu-icon" aria-hidden="true" />
|
||||
<span className="global-context-menu-label">{action.label}</span>
|
||||
{action.shortcut && <kbd className="global-context-menu-shortcut">{action.shortcut}</kbd>}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</React.Fragment>
|
||||
))}
|
||||
{error && (
|
||||
<div className="global-context-menu-error" role="alert">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
</motion.div>
|
||||
)}
|
||||
</AnimatePresence>,
|
||||
document.body,
|
||||
);
|
||||
}
|
||||
@@ -38,7 +38,7 @@ const ScrollBar = React.forwardRef<
|
||||
ref={ref}
|
||||
orientation={orientation}
|
||||
className={cn(
|
||||
'flex touch-none select-none p-0.5 transition-colors',
|
||||
'flex touch-none select-none p-0.5 transition-colors cursor-grab active:cursor-grabbing',
|
||||
orientation === 'vertical' && 'h-full w-2 border-l border-l-transparent',
|
||||
orientation === 'horizontal' && 'h-2 w-full flex-col border-t border-t-transparent',
|
||||
className
|
||||
|
||||
Reference in new issue
Block a user