Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/edge updata #105

Merged
merged 2 commits into from
Aug 7, 2024
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
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
Loading