Skip to content

Commit

Permalink
✨ feat: add addEdge function, add hotkeymanager switch
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangchu committed Jul 22, 2024
1 parent ddc8300 commit c7eea15
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/FlowEditor/container/FlowEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface FlowEditorAppProps {
children?: React.ReactNode;
background?: boolean;
miniMap?: boolean;
hotkeyManager?: boolean;
}

const FlowEditor = forwardRef<any, FlowEditorAppProps>(
Expand All @@ -102,6 +103,7 @@ const FlowEditor = forwardRef<any, FlowEditorAppProps>(
children,
background = true,
miniMap = true,
hotkeyManager = true,
onNodesInit,

beforeConnect = () => true,
Expand Down Expand Up @@ -148,7 +150,7 @@ const FlowEditor = forwardRef<any, FlowEditorAppProps>(
const instance = useReactFlow();

// 添加快捷键监听
useHotkeyManager();
useHotkeyManager(hotkeyManager);

// 抛出 viewport 变化的事件
useOnViewportChange({
Expand Down
8 changes: 7 additions & 1 deletion src/FlowEditor/hooks/useHotkeyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useHotkeys } from 'react-hotkeys-hook';

import { useStore } from '../store';

export const useHotkeyManager = () => {
export const useHotkeyManager = (open = true) => {
const [selectAll, undo, redo, copySelection, paste] = useStore((s) => [
s.selectAll,
s.undo,
Expand All @@ -12,27 +12,32 @@ export const useHotkeyManager = () => {
]);

useHotkeys('meta+a', (e) => {
if (!open) return;
e.preventDefault();

selectAll();
});
useHotkeys('meta+z', (e) => {
if (!open) return;
e.preventDefault();

undo();
});
useHotkeys('meta+c', (e) => {
if (!open) return;
e.preventDefault();

copySelection();
});
useHotkeys('meta+v', (e) => {
if (!open) return;
e.preventDefault();

paste();
});

useHotkeys('meta+shift+z', (e) => {
if (!open) return;
e.preventDefault();

redo();
Expand All @@ -41,6 +46,7 @@ export const useHotkeyManager = () => {
// 由于 react-flow 的 Backspace 实现逻辑有瑕疵,因此自行实现了一遍
// refs: https://github.com/wbkd/react-flow/issues/2826
useHotkeys('backspace', (e) => {
if (!open) return;
e.preventDefault();

// beforeActionCallback(handleDelete, HotKeyAction.deleteSelection);
Expand Down
5 changes: 5 additions & 0 deletions src/FlowEditor/store/slices/edgesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { EdgeDispatch, edgesReducer } from '../reducers/edge';

export interface PublicEdgesAction {
dispatchEdges: (payload: EdgeDispatch, options?: ActionOptions) => void;
addEdge: (edge: Edge) => void;
addEdges: (edges: Record<string, Edge>, options?: ActionOptions) => void;
deleteEdge: (id: string) => void;
deleteEdges: (ids: string[]) => void;
Expand Down Expand Up @@ -73,6 +74,10 @@ export const edgesSlice: StateCreator<
);
}
},
addEdge: (edge) => {
get().dispatchEdges({ type: 'addEdge', edge: edge });
},

addEdges: (edges, options) => {
get().dispatchEdges({ type: 'addEdges', edges: edges }, options);
},
Expand Down

0 comments on commit c7eea15

Please sign in to comment.