Skip to content

Commit

Permalink
Fix/edge updata (#105)
Browse files Browse the repository at this point in the history
* ✨ feat: add addEdge function, add hotkeymanager switch

* ✨ feat: add addNodes function ways

---------

Co-authored-by: jiangchu <jiangchu.wzy@antgroup.com>
  • Loading branch information
ModestFun and jiangchu authored Aug 7, 2024
1 parent ddc8300 commit ca49e83
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 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
16 changes: 11 additions & 5 deletions src/FlowEditor/store/reducers/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ export const edgesReducer = (state: EdgesState, payload: EdgeDispatch): EdgesSta
const { edges } = payload;
if (!edges) return;

Object.keys(edges).forEach((id) => {
if (!draftState[id]) {
draftState[id] = edges[id];
}
});
if (Array.isArray(edges)) {
edges.forEach((edge) => {
draftState[edge.id] = edge;
});
} else if (typeof edges === 'object') {
Object.keys(edges).forEach((id) => {
if (!draftState[id]) {
draftState[id] = edges[id];
}
});
}
});

case 'updateEdge':
Expand Down
16 changes: 11 additions & 5 deletions src/FlowEditor/store/reducers/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,17 @@ export const nodeReducer = (state: FlattenNodes, action: NodeDispatch): FlattenN
const { nodes } = action;
if (!nodes) return;

Object.keys(nodes).forEach((id) => {
if (!draftState[id]) {
draftState[id] = nodes[id];
}
});
if (Array.isArray(nodes)) {
nodes.forEach((node) => {
draftState[node.id] = node;
});
} else if (typeof nodes === 'object') {
Object.keys(nodes).forEach((id) => {
if (!draftState[id]) {
draftState[id] = nodes[id];
}
});
}
});

case 'deleteNode':
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 ca49e83

Please sign in to comment.