Skip to content
Open
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
81 changes: 81 additions & 0 deletions packages/site/docs/manual/element/node/react-node.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,87 @@ const graph = new Graph({
});
```

## FAQ

### 1. With scroll-canvas enabled, scrolling is not triggered when the cursor is on a custom React node.

Simply add an `onWheel` event to your React custom node and forward it to the canvas DOM element.

```js | ob { inject: true }
import { DatabaseFilled } from '@ant-design/icons';
import { ExtensionCategory, Graph, register } from '@antv/g6';
import { ReactNode } from '@antv/g6-extension-react';
import { Badge, Flex, Input, Tag, Typography } from 'antd';
import { useEffect, useRef } from 'react';
import { createRoot } from 'react-dom/client';

const { Text } = Typography;
register(ExtensionCategory.NODE, 'react', ReactNode);

const Node = ({ data, onChange, graph }) => {
const { status, type } = data.data;
const ref = useRef();
useEffect(() => {
if (ref.current) {
ref.current.parentNode.addEventListener('wheel', (e) => e.preventDefault());
}
}, []);
Comment on lines +266 to +270
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

useEffect 中添加的事件监听器在组件卸载时没有被移除,这可能导致内存泄漏。应该从 useEffect 返回一个清理函数来移除事件监听器。作为文档示例,展示最佳实践非常重要。

Suggested change
useEffect(() => {
if (ref.current) {
ref.current.parentNode.addEventListener('wheel', (e) => e.preventDefault());
}
}, []);
useEffect(() => {
const parent = ref.current?.parentNode;
const onWheel = (e) => e.preventDefault();
if (parent) {
parent.addEventListener('wheel', onWheel);
}
return () => {
if (parent) {
parent.removeEventListener('wheel', onWheel);
}
};
}, []);


return (
<Flex
ref={ref}
onWheel={(e) => {
// Forward the event to the canvas DOM element
const canvas = graph.context.graph.getCanvas().getContextService().getDomElement();
const evt = new WheelEvent('wheel', e.nativeEvent);
canvas.dispatchEvent(evt);
Comment on lines +277 to +279
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

获取画布 DOM 元素的方式不正确。graph prop 是一个 ReactNode 图形实例,graph.context.graph 的值是 undefined,这将导致运行时错误。你可以通过 ReactNode 实例的 ownerDocument 属性来获取画布 DOM 元素。

Suggested change
const canvas = graph.context.graph.getCanvas().getContextService().getDomElement();
const evt = new WheelEvent('wheel', e.nativeEvent);
canvas.dispatchEvent(evt);
const canvas = graph.ownerDocument.defaultView.getContextService().getDomElement();
const evt = new WheelEvent('wheel', e.nativeEvent);
canvas.dispatchEvent(evt);

}}
style={{ background: '#fff', padding: 10, borderRadius: 5, border: '1px solid gray' }}
vertical
>
<Flex align="center" justify="space-between">
<Text>
<DatabaseFilled />
Server
<Tag>{type}</Tag>
</Text>
<Badge status={status} />
</Flex>
<Text type="secondary">{data.id}</Text>
</Flex>
);
};

export const ReactNodeDemo = () => {
const containerRef = useRef();

useEffect(() => {
const graph = new Graph({
container: containerRef.current,
data: {
nodes: [{ id: 'remote-server-1', data: { status: 'warning', type: 'remote' }, style: { x: 150, y: 50 } }],
},
node: {
type: 'react',
style: {
size: [240, 75],
component: function (data) {
return <Node data={data} graph={this} />;
},
},
},
behaviors: ['drag-element', 'scroll-canvas', 'drag-canvas'],
});
graph.render();
}, []);

return <div style={{ width: '100%', height: 200 }} ref={containerRef}></div>;
};

const root = createRoot(document.getElementById('container'));
root.render(<ReactNodeDemo />);
```

## Real Cases

```js | ob { inject: true }
Expand Down
81 changes: 81 additions & 0 deletions packages/site/docs/manual/element/node/react-node.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,87 @@ const graph = new Graph({
});
```

## 常见问题

### 1. 开启 scroll-canvas, 光标在React自定义节点上无法触发滚动

给React自定义节点添加`onWheel`事件,并转发到画布DOM元素上即可。

```js | ob { inject: true }
import { DatabaseFilled } from '@ant-design/icons';
import { ExtensionCategory, Graph, register } from '@antv/g6';
import { ReactNode } from '@antv/g6-extension-react';
import { Badge, Flex, Input, Tag, Typography } from 'antd';
import { useEffect, useRef } from 'react';
import { createRoot } from 'react-dom/client';

const { Text } = Typography;
register(ExtensionCategory.NODE, 'react', ReactNode);

const Node = ({ data, onChange, graph }) => {
const { status, type } = data.data;
const ref = useRef();
useEffect(() => {
if (ref.current) {
ref.current.parentNode.addEventListener('wheel', (e) => e.preventDefault());
}
}, []);
Comment on lines +266 to +270
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

useEffect 中添加的事件监听器在组件卸载时没有被移除,这可能导致内存泄漏。应该从 useEffect 返回一个清理函数来移除事件监听器。作为文档示例,展示最佳实践非常重要。

Suggested change
useEffect(() => {
if (ref.current) {
ref.current.parentNode.addEventListener('wheel', (e) => e.preventDefault());
}
}, []);
useEffect(() => {
const parent = ref.current?.parentNode;
const onWheel = (e) => e.preventDefault();
if (parent) {
parent.addEventListener('wheel', onWheel);
}
return () => {
if (parent) {
parent.removeEventListener('wheel', onWheel);
}
};
}, []);


return (
<Flex
ref={ref}
onWheel={(e) => {
// 转发事件到画布Dom元素上
const canvas = graph.context.graph.getCanvas().getContextService().getDomElement();
const evt = new WheelEvent('wheel', e.nativeEvent);
canvas.dispatchEvent(evt);
Comment on lines +277 to +279
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

获取画布 DOM 元素的方式不正确。graph prop 是一个 ReactNode 图形实例,graph.context.graph 的值是 undefined,这将导致运行时错误。你可以通过 ReactNode 实例的 ownerDocument 属性来获取画布 DOM 元素。

Suggested change
const canvas = graph.context.graph.getCanvas().getContextService().getDomElement();
const evt = new WheelEvent('wheel', e.nativeEvent);
canvas.dispatchEvent(evt);
const canvas = graph.ownerDocument.defaultView.getContextService().getDomElement();
const evt = new WheelEvent('wheel', e.nativeEvent);
canvas.dispatchEvent(evt);

}}
style={{ background: '#fff', padding: 10, borderRadius: 5, border: '1px solid gray' }}
vertical
>
<Flex align="center" justify="space-between">
<Text>
<DatabaseFilled />
Server
<Tag>{type}</Tag>
</Text>
<Badge status={status} />
</Flex>
<Text type="secondary">{data.id}</Text>
</Flex>
);
};

export const ReactNodeDemo = () => {
const containerRef = useRef();

useEffect(() => {
const graph = new Graph({
container: containerRef.current,
data: {
nodes: [{ id: 'remote-server-1', data: { status: 'warning', type: 'remote' }, style: { x: 150, y: 50 } }],
},
node: {
type: 'react',
style: {
size: [240, 75],
component: function (data) {
return <Node data={data} graph={this} />;
},
},
},
behaviors: ['drag-element', 'scroll-canvas', 'drag-canvas'],
});
graph.render();
}, []);

return <div style={{ width: '100%', height: 200 }} ref={containerRef}></div>;
};

const root = createRoot(document.getElementById('container'));
root.render(<ReactNodeDemo />);
```

## 实际案例

```js | ob { inject: true }
Expand Down
23 changes: 20 additions & 3 deletions packages/site/examples/element/custom-node/demo/react-node.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@ const { Text } = Typography;

register(ExtensionCategory.NODE, 'react', ReactNode);

const Node = ({ data, onChange }) => {
const Node = ({ data, onChange, graph }) => {
const { status, type } = data.data;
const ref = useRef();
useEffect(() => {
if (ref.current) {
ref.current.parentNode.addEventListener('wheel', (e) => {
e.preventDefault();
});
}
}, []);
Comment on lines +15 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

useEffect hook 中添加的事件监听器在组件卸载时没有被移除,这可能会导致内存泄漏。应该从 useEffect 返回一个清理函数来移除事件监听器。

  useEffect(() => {
    const parent = ref.current?.parentNode;
    const onWheel = (e) => {
      e.preventDefault();
    };

    if (parent) {
      parent.addEventListener('wheel', onWheel);
    }

    return () => {
      if (parent) {
        parent.removeEventListener('wheel', onWheel);
      }
    };
  }, []);


return (
<Flex
ref={ref}
onWheel={(e) => {
// 转发事件到画布Dom元素上
const canvas = graph.context.graph.getCanvas().getContextService().getDomElement();
const evt = new WheelEvent('wheel', e.nativeEvent);
canvas.dispatchEvent(evt);
Comment on lines +28 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

获取画布 DOM 元素的方式不正确。graph prop 是一个 ReactNode 图形实例,因此 graph.context.graphundefined,这将导致运行时错误。你可以通过 ReactNode 实例的 ownerDocument 属性来获取画布 DOM 元素。

        const canvas = graph.ownerDocument.defaultView.getContextService().getDomElement();
        const evt = new WheelEvent('wheel', e.nativeEvent);
        canvas.dispatchEvent(evt);

}}
style={{
width: '100%',
height: '100%',
Expand Down Expand Up @@ -76,10 +91,12 @@ export const ReactNodeDemo = () => {
type: 'react',
style: {
size: [240, 100],
component: (data) => <Node data={data} />,
component: function (data) {
return <Node data={data} graph={this} />;
},
},
},
behaviors: ['drag-element', 'zoom-canvas', 'drag-canvas'],
behaviors: ['drag-element', 'scroll-canvas', 'drag-canvas'],
});

graph.render();
Expand Down
Loading