Copy & paste blocks in one workspace another workspace

861 views
Skip to first unread message

Sagar Raviprolu

unread,
Jan 25, 2021, 1:06:27 AM1/25/21
to Blockly
Can someone help on copy & paste blocks in one workspace another workspace with config or javascript?

Abby Schmiedt

unread,
Jan 25, 2021, 12:46:14 PM1/25/21
to Blockly
Hello!

This post on github might be helpful to you. The code in the post adds a menu item to the context menu. You can test this, by right clicking on the block and choosing "Copy to stash", and then choosing another workspace and choosing "Paste from stash".
I had to make a few very minor changes to make it work for me, so I will copy the code with those changes below: 
Blockly.ContextMenuItems.blockCopyToStorage = function() {
/** @type {!Blockly.ContextMenuRegistry.RegistryItem} */
var copyToStorageOption = {
displayText: function() {
return 'Copy to stash';
},
preconditionFn: function(/** @type {!Blockly.ContextMenuRegistry.Scope} */ scope) {
return 'enabled';
},
callback: function(/** @type {!Blockly.ContextMenuRegistry.Scope} */ scope) {
var blockDom = Blockly.Xml.blockToDomWithXY(scope.block);
var blockText = Blockly.Xml.domToText(blockDom);
localStorage.setItem('blocklyStash', blockText);
},
scopeType: Blockly.ContextMenuRegistry.ScopeType.BLOCK,
id: 'blockCopyToStorage',
weight: 0,
};
Blockly.ContextMenuRegistry.registry.register(copyToStorageOption);
};
Blockly.ContextMenuItems.blockPasteFromStorage = function() {
/** @type {!Blockly.ContextMenuRegistry.RegistryItem} */
var pasteFromStorageOption = {
displayText: function() {
return 'Paste from stash';
},
preconditionFn: function(/** @type {!Blockly.ContextMenuRegistry.Scope} */ scope) {
if (localStorage.getItem('blocklyStash')){
return 'enabled';
} else {
return 'disabled';
}

},
callback: function(/** @type {!Blockly.ContextMenuRegistry.Scope} */ scope) {
var blockText = localStorage.getItem('blocklyStash');
var blockDom = Blockly.Xml.textToDom(blockText);
Blockly.Xml.domToBlock(blockDom, scope.workspace);
},
scopeType: Blockly.ContextMenuRegistry.ScopeType.WORKSPACE,
id: 'blockPasteFromStorage',
weight: 0,
};
Blockly.ContextMenuRegistry.registry.register(pasteFromStorageOption);
};
// Register the menus

Blockly.ContextMenuItems.blockCopyToStorage();
Blockly.ContextMenuItems.blockPasteFromStorage();


Hope this helps!
Abby 

Sagar Raviprolu

unread,
Feb 3, 2021, 6:15:03 AM2/3/21
to Blockly
facing below issue can you help me on this(implementing in react.js)

TypeError: Cannot read property 'ScopeType' of undefined
push../src/Resource/Blockly/Layout/Generator.js.blockly_core__WEBPACK_IMPORTED_MODULE_0___default.a.ContextMenu.blockCopyToStorage
C:/GLAMS_Project/GLAMS 6/project-center/src/Resource/Blockly/Layout/Generator.js:18
15 | var blockText = Blockly.Xml.domToText(blockDom);
16 | localStorage.setItem('blocklyStash', blockText);
17 | },
> 18 | scopeType: Blockly.ContextMenuRegistry.ScopeType.WORKSPACE,
| ^ 19 | id: 'blockCopyToStorage',
20 | weight: 0,
21 | };

Beka Westberg

unread,
Feb 3, 2021, 6:00:42 PM2/3/21
to blo...@googlegroups.com
Hello,

Are you using the latest version of Blockly (4.20201217.0)? The ContextMenuRegistry only got added recently, so if you're using an older version it might not be available.

I hope that helps!
--Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/32370b15-036f-4af5-8844-cd12b2180aecn%40googlegroups.com.

Sagar Raviprolu

unread,
Feb 4, 2021, 1:50:44 AM2/4/21
to Blockly
Thank you so much for your help.

after doing all my changes I am facing below issue 

Error: Menu item with id "1111111111111111111hello_world" is already registered.  

Beka Westberg

unread,
Feb 4, 2021, 4:19:59 PM2/4/21
to blo...@googlegroups.com
Hmm, it looks like you're probably registering a menu item twice, or registering two menu items with the same id =) What code are you using that's throwing this error?

--Beka

Sagar Raviprolu

unread,
Feb 5, 2021, 2:25:16 AM2/5/21
to Blockly
function registerOutputOption() {
    const outputOption = {
      displayText: 'I have an output connection',
      preconditionFn: function(scope) {
        if (scope.block.outputConnection) {
          return 'enabled';
        }
        return 'hidden';
      },
      callback: function(scope) {
      },
      scopeType: Blockly.ContextMenuRegistry.ScopeType.BLOCK,
      id: 'test_block',
      weight: 100,
    };
    Blockly.ContextMenuRegistry.registry.register(outputOption);
  }

  registerOutputOption()

Beka Westberg

unread,
Feb 5, 2021, 6:26:12 PM2/5/21
to blo...@googlegroups.com
Hmm that's weird, it doesn't look like that code would generate that error. If you look at the source code, you can see the error includes the `item.id`. So for this option I would expect it to throw the error "Error: Menu item with id "test_block" is already registered." Is there somewhere you're registering an option with the ID "1111111111111111111hello_world"?

Best,
--Beka

Reply all
Reply to author
Forward
0 new messages