mxGraph实现按住ctrl键盘拖动图形实现复制图形功能

实现这个功能很easy,仅仅须要重写moveCells方法就能够了。以下是源文件里的代码:

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;
};
接下来要对这种方法进行改造。加一句就能够了
mxGraph.prototype.moveCells = function(cells, dx, dy, clone, target, evt) {
     clone=evt.ctrlKey;//对。就是这啦!

  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; };


是的。实现这个功能的确非常easy。可是往往实际项目中会有不同的需求。比方一个数据库关系图,复制一个字段到还有一张表中的时候;选择了多个图形而且包含关系线的是时候是否须要复制关系;假设图形存在子图形。是否须要一同复制;当前选择的图形是不是同意移动/复制。移动进入目标图形,目标图形是否同意该操作等等,这些就须要在这种方法区域中进行复杂的推断。

原文地址:https://www.cnblogs.com/mengfanrong/p/5275394.html