-
Notifications
You must be signed in to change notification settings - Fork 1.6k
docs: react节点添加一个组合scroll-canvas的FAQ #7520
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
base: v5
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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()); | ||||||||||||||
| } | ||||||||||||||
| }, []); | ||||||||||||||
|
|
||||||||||||||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 获取画布 DOM 元素的方式不正确。
Suggested change
|
||||||||||||||
| }} | ||||||||||||||
| 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 } | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 获取画布 DOM 元素的方式不正确。
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||||||||||
| 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 } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 获取画布 DOM 元素的方式不正确。 const canvas = graph.ownerDocument.defaultView.getContextService().getDomElement();
const evt = new WheelEvent('wheel', e.nativeEvent);
canvas.dispatchEvent(evt); |
||
| }} | ||
| style={{ | ||
| width: '100%', | ||
| height: '100%', | ||
|
|
@@ -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(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useEffect中添加的事件监听器在组件卸载时没有被移除,这可能导致内存泄漏。应该从useEffect返回一个清理函数来移除事件监听器。作为文档示例,展示最佳实践非常重要。