Singleton that implements a clipboard for graph cells.

To copy the selection cells from the graph to the clipboard and paste them into graph2, do the following:

Clipboard.copy(graph);
Clipboard.paste(graph2);

For fine-grained control of the clipboard data the Graph.canExportCell and Graph.canImportCell functions can be overridden.

To restore previous parents for pasted cells, the implementation for copy and paste can be changed as follows.

Clipboard.copy = function(graph: Graph, cells: Cell[]): void {
cells = cells ?? graph.getSelectionCells();
const result = graph.getExportableCells(cells);

Clipboard.parents = new Object();
for (let i = 0; i < result.length; i++) {
Clipboard.parents[i] = graph.model.getParent(cells[i]);
}

Clipboard.insertCount = 1;
Clipboard.setCells(graph.cloneCells(result));

return result;
};

Clipboard.paste = function(graph: Graph): void {
if (Clipboard.isEmpty()) {
return;
}
const cells = graph.getImportableCells(Clipboard.getCells());
const delta = Clipboard.insertCount * Clipboard.STEPSIZE;
const parent = graph.getDefaultParent();

graph.model.beginUpdate();
try {
for (let i = 0; i < cells.length; i++) {
const tmp = (Clipboard.parents && graph.model.contains(Clipboard.parents[i])) ?
Clipboard.parents[i] : parent;
cells[i] = graph.importCells([cells[i]], delta, delta, tmp)[0];
}
}
finally {
graph.model.endUpdate();
}

// Increments the counter and selects the inserted cells
Clipboard.insertCount++;
graph.setSelectionCells(cells);
};

Constructors

Properties

cells: Cell[]

Holds the array of Cell currently in the clipboard.

insertCount: number = 1

Counts the number of times the clipboard data has been inserted.

STEPSIZE: number = 10

Defines the step size to offset the cells after each paste operation.

10

Methods

  • Copies the given array of Cell from the specified graph to cells. Returns the original array of cells that has been cloned. Descendants of cells in the array are ignored.

    Parameters

    • graph: Graph

      graph that contains the cells to be copied.

    • Optionalcells: Cell[]

      Optional array of Cell to be copied.

    Returns Cell[]

  • Cuts the given array of Cell from the specified graph. If cells is null then the selection cells of the graph will be used.

    Parameters

    • graph: Graph

      graph that contains the cells to be cut.

    • cells: Cell[] = []

      Optional array of Cell to be cut.

    Returns Cell[]

    Returns the cells that have been cut from the graph.

  • Returns true if the clipboard currently has no data stored.

    Returns boolean

  • Pastes the Cells into the specified graph associating them to the default parent. The cells are added to the graph using graph.importCells and returned.

    Parameters

    Returns null | Cell[]

  • Hook to remove the given cells from the given graph after a cut operation.

    Parameters

    • graph: Graph

      graph that contains the cells to be cut.

    • cells: Cell[]

      Array of Cell to be cut.

    Returns void