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

实现这个功能非常简单,只需要重写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;
};

是的,实现这个功能的确很简单。但是往往实际项目中会有不同的需求。比如一个数据库关系图,复制一个字段到另一张表中的时候;选择了多个图形并且包括关系线的是时候是否需要复制关系;如果图形存在子图形,是否需要一同复制;当前选择的图形是不是允许移动/复制;移动进入目标图形,目标图形是否允许该操作等等,这些就需要在这个方法区域中进行复杂的判断。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值