From 683eea771a730d6646a57dc122395a9ab88bc984 Mon Sep 17 00:00:00 2001 From: zhandouxiaojiji Date: Wed, 21 Oct 2020 20:04:24 +0800 Subject: [PATCH] drag and focusItem --- render-process/Editor/index.tsx | 38 ++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/render-process/Editor/index.tsx b/render-process/Editor/index.tsx index fa6f64b..4a9d2bb 100644 --- a/render-process/Editor/index.tsx +++ b/render-process/Editor/index.tsx @@ -39,8 +39,7 @@ export default class Editor extends React.Component { width: window.screen.width * 0.66, height: window.screen.height, animate: false, - fitView: true, - fitViewPadding: [20, 20, 20, 20], + // fitCenter: true, modes: { default: [ 'drag-canvas', @@ -86,14 +85,7 @@ export default class Editor extends React.Component { }); graph.on('nodeselectchange', (e: G6GraphEvent) => { - if (this.state.curNode) { - graph.setItemState(this.state.curNode, 'selected', false); - } - const curNode = e.target as INode; - this.setState({ curNode }); - if (this.state.curNode) { - graph.setItemState(this.state.curNode, 'selected', true); - } + this.onSelectNode(e.target as INode) }); const clearDragDstState = () => { @@ -194,9 +186,10 @@ export default class Editor extends React.Component { return; } - Utils.refreshNodeId(graph.findDataById('1')); + const zoom = graph.getZoom(); graph.changeData(rootData); - // graph.layout() + graph.focusItem(srcData.id); + this.onSelectNode(srcData.id); clearDragDstState(); }); @@ -205,9 +198,30 @@ export default class Editor extends React.Component { const data = Utils.createTreeData(tree.root); graph.data(data); graph.render(); + graph.fitCenter(); + this.graph = graph; } + onSelectNode(node: string | INode) { + const graph = this.graph; + var curNode: INode; + if(typeof node == "string") { + curNode = graph.findById(node) as INode; + } else { + curNode = node; + } + + if (this.state.curNode) { + graph.setItemState(this.state.curNode, 'selected', false); + } + + this.setState({ curNode }); + if (this.state.curNode) { + graph.setItemState(this.state.curNode, 'selected', true); + } + } + render() { const { curNode } = this.state; return (