Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/providers/DialogProvider/hooks/useDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ describe("useDialog", () => {

expect(result.current).toHaveProperty("open");
expect(result.current).toHaveProperty("close");
expect(result.current).toHaveProperty("cancel");
expect(result.current).toHaveProperty("closeAll");
expect(result.current).toHaveProperty("stack");
Comment thread
maxy-shpfy marked this conversation as resolved.
expect(Array.isArray(result.current.stack)).toBe(true);
expect(typeof result.current.open).toBe("function");
});

Expand Down
2 changes: 2 additions & 0 deletions src/providers/DialogProvider/hooks/useDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export function useDialog() {
return {
open: context.open,
close: context.close,
cancel: context.cancel,
closeAll: context.closeAll,
stack: context.stack,
};
}
2 changes: 2 additions & 0 deletions src/routes/v2/pages/Editor/EditorV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { EditorMenuBar } from "./components/EditorMenuBar/EditorMenuBar";
import { EmptyEditorState } from "./components/EmptyEditorState";
import { FlowCanvas } from "./components/FlowCanvas/FlowCanvas";
import { useComponentLibraryWindow } from "./hooks/useComponentLibraryWindow";
import { useEditorEscapeShortcut } from "./hooks/useEditorEscapeShortcut";
import { useHistoryWindow } from "./hooks/useHistoryWindow";
import { useLinkedWindowCleanup } from "./hooks/useLinkedWindowCleanup";
import { useLoadSpec } from "./hooks/useLoadSpec";
Expand Down Expand Up @@ -82,6 +83,7 @@ const PipelineEditor = withSuspenseWrapper(
useUndoRedoKeyboard();
useFocusMode();
useShortcutListener();
useEditorEscapeShortcut();
useDebugPanelWindow();

const activeSpec = navigation.activeSpec;
Expand Down
61 changes: 61 additions & 0 deletions src/routes/v2/pages/Editor/hooks/useEditorEscapeShortcut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useReactFlow } from "@xyflow/react";
import { useEffect } from "react";

import { useDialog } from "@/providers/DialogProvider/hooks/useDialog";
import { ESCAPE } from "@/routes/v2/shared/shortcuts/keys";
import { useSharedStores } from "@/routes/v2/shared/store/SharedStoreContext";

function hasReactFlowSelection(
reactFlow: ReturnType<typeof useReactFlow>,
): boolean {
return (
reactFlow.getNodes().some((n) => n.selected) ||
reactFlow.getEdges().some((e) => e.selected)
);
}

/**
* Centralized ESC handler for the editor. Priority:
* 1. If a dialog is open, return false so Radix's portal handler closes it.
* 2. Restore the front-most maximized window (if any).
* 3. Otherwise clear ReactFlow + editor selection state.
* Returns false when nothing applies, so the event keeps propagating.
*/
export function useEditorEscapeShortcut(): void {
const { stack } = useDialog();
const { editor, keyboard, windows } = useSharedStores();
const reactFlow = useReactFlow();

useEffect(() => {
const unregister = keyboard.registerShortcut({
id: "editor-escape",
keys: [ESCAPE],
label: "Dismiss / clear selection",
allowInEditable: true,
action: () => {
if (stack.length > 0) return false;

const front = windows.getFrontMaximizedWindow();
if (front) {
Comment thread
maxy-shpfy marked this conversation as resolved.
front.restore();
return;
}

const rfSelected = hasReactFlowSelection(reactFlow);
const editorSelected = editor.hasAnySelection;
if (!rfSelected && !editorSelected) return false;

editor.clearSelection();
if (rfSelected) {
reactFlow.setNodes((ns) =>
ns.map((n) => (n.selected ? { ...n, selected: false } : n)),
);
reactFlow.setEdges((es) =>
es.map((e) => (e.selected ? { ...e, selected: false } : e)),
);
}
},
});
return unregister;
}, [editor, keyboard, reactFlow, stack, windows]);
Comment thread
maxy-shpfy marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { Edge } from "@xyflow/react";
import { useEffect } from "react";

import type { ComponentSpec } from "@/models/componentSpec";
import {
getConduits,
toggleEdgeOnConduit,
} from "@/routes/v2/pages/Editor/nodes/ConduitNode/conduits.actions";
import { useEditorSession } from "@/routes/v2/pages/Editor/store/EditorSessionContext";
import { ESCAPE } from "@/routes/v2/shared/shortcuts/keys";
import type { EditorStore } from "@/routes/v2/shared/store/editorStore";
import { useSharedStores } from "@/routes/v2/shared/store/SharedStoreContext";

Expand Down Expand Up @@ -61,7 +59,8 @@ function useConduitSelectionMode(
* Encapsulates conduit-specific canvas behaviour:
* - styles edges when a conduit is selected (assignment-mode highlighting)
* - provides an `onEdgeClick` handler that toggles edge assignment
* - registers an Escape-key shortcut that deselects the conduit
*
* ESC deselect for conduit is handled by `useEditorEscapeShortcut`.
*/
export function useConduitEdgeMode(
edges: Edge[],
Expand All @@ -72,25 +71,10 @@ export function useConduitEdgeMode(
| ((event: React.MouseEvent, edge: { id: string }) => void)
| undefined;
} {
const { editor, keyboard } = useSharedStores();
const { editor } = useSharedStores();
const { undo } = useEditorSession();
const isConduitSelected = editor.selectedNodeType === "conduit";

useEffect(() => {
const unregister = keyboard.registerShortcut({
id: "conduit-escape",
keys: [ESCAPE],
label: "Deselect conduit",
action: () => {
if (editor.selectedNodeType === "conduit" && editor.selectedNodeId) {
editor.clearSelection();
}
},
});

return unregister;
}, [editor, keyboard, undo]);

const styledEdges = useConduitSelectionMode(edges, spec, editor);

const handleEdgeClick = (_event: React.MouseEvent, edge: { id: string }) => {
Expand Down
14 changes: 9 additions & 5 deletions src/routes/v2/shared/shortcuts/useShortcutListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ export function useShortcutListener(): void {

for (const shortcut of keyboard.shortcuts.values()) {
if (editable && !shortcut.allowInEditable) continue;
if (keyboard.matchesPressed(shortcut.keys)) {
event.preventDefault();
keyboard.clearPressed();
shortcut.action(event);
return;
if (!keyboard.matchesPressed(shortcut.keys)) continue;

const handled = shortcut.action(event);
keyboard.clearPressed();

if (handled === false) {
break;
}
event.preventDefault();
return;
}
};

Expand Down
12 changes: 11 additions & 1 deletion src/routes/v2/shared/store/editorStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { action, makeObservable, observable } from "mobx";
import { action, computed, makeObservable, observable } from "mobx";

import type { ValidationIssue } from "@/models/componentSpec";

Expand Down Expand Up @@ -73,6 +73,16 @@ export class EditorStore {
this.selectedValidationIssue = null;
}

@computed get hasAnySelection(): boolean {
return (
this.selectedNodeId !== null ||
this.selectedNodeType !== null ||
this.multiSelection.length > 0 ||
this.selectedValidationIssue !== null ||
this.focusedArgumentName !== null
);
}

@action setMultiSelection(nodes: SelectedNode[]) {
this.multiSelection = nodes;
if (nodes.length > 1) {
Expand Down
9 changes: 8 additions & 1 deletion src/routes/v2/shared/store/keyboardStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ interface ShortcutDefinition {
allowInEditable?: boolean;
// todo: add DOM element as a scope for the shortcut
// todo: add enabled: boolean;
action: (event: KeyboardEvent, params?: ShortcutParams) => void;
/**
* Return `false` to allow the native event to propagate:
* the listener skips preventDefault so the
* browser / Radix portal handlers (e.g. dialog ESC) can run.
* The shortcut registry holds at most one handler per combo,
* so this is NOT a fallthrough to another shortcut.
*/
action: (event: KeyboardEvent, params?: ShortcutParams) => void | false;
}

export class KeyboardStore {
Expand Down
10 changes: 10 additions & 0 deletions src/routes/v2/shared/windows/windowStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ export class WindowStoreImpl implements WindowStoreRef {
return this.windowOrder.map((id) => this.windows[id]);
}

/** Top-most maximized window (last in z-order among maximized). */
getFrontMaximizedWindow(): WindowModel | undefined {
for (let i = this.windowOrder.length - 1; i >= 0; i--) {
const id = this.windowOrder[i];
const win = this.windows[id];
if (win?.isMaximized) return win;
}
return undefined;
}

/** Close all windows linked to a specific entity */
@action closeWindowsByLinkedEntity(entityId: string): void {
const windowsToClose = this.windowOrder.filter(
Expand Down
Loading