Listen to event on paste / duplicate node

I’m researching possibilities for our plugin.

Our use case:

  • we store some plugin data using Figma’s getPluginData() - we arbitrarily tie the stored data to provided by Figma
  • on node copy+paste / duplicate, we would like to trigger a callback to check the original node, copy its plugin data and store it also for the that has been created by the duplication / paste operation

From what I researched so far, there is no way to hook on paste / duplicate events (also because the callback would only be executed if the user performs such actions with our plugin open). But nevertheless I’d like to ask here, maybe someone could point me in the right direction?

Thank you, best regards,
Ondřej Frei

An alternative idea to achieve this: Can we somehow hook into node instantiation? In that moment, we could compare the just-created node with the existing ones and thus detect if it has been copied / duplicated.

If there is such way, does the plugin need to be open for it to work? Meaning that if the user does not open the plugin, there would be no chance to monitor newly created nodes like this?

You can store directly in the plugin data, and the next time you open the plugin, simply check if the current is equal to the saved one. If not, you can do your stuff.

Thank you @tonypinkevych! Let me make sure I understand you correctly - the node id can change over time, or you mean a situation that it’s in fact a different node?

No it won’t change overtime automatically :slight_smile:
According to my observations it changes only when you make copy/paste or duplication within the same Figma file. But if you copy-paste from one file to another it will keep the same id.

1 Like

@tonypinkevych thank you for this insight! So on opening the plugin, you’d suggest to go through all the nodes and check if they have previously been saved into the plugin data? And if not, it means they’re new and possibly copied?

  1. When your plugin do something with node → you store node id with setPluginData;
  2. Next time when your plugin want to do something with this node you can compare === node.getPluginData().storedId. And if they are not equal do something with this.
1 Like

Thank you for your help @tonypinkevych :rocket: