Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add site demos
Browse files Browse the repository at this point in the history
Aarebecca committed Nov 28, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 8e2929e commit f578d5b
Showing 10 changed files with 686 additions and 61 deletions.
7 changes: 4 additions & 3 deletions packages/react-node/src/GNode/node.tsx
Original file line number Diff line number Diff line change
@@ -25,9 +25,7 @@ export const createReactGNode = (
shapeMap: NodeShapeMap,
): DisplayObject<any, any> {
const { data } = model;
const {
size: [width, height],
} = data as any;
const { size: [width, height] = [0, 0] } = data as any;
return this.upsertShape(
shape,
'keyShape',
@@ -80,6 +78,9 @@ export const createReactGNode = (
);
render(content, group);
});
group.addEventListener('destroy', () => {
group.removeAllEventListeners();
});

return {
[groupId]: group,
9 changes: 7 additions & 2 deletions packages/react-node/src/ReactNode/node.tsx
Original file line number Diff line number Diff line change
@@ -83,19 +83,24 @@ export const createReactNode = (
{ leading: true, trailing: true },
) as EventListener;

dom.addEventListener('pointermove', onMouseMove);
document.addEventListener('pointermove', onMouseMove);

dom.addEventListener(
'pointerup',
() => {
dom.style.userSelect = 'auto';
dom.removeEventListener('pointermove', onMouseMove);
document.removeEventListener('pointermove', onMouseMove);
},
{ once: true },
);
});
});

html.addEventListener('destroy', () => {
html.removeAllEventListeners();
html.getDomElement().remove();
});

html.getRenderBounds = html.getBounds;

ShapeCollection.set(model.id, html);
8 changes: 8 additions & 0 deletions packages/site/.dumi/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if (typeof window !== 'undefined') {
window.onresize = () => {
const { graph, container } = window as any;
if (!graph || graph.destroyed) return;
if (!container || !container.scrollWidth || !container.scrollHeight) return;
graph.setSize([container.scrollWidth, container.scrollHeight]);
};
}
4 changes: 4 additions & 0 deletions packages/site/.dumi/global.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-nocheck

if (window) {
// window.g6 = require('@antv/g6/es'); // import the source for debugging
window.g6 = require('@antv/g6/lib'); // import the source for debugging
@@ -15,7 +17,9 @@ if (window) {
window.GraphLayoutPredict = require('@antv/vis-predict-engine');
window.stats = require('stats.js');
window.g2 = require('@antv/g2');
window.antd = require('antd');

window.React = require('react');
window.ReactDOM = require('react-dom');
window.g6ReactNode = require('@antv/g6-react-node');
}
55 changes: 55 additions & 0 deletions packages/site/examples/item/customNode/demo/jsx-g-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Graph, extend } from '@antv/g6';
import { createReactGNode, Rect, Text } from '@antv/g6-react-node';

const Node = ({ model }) => {
const { data } = model;
const {
value,
size: [width, height],
} = data;

return (
<Rect width={width} height={height} fill="#f1f0ff">
<Text text={`${(value * 100).toFixed(2)}%`}></Text>
<Rect y={4} width={value * width} height={height - 8} fill="#6395fa" />
</Rect>
);
};

const ReactGNode = createReactGNode(Node);

const ExtendGraph = extend(Graph, {
nodes: {
'react-g-node': ReactGNode,
},
});

const container = document.getElementById('container');
const width = container.scrollWidth;
const height = container.scrollHeight || 500;

window.graph = new ExtendGraph({
container: 'container',
width,
height,
autoFit: 'center',
modes: {
default: [{ type: 'drag-node', enableTransient: false }],
},
data: {
nodes: [
{ id: 'node1', data: { size: [100, 30], value: 0.5 } },
{ id: 'node2', data: { size: [100, 30], value: 0.8 } },
],
edges: [
{
source: 'node1',
target: 'node2',
},
],
},
node: {
type: 'react-g-node',
otherShapes: {},
},
});
16 changes: 16 additions & 0 deletions packages/site/examples/item/customNode/demo/meta.json
Original file line number Diff line number Diff line change
@@ -51,6 +51,22 @@
"en": "G2 Active Node"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*GVyoQKk2WIIAAAAAAAAAAAAADmJ7AQ/original"
},
{
"filename": "jsx-g-node.js",
"title": {
"zh": "使用 JSX 写法创建自定义 G 节点",
"en": "JSX G Node"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*nFzoQ7qBLvEAAAAAAAAAAAAADmJ7AQ/original"
},
{
"filename": "react-node.js",
"title": {
"zh": "使用 React 写法创建自定义 G 节点",
"en": "React Node"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*wyOxRoAbiPYAAAAAAAAAAAAADmJ7AQ/original"
}
]
}
47 changes: 47 additions & 0 deletions packages/site/examples/item/customNode/demo/react-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Graph, extend } from '@antv/g6';
import { createReactNode } from '@antv/g6-react-node';
import { Alert } from 'antd';

const Node = ({ model }) => {
const { data } = model;
const { message, nodeType } = data;
return <Alert message={message} type={nodeType} showIcon />;
};

const ReactNode = createReactNode(Node);

const ExtendGraph = extend(Graph, {
nodes: {
'react-node': ReactNode,
},
});

const container = document.getElementById('container');
const width = container.scrollWidth;
const height = container.scrollHeight || 500;

window.graph = new ExtendGraph({
container: 'container',
width,
height,
autoFit: 'center',
modes: {
default: [{ type: 'drag-node', enableTransient: false }],
},
data: {
nodes: [
{ id: 'node1', data: { size: [120, 40], message: 'Success', nodeType: 'success' } },
{ id: 'node2', data: { size: [120, 40], message: 'Warning', nodeType: 'warning' } },
],
edges: [
{
source: 'node1',
target: 'node2',
},
],
},
node: {
type: 'react-node',
otherShapes: {},
},
});
4 changes: 4 additions & 0 deletions packages/site/global.d.ts
Original file line number Diff line number Diff line change
@@ -16,5 +16,9 @@ declare global {
g2: any;
React: any;
ReactDOM: any;
antd: any;

graph: any;
container: any;
}
}
3 changes: 2 additions & 1 deletion packages/site/package.json
Original file line number Diff line number Diff line change
@@ -39,14 +39,15 @@
"@antv/g2": "^5.1.5",
"@antv/g6": "workspace:*",
"@antv/g6-plugin-map-view": "workspace:*",
"@antv/g6-react-node": "^1.4.5",
"@antv/g6-react-node": "^2.0.0-beta.0",
"@antv/graphlib": "^2.0.2",
"@antv/layout-gpu": "^1.1.5",
"@antv/layout-wasm": "^1.3.4",
"@antv/util": "^2.0.9",
"@antv/vis-predict-engine": "^0.1.1",
"@faker-js/faker": "^8.0.2",
"@microsoft/api-extractor": "^7.33.6",
"antd": "^5.10.2",
"dumi": "^2.2.14",
"fs-extra": "latest",
"google-translate-api-x": "^10.6.7",
594 changes: 539 additions & 55 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

0 comments on commit f578d5b

Please sign in to comment.