Singleton that implements a clipboard for graph cells.
Example
mxClipboard.copy(graph);
mxClipboard.paste(graph2);
This copies the selection cells from the graph to the clipboard and pastes them into graph2.
For fine-grained control of the clipboard data the mxGraph.canExportCell and mxGraph.canImportCell functions can be overridden.
To restore previous parents for pasted cells, the implementation for copy and paste can be changed as follows.
mxClipboard.copy = function(graph, cells)
{
cells = cells || graph.getSelectionCells();
var result = graph.getExportableCells(cells);
mxClipboard.parents = new Object();
for (var i = 0; i < result.length; i++)
{
mxClipboard.parents[i] = graph.model.getParent(cells[i]);
}
mxClipboard.insertCount = 1;
mxClipboard.setCells(graph.cloneCells(result));
return result;
};
mxClipboard.paste = function(graph)
{
if (!mxClipboard.isEmpty())
{
var cells = graph.getImportableCells(mxClipboard.getCells());
var delta = mxClipboard.insertCount * mxClipboard.STEPSIZE;
var parent = graph.getDefaultParent();
graph.model.beginUpdate();
try
{
for (var i = 0; i < cells.length; i++)
{
var tmp = (mxClipboard.parents != null && graph.model.contains(mxClipboard.parents[i])) ?
mxClipboard.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
mxClipboard.insertCount++;
graph.setSelectionCells(cells);
}
};
Summary
mxClipboard | Singleton that implements a clipboard for graph cells. |
Variables | |
STEPSIZE | Defines the step size to offset the cells after each paste operation. |
insertCount | Counts the number of times the clipboard data has been inserted. |
cells | Holds the array of mxCells currently in the clipboard. |
Functions | |
setCells | Sets the cells in the clipboard. |
getCells | Returns the cells in the clipboard. |
isEmpty | Returns true if the clipboard currently has not data stored. |
cut | Cuts the given array of mxCells from the specified graph. |
removeCells | Hook to remove the given cells from the given graph after a cut operation. |
copy | Copies the given array of mxCells from the specified graph to cells. |
paste | Pastes the cells into the specified graph restoring the relation to <parents>, if possible. |