1

我正在使用 angular 8 的联合 js 和 rappid,并且我已经完成了大部分任务,但是在使用键盘事件时似乎存在问题。当我复制一个元素并将其粘贴到图形上时,它可以正常工作。但是对于选择的下一个元素,它会多次粘贴该新元素。

这是我的代码。

var keyboard = this.keyboard = new joint.ui.Keyboard();
var clipboard = this.clipboard = new joint.ui.Clipboard();

selection.collection.on('reset add remove', this.onSelectionChange.bind(this));

paper.on('element:pointerdown', function(elementView: joint.dia.ElementView,evt: joint.dia.Event) {

            clipboard.clear();
            keyboard.on({
            'ctrl+c': function(evt) {

                selection.collection.reset();
                //clipboard.clear();
                selection.collection.add(elementView.model);
                clipboard.copyElements(selection.collection, paper.model);
                //console.log(clipboard);

            },
            'ctrl+v': function(evt) {
                //console.log(clipboard);
                var pastedCells = clipboard.pasteCells(graph, {
                    translate: { dx: 20, dy: 20 },
                    useLocalStorage: true
                });

                var elements = _.filter(pastedCells, function(cell) {
                    return cell.isElement();
                });

                //console.log(elements);
                // Make sure pasted elements get selected immediately. This makes the UX better as
                // the user can immediately manipulate the pasted elements.
                selection.collection.reset(elements);

            },

        });


    });

onSelectionChange() {
        const { paper, selection,clipboard } = this;
        const { collection } = selection;
        //console.log(collection.models.child);
       // collection.models.forEach(function(model: joint.dia.Element) { if(!model.collection) { clipboard.clear();}});
        paper.removeTools();
        joint.ui.Halo.clear(paper);
        joint.ui.FreeTransform.clear(paper);
        joint.ui.Inspector.close();
        if(collection.first() == undefined){
            clipboard.clear();
        }
        if (collection.length === 1) {
            var primaryCell = collection.first();
            var primaryCellView = paper.requireView(primaryCell);
            selection.destroySelectionBox(primaryCell);
            this.selectPrimaryCell(primaryCellView);
        } else if (collection.length === 2) {
            collection.each(function(cell) {
                selection.createSelectionBox(cell);
            });
        }

    }
    selectPrimaryCell(cellView) {
        var cell = cellView.model
        if (cell.isElement()) {
            this.selectPrimaryElement(cellView);
        } else {
            this.selectPrimaryLink(cellView);
        }
        //this.createInspector(cell);
    }
    selectPrimaryElement(elementView) {

        var element = elementView.model;
        console.log(element.collection);
        new joint.ui.FreeTransform({
            cellView: elementView,
            allowRotation: false,
            preserveAspectRatio: !!element.get('preserveAspectRatio'),
            allowOrthogonalResize: element.get('allowOrthogonalResize') !== false
        }).render();
    }

我有不同的东西,比如重置剪贴板、重置选择和重置键盘,但似乎没有任何效果。

4

0 回答 0