Skip to content

Commit

Permalink
🐛 fix: add node will reset viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangchu committed Mar 7, 2024
1 parent c291e42 commit fa60abc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 71 deletions.
6 changes: 6 additions & 0 deletions docs/guide/demos/flowViewIntro/autoFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ function App() {
<FlowView
nodes={nodes}
edges={edges}
layoutOptions={{
rankdir: 'BT',
align: 'DL',
nodesep: 1000,
ranksep: 500,
}}
onEdgeClick={(e, edge) => {
console.log(edge);
}}
Expand Down
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

0 comments on commit fa60abc

Please sign in to comment.