Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(whiteboard): keep the live editor's events out of the canvas node

enes1cff97310aa5
diff --git a/docs/whiteboard-node.md b/docs/whiteboard-node.md
index ce8f0dc3b..b865183a3 100644
--- a/docs/whiteboard-node.md
+++ b/docs/whiteboard-node.md
@@ -29,6 +29,13 @@ capture-phase wheel listener on `.flow-wrap` (⌘-wheel and pinch always zoom th
file stores only the board zoom;
- is clipped to the canvas area and stacked at z 10, under the sessions sidebar (12) and Dock (20);
- treats a press on the node's own frame (header, Maximize, title) as staying in the board.
+- keeps its events out of the node. A portal leaves the node's DOM but not its React tree, so
+ React bubbled the editor's events into React Flow's node wrapper: a right-click opened the node
+ menu over Excalidraw's own, an arrow key nudging a shape also moved the node (and every other
+ selected node), and a double-click (Excalidraw's "add text") flew the camera to the node. The
+ portal root stops click, double-click and context-menu, and only the keys the wrapper acts on
+ (arrows, Enter, Space, Escape): stopping a React event also stops the native one at the React
+ root, so every other key still reaches document and window listeners.
Only one editor is ever mounted, and it unmounts when editing ends — Excalidraw keeps some state
module-global, and one live instance costs ~3 MB the first time. Measured: no heap growth over five
diff --git a/src/renderer/nodes/WhiteboardNode.test.tsx b/src/renderer/nodes/WhiteboardNode.test.tsx
index fa6423e20..d6abfa539 100644
--- a/src/renderer/nodes/WhiteboardNode.test.tsx
+++ b/src/renderer/nodes/WhiteboardNode.test.tsx
@@ -60,7 +60,15 @@ const SCENE = JSON.stringify({ type: 'excalidraw', elements: [] })
let host: HTMLDivElement
let root: Root
-function mount(data: Partial<CanvasNode['data']> = {}) {
+/** React Flow's node wrapper, as far as the React tree is concerned: the handlers it hangs on the
+ * node's parent element (select on click, focus on double-click, the node menu, keyboard
+ * select/move). The editor is portaled out of the node's DOM but NOT out of this React tree. */
+type WrapperHandlers = Pick<
+ React.HTMLAttributes<HTMLDivElement>,
+ 'onClick' | 'onDoubleClick' | 'onContextMenu' | 'onKeyDown'
+>
+
+function mount(data: Partial<CanvasNode['data']> = {}, wrapper: WrapperHandlers = {}) {
host = document.createElement('div')
// The overlay is portaled into `.canvas-root` and clipped to `.flow-wrap`; the node finds both
// by ancestry before it mounts an editor.
@@ -82,7 +90,9 @@ function mount(data: Partial<CanvasNode['data']> = {}) {
act(() =>
root.render(
<ReactFlowProvider>
- <WhiteboardNode {...props} />
+ <div {...wrapper}>
+ <WhiteboardNode {...props} />
+ </div>
</ReactFlowProvider>
)
)
@@ -203,6 +213,60 @@ describe('a save that did not land offers a way forward instead of losing the wo
})
})
+describe('the live editor keeps its events away from the canvas node', () => {
+ // Measured in the dev app: a right-click on the board opened Excalidraw's menu AND the node menu,
+ // an arrow key nudging a shape also moved the node (every selected node), and a double-click
+ // (Excalidraw's "add text") flew the camera to the node.
+ async function openWithWrapper() {
+ files.readWhiteboard.mockResolvedValue({ scene: SCENE, rev: 'r1' } as WhiteboardFile)
+ const wrapper = {
+ onClick: vi.fn(),
+ onDoubleClick: vi.fn(),
+ onContextMenu: vi.fn(),
+ onKeyDown: vi.fn()
+ }
+ mount({}, wrapper)
+ await settle()
+ act(() => {
+ host.querySelector('.whiteboard-node__body')!.dispatchEvent(new MouseEvent('dblclick', { bubbles: true }))
+ })
+ await settle()
+ // The double-click that OPENED the board is the node's own and reaches the wrapper; only what
+ // happens inside the editor must not.
+ wrapper.onDoubleClick.mockClear()
+ const target = host.querySelector('[data-testid="editor"]')!
+ expect(target).not.toBeNull()
+ return { wrapper, target }
+ }
+
+ it('a right-click, click or double-click on the board never reaches the node', async () => {
+ const { wrapper, target } = await openWithWrapper()
+ act(() => {
+ target.dispatchEvent(new MouseEvent('contextmenu', { bubbles: true, cancelable: true }))
+ target.dispatchEvent(new MouseEvent('click', { bubbles: true }))
+ target.dispatchEvent(new MouseEvent('dblclick', { bubbles: true }))
+ })
+ expect(wrapper.onContextMenu).not.toHaveBeenCalled()
+ expect(wrapper.onClick).not.toHaveBeenCalled()
+ expect(wrapper.onDoubleClick).not.toHaveBeenCalled()
+ })
+
+ it('the keys the node wrapper acts on (arrows, Enter, Space, Escape) stay in the board', async () => {
+ const { wrapper, target } = await openWithWrapper()
+ for (const key of ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Enter', ' ', 'Escape']) {
+ act(() => {
+ target.dispatchEvent(new KeyboardEvent('keydown', { key, bubbles: true }))
+ })
+ }
+ expect(wrapper.onKeyDown).not.toHaveBeenCalled()
+ // Every other key is left alone: stopping a React event stops the native one at the root too.
+ act(() => {
+ target.dispatchEvent(new KeyboardEvent('keydown', { key: 'a', bubbles: true }))
+ })
+ expect(wrapper.onKeyDown).toHaveBeenCalledTimes(1)
+ })
+})
+
describe('a hosted-team Viewer', () => {
it('opens the board read-only (the host refuses their saves)', async () => {
useHostedTeams.getState().set('local', { role: 'viewer', teamLabel: 'Team' })
diff --git a/src/renderer/nodes/WhiteboardNode.tsx b/src/renderer/nodes/WhiteboardNode.tsx
index ee902faab..f9f6e7d85 100644
--- a/src/renderer/nodes/WhiteboardNode.tsx
+++ b/src/renderer/nodes/WhiteboardNode.tsx
@@ -44,6 +44,27 @@ interface Geometry {
const pngDataUrl = (base64: string): string => `data:image/png;base64,${base64}`
+/** The keys React Flow's node wrapper acts on: Enter / Space / Escape (select, unselect) and the
+ * arrows (move every selected node). */
+const NODE_WRAPPER_KEYS = new Set(['Enter', ' ', 'Escape', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'])
+
+/**
+ * The editor is portaled out of the node's DOM but not out of its React tree, so React bubbles
+ * the editor's events into React Flow's node wrapper. Measured in the dev app: a right-click on
+ * the board opened the node menu over Excalidraw's own, an arrow key nudging a shape also moved
+ * the node (and every other selected node), and a double-click (Excalidraw's "add text") flew the
+ * camera to the node. Excalidraw's handlers sit below this point and run first.
+ *
+ * Stopping a React event also stops the native one at the React root, so document and window
+ * listeners do not see it. That is why the keyboard is narrowed to the keys the wrapper acts on:
+ * every other key still reaches them, and the canvas dispatcher already stands down for the
+ * overlay (`data-nt-keyboard-owner`).
+ */
+const keepInBoard = (e: React.SyntheticEvent): void => e.stopPropagation()
+const keepWrapperKeysInBoard = (e: React.KeyboardEvent): void => {
+ if (NODE_WRAPPER_KEYS.has(e.key)) e.stopPropagation()
+}
+
/** The resting picture's bytes as a PNG Blob, decoded here. NOT `fetch(dataUrl)`: the app's CSP
* (`default-src 'self'`, no `connect-src data:`) refuses a fetch of a data: URL, which is how
* "Copy as image" used to fail without a word. */
@@ -394,7 +415,14 @@ export function WhiteboardNode({ id, data, selected, positionAbsoluteX, position
{mode &&
geom &&
createPortal(
- <div className="whiteboard-clip" style={geom.clip}>
+ <div
+ className="whiteboard-clip"
+ style={geom.clip}
+ onClick={keepInBoard}
+ onDoubleClick={keepInBoard}
+ onContextMenu={keepInBoard}
+ onKeyDown={keepWrapperKeysInBoard}
+ >
<div ref={overlayRef} className="whiteboard-overlay" data-nt-keyboard-owner="" style={geom.board}>
<Suspense fallback={<div className="whiteboard-overlay__loading">Loading whiteboard…</div>}>
<WhiteboardEditor