Integrating the Cross-Tab-Copy-Paste correctly

149 views
Skip to first unread message

Stefan Höhn

unread,
Dec 29, 2022, 6:01:49 PM12/29/22
to Blockly
I recently discovered this awesome plugin:

https://github.com/google/blockly-samples/tree/master/plugins/cross-tab-copy-paste

and I immediately jumped on it and integrated it into our application (openHAB).

However, there is a bit of an issue: it turns out that this plugin must be loaded ONLY ONCE. Even when recreating a new workspace it must not be loaded again, otherwise it throws an exception complaining that it already exists.

While for example the Zoom-Control-Plugin links to the workspace like so

const zoomToFit = new ZoomToFitControl(this.workspace)
zoomToFit.init()

it seems that I have to use a GLOBAL variable make sure that the plugin is not yet loaded like so

if (!copyPastePluginInitialized) {
   const copyAndPasteOptions = { ... }
   const copyAndPastePlugin = new CrossTabCopyPaste()
    copyAndPastePlugin.init(copyAndPasteOptions, () => {
     console.log('There has been a block type error during copying and pasting')
    })

    copyPastePluginInitialized = true

but even then sometimes it seems that this doesn't really prevent it. 

I wonder if there is a better of doing it to make it fail-save or why the plugin was created that way?

TIA
Stefan

PS: As a sidenote, I upgraded from 6.x to 9.2 and the migration was amazingly smooth! Well done, team!


 

Stefan Höhn

unread,
Dec 29, 2022, 6:16:10 PM12/29/22
to Blockly
As an additional information: The error message is 

Error while retrieving Blockly data - Error: Menu item with ID "blockCopyToStorage" is already registered.:undefined

Maybe that allows us to "query" or detect somehow that the plugin has already been installed?

Maribeth Bottorff

unread,
Jan 3, 2023, 2:08:30 PM1/3/23
to Blockly
The reason for this is Blockly's shortcut registry is a singleton; all workspaces share a single shortcut registry. So it doesn't make sense to add this plugin multiple times, even if you have multiple workspaces.

 It would be best to initialize this plugin only once. Since it's not associated with a workspace, there's no need to call it every time you create a workspace, and you can do it once per application load.

Maribeth

Stefan Höhn

unread,
Jan 3, 2023, 2:14:39 PM1/3/23
to blo...@googlegroups.com
Thanks Maribeth,

is there an easy way to detect if the singleton is already loaded? Currently I am using a global variable but that doesn't sound like an elegant solution

Stefan

--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/F2hxyEfy7n4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/32a9151c-65f9-417c-af4a-34587c951b7fn%40googlegroups.com.

Maribeth Bottorff

unread,
Jan 3, 2023, 2:24:39 PM1/3/23
to Blockly
You could check if one of the registry items is already present, e.g. 
Blockly.ContextMenuRegistry.registry.getItem('blockCopyToStorage')

Or you could put the code in a location you know will only run once and not have to check.

Btw, glad to hear your upgrade experience was smooth! Best,

Maribeth

Stefan Höhn

unread,
Jan 3, 2023, 2:35:26 PM1/3/23
to blo...@googlegroups.com
This

Blockly.ContextMenuRegistry.registry.getItem('blockCopyToStorage')

seems to be exactly what I was looking for.

Thanks Maribeth!

Cheers
Stefan

Reply all
Reply to author
Forward
0 new messages