Differentiate between whether transformPaste is called on drag and drop or due to a paste

Hello folks,

I have a use-case for a custom node with an attribute “name” that has the following constraints:

  • At any given instance on editor, only with node with a specific “name” can exist. Meaning, at a given snapshot of the editor state, the number of nodes with a particular “name” attribute should be unique

This means:

  • when users cut and paste, we want to keep the “name” attribute of the node
  • when users copy and paste, we want to replace the “name” attribute of the node with a new one if a node with that name already exists

I am using transformPaste with editor state (which preserves different values of the “name” attribute across nodes) to resolve this during cut/copy paste.

However, I noticed that transformPaste gets called even when a node is dragged and dropped. This is causing the name attribute to be generated again while in reality it should be preserved

Is there a way to differentiate between how transformPaste is called (whether it is the drag and drop that is callng it or if it is being called on a paste event)

I’d recommend doing this in an appendTransaction hook (checking the "uiEvent" metadata property to recognize cut/copy/paste) and scanning the existing document for duplicate names—patching up duplicates you find—to avoid unnecessary renaming.

1 Like