UNPKG

@maxgraph/core

Version:

maxGraph is a fully client side JavaScript diagramming library that uses SVG and HTML for rendering.

120 lines (119 loc) 4.26 kB
import type Cell from '../view/cell/Cell.js'; import type { AbstractGraph } from '../view/AbstractGraph.js'; /** * @class * * 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: * ```javascript * Clipboard.copy(graph); * Clipboard.paste(graph2); * ``` * * For fine-grained control of the clipboard data the {@link AbstractGraph.canExportCell} and {@link AbstractGraph.canImportCell} functions can be overridden. * * To restore previous parents for pasted cells, the implementation for {@link copy} and {@link paste} can be changed as follows. * * ```typescript * 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); * }; * ``` */ declare class Clipboard { /** * Defines the step size to offset the cells after each paste operation. * @default 10 */ static STEPSIZE: number; /** * Counts the number of times the clipboard data has been inserted. */ static insertCount: number; /** * Holds the array of {@link Cell} currently in the clipboard. */ static cells: Cell[]; /** * Sets the cells in the clipboard. Fires a {@link InternalEvent.CHANGE} event. */ static setCells(cells: Cell[]): void; /** * Returns the cells in the clipboard. */ static getCells(): Cell[]; /** * Returns `true` if the clipboard currently has no data stored. */ static isEmpty(): boolean; /** * Cuts the given array of {@link Cell} from the specified graph. * If {@link cells} is `null` then the selection cells of the graph will be used. * * @param graph - {@link AbstractGraph} that contains the cells to be cut. * @param cells - Optional array of {@link Cell} to be cut. * @returns Returns the cells that have been cut from the graph. */ static cut(graph: AbstractGraph, cells?: Cell[]): Cell[]; /** * Hook to remove the given cells from the given graph after a cut operation. * * @param graph - {@link AbstractGraph} that contains the cells to be cut. * @param cells - Array of {@link Cell} to be cut. */ static removeCells(graph: AbstractGraph, cells: Cell[]): void; /** * Copies the given array of {@link Cell} from the specified graph to {@link cells}. * Returns the original array of cells that has been cloned. * Descendants of cells in the array are ignored. * * @param graph - {@link AbstractGraph} that contains the cells to be copied. * @param cells - Optional array of {@link Cell} to be copied. */ static copy(graph: AbstractGraph, cells?: Cell[]): Cell[]; /** * Pastes the {@link Cell}s into the specified graph associating them to the default parent. * The cells are added to the graph using {@link AbstractGraph.importCells} and returned. * * @param graph - {@link AbstractGraph} to paste the {@link Cell}s into. */ static paste(graph: AbstractGraph): Cell[] | null; } export default Clipboard;