-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: 📚️ add path-editor demo (#1396)
- Loading branch information
1 parent
6a591e2
commit 37eb7b9
Showing
3 changed files
with
243 additions
and
4 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
examples/x6-example-features/src/pages/edge/edge-editor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import React from 'react' | ||
import { Graph, Node, Edge, EdgeView } from '@antv/x6' | ||
import '../index.less' | ||
|
||
export default class Example extends React.Component { | ||
private container: HTMLDivElement | ||
|
||
componentDidMount() { | ||
const graph = new Graph({ | ||
container: this.container, | ||
width: 800, | ||
height: 600, | ||
grid: true, | ||
}) | ||
|
||
let edge: Edge | null = null | ||
let node: Node | null = null | ||
|
||
const init = (pos: { x: number; y: number }) => { | ||
node = graph.addNode({ | ||
shape: 'circle', | ||
width: 10, | ||
height: 10, | ||
...pos, | ||
attrs: { | ||
body: { | ||
strokeWidth: 1, | ||
stroke: '#5F95FF', | ||
fill: '#EFF4FF', | ||
}, | ||
}, | ||
}) | ||
edge = graph.addEdge({ | ||
source: node.getBBox().center, | ||
target: pos, | ||
attrs: { | ||
line: { | ||
targetMarker: null, | ||
stroke: '#A2B1C3', | ||
strokeWidth: 2, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const addVertices = (pos: { x: number; y: number }) => { | ||
if (edge) { | ||
edge.appendVertex(pos) | ||
} | ||
} | ||
|
||
const onMouseMove = (e: MouseEvent) => { | ||
if (edge) { | ||
const pos = graph.clientToLocal(e.clientX, e.clientY) | ||
edge.setTarget(pos) | ||
} | ||
} | ||
|
||
const print = () => { | ||
if (edge) { | ||
const view = graph.findViewByCell(edge) as EdgeView | ||
console.log(view.path.serialize()) | ||
} | ||
} | ||
|
||
const finish = (closed: boolean) => { | ||
if (node && edge) { | ||
const vertices = edge.getVertices() | ||
if (closed) { | ||
if (vertices.length >= 2) { | ||
edge.setTarget(node.getBBox().center) | ||
graph.removeNode(node) | ||
node = null | ||
print() | ||
} else { | ||
graph.removeCells([node, edge]) | ||
node = null | ||
edge = null | ||
} | ||
} else { | ||
if (vertices.length >= 1) { | ||
edge.setTarget(vertices[vertices.length - 1]) | ||
graph.removeNode(node) | ||
node = null | ||
print() | ||
} else { | ||
graph.removeCells([node, edge]) | ||
node = null | ||
edge = null | ||
} | ||
} | ||
this.container.removeEventListener('mousemove', onMouseMove) | ||
} | ||
} | ||
|
||
graph.on('blank:click', ({ e }) => { | ||
const pos = graph.clientToLocal(e.clientX, e.clientY) | ||
init(pos) | ||
this.container.addEventListener('mousemove', onMouseMove) | ||
}) | ||
|
||
graph.on('edge:click', ({ e }) => { | ||
const pos = graph.clientToLocal(e.clientX, e.clientY) | ||
if (edge) { | ||
const nodes = graph.getNodesFromPoint(pos.x, pos.y) | ||
if (nodes.length && nodes[0] === node) { | ||
finish(true) | ||
} else { | ||
addVertices(pos) | ||
} | ||
} | ||
}) | ||
|
||
graph.on('edge:contextmenu', () => { | ||
finish(false) | ||
}) | ||
} | ||
|
||
refContainer = (container: HTMLDivElement) => { | ||
this.container = container | ||
} | ||
|
||
render() { | ||
return ( | ||
<div className="x6-graph-wrap"> | ||
<div ref={this.refContainer} className="x6-graph" /> | ||
</div> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
sites/x6-sites/examples/showcase/practices/demo/path-editor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { Graph, Node, Edge, EdgeView } from '@antv/x6' | ||
|
||
let edge: Edge | null = null | ||
let node: Node | null = null | ||
const container = document.getElementById('container')! | ||
|
||
const graph = new Graph({ | ||
container, | ||
grid: true, | ||
}) | ||
|
||
const init = (pos: { x: number; y: number }) => { | ||
node = graph.addNode({ | ||
shape: 'circle', | ||
width: 10, | ||
height: 10, | ||
...pos, | ||
attrs: { | ||
body: { | ||
strokeWidth: 1, | ||
stroke: '#5F95FF', | ||
fill: '#EFF4FF', | ||
}, | ||
}, | ||
}) | ||
edge = graph.addEdge({ | ||
source: node.getBBox().center, | ||
target: pos, | ||
attrs: { | ||
line: { | ||
targetMarker: null, | ||
stroke: '#A2B1C3', | ||
strokeWidth: 2, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const addVertices = (pos: { x: number; y: number }) => { | ||
if (edge) { | ||
edge.appendVertex(pos) | ||
} | ||
} | ||
|
||
const onMouseMove = (e: MouseEvent) => { | ||
if (edge) { | ||
const pos = graph.clientToLocal(e.clientX, e.clientY) | ||
edge.setTarget(pos) | ||
} | ||
} | ||
|
||
const print = () => { | ||
if (edge) { | ||
const view = graph.findViewByCell(edge) as EdgeView | ||
console.log(view.path.serialize()) | ||
} | ||
} | ||
|
||
const finish = (closed: boolean) => { | ||
if (node && edge) { | ||
const vertices = edge.getVertices() | ||
if (closed) { | ||
if (vertices.length >= 2) { | ||
edge.setTarget(node.getBBox().center) | ||
graph.removeNode(node) | ||
node = null | ||
print() | ||
} else { | ||
graph.removeCells([node, edge]) | ||
node = null | ||
edge = null | ||
} | ||
} else { | ||
if (vertices.length >= 1) { | ||
edge.setTarget(vertices[vertices.length - 1]) | ||
graph.removeNode(node) | ||
node = null | ||
print() | ||
} else { | ||
graph.removeCells([node, edge]) | ||
node = null | ||
edge = null | ||
} | ||
} | ||
container.removeEventListener('mousemove', onMouseMove) | ||
} | ||
} | ||
|
||
graph.on('blank:click', ({ e }) => { | ||
const pos = graph.clientToLocal(e.clientX, e.clientY) | ||
init(pos) | ||
container.addEventListener('mousemove', onMouseMove) | ||
}) | ||
|
||
graph.on('edge:click', ({ e }) => { | ||
const pos = graph.clientToLocal(e.clientX, e.clientY) | ||
if (edge) { | ||
const nodes = graph.getNodesFromPoint(pos.x, pos.y) | ||
if (nodes.length && nodes[0] === node) { | ||
finish(true) | ||
} else { | ||
addVertices(pos) | ||
} | ||
} | ||
}) | ||
|
||
graph.on('edge:contextmenu', () => { | ||
finish(false) | ||
}) |