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

Add error callback to the cross tab copy paste plugin

Open BeksOmega opened this issue 3 years ago • 1 comments

Category

  • Plugins

Component

cross-tab-copy-paste

Is your feature request related to a problem? Please describe.

There is the possibility that two unrelated projects could both have the cross tab copy paste plugin installed. This would allow people to try to paste blocks into a workspace where the definitions for those blocks do not exist (causing an error). External developers may want to handle those errors (e.g. presenting a message to the user when they happen). But currently this is not possible because there is no where for external developers to wrap the input event listeners in a try... catch.

Describe the solution you'd like

External developers should be able to pass a callback to the cross-tab-copy-paste plugin that is called when the plugin triggers an error when pasting.

Describe alternatives you've considered

N/A

Additional context

N/A

BeksOmega avatar Aug 09 '22 20:08 BeksOmega

This issue has a few steps to resolve:

  1. Reproduce the error using the steps outlined in the "To Reproduce" section below. Once you can reproduce the bad behavior, you can test if your changes fixed it.
  2. Modify the code in plugins/cross-tab-copy-paste/src/index.js so that the CrossTabCopyPaste class accepts a callback as a parameter to its init function.
  3. Wrap any calls to workspace.paste() in a try...catch which checks for a TypeError and calls the callback if so (otherwise throws).
  4. In plugins/cross-tab-copy-paste/test/index.js pass a callback to the init function. E.g:
copyPastePlugin.init({}, () => console.log('callback called'));
  1. Run the reproduction steps again, and verify that your callback is properly called. I.e. you get a 'callback called' message in the browser console.

After your PR is accepted, a Blockly team member will have to publish the GitHub Pages site, and then you'll be able to see your change live!

To Reproduce

  1. Open up the plugins/cross-tab-copy-paste/test/index.js file.
  2. Add the following code snippet below the import statements, but above the createWorkspace function:
Blockly.Blocks['test'] = {
  init: function() {
    this.appendDummyInput('test').appendField('test');
  },
};
  1. Run npm run start from the cross-tab-copy-paste directory.
  2. Paste the following JSON into the 'JSON' tab on the right of the resulting webpage:
{
  "blocks": {
    "languageVersion": 0,
    "blocks": [
      {
        "type": "test",
        "x": 10,
        "y": 10
      }
    ]
  }
}
  1. Right click the pasted JSON and select the "Import from JSON" option.
  2. Right click the resulting block, and select the "Copy" option.
  3. Comment out the block definition from step 2.
  4. Wait for the page to reload.
  5. Right click the workspace, and select the "Paste" option.
  6. Open the console and observe the error.

BeksOmega avatar Aug 09 '22 20:08 BeksOmega

@BeksOmega since this is open and none of the OSD participants picked it up, maybe I can work on this!

debaraj-barua avatar Sep 16 '22 22:09 debaraj-barua

@BeksOmega since this is open and none of the OSD participants picked it up, maybe I can work on this!

Just assigned you! That would be awesome : )

alschmiedt avatar Sep 16 '22 22:09 alschmiedt