Can links be included when copying and pasting a node?

We have a case where, if a user has a node linked to another node, selects only the second node, and then does a copy/paste operation, we would like the link to also be duplicated, such as this:

Only the second node is selected:
image

After the copy/paste operation, the link is duplicated:
image

We have a custom command handler and I’ve been working with copyToClipboard and pasteFromClipboard in there. In copyToClipboard, I find the link that I want included in the copy/paste operation and add it to the set that’s passed into the function, then call super.copyToClipboard with the updated set. In pasteFromClipboard, I first call:

const coll = CommandHandler.prototype.pasteFromClipboard.apply(this);

and the coll variable returned by that call contains the link I added in copyToClipboard. However, its fromNode has been set to null. Without a valid from node, the link isn’t created in the diagram and we also can’t build the client side model we use to track node connections. Any ideas on why the fromNode is being nulled out, and is there a better way to approach the problem I’m trying to solve?

Are you using a GraphLinksModel or a TreeModel?

If you are using TreeModel, and Beta is a tree child of Alpha, then if you set CommandHandler.copiesParentKey to true, I think you’ll get the behavior that you are asking for in this case.

But if you are using GraphLinksModel, then in your override of CommandHandler.copyToClipboard you’ll need to include enough information in order to be able to add a new Link in CommandHandler.pasteFromClipboard.

The reason that the link that was copied into the clipboard didn’t have a “from” Node is because that Node wasn’t included in the clipboard collection, so the link’s reference had to be removed when it was in the clipboard. And so the paste wouldn’t have that information either.

We are using GraphLinksModel, so I’ll have to use your second approach. Thanks for the quick reply.

OK. I’ll caution you to consider a lot of cases, some of which might not apply to your app.

  • can repeated pastes work?
  • what if there are multiple links coming into a copied node?
  • what should happen if the “from” node is deleted before the paste happens?
  • what should happen if the whole model is replaced between the copy and the paste?
  • what if the link was also selected prior to copying into the clipboard?
  • are you properly handling the cases where the “from” node was also selected and copied into the clipboard?

Those are just off the top of my head – I bet there are a lot more cases to consider.