Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(editor): focused panels own their editing keys

yhh65bc998e9462
diff --git a/desktop/src/panels/ContentBrowser.tsx b/desktop/src/panels/ContentBrowser.tsx
index 3f3ef1f0a..4f6434030 100644
--- a/desktop/src/panels/ContentBrowser.tsx
+++ b/desktop/src/panels/ContentBrowser.tsx
@@ -432,6 +432,101 @@ export function ContentBrowser() {
}
};
+ // Keyboard, scoped to the focused item area (it carries tabIndex, so clicking a
+ // tile lands focus there): arrow/Home/End navigation, Enter open, F2 rename,
+ // Delete, Ctrl+D duplicate — the same conventions as the Outliner. Delete is
+ // consumed even with nothing selected, so it can never fall through to the
+ // global entity delete while the user is working in the browser.
+ const scrollRef = useRef<HTMLDivElement>(null);
+ const gridColumns = () => {
+ const tiles = scrollRef.current?.querySelectorAll<HTMLElement>('[data-path]');
+ if (!tiles || tiles.length < 2) return 1;
+ const top0 = tiles[0].offsetTop;
+ let cols = 1;
+ while (cols < tiles.length && tiles[cols].offsetTop === top0) cols++;
+ return cols;
+ };
+ const onGridKey = (e: React.KeyboardEvent) => {
+ const t = e.target as HTMLElement;
+ if (t.tagName === 'INPUT' || renaming != null) return; // typing / inline rename
+ const idx = items.findIndex((it) => it.path === selected);
+ const focusIndex = (i: number) => {
+ const it = items[Math.max(0, Math.min(items.length - 1, i))];
+ if (!it) return;
+ selectAsset(it.path);
+ requestAnimationFrame(() => {
+ scrollRef.current
+ ?.querySelector(`[data-path="${CSS.escape(it.path)}"]`)
+ ?.scrollIntoView({ block: 'nearest' });
+ });
+ };
+ switch (e.key) {
+ case 'ArrowLeft':
+ case 'ArrowRight':
+ case 'ArrowUp':
+ case 'ArrowDown': {
+ e.preventDefault();
+ e.stopPropagation(); // grid navigation, not the viewport's selection nudge
+ const rowStep = view === 'grid' ? gridColumns() : 1;
+ const step =
+ e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : e.key === 'ArrowDown' ? rowStep : -rowStep;
+ focusIndex(idx < 0 ? 0 : idx + step);
+ break;
+ }
+ case 'Home': {
+ e.preventDefault();
+ e.stopPropagation();
+ focusIndex(0);
+ break;
+ }
+ case 'End': {
+ e.preventDefault();
+ e.stopPropagation();
+ focusIndex(items.length - 1);
+ break;
+ }
+ case 'Enter': {
+ const it = items[idx];
+ if (it) {
+ e.preventDefault();
+ onOpen(it.path, it.isDir, it.name);
+ }
+ break;
+ }
+ case 'F2': {
+ if (idx >= 0 && selected) {
+ e.preventDefault();
+ setRenaming(selected);
+ }
+ break;
+ }
+ case 'Delete':
+ case 'Backspace': {
+ e.preventDefault();
+ e.stopPropagation();
+ const it = items[idx];
+ if (it) void remove(it.path, it.name);
+ break;
+ }
+ case 'd':
+ case 'D': {
+ if ((e.ctrlKey || e.metaKey) && idx >= 0 && selected) {
+ e.preventDefault();
+ e.stopPropagation();
+ void duplicate(selected);
+ }
+ break;
+ }
+ case 'Escape': {
+ if (selected) {
+ e.stopPropagation();
+ selectAsset(null);
+ }
+ break;
+ }
+ }
+ };
+
const newFolder = async () => {
const taken = new Set(entries.map((e) => e.name));
let name = 'New Folder';
@@ -815,6 +910,9 @@ export function ContentBrowser() {
<div
className={`cb-scroll${view === 'list' ? ' list' : ''}${fileDrop ? ' is-file-drop' : ''}`}
+ ref={scrollRef}
+ tabIndex={0}
+ onKeyDown={onGridKey}
onClick={(e) => {
if (e.target === e.currentTarget) selectAsset(null);
}}
diff --git a/desktop/src/panels/NodeGraphCanvas.tsx b/desktop/src/panels/NodeGraphCanvas.tsx
index b1aa32f90..726af21b7 100644
--- a/desktop/src/panels/NodeGraphCanvas.tsx
+++ b/desktop/src/panels/NodeGraphCanvas.tsx
@@ -228,17 +228,71 @@ export function NodeGraphCanvas<N extends CanvasNode, E extends CanvasEdge>(prop
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [nodes, vp]);
- useEffect(() => {
- const onKey = (e: KeyboardEvent) => {
- if (e.key !== 'Delete' && e.key !== 'Backspace') return;
- const el = document.activeElement;
- if (el && (el.tagName === 'INPUT' || el.tagName === 'SELECT' || el.tagName === 'TEXTAREA')) return;
+ // Keyboard, scoped to the focused canvas (the canvas div carries tabIndex, so
+ // clicking a node lands focus here) — a window listener would make two open
+ // graph editors both react to one Delete.
+ const KEY_DIR: Record<string, [number, number]> = {
+ ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1],
+ };
+ const center = (n: N) => {
+ const s = nodeSize(n);
+ return { x: nx(n) + s.width / 2, y: ny(n) + s.height / 2 };
+ };
+ const onKeyDown = (e: React.KeyboardEvent) => {
+ const el = e.target as HTMLElement;
+ if (el.tagName === 'INPUT' || el.tagName === 'SELECT' || el.tagName === 'TEXTAREA') return;
+ if (e.key === 'Escape') {
+ if (wire.current) {
+ wire.current = null;
+ setCursor(null);
+ setWiring(false);
+ e.stopPropagation();
+ } else if (selectedNode || selectedEdge) {
+ onSelectNode(null);
+ onSelectEdge(null);
+ e.stopPropagation();
+ }
+ return;
+ }
+ if (e.key === 'Delete' || e.key === 'Backspace') {
+ e.preventDefault();
+ e.stopPropagation(); // the canvas owns Delete — never the scene's entity delete
if (selectedNode) onDeleteNode(selectedNode);
else if (selectedEdge) onDeleteEdge(selectedEdge);
- };
- window.addEventListener('keydown', onKey);
- return () => window.removeEventListener('keydown', onKey);
- }, [selectedNode, selectedEdge, onDeleteNode, onDeleteEdge]);
+ return;
+ }
+ const dir = KEY_DIR[e.key];
+ if (!dir) return;
+ e.preventDefault();
+ e.stopPropagation(); // graph navigation, not the viewport's selection nudge
+ const cur = selectedNode ? byId.get(selectedNode) : null;
+ if (!cur) {
+ if (nodes.length) { onSelectNode(nodes[0].id); onSelectEdge(null); }
+ return;
+ }
+ if (e.shiftKey) {
+ // Nudge one grid step as a single undo step (same seam as a pointer drag).
+ onMoveNodeStart?.(cur.id);
+ onMoveNode(cur.id, nx(cur) + dir[0] * 10, ny(cur) + dir[1] * 10);
+ onMoveNodeEnd?.(cur.id);
+ return;
+ }
+ // Move the selection to the nearest node in that direction, preferring the
+ // straight-ahead candidate over a closer but off-axis one.
+ const c = center(cur);
+ let best: N | null = null;
+ let bestScore = Infinity;
+ for (const n of nodes) {
+ if (n.id === cur.id) continue;
+ const p = center(n);
+ const along = (p.x - c.x) * dir[0] + (p.y - c.y) * dir[1];
+ if (along <= 0) continue;
+ const across = Math.abs((p.x - c.x) * dir[1]) + Math.abs((p.y - c.y) * dir[0]);
+ const score = along + across * 2;
+ if (score < bestScore) { bestScore = score; best = n; }
+ }
+ if (best) { onSelectNode(best.id); onSelectEdge(null); }
+ };
const selfLoopPath = (n: N): string => {
const cx = nx(n) + nodeSize(n).width / 2;
@@ -276,9 +330,9 @@ export function NodeGraphCanvas<N extends CanvasNode, E extends CanvasEdge>(prop
};
return (
- <div className="panel ng-root">
+ <div className="panel ng-root" onKeyDown={onKeyDown}>
{toolbar && <div className="ng-bar">{toolbar}</div>}
- <div className="ng-canvas" style={{ backgroundSize: `${20 * vp.zoom}px ${20 * vp.zoom}px`, backgroundPosition: `${vp.x}px ${vp.y}px` }} ref={canvasRef}
+ <div className="ng-canvas" tabIndex={0} style={{ backgroundSize: `${20 * vp.zoom}px ${20 * vp.zoom}px`, backgroundPosition: `${vp.x}px ${vp.y}px` }} ref={canvasRef}
onPointerDown={e => {
if (e.button === 1) { e.preventDefault(); pan.current = { sx: e.clientX, sy: e.clientY, vx: vp.x, vy: vp.y }; return; }
onSelectNode(null); onSelectEdge(null);
diff --git a/desktop/src/panels/Sequencer.tsx b/desktop/src/panels/Sequencer.tsx
index 02eddf201..e485ee1db 100644
--- a/desktop/src/panels/Sequencer.tsx
+++ b/desktop/src/panels/Sequencer.tsx
@@ -245,6 +245,70 @@ function SequencerBody() {
}
};
+ // The focused panel owns its editing keys (the root carries tabIndex, so any
+ // click inside lands focus here). Delete is consumed even with no key selected —
+ // it must never fall through to the global entity delete while the user is
+ // working in the sequencer.
+ const onPanelKey = (e: React.KeyboardEvent) => {
+ const t = e.target as HTMLElement;
+ if (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.isContentEditable) return;
+ switch (e.key) {
+ case 'Delete':
+ case 'Backspace': {
+ e.preventDefault();
+ e.stopPropagation();
+ if (!selectedKey) break;
+ const at = selectedKey.lastIndexOf('@');
+ const row = rows.find((r) => r.id === selectedKey.slice(0, at));
+ if (row?.ref) {
+ TimelineCommands.deleteKey(row.ref, parseFloat(selectedKey.slice(at + 1)));
+ useSequencerStore.getState().selectKey(null);
+ setInterpPopover(null);
+ }
+ break;
+ }
+ case ' ': {
+ if (t.tagName === 'BUTTON') break; // Space on a focused button activates it
+ e.preventDefault();
+ e.stopPropagation();
+ useSequencerStore.getState().togglePlay();
+ break;
+ }
+ case 'ArrowLeft':
+ case 'ArrowRight': {
+ e.preventDefault();
+ e.stopPropagation(); // frame-step, not the viewport's selection nudge
+ useSequencerStore.getState().setPlaying(false);
+ setTime(time + (e.key === 'ArrowRight' ? 1 : -1) / fps);
+ break;
+ }
+ case 'Home': {
+ e.preventDefault();
+ e.stopPropagation();
+ setTime(0);
+ break;
+ }
+ case 'End': {
+ e.preventDefault();
+ e.stopPropagation();
+ setTime(duration);
+ break;
+ }
+ case 'Escape': {
+ if (interp || pickerOpen || settingsOpen) {
+ e.stopPropagation();
+ setInterpPopover(null);
+ setPickerOpen(null);
+ setSettingsOpen(null);
+ } else if (selectedKey) {
+ e.stopPropagation();
+ useSequencerStore.getState().selectKey(null);
+ }
+ break;
+ }
+ }
+ };
+
const onScrubDown = (e: React.PointerEvent) => {
if ((e.target as HTMLElement).closest('.seq-key')) return;
useSequencerStore.getState().setPlaying(false);
@@ -271,7 +335,11 @@ function SequencerBody() {
const trackCount = rows.filter((r) => r.kind === 'channel' || r.kind === 'track').length;
return (
- <div className={`seq${recording ? ' is-rec' : ''}${view === 'curve' ? ' is-curve' : ''}`}>
+ <div
+ className={`seq${recording ? ' is-rec' : ''}${view === 'curve' ? ' is-curve' : ''}`}
+ tabIndex={0}
+ onKeyDown={onPanelKey}
+ >
{/* transport */}
<div className="seq-bar">
<span className="seq-meta">
diff --git a/desktop/src/panels/Viewport.tsx b/desktop/src/panels/Viewport.tsx
index ff86a93b9..e7a52cefe 100644
--- a/desktop/src/panels/Viewport.tsx
+++ b/desktop/src/panels/Viewport.tsx
@@ -888,9 +888,11 @@ export function Viewport() {
return () => cancelAnimationFrame(raf);
}, []);
- // Esc, capture phase (pre-empts the global Esc→deselect), in priority order:
- // cancel an in-progress stroke → cancel a pan → leave paint mode → (fall through
- // to deselect). So Esc reverts a live drag, then exits painting, then deselects.
+ // Esc, capture phase (pre-empts the global keymap's Esc→stop-play), in priority
+ // order: cancel an in-progress stroke → cancel a pan → leave paint mode. There is
+ // deliberately no global Esc→deselect: transient UI (menus, popovers, dialogs)
+ // closes on Esc without stopping propagation, so a global binding would drop the
+ // selection every time one of them is dismissed.
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (e.key !== 'Escape') return;