Copy (ctrl+c) and paste (ctrl+v) node behaviour

Hello,

I’m trying to copy/paste a node with some properties as shown below:

image

I would really like to copy/paste the whole node just as it’s implemented by default, but with one change. The newly created node (with ctrl+c/ctrl+v), should only be changed in the start property as shown above.

I tried to achieve this, by adding a diagram listener as shown below:

    this.diagram.addDiagramListener('ClipboardPasted', (e) => {
      e.subject.each((p) => {
        console.log(p.part.data);
        p.part.data.typeAttributes.status.start = false;
      })
    });

But this seems to change the start property of both, copied and pasted node, even though the console.log is loging only the pasted node (and not the copied one).

I also tried the ChangedSelection event, but with the same effect, both changes.

I would be grateful for any advice.

Best regards.

By default, node data objects are copied via a shallow copy, so because you have a nested object in your data, changing a property will affect both nodes. Maybe you want to write your own copyNodeData function to handle the nested data as necessary.

Thanks!

I tried to implement copyNodeDataFunction as shown below:

this.diagram.model.copyNodeDataFunction = test;


    function test (data, model) {
      console.log(data);
      return data;
    }

This function is logging the copied data, but when I try to paste it … nothing happens.

Have you got any suggestion of what could I possibly doing wrong?

That function must be responsible for creating and returning a new Object. It cannot return the same old object.

1 Like