listen hotkey in workspace

This commit is contained in:
codetypes committed 2025-05-08 18:19:12 +08:00
1 parent 6f670786e4
commit bb40b4fd5b
3 files changed
+43 -47

No files matched your search

+2 -45
View File
@@ -8,7 +8,6 @@ import { FiDelete } from "react-icons/fi";
import { IoMdReturnLeft } from "react-icons/io"; import { IoMdReturnLeft } from "react-icons/io";
import { RiFocus3Line } from "react-icons/ri"; import { RiFocus3Line } from "react-icons/ri";
import { VscCaseSensitive } from "react-icons/vsc"; import { VscCaseSensitive } from "react-icons/vsc";
import { mergeRefs } from "react-merge-refs";
import { useDebounceCallback } from "usehooks-ts"; import { useDebounceCallback } from "usehooks-ts";
import { useShallow } from "zustand/react/shallow"; import { useShallow } from "zustand/react/shallow";
import { import {
@@ -19,7 +18,7 @@ import {
useWorkspace, useWorkspace,
} from "../contexts/workspace-context"; } from "../contexts/workspace-context";
import i18n from "../misc/i18n"; import i18n from "../misc/i18n";
import { Hotkey, isMacos, useKeyPress } from "../misc/keys"; import { Hotkey, isMacos } from "../misc/keys";
import { mergeClassNames } from "../misc/util"; import { mergeClassNames } from "../misc/util";
import { FilterOption, Graph } from "./graph"; import { FilterOption, Graph } from "./graph";
import "./register-node"; import "./register-node";
@@ -109,18 +108,6 @@ const createMenu = () => {
return arr; return arr;
}; };
const hotkeyMap: Record<string, EditEvent> = {
[Hotkey.Copy]: "copy",
[Hotkey.Replace]: "replace",
[Hotkey.Paste]: "paste",
[Hotkey.Insert]: "insert",
[Hotkey.Enter]: "insert",
[Hotkey.Delete]: "delete",
[Hotkey.Backspace]: "delete",
[Hotkey.Undo]: "undo",
[Hotkey.Redo]: "redo",
};
export const Editor: FC<EditorProps> = ({ onChange, data: editor, ...props }) => { export const Editor: FC<EditorProps> = ({ onChange, data: editor, ...props }) => {
const workspace = useWorkspace( const workspace = useWorkspace(
useShallow((state) => ({ useShallow((state) => ({
@@ -128,34 +115,6 @@ export const Editor: FC<EditorProps> = ({ onChange, data: editor, ...props }) =>
})) }))
); );
const keysRef = useRef<HTMLDivElement>(null);
useKeyPress(
[
Hotkey.Copy,
Hotkey.Replace,
Hotkey.Paste,
Hotkey.Insert,
Hotkey.Enter,
Hotkey.Delete,
Hotkey.Backspace,
],
keysRef,
(e, key) => {
e.preventDefault();
e.stopImmediatePropagation();
e.stopPropagation();
editor.dispatch?.(hotkeyMap[key]);
}
);
useKeyPress([Hotkey.Undo, Hotkey.Redo], null, (e, key) => {
if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) {
return;
}
e.stopPropagation();
editor.dispatch?.(hotkeyMap[key]);
});
const searchInputRef = useRef<InputRef>(null); const searchInputRef = useRef<InputRef>(null);
const graphRef = useRef(null); const graphRef = useRef(null);
const sizeRef = useRef(null); const sizeRef = useRef(null);
@@ -309,7 +268,6 @@ export const Editor: FC<EditorProps> = ({ onChange, data: editor, ...props }) =>
} else if (event === "clickVar") { } else if (event === "clickVar") {
graph.clickVar(data as string); graph.clickVar(data as string);
} }
keysRef.current?.focus();
}; };
if (graph) { if (graph) {
@@ -369,7 +327,7 @@ export const Editor: FC<EditorProps> = ({ onChange, data: editor, ...props }) =>
<div <div
{...props} {...props}
className="b3-editor" className="b3-editor"
ref={mergeRefs([sizeRef, keysRef])} ref={sizeRef}
tabIndex={-1} tabIndex={-1}
style={{ maxWidth: "inherit", maxHeight: "inherit" }} style={{ maxWidth: "inherit", maxHeight: "inherit" }}
> >
@@ -491,7 +449,6 @@ export const Editor: FC<EditorProps> = ({ onChange, data: editor, ...props }) =>
filterType: "content", filterType: "content",
placeholder: "", placeholder: "",
}); });
keysRef.current?.focus();
}} }}
/> />
</Flex> </Flex>
+40 -1
View File
@@ -9,7 +9,7 @@ import useForceUpdate from "use-force-update";
import { useWindowSize } from "usehooks-ts"; import { useWindowSize } from "usehooks-ts";
import { useShallow } from "zustand/react/shallow"; import { useShallow } from "zustand/react/shallow";
import { useSetting } from "../contexts/setting-context"; import { useSetting } from "../contexts/setting-context";
import { EditorStore, useWorkspace } from "../contexts/workspace-context"; import { EditEvent, EditorStore, useWorkspace } from "../contexts/workspace-context";
import { modal } from "../misc/hooks"; import { modal } from "../misc/hooks";
import { Hotkey, isMacos, setInputFocus, useKeyPress } from "../misc/keys"; import { Hotkey, isMacos, setInputFocus, useKeyPress } from "../misc/keys";
import Path from "../misc/path"; import Path from "../misc/path";
@@ -20,6 +20,18 @@ import { TitleBar } from "./titlebar";
const { Header, Content, Sider } = Layout; const { Header, Content, Sider } = Layout;
const hotkeyMap: Record<string, EditEvent> = {
[Hotkey.Copy]: "copy",
[Hotkey.Replace]: "replace",
[Hotkey.Paste]: "paste",
[Hotkey.Insert]: "insert",
[Hotkey.Enter]: "insert",
[Hotkey.Delete]: "delete",
[Hotkey.Backspace]: "delete",
[Hotkey.Undo]: "undo",
[Hotkey.Redo]: "redo",
};
export const Workspace: FC = () => { export const Workspace: FC = () => {
const workspace = useWorkspace( const workspace = useWorkspace(
useShallow((state) => ({ useShallow((state) => ({
@@ -93,6 +105,33 @@ export const Workspace: FC = () => {
} }
}, [workspace.isShowingSearch]); }, [workspace.isShowingSearch]);
useKeyPress(
[
Hotkey.Copy,
Hotkey.Replace,
Hotkey.Paste,
Hotkey.Insert,
Hotkey.Enter,
Hotkey.Delete,
Hotkey.Backspace,
],
keysRef,
(e, key) => {
e.preventDefault();
e.stopImmediatePropagation();
e.stopPropagation();
workspace.editing?.dispatch?.(hotkeyMap[key]);
}
);
useKeyPress([Hotkey.Undo, Hotkey.Redo], null, (e, key) => {
if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) {
return;
}
e.stopPropagation();
workspace.editing?.dispatch?.(hotkeyMap[key]);
});
useEffect(() => { useEffect(() => {
const editor = workspace.editing; const editor = workspace.editing;
if (editor?.alertReload) { if (editor?.alertReload) {
+1 -1
View File
@@ -1,6 +1,6 @@
import { NodeDef } from "../behavior3/src/behavior3"; import { NodeDef } from "../behavior3/src/behavior3";
export const VERSION = "1.8.1"; export const VERSION = "1.8.2";
export type NodeType = NodeDef["type"] | "Other" | "Error"; export type NodeType = NodeDef["type"] | "Other" | "Error";
export type NodeArg = Exclude<NodeDef["args"], undefined>[number]; export type NodeArg = Exclude<NodeDef["args"], undefined>[number];