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
20 changes: 7 additions & 13 deletions src/routes/v2/pages/Editor/hooks/useEditorEscapeShortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useReactFlow } from "@xyflow/react";
import { useEffect } from "react";

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

Expand All @@ -25,6 +26,7 @@ export function useEditorEscapeShortcut(): void {
const { stack } = useDialog();
const { editor, keyboard, windows } = useSharedStores();
const reactFlow = useReactFlow();
const deselectAll = useDeselectAll();

useEffect(() => {
const unregister = keyboard.registerShortcut({
Expand All @@ -41,21 +43,13 @@ export function useEditorEscapeShortcut(): void {
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)),
);
if (!hasReactFlowSelection(reactFlow) && !editor.hasAnySelection) {
return false;
}

deselectAll();
},
});
return unregister;
}, [editor, keyboard, reactFlow, stack, windows]);
}, [editor, keyboard, reactFlow, stack, windows, deselectAll]);
}
13 changes: 10 additions & 3 deletions src/routes/v2/pages/Editor/hooks/useSelectionWindowSync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useEffect } from "react";

import { ContextPanelContent } from "@/routes/v2/pages/Editor/components/ContextPanel/ContextPanel";
import { PinnedTaskContent } from "@/routes/v2/pages/Editor/components/PinnedTaskContent/PinnedTaskContent";
import { useDeselectAll } from "@/routes/v2/shared/hooks/useDeselectAll";
import type { EditorStore } from "@/routes/v2/shared/store/editorStore";
import type { NavigationStore } from "@/routes/v2/shared/store/navigationStore";
import { useSharedStores } from "@/routes/v2/shared/store/SharedStoreContext";
Expand Down Expand Up @@ -76,7 +77,10 @@ function scrollWindowIntoView() {
}, 100);
}

function ensureContextPanelVisible(windows: WindowStoreImpl) {
function ensureContextPanelVisible(
windows: WindowStoreImpl,
deselectAll: () => void,
) {
const existing = windows.getWindowById(CONTEXT_PANEL_WINDOW_ID);

if (existing) {
Expand All @@ -96,6 +100,8 @@ function ensureContextPanelVisible(windows: WindowStoreImpl) {
startVisible: true,
persisted: true,
defaultDockState: "right",
disabledActions: ["hide"],
onClose: deselectAll,
});
scrollWindowIntoView();
}
Expand All @@ -107,6 +113,7 @@ function closeContextPanel(windows: WindowStoreImpl) {

export function useSelectionWindowSync() {
const { editor, navigation, windows } = useSharedStores();
const deselectAll = useDeselectAll();

useEffect(() => {
const disposeSelectionWatcher = reaction(
Expand Down Expand Up @@ -138,13 +145,13 @@ export function useSelectionWindowSync() {
multiSelectionLength > 1 || (selectedNodeId && selectedNodeType);

if (shouldShowPanel) {
ensureContextPanelVisible(windows);
ensureContextPanelVisible(windows, deselectAll);
} else {
closeContextPanel(windows);
}
},
);

return disposeSelectionWatcher;
}, [editor, navigation, windows]);
}, [editor, navigation, windows, deselectAll]);
}
22 changes: 22 additions & 0 deletions src/routes/v2/shared/hooks/useDeselectAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useReactFlow } from "@xyflow/react";

import { useSharedStores } from "@/routes/v2/shared/store/SharedStoreContext";

/**
* Returns a function that clears all selection state: the editor store and
* ReactFlow's internal node/edge `selected` flags.
*/
export function useDeselectAll(): () => void {
const { editor } = useSharedStores();
const reactFlow = useReactFlow();

return () => {
editor.clearSelection();
reactFlow.setNodes((ns) =>
ns.map((n) => (n.selected ? { ...n, selected: false } : n)),
);
reactFlow.setEdges((es) =>
es.map((e) => (e.selected ? { ...e, selected: false } : e)),
);
};
}
Loading