Hi,
I’m trying to customize the paste function in GoJS version 2.2.13.
In GoJS, I have implemented cut, copy, and paste function in a ContextMenu as follows (as seen in the sample at Basic GoJS Sample):
makeButton("Cut",
(e, obj) => e.diagram.commandHandler.cutSelection(),
o => o.diagram.commandHandler.canCutSelection()),
makeButton("Copy",
(e, obj) => e.diagram.commandHandler.copySelection(),
o => o.diagram.commandHandler.canCopySelection()),
makeButton("Paste",
(e, obj) => e.diagram.commandHandler.pasteSelection(e.diagram.toolManager.contextMenuTool.mouseDownPoint),
o => o.diagram.commandHandler.canPasteSelection(o.diagram.toolManager.contextMenuTool.mouseDownPoint))
With this implementation, it pastes the data saved in the clipboard.
However, I would like to edit the data saved in the clipboard before pasting it. For instance, I want to add a unique identifier like “(1)” to node names to avoid duplicates when pasting nodes.
Is it possible to get the clipboard data in the GoJS library, and are there any other resources or information that could be helpful for achieving this custom paste function?
The user might repeatedly paste, and you won’t know about possible duplicates until the paste (i.e. copy into the diagram/model) actually happens, so you are correct to want to modify the pasting process, not the contents of the clipboard.
Did you also want to avoid duplicates when the user does a control-drag‐and‐drop to add a copy of the selection? Then it might be best to customize the manner in which data objects in the model are copied. Set the Model.copyNodeDataFunction property: Model | GoJS API
Or you could override CommandHandler.pasteFromClipboard: CommandHandler | GoJS API. Call the super method, modify data as needed, and return the Set. Clearly this only applies to pasting.
More generally you could implement “ClipboardPasted” (and “SelectionCopied”?) DiagramEvent listeners: GoJS Events -- Northwoods Software
Walter, thank you for your response. As you pointed out, I was concerned about the possibility of users repeatedly pasting.
To be honest, due to my limited understanding GoJS API, I was not able to try to override CommandHandler.pasteFromClipboard as you suggested.
Instead, I was able to implement my desired paste function by setting copyNodeDataFunction.
Thank you very much for your guidance, it is much appreciated.