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: add node will reset viewport #91

Merged
merged 2 commits into from
Mar 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
70 changes: 6 additions & 64 deletions src/FlowEditor/container/FlowEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createStyles, cx } from 'antd-style';
import isEqual from 'fast-deep-equal';
import { debounce } from 'lodash-es';
import { JSXElementConstructor, forwardRef, useCallback, useEffect, useMemo } from 'react';
import { JSXElementConstructor, forwardRef, useCallback, useEffect, useMemo, useRef } from 'react';
import { Flexbox } from 'react-layout-kit';
import ReactFlow, {
Background,
Expand Down Expand Up @@ -117,6 +117,7 @@ const FlowEditor = forwardRef<any, FlowEditorAppProps>(
const editor = useFlowEditor();

const nodesInitialized = useNodesInitialized();
const firstRender = useRef(false);

const flowInit = useMemo(() => {
if (nodesInitialized) {
Expand Down Expand Up @@ -155,6 +156,10 @@ const FlowEditor = forwardRef<any, FlowEditorAppProps>(
});

useEffect(() => {
if (firstRender.current) {
return;
}
firstRender.current = true;
// 先把画布的 viewport 设置好
if (!defaultViewport) {
instance.fitView();
Expand All @@ -168,69 +173,6 @@ const FlowEditor = forwardRef<any, FlowEditorAppProps>(
}
}, [nodesInitialized]);

// const handleNodesChange = useCallback(
// (changes: NodeChange[]) => {
// if (!get().beforeNodesChange(changes)) {
// return;
// }
// // 选择逻辑 nodes 和 edges 一致
// changes.forEach((c) => {
// switch (c.type) {
// case 'add':
// dispatchNodes({ type: 'addNode', node: c.item });
// break;
// case 'position':
// // 结束拖拽时,会触发一次 position,此时 dragging 为 false
// if (!c.dragging) break;

// dispatchNodes({ type: 'updateNodePosition', position: c.position, id: c.id });

// break;
// case 'remove':
// deselectElement(c.id);
// dispatchNodes({ type: 'deleteNode', id: c.id });
// break;
// case 'select':
// onElementSelectChange(c.id, c.selected);
// }
// });

// if (onNodesChange) {
// throttle(onNodesChange, 50)(changes);
// }

// if (afterNodesChange) {
// afterNodesChange(changes);
// }
// },
// [onNodesChange],
// );

// const handleEdgesChange = useCallback((changes: EdgeChange[]) => {
// if (!beforeEdgesChange(changes)) {
// return;
// }

// // reactflow 的 edges change 事件,只有 select 和 remove
// updateEdgesOnEdgeChange(changes);

// // 选择逻辑 nodes 和 edges 一致
// changes.forEach((c) => {
// switch (c.type) {
// case 'select':
// onElementSelectChange(c.id, c.selected);
// }
// });

// if (onEdgesChange) {
// onEdgesChange(changes);
// }

// if (afterEdgeChange) {
// afterEdgeChange(changes);
// }
// }, []);

const handleConnect = useCallback(
(connection: Connection) => {
if (!beforeConnect(connection)) {
Expand Down
2 changes: 1 addition & 1 deletion src/FlowEditor/store/reducers/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const edgesReducer = (state: EdgesState, payload: EdgeDispatch): EdgesSta
case 'updateEdge':
return produce(state, (draftState) => {
const { id, edge } = payload;
console.log(draftState[id]);

draftState[id] = { ...draftState[id], ...edge };
});

Expand Down
12 changes: 6 additions & 6 deletions src/FlowView/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ description: 基础画布容器

#### LayoutOptions

| 属性名 | 类型 | 描述 | 默认值 | 必选 |
| ------- | ------------------------------ | ------------ | ------ | ---- |
| rankdir | `'TB' \| 'BT' \| 'LR' \| 'RL'` | 无级缩放监听 | - | - |
| align | `'UL' \| 'DL' \| 'UR' \| 'DR'` | 无级缩放监听 | - | - |
| nodesep | `number` | 自动布局参数 | - | - |
| ranksep | `number` | 自动布局参数 | - | - |
| 属性名 | 类型 | 描述 | 默认值 | 必选 |
| ------- | ------------------------------ | -------- | ------ | ---- |
| rankdir | `'TB' \| 'BT' \| 'LR' \| 'RL'` | 布局样式 | - | - |
| align | `'UL' \| 'DL' \| 'UR' \| 'DR'` | 左右对齐 | - | - |
| nodesep | `number` | 列间距 | - | - |
| ranksep | `number` | 行间距 | - | - |

### FlowViewNode

Expand Down
Loading