To implement this function, you only need to override the moveCells method. The code in the source file is as follows:
mxGraph.prototype.moveCells = function(cells, dx, dy, clone, target, evt) { if (cells != null && (dx != 0 || dy != 0 || clone || target != null)) { this.model.beginUpdate(); try { if (clone) { cells = this.cloneCells(cells, this.isCloneInvalidEdges()); if (target == null) { target = this.getDefaultParent(); } } this.cellsMoved(cells, dx, dy, !clone && this.isDisconnectOnMove() && this.isAllowDanglingEdges(), target == null); if (target != null) { var index = this.model.getChildCount(target); this.cellsAdded(cells, target, index, null, null, true); } this.fireEvent(new mxEventObject(mxEvent.MOVE_CELLS, 'cells', cells, 'dx', dx, 'dy', dy, 'clone', clone, 'target', target, 'event', evt)); } finally { this.model.endUpdate(); } } return cells;};
Next, we need to modify this method. Just add one sentence.
MxGraph. prototype. moveCells = function (cells, dx, dy, clone, target, evt) {clone = evt. ctrlKey; // that's it! If (cells! = Null & (dx! = 0 | dy! = 0 | clone | target! = Null) {this. model. beginUpdate (); try {if (clone) {cells = this. cloneCells (cells, this. isCloneInvalidEdges (); if (target = null) {target = this. getdefaparparent () ;}} this. cellsMoved (cells, dx, dy ,! Clone & this. isDisconnectOnMove () & this. isAllowDanglingEdges (), target = null); if (target! = Null) {var index = this. model. getChildCount (target); this. cellsAdded (cells, target, index, null, null, true);} this. fireEvent (new mxEventObject (mxEvent. MOVE_CELLS, 'cells ', cells, 'dx', dx, 'dy ', dy, 'clone', clone, 'target', target, 'event ', evt);} finally {this. model. endUpdate () ;}} return cells ;};
Yes, it is really easy to implement this function. However, there are usually different requirements in actual projects. For example, if a database relational graph is used to copy a field to another table, whether to copy the relationship when multiple graphs are selected and the link line is included. If the graph has a subgraph, whether to copy the image together; whether the selected image can be moved or copied; whether to move to the target image, whether the target image allows this operation, and so on, this requires complicated judgment in this method area.