Skip to content

Commit

Permalink
chore: make it easier to test the single workspace setup
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Sep 7, 2023
1 parent 81d1b09 commit 64b7f99
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 35 deletions.
2 changes: 1 addition & 1 deletion plugins/block-shareable-procedures/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<body>
<div id="root">
<div class="blockly" id="blockly1"></div>
<!-- <div class="blockly" id="blockly2"></div> -->
<div class="blockly" id="blockly2"></div>
<div id="saveArea">
<h2>Saved State</h2>
<input id="load" type="button" value="Reload"/>
Expand Down
101 changes: 67 additions & 34 deletions plugins/block-shareable-procedures/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,90 @@
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);

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
* connected workspace to the other workspace.
* @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
Expand All @@ -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);
}

0 comments on commit 64b7f99

Please sign in to comment.