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.

feat(editor): route the global keymap to every editor window

yhh020425860301
diff --git a/desktop/src/App.tsx b/desktop/src/App.tsx
index e49feef84..0e4183374 100644
--- a/desktop/src/App.tsx
+++ b/desktop/src/App.tsx
@@ -35,6 +35,7 @@ import { TimelineRecorder } from '@/timeline/TimelineRecorder';
import { ProjectStore } from '@/project/ProjectStore';
import { EditorHistory } from '@/engine/EditorHistory';
import { dockApi } from '@/layout/dockApi';
+import { forEachEditorWindow } from '@/layout/editorWindows';
import { Toasts } from '@/store/Toasts';
import { t } from '@/i18n';
@@ -43,7 +44,8 @@ import { t } from '@/i18n';
export function App() {
// Global keymap: every shortcut is declared on its Command (single source).
// Skipped while a text field is focused so typing, native text undo, and
- // backspace-to-delete-text aren't hijacked.
+ // backspace-to-delete-text aren't hijacked. Attached to EVERY editor window
+ // (main + each popped-out panel) so shortcuts fire whichever one has focus.
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
// Ctrl+Space summons the Content Drawer — works even from a field.
@@ -52,7 +54,9 @@ export function App() {
useEditorStore.getState().toggleContentDrawer();
return;
}
- const el = document.activeElement as HTMLElement | null;
+ // The keydown's own target is the focused element in whichever window fired
+ // it — correct across popouts, unlike the main window's document.activeElement.
+ const el = e.target as HTMLElement | null;
if (el && (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.isContentEditable)) {
return;
}
@@ -68,8 +72,10 @@ export function App() {
cmd.run();
}
};
- window.addEventListener('keydown', onKey);
- return () => window.removeEventListener('keydown', onKey);
+ return forEachEditorWindow((win) => {
+ win.addEventListener('keydown', onKey);
+ return () => win.removeEventListener('keydown', onKey);
+ });
}, []);
// Reveal a mode's companion panels when the selection enters that mode (only on the
diff --git a/desktop/src/components/PanelWindow.tsx b/desktop/src/components/PanelWindow.tsx
index 796a5504d..addcc1290 100644
--- a/desktop/src/components/PanelWindow.tsx
+++ b/desktop/src/components/PanelWindow.tsx
@@ -22,7 +22,7 @@ import type { DockviewPanelApi } from 'dockview';
// The main editor window — resolved defensively so importing this module in a
// non-DOM context (unit tests run under node) doesn't touch an undefined `window`
// at load time. In the browser/Electron renderer this is always the real window.
-const mainWindow: Window =
+export const mainWindow: Window =
typeof window !== 'undefined' ? window : (globalThis as unknown as Window);
const PanelWindowContext = createContext<Window>(mainWindow);
diff --git a/desktop/src/layout/DockLayout.tsx b/desktop/src/layout/DockLayout.tsx
index 0d364e98d..d0269ca83 100644
--- a/desktop/src/layout/DockLayout.tsx
+++ b/desktop/src/layout/DockLayout.tsx
@@ -32,6 +32,7 @@ import { Perf } from '@/components/Perf';
import { ErrorBoundary } from '@/components/ErrorBoundary';
import { PanelWindowProvider } from '@/components/PanelWindow';
import { dockApi, applyPopoutTheme } from '@/layout/dockApi';
+import { addEditorWindow, removeEditorWindow } from '@/layout/editorWindows';
import { t } from '@/i18n';
// Each dock panel is a thin wrapper so dockview owns mount/unmount. The
@@ -261,12 +262,17 @@ export function DockLayout() {
dockApi.set(api); // expose to the activity bar (reveal/focus panels)
// Every popout group (a manual pop-out AND one dockview reopens while restoring a
- // saved layout) is re-themed here: dockview copies only the first `dockview-theme-*`
- // class into the child window, so re-add our estella override so the popped-out
- // chrome matches the dock exactly. One hook covers both paths.
+ // saved layout) is registered here: re-theme its window (dockview copies only the
+ // first `dockview-theme-*` class over, so re-add our estella override), and add it
+ // to the editor-window set so window-global features (the keymap) attach to it.
+ // Its own pagehide unregisters it when the popout closes or docks back.
api.onDidAddGroup((group) => {
const loc = group.api.location;
- if (loc.type === 'popout') applyPopoutTheme(loc.getWindow());
+ if (loc.type !== 'popout') return;
+ const w = loc.getWindow();
+ applyPopoutTheme(w);
+ addEditorWindow(w);
+ w.addEventListener('pagehide', () => removeEditorWindow(w), { once: true });
});
const saved = localStorage.getItem(LAYOUT_KEY);
diff --git a/desktop/src/layout/editorWindows.ts b/desktop/src/layout/editorWindows.ts
new file mode 100644
index 000000000..4d5790c21
--- /dev/null
+++ b/desktop/src/layout/editorWindows.ts
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file editorWindows.ts
+ * @brief The set of live editor windows — the main window plus every popped-out
+ * dock panel's OS window — and a way to run per-window setup for each
+ * current AND future one.
+ *
+ * A popout shares the main window's JS realm, but it's still a distinct
+ * Window with its own event target: a keydown in a popped-out panel fires
+ * there, not on the main window. Window-global features (the editor keymap)
+ * register their setup with forEachEditorWindow so they attach to every
+ * window and keep working no matter which one has focus. DockLayout feeds
+ * popout openings/closings in as dockview creates/destroys them.
+ */
+import { mainWindow } from '@/components/PanelWindow';
+
+/** Per-window setup: attach to `win`, optionally return a teardown run on removal. */
+type PerWindow = (win: Window) => (() => void) | void;
+
+const windows = new Set<Window>([mainWindow]);
+const setups = new Set<PerWindow>();
+// Teardown callbacks, per window, per setup — so removing a window (or a setup)
+// runs exactly the cleanups it created.
+const teardowns = new Map<Window, Map<PerWindow, () => void>>();
+
+function runSetup(win: Window, setup: PerWindow): void {
+ const teardown = setup(win);
+ if (!teardown) return;
+ let bySetup = teardowns.get(win);
+ if (!bySetup) teardowns.set(win, (bySetup = new Map()));
+ bySetup.set(setup, teardown);
+}
+
+function tearDown(win: Window, setup: PerWindow): void {
+ const bySetup = teardowns.get(win);
+ const t = bySetup?.get(setup);
+ if (t) {
+ t();
+ bySetup!.delete(setup);
+ }
+}
+
+/** Register a popout window; runs every registered setup for it. Idempotent. */
+export function addEditorWindow(win: Window): void {
+ if (windows.has(win)) return;
+ windows.add(win);
+ for (const setup of setups) runSetup(win, setup);
+}
+
+/** Unregister a window (its popout closed); runs every teardown it accrued. */
+export function removeEditorWindow(win: Window): void {
+ if (win === mainWindow || !windows.delete(win)) return;
+ const bySetup = teardowns.get(win);
+ if (bySetup) {
+ for (const t of bySetup.values()) t();
+ teardowns.delete(win);
+ }
+}
+
+/**
+ * Run `setup` for every current and future editor window; returns an unsubscribe
+ * that tears the setup down in each window. The single seam a window-global
+ * feature uses instead of `window.addEventListener`.
+ */
+export function forEachEditorWindow(setup: PerWindow): () => void {
+ setups.add(setup);
+ for (const win of windows) runSetup(win, setup);
+ return () => {
+ setups.delete(setup);
+ for (const win of windows) tearDown(win, setup);
+ };
+}