Skip to content

Commit

Permalink
Copy paste between blockly nodes (#122)
Browse files Browse the repository at this point in the history
* Dependency plugin-cross-tab-copy-paste

* copy paste plugin

* Cleanup copy-paste plugin

* Load copypaste plugin globally
  • Loading branch information
bartbutenaers authored Nov 22, 2023
1 parent ee0c6e1 commit 2179473
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
54 changes: 34 additions & 20 deletions blockly.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<script type="text/javascript">
function loadResourcesFromServer(node, language, categories) {
var lastScriptToLoad;

// When no categories are passed (e.g. when there is no config node selected), then use the default categories
if (!categories) {
categories = getDefaultBlocklyCategories();
Expand Down Expand Up @@ -47,7 +47,8 @@
"blockly-contrib/npm/@blockly___SEPARATOR___zoom-to-fit/dist/index.js",
"blockly-contrib/npm/@blockly___SEPARATOR___workspace-backpack/dist/index.js",
"blockly-contrib/npm/@blockly___SEPARATOR___toolbox-search/dist/index.js",
"blockly-contrib/npm/@blockly___SEPARATOR___workspace-minimap/dist/index.js"
"blockly-contrib/npm/@blockly___SEPARATOR___workspace-minimap/dist/index.js",
"blockly-contrib/npm/@blockly___SEPARATOR___plugin-cross-tab-copy-paste/dist/index.js"
]
});

Expand Down Expand Up @@ -102,7 +103,20 @@
// And then the language files can be loaded into the new empty Blockly.Msg
lastScriptToLoad.onload = function() {
node.librariesLoaded = true;


// Unlike the other plugins below, the CrossTabCopyPaste plugin needs to be initialized globally, and as a resulting
// it doesn't need to be disposed. See more info on https://github.com/google/blockly-samples/pull/2061
// We will initialize the plugin here, because at this point a new empty Blockly instance has been loaded.
// On that new instance, the init method will e.g. register the 'copy' and 'paste' context menu actions.
// Note that this can't be done at the point where the other plugins are being initialized, because then an
// exception would be thrown every time the workspace is being recreated (e.g. when switching to full-screen),
// because the context menu actions have already been created. Because the global plugin is never disposed....
var copyPastePlugin = new CrossTabCopyPaste();
copyPastePlugin.init({
contextMenu: true,
shortcut: true,
});

Blockly.blocklyEditorVisible = true;

// Make sure the Blockly datetime_convert_from_date block is always aware about the number of outputs
Expand Down Expand Up @@ -377,22 +391,13 @@
// Remember the current config node id for the next time we arrive in this function
node.previousConfigNodeId = configNodeId;

// When previously a workspace-backpack plugin has been activated (for the previous workspace instance), then clean it up
if (node.backpack) {
node.backpack.dispose();
node.backpack = null;
}

// Blockly requires an empty array for the content of an empty backpack (instead of undefined or null)
if (!backpackContents) {
backpackContents = [];
}
cleanup_plugins(node);

// When previously another workspace was already created, then cleanup that one first (to avoid displaying multiple workspaces).
if (node.workspace) {
node.workspace.dispose();
node.workspace = null;
}
}

// Ralph: This doesn't work!
// try {
Expand Down Expand Up @@ -456,7 +461,7 @@
node.backpack.setContents(backpackContents);
}
}

// Trigger an event to indicate that a new Blockly workspace has been created.
// Blockly doesn't offer such an event. See https://groups.google.com/g/blockly/c/sSpvp_Kz-To
// Such an event is required, because this node switches often its workspace (and some blocks need to be aware of that...).
Expand Down Expand Up @@ -524,6 +529,19 @@
node.aceXMLEditor.destroy();
delete node.aceXMLEditor;

cleanup_plugins(node);

// Blockly requires an empty array for the content of an empty backpack (instead of undefined or null)
if (!backpackContents) {
backpackContents = [];
}

// Cleanup the Blockly workspace
node.workspace.dispose();
delete node.workspace;
}

function cleanup_plugins(node) {
// When previously a zoom-to-fit plugin has been activated, then clean it up
if (node.zoomToFit) {
node.zoomToFit.dispose();
Expand All @@ -541,12 +559,8 @@
node.backpack.dispose();
node.backpack = null;
}

// Cleanup the Blockly workspace
node.workspace.dispose();
delete node.workspace;
}

// See https://developers.google.com/blockly/guides/configure/web/resizable
function resize(node) {
// Resize the Blockly workspace, as soon as available (i.e. not when called from oneditprepare).
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "node-red-contrib-blockly",
"version" : "2.3.0",
"version" : "2.4.0",
"description" : "A Node Red node for visual programming a function using Blockly",
"dependencies": {
"blockly": "^10.1.3",
Expand All @@ -11,6 +11,7 @@
"@blockly/plugin-workspace-search": "^8.0.4",
"@blockly/toolbox-search": "^1.1.6",
"@blockly/workspace-minimap": "^0.1.0",
"@blockly/plugin-cross-tab-copy-paste": "^5.0.2",
"@blockly/theme-dark": "^6.0.1",
"@blockly/theme-highcontrast": "^5.0.1",
"@blockly/theme-tritanopia": "^5.0.1",
Expand Down

0 comments on commit 2179473

Please sign in to comment.