diff --git a/plugins/block-shareable-procedures/test/index.html b/plugins/block-shareable-procedures/test/index.html index c0731becd6..de969229e2 100644 --- a/plugins/block-shareable-procedures/test/index.html +++ b/plugins/block-shareable-procedures/test/index.html @@ -45,7 +45,7 @@
- +

Saved State

diff --git a/plugins/block-shareable-procedures/test/index.js b/plugins/block-shareable-procedures/test/index.js index d53c567d61..c03d8b82a1 100644 --- a/plugins/block-shareable-procedures/test/index.js +++ b/plugins/block-shareable-procedures/test/index.js @@ -10,7 +10,9 @@ import * as Blockly from 'blockly'; import {toolboxCategories} from '@blockly/dev-tools'; import {blocks, unregisterProcedureBlocks, registerProcedureSerializer} from '../src/index'; -// import {ProcedureBase} from '../src/events_procedure_base'; +import {ProcedureBase} from '../src/events_procedure_base'; + +/* eslint no-unused-vars: "off" */ unregisterProcedureBlocks(); Blockly.common.defineBlocks(blocks); @@ -18,29 +20,54 @@ Blockly.common.defineBlocks(blocks); registerProcedureSerializer(); let workspace1; -// let workspace2; +let workspace2; document.addEventListener('DOMContentLoaded', function() { + injectTwoWorkspaces(); + // injectOneWorkspace(); +}); + +/** + * Injects two workspaces that share procedure models. Undo and redo are off. + */ +function injectTwoWorkspaces() { const options = { toolbox: toolboxCategories, }; workspace1 = Blockly.inject('blockly1', options); - // workspace2 = Blockly.inject('blockly2', options); + workspace2 = Blockly.inject('blockly2', options); // If we allow undoing operations, it can create event loops when sharing // events between workspaces. - // workspace1.MAX_UNDO = 0; - // workspace2.MAX_UNDO = 0; - // workspace1.addChangeListener(createEventSharer(workspace2)); - // workspace2.addChangeListener(createEventSharer(workspace1)); + workspace1.MAX_UNDO = 0; + workspace2.MAX_UNDO = 0; + workspace1.addChangeListener(createEventSharer(workspace2)); + workspace2.addChangeListener(createEventSharer(workspace1)); workspace1.addChangeListener( createSerializationListener(workspace1, 'save1')); - // workspace2.addChangeListener( - // createSerializationListener(workspace2, 'save2')); + workspace2.addChangeListener( + createSerializationListener(workspace2, 'save2')); document.getElementById('load') .addEventListener('click', () => reloadWorkspaces()); -}); +} + +/** Injects one workspace with undo and redo enabled. */ +function injectOneWorkspace() { + const elem = document.getElementById('blockly2'); + elem.parentElement.removeChild(elem); + + const options = { + toolbox: toolboxCategories, + }; + workspace1 = Blockly.inject('blockly1', options); + + workspace1.addChangeListener( + createSerializationListener(workspace1, 'save1')); + + document.getElementById('load') + .addEventListener('click', () => loadWorkspace()); +} /** * Returns an event listener that shares procedure and var events from the @@ -48,25 +75,25 @@ document.addEventListener('DOMContentLoaded', function() { * @param {!Blockly.Workspace} otherWorkspace The workspace to share events to. * @returns {function(Blockly.Events.Abstract)} The listener. */ -// function createEventSharer(otherWorkspace) { -// return (e) => { -// if (!(e instanceof ProcedureBase) && -// !(e instanceof Blockly.Events.VarBase)) { -// return; -// } -// let event; -// try { -// event = Blockly.Events.fromJson(e.toJson(), otherWorkspace); -// } catch (e) { -// // Could not deserialize event. This is expected to happen. E.g. -// // when round-tripping parameter deletes, the delete in the -// // secondary workspace cannot be deserialized into the original -// // workspace. -// return; -// } -// event.run(true); -// }; -// } +function createEventSharer(otherWorkspace) { + return (e) => { + if (!(e instanceof ProcedureBase) && + !(e instanceof Blockly.Events.VarBase)) { + return; + } + let event; + try { + event = Blockly.Events.fromJson(e.toJson(), otherWorkspace); + } catch (e) { + // Could not deserialize event. This is expected to happen. E.g. + // when round-tripping parameter deletes, the delete in the + // secondary workspace cannot be deserialized into the original + // workspace. + return; + } + event.run(true); + }; +} /** * Returns an event listener that serializes the given workspace to JSON and @@ -91,13 +118,19 @@ function createSerializationListener(workspace, textAreaId) { * Reloads both workspaces to showcase serialization working. */ async function reloadWorkspaces() { - const save1 = JSON.parse(document.getElementById('save1').value); - // const save1 = Blockly.serialization.workspaces.save(workspace1); - // const save2 = Blockly.serialization.workspaces.save(workspace2); + const save1 = Blockly.serialization.workspaces.save(workspace1); + const save2 = Blockly.serialization.workspaces.save(workspace2); workspace1.clear(); - // workspace2.clear(); + workspace2.clear(); // Show people the cleared workspace so they know the reload did something. await new Promise((resolve) => setTimeout(resolve, 500)); Blockly.serialization.workspaces.load(save1, workspace1); - // Blockly.serialization.workspaces.load(save2, workspace2); + Blockly.serialization.workspaces.load(save2, workspace2); +} + +/** Loads the workspace in single workspace mode. */ +function loadWorkspace() { + const save1 = JSON.parse(document.getElementById('save1').value); + workspace1.clear(); + Blockly.serialization.workspaces.load(save1, workspace1); }