add drag state

This commit is contained in:
zhandouxiaojiji committed 2020-10-21 10:43:20 +08:00
1 parent 841e2b4c79
commit 4fd1992f1f
2 files changed
+60 -44

No files matched your search

+30 -28
View File
@@ -23,7 +23,8 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
} }
private graph: TreeGraph; private graph: TreeGraph;
private dragNode: INode; private dragSrcNode: INode;
private dragDstNode: INode;
constructor(props: EditorProps) { constructor(props: EditorProps) {
super(props); super(props);
@@ -40,8 +41,10 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
modes: { modes: {
default: [ default: [
'drag-canvas', 'drag-canvas',
'zoom-canvas',
'click-select', 'click-select',
'hover', 'hover',
'dragRight',
{ {
type: 'collapse-expand', type: 'collapse-expand',
trigger: 'dblclick', trigger: 'dblclick',
@@ -59,31 +62,6 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
stroke: '#A3B1BF' stroke: '#A3B1BF'
} }
}, },
// defaultNode: {
// type: 'rect',
// labelCfg: {
// style: {
// fill: 'blue',
// fontSize: 10
// }
// },
// style: {
// stroke: '#72CC4A',
// width: 150
// }
// },
// nodeStateStyles: {
// hover: {
// fill: '#d3adf7',
// },
// selected: {
// stroke: '#000',
// lineWidth: 3,
// },
// dragSrc: {
// fill: 'gray',
// }
// },
defaultNode: { defaultNode: {
type: "TreeNode", type: "TreeNode",
}, },
@@ -117,7 +95,8 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
}); });
graph.on('node:dragstart', (e: G6GraphEvent) => { graph.on('node:dragstart', (e: G6GraphEvent) => {
this.dragNode = e.item as INode; this.dragSrcNode = e.item as INode;
console.log("drag start")
graph.setItemState(e.item, 'dragSrc', true); graph.setItemState(e.item, 'dragSrc', true);
}); });
graph.on('node:dragend', (e: G6GraphEvent) => { graph.on('node:dragend', (e: G6GraphEvent) => {
@@ -125,15 +104,38 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
}); });
graph.on('node:dragover', (e: G6GraphEvent) => { graph.on('node:dragover', (e: G6GraphEvent) => {
if (this.dragDstNode) {
graph.setItemState(this.dragDstNode, 'dragRight', false);
graph.setItemState(this.dragDstNode, 'dragDown', false);
graph.setItemState(this.dragDstNode, 'dragUp', false);
}
const dstNode = e.item as INode;
if (dstNode == this.dragSrcNode) {
return;
}
const box = dstNode.getBBox();
if (e.x > box.minX + box.width * 0.6) {
console.log("right");
graph.setItemState(dstNode, 'dragRight', true);
} else if (e.y > box.minY + box.height * 0.5) {
console.log("down");
graph.setItemState(dstNode, 'dragDown', true);
} else {
console.log("up");
graph.setItemState(dstNode, 'dragUp', true);
}
this.dragDstNode = dstNode;
}); });
graph.on('node:drop', (e: G6GraphEvent) => { graph.on('node:drop', (e: G6GraphEvent) => {
const srcNode = this.dragNode; const srcNode = this.dragSrcNode;
const dstNode = e.item as INode; const dstNode = e.item as INode;
if (srcNode != dstNode) { if (srcNode != dstNode) {
} }
this.dragSrcNode = null;
this.dragDstNode = null;
}); });
const data: TreeGraphData = { const data: TreeGraphData = {
+30 -16
View File
@@ -14,7 +14,7 @@ import G6 from '@antv/g6';
import 'antd/dist/antd.dark.css'; import 'antd/dist/antd.dark.css';
import './index.css'; import './index.css';
import { ModelConfig } from "@antv/g6/lib/types"; import { ModelConfig, Item } from "@antv/g6/lib/types";
const { Header, Sider, Content, Footer } = Layout; const { Header, Sider, Content, Footer } = Layout;
const { TabPane } = Tabs; const { TabPane } = Tabs;
@@ -63,7 +63,22 @@ export default class Main extends Component {
}, },
dragSrc: { dragSrc: {
fill: 'gray', fill: 'gray',
} },
dragRight: {
'drag-right': {
fillOpacity: 0.6,
}
},
dragUp: {
'drag-up': {
fillOpacity: 0.6,
}
},
dragDown: {
'drag-down': {
fillOpacity: 0.6,
}
},
}, },
}, },
draw(cfg, group) { draw(cfg, group) {
@@ -104,13 +119,13 @@ export default class Main extends Component {
attrs: { attrs: {
x: -w / 2, x: -w / 2,
y: -h / 2, y: -h / 2,
width: w * 0.6, width: w,
height: h / 2, height: h / 2,
fill: 'blue', fill: 'blue',
fillOpacity: 0.5, fillOpacity: 0,
radius: r,
}, },
visible: false, draggable: true,
// visible: false,
}); });
group.addShape('rect', { group.addShape('rect', {
@@ -118,13 +133,13 @@ export default class Main extends Component {
attrs: { attrs: {
x: -w / 2, x: -w / 2,
y: 0, y: 0,
width: w * 0.6, width: w,
height: h / 2, height: h / 2,
fill: 'green', fill: 'blue',
fillOpacity: 0.5, fillOpacity: 0,
radius: r,
}, },
visible: false, draggable: true,
// visible: false,
}); });
group.addShape('rect', { group.addShape('rect', {
@@ -134,15 +149,14 @@ export default class Main extends Component {
y: -h / 2, y: -h / 2,
width: w * 0.4, width: w * 0.4,
height: h, height: h,
fill: 'yellow', fill: 'blue',
fillOpacity: 0.5, fillOpacity: 0,
radius: r,
}, },
visible: false, draggable: true,
// visible: false,
}); });
return shape; return shape;
}, },
}, },
'single-node', 'single-node',
); );