Loading...
Node tools are small components rendered on nodes that typically come with some interactive features, such as a delete button that removes the corresponding node when clicked. We can add or remove tools based on the following scenarios.
// Add tools when creating a nodegraph.addNode({...,tools: [{name: 'button-remove',args: { x: 10, y: 10 },},],})// Add tools after creating a nodenode.addTools([{name: 'button-remove',args: { x: 10, y: 10 },},])// Remove toolgraph.on("node:mouseleave", ({ node }) => {if (node.hasTool("button-remove")) {node.removeTool("button-remove");}});
X6 provides the following default tools for nodes:
Renders a button at a specified position, supporting custom click interactions. The configuration is as follows:
Parameter Name | Type | Default Value | Description |
---|---|---|---|
x | number | string | 0 | The X coordinate relative to the top-left corner of the node, with decimals and percentages indicating relative positions. |
y | number | string | 0 | The Y coordinate relative to the top-left corner of the node, with decimals and percentages indicating relative positions. |
offset | number | { x: number, y: number } | 0 | The offset based on x and y . |
rotate | boolean | - | Whether to follow the rotation of the node. |
useCellGeometry | boolean | true | Whether to use geometric calculations to compute the element's bounding box. Enabling this improves performance; if accuracy issues arise, set it to false . |
markup | Markup.JSONMarkup | - | The Markup definition for rendering the button. |
onClick | (args: {e: Dom.MouseDownEvent, cell: Cell, view: CellView }) => void | - | Callback function for button clicks. |
// Add button on mouse hovergraph.on('node:mouseenter', ({ node }) => {node.addTools({name: 'button',args: {markup: ...,x: 0,y: 0,offset: { x: 18, y: 18 },onClick({ view }) { ... },},})})// Remove button on mouse leavegraph.on('node:mouseleave', ({ node }) => {node.removeTools() // Remove all tools})
Renders a delete button at a specified position, which deletes the corresponding node when clicked. It is a special case of the button
tool, so it supports all configurations of button
.
const source = graph.addNode({...,// Add a delete button that is always visibletools: [{name: 'button-remove',args: {x: '100%',y: 0,offset: { x: -10, y: 10 },},},],})
Renders a rectangle surrounding the node based on its bounding box. Note that this tool only renders a rectangle without any interaction. The configuration is as follows:
Parameter Name | Type | Default Value | Description |
---|---|---|---|
tagName | string | rect | The shape used for rendering. |
rotate | boolean | - | Whether the shape follows the rotation of the node. |
padding | SideOptions | 10 | Margin. |
attrs | KeyValue | object | Shape attributes. |
useCellGeometry | boolean | true | Whether to use geometric calculations to compute the element's bounding box. Enabling this improves performance; if accuracy issues arise, set it to false . |
The default values (default styles) for attrs
are:
{fill: 'none',stroke: '#333','stroke-width': 0.5,'stroke-dasharray': '5, 5','pointer-events': 'none',}
The type definition for SideOptions
is as follows:
type SideOptions =| number| {vertical?: numberhorizontal?: numberleft?: numbertop?: numberright?: numberbottom?: number}
The tool usage is as follows:
const source = graph.addNode({...,tools: [{name: 'boundary',args: {padding: 5,attrs: {fill: '#7c68fc',stroke: '#333','stroke-width': 1,'fill-opacity': 0.2,},},},],})
Provides text editing functionality on the node. The configuration is as follows:
Parameter Name | Type | Default Value | Description |
---|---|---|---|
x | number | string | - | The X coordinate relative to the top-left corner of the node, with decimals and percentages indicating relative positions. |
y | number | string | - | The Y coordinate relative to the top-left corner of the node, with decimals and percentages indicating relative positions. |
attrs/fontSize | string | 14 | Font size of the edited text. |
attrs/color | string | #000 | Font color of the edited text. |
attrs/fontFamily | string | Arial, helvetica, sans-serif | Font of the edited text. |
attrs/backgroundColor | string | #fff | Background color of the editing area. |
getText | string | (this: CellView, args: {cell: Cell}) => string | - | Method to get the original text; needs to customize getText method in custom markup scenarios. |
setText | string | (this: CellView, args: {cell: Cell, value: string}) => void | - | Method to set new text; needs to customize setText method in custom markup scenarios. |
It is important to note that after version 2.8.0, there is no need to dynamically add tools in the double-click event, and event parameters do not need to be passed.
// Usage before version 2.8.0graph.on('node:dblclick', ({ node, e }) => {node.addTools({name: 'node-editor',args: {event: e,},})})// Usage after version 2.8.0node.addTools({name: 'node-editor',})
It is also important to note that if a custom markup
is defined in the node, it is often necessary to customize the getText
and setText
methods to correctly get and set the edited text. Both configurations support both function and string forms; the function form is easier to understand, while the string form is essentially the property path of the text to be retrieved or set. Generally, it is recommended to use the string form so that the graph data can be fully serialized (since functions cannot be serialized), otherwise, issues may arise with the text editing functionality after rendering the canvas, for example:
node.addTools({name: 'node-editor',args: {getText: 'a/b',setText: 'c/d',},})
The above configuration indicates:
node.attr('a/b')
node.attr('c/d', value)
Inherit from ToolItem
to implement a tool class, which is more challenging and requires understanding of the ToolItem class. You can refer to the source code of the built-in tools above; this will not be elaborated here.
Graph.registerNodeTool('button', Button)
Inherit from an already registered tool and modify the configuration based on the inheritance. We provide a static method define
on the ToolItem
base class to quickly implement inheritance and modify configurations.
const MyButton = Button.define<Button.Options>({name: 'my-btn',markup: ...,onClick({ view }) { ... },})Graph.registerNodeTool('my-btn', MyButton, true)
Additionally, we provide a quick inheritance implementation for the Graph.registerNodeTool
method to specify default options:
Graph.registerNodeTool('my-btn', {inherit: 'button', // Base class name, using the name of the already registered tool.markup: ...,onClick: ...,})