|
@@ -7,22 +7,61 @@ import { Clipboard } from '@antv/x6-plugin-clipboard'
|
|
|
import { Selection } from '@antv/x6-plugin-selection'
|
|
|
import { History } from '@antv/x6-plugin-history'
|
|
|
import { Keyboard } from '@antv/x6-plugin-keyboard'
|
|
|
+import { useModel } from 'umi'
|
|
|
export default function GraphModel() {
|
|
|
const [graph, setGraph] = useState<Graph>();
|
|
|
const [dnd, setDnd] = useState<Dnd>();
|
|
|
|
|
|
+ const pageNodeRef = useRef<Node>();
|
|
|
const dndRef = useRef(dnd);
|
|
|
const graphRef = useRef(graph);
|
|
|
+ const { pageState } = useModel('appModel');
|
|
|
|
|
|
+ /**初始化页面节点 */
|
|
|
+ const initPageNode = () => {
|
|
|
+ const graph = graphRef.current;
|
|
|
+ const background = pageState.backgroundColor === 'transparent' ? '#fff' : pageState.backgroundColor;
|
|
|
+ pageNodeRef.current = graph?.addNode({
|
|
|
+ shape: 'rect',
|
|
|
+ width: pageState.width,
|
|
|
+ height: pageState.height,
|
|
|
+ zIndex: -1,
|
|
|
+ attrs: {
|
|
|
+ body: {
|
|
|
+ strokeWidth: 0,
|
|
|
+ fill: background,
|
|
|
+ filter: {
|
|
|
+ name: 'dropShadow',
|
|
|
+ args: {
|
|
|
+ dx: 2,
|
|
|
+ dy: 2,
|
|
|
+ blur: 4,
|
|
|
+ color: 'rgba(0, 0, 0, .1)',
|
|
|
+ }
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ data: {
|
|
|
+ isPage: true
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ const enabledTransform = (node: Node) => {
|
|
|
+ const data = node.getData<{ isPage: boolean }>();
|
|
|
+ return !data?.isPage;
|
|
|
+ }
|
|
|
/**初始化画布 */
|
|
|
const initGraph = (instance: Graph) => {
|
|
|
// 添加插件
|
|
|
instance
|
|
|
.use(
|
|
|
new Transform({
|
|
|
- resizing: true,
|
|
|
+ resizing: {
|
|
|
+ enabled: enabledTransform
|
|
|
+ },
|
|
|
rotating: {
|
|
|
- enabled: true,
|
|
|
+ enabled: enabledTransform,
|
|
|
grid: 1
|
|
|
},
|
|
|
}),
|
|
@@ -35,31 +74,39 @@ export default function GraphModel() {
|
|
|
.use(new Snapline())
|
|
|
.use(new Keyboard())
|
|
|
.use(new Clipboard())
|
|
|
- .use(new History())
|
|
|
+ .use(new History());
|
|
|
|
|
|
// 控制连接桩显示/隐藏
|
|
|
const showPorts = (ports: NodeListOf<SVGElement>, show: boolean) => {
|
|
|
+ console.log(ports)
|
|
|
for (let i = 0, len = ports.length; i < len; i += 1) {
|
|
|
ports[i].style.visibility = show ? 'visible' : 'hidden'
|
|
|
}
|
|
|
}
|
|
|
- instance.on('node:mouseenter', () => {
|
|
|
- const container = document.getElementById('graph-container')!
|
|
|
+ instance.on('node:mouseenter', ({_e, node}:{_e: React.MouseEvent<HTMLDivElement, MouseEvent>, node: Node.Metadata}) => {
|
|
|
+ if(node.data?.isPage) return;
|
|
|
+
|
|
|
+ const container = document.querySelector(`[data-cell-id="${node.id}"]`)!;
|
|
|
+ if(!container) return;
|
|
|
const ports = container.querySelectorAll(
|
|
|
'.x6-port-body',
|
|
|
- ) as NodeListOf<SVGElement>
|
|
|
- showPorts(ports, true)
|
|
|
+ ) as NodeListOf<SVGElement>;
|
|
|
+ showPorts(ports, true);
|
|
|
})
|
|
|
- instance.on('node:mouseleave', () => {
|
|
|
- const container = document.getElementById('graph-container')!
|
|
|
+ instance.on('node:mouseleave', ({_e, node}:{_e: React.MouseEvent<HTMLDivElement, MouseEvent>, node: Node.Metadata}) => {
|
|
|
+ if(node.data?.isPage) return;
|
|
|
+
|
|
|
+ const container = document.querySelector('#graph-container')!;
|
|
|
+ if(!container) return;
|
|
|
const ports = container.querySelectorAll(
|
|
|
'.x6-port-body',
|
|
|
- ) as NodeListOf<SVGElement>
|
|
|
- showPorts(ports, false)
|
|
|
+ ) as NodeListOf<SVGElement>;
|
|
|
+ showPorts(ports, false);
|
|
|
})
|
|
|
|
|
|
setGraph(instance);
|
|
|
graphRef.current = instance;
|
|
|
+ initPageNode();
|
|
|
}
|
|
|
|
|
|
/**初始化拖拽 */
|
|
@@ -73,8 +120,7 @@ export default function GraphModel() {
|
|
|
if(!dndRef.current || !graphRef.current) return;
|
|
|
|
|
|
// 往画布添加节点
|
|
|
- const n = graphRef.current.createNode(node)
|
|
|
-
|
|
|
+ const n = graphRef.current.createNode(node);
|
|
|
dndRef.current.start(n, e.nativeEvent as any)
|
|
|
};
|
|
|
|