Skip to content

Clipboard

This section mainly introduces the Clipboard plugin. By reading, you will learn:

  • How to use copy and paste

Usage

The clipboard is used for copying/pasting nodes and edges. Enable the feature via the Clipboard plugin:

import { Graph, Clipboard } from '@antv/x6'

const graph = new Graph({
  background: {
    color: '#F2F7FA',
  },
})
graph.use(
  new Clipboard({
    enabled: true,
  }),
)

Demo

  • After selecting a node, click the copy button to copy the node.
  • Set different offset values to observe the effect on the node’s position when pasting.
  • After enabling localStorage, copy a node, refresh the page or reopen the browser, then click the paste button.
import './index.less'
import { createRoot } from 'react-dom/client'
import React from 'react'
import { Button, message } from 'antd'
import { Graph, Clipboard, Selection } from '@antv/x6'
import { Settings, State } from './settings.tsx'

export default class Example extends React.Component {
  private container: HTMLDivElement
  private graph: Graph
  private options: State

  componentDidMount() {
    this.graph = new Graph({
      container: this.container,
      background: {
        color: '#F2F7FA',
      },
    })

    this.graph.use(
      new Selection({
        enabled: true,
        showNodeSelectionBox: true,
      }),
    )

    this.graph.use(
      new Clipboard({
        enabled: true,
        useLocalStorage: true,
      }),
    )

    this.graph.addNode({
      x: 280,
      y: 80,
      width: 100,
      height: 40,
      label: 'Rect',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
          rx: 6,
          ry: 6,
        },
      },
    })

    const source = this.graph.addNode({
      x: 32,
      y: 32,
      width: 100,
      height: 40,
      label: 'Hello',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
          rx: 6,
          ry: 6,
        },
      },
    })

    const target = this.graph.addNode({
      shape: 'circle',
      x: 180,
      y: 160,
      width: 60,
      height: 60,
      label: 'World',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
        },
      },
    })

    this.graph.addEdge({
      source,
      target,
      attrs: {
        line: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
        },
      },
    })
  }

  onSettingsChanged = (options: State) => {
    this.options = { ...options }
  }

  refContainer = (container: HTMLDivElement) => {
    this.container = container
  }

  onCopy = () => {
    const cells = this.graph.getSelectedCells()
    if (cells && cells.length) {
      this.graph.copy(cells, this.options)
      message.success('复制成功')
    } else {
      message.info('请先选中节点再复制')
    }
  }

  onPaste = () => {
    if (this.graph.isClipboardEmpty()) {
      message.info('剪切板为空,不可粘贴')
    } else {
      const cells = this.graph.paste(this.options)
      this.graph.cleanSelection()
      this.graph.select(cells)
      message.success('粘贴成功')
    }
  }

  render() {
    return (
      <div className="clipboard-app">
        <div className="app-side">
          <Settings onChange={this.onSettingsChanged} />
          <div className="app-btns">
            <Button onClick={this.onCopy} type="primary">
              Copy Selected Cells
            </Button>
            <Button onClick={this.onPaste} type="ghost">
              Paste
            </Button>
          </div>
        </div>
        <div className="app-content" ref={this.refContainer} />
      </div>
    )
  }
}

const root = createRoot(document.getElementById('container')!)
root.render(<Example />)
window.addEventListener('pagehide', () => root.unmount(), { once: true })

Configuration

Property Name Type Default Value Required Description
useLocalStorage boolean false When enabled, the copied nodes/edges are also saved to localStorage, allowing copy/paste to work after refreshing or reopening the browser.

API

graph.copy(…)

copy(cells: Cell[], options: CopyOptions = {}): this

Copy nodes/edges. Parameters are as follows:

Name Type Required Default Value Description
cells Cell[] ✓ The nodes/edges to be copied.
options.deep boolean - Whether to recursively copy all child nodes/edges.
options.useLocalStorage boolean - Whether to save the copied nodes/edges in localStorage.

graph.cut(…)

cut(cells: Cell[], options: CopyOptions = {}): this

Cut nodes/edges. Parameters are as follows:

Name Type Required Default Value Description
cells Cell[] ✓ The nodes/edges to be cut.
options.deep boolean - Whether to recursively copy all child nodes/edges.
options.useLocalStorage boolean - Whether to save the copied nodes/edges in localStorage.

graph.paste(…)

paste(options?: PasteOptions, graph?: Graph): Cell[]

Paste and return the nodes/edges pasted onto the canvas. Parameters are as follows:

Name Type Required Default Value Description
options.useLocalStorage boolean - Whether to use nodes/edges from localStorage.
options.offset number | { dx: number; dy: number } 20 The offset for the nodes/edges pasted onto the canvas.
options.nodeProps Node.Properties - Additional properties for the nodes pasted onto the canvas.
options.edgeProps Edge.Properties - Additional properties for the edges pasted onto the canvas.
graph Graph this The target canvas for pasting, defaults to the current canvas.

graph.getCellsInClipboard()

getCellsInClipboard(): Cell[]

Get the nodes/edges in the clipboard.

graph.cleanClipboard()

cleanClipboard(): this

Clear the clipboard.

graph.isClipboardEmpty()

isClipboardEmpty(): boolean

Return whether the clipboard is empty.

graph.isClipboardEnabled()

isClipboardEnabled(): boolean

Return whether the clipboard is enabled.

graph.enableClipboard()

enableClipboard(): this

Enable the clipboard.

graph.disableClipboard()

disableClipboard(): this

Disable the clipboard.

graph.toggleClipboard(…)

toggleClipboard(enabled?: boolean): this

Toggle the clipboard’s enabled state. Parameters are as follows:

Name Type Required Default Value Description
enabled boolean - Whether to enable the clipboard; if omitted, toggles the current enabled state.

Events

Event Name Parameter Type Description
clipboard:changed { cells: Cell[] } Triggered when copying, cutting, or clearing the clipboard.
graph.on('clipboard:changed', ({ cells }) => {
  console.log(cells)
})

// We can also listen to events on the plugin instance
clipboard.on('clipboard:changed', ({ cells }) => {
  console.log(cells)
})
© Copyright 2026 Ant Group Co., Ltd..备案号:京ICP备15032932号-38
Site search
Documentation and examplesType to start searching

Search documentation, APIs, and examples

↑↓Select↵OpenEscClose