blockly-samples icon indicating copy to clipboard operation
blockly-samples copied to clipboard

plugin-cross-tab-copy-paste

Open bartbutenaers opened this issue 2 years ago • 3 comments

Check for duplicates

  • [X] I have searched for similar issues before opening a new one.

Component

plugin-cross-tab-copy-paste

Description

Hi everybody,

I have used the plugin-cross-tab-copy-paste plugin for the first time, and it works very well. Very useful plugin!

However in our application we create a new workspace when we show the old workspace in full screen. When we do that now, the following exceptions is raised. All my other plugins have no issue, but they all have a dispose function that does the cleanup of all stuff. However the plugin-cross-tab-copy-paste plugin does 't offer such a function...

Thanks! Bart

Reproduction steps

  1. Call ´Blockly.inject´
  2. Initialize the plugin:
    node.copyPastePlugin = new CrossTabCopyPaste();
    node.copyPastePlugin.init(copyPastePluginOptions, function() {
       //console.log('Use this error callback to handle TypeError while pasting');
    });
    
  3. For all other plugins I call now their dispose function, but this plugin doesn' t have it...
  4. Call ´Blockly.inject´ again. Now an exception occurs

Stack trace

Uncaught Error: Menu item with ID "blockCopyToStorage" is already registered.
    at ContextMenuRegistry$$module$build$src$core$contextmenu_registry.register (contextmenu_registry.ts:45:13)
    at t.blockCopyToStorageContextMenu (index.js:90:5)
    at t.init (index.js:33:12)
    at createWorkspace (<anonymous>:455:34)
    at Object.open (<anonymous>:918:29)
    at n (red.min.js?v=:19:44660)
    at red.min.js?v=:19:45794

Screenshots

No response

bartbutenaers avatar Nov 03 '23 22:11 bartbutenaers

does it work if you call Blockly.ContextMenuRegistry.registry.unregister('blockCopyToStorage'); in place of plugin.dispose()

alicialics avatar Nov 04 '23 00:11 alicialics

Hi @alicialics,

Thanks for joining (again)!

  1. In some circumstances I arrive at the cleanup part before the plugins have been initialized, and then your statement results into this:

    Uncaught Error: Menu item with ID "blockCopyToStorage" not found.
        at ContextMenuRegistry$$module$build$src$core$contextmenu_registry.unregister (contextmenu_registry.ts:58:17)
        at cleanup_plugins (<anonymous>:549:50)
     at createWorkspace (<anonymous>:369:9)
     at lastScriptToLoad.onload (<anonymous>:217:13)
    

    Which can be resolved by checking first if the context menu item has already been registered.

  2. Beside the blockCopyToStorage menu item, there is also a blockPasteFromStorage menu item. Which needs the same treatement.

As a result, the following code snippet seems to do the job for me:

if (Blockly.ContextMenuRegistry.registry.getItem("blockCopyToStorage")) {
   Blockly.ContextMenuRegistry.registry.unregister('blockCopyToStorage');
}

if (Blockly.ContextMenuRegistry.registry.getItem("blockPasteFromStorage")) {
   Blockly.ContextMenuRegistry.registry.unregister('blockPasteFromStorage');
}

So a dispose function with that would be sufficient...

BTW I am not familiar with Typescript. Is there perhaps somewhere a description of how to contribute something like this (e.g. how to generate the javascript...)?

Enjoy your weekend! Bart

bartbutenaers avatar Nov 04 '23 08:11 bartbutenaers

Thanks Bart! =)

The typescript declarations are fairly straightforward I think, you can a new function declaration here:

dispose(): void;

I took a look at CrossTabCopyPaste and suspect that it doesn't have dispose because having one meant that it might need to cleanup these shortcuts

alicialics avatar Nov 04 '23 13:11 alicialics