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