From 2545d1955d81c0d06b9559813c26ed00914ba499 Mon Sep 17 00:00:00 2001 From: DD Date: Tue, 13 Feb 2018 18:09:15 -0500 Subject: [PATCH] Repopulate blocks with ids --- src/engine/adapter.js | 31 +++++++++++++++++++++++++++++++ src/engine/blocks.js | 2 +- src/virtual-machine.js | 1 + 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/engine/adapter.js b/src/engine/adapter.js index 5874afd55..8a6a7fafd 100644 --- a/src/engine/adapter.js +++ b/src/engine/adapter.js @@ -1,6 +1,33 @@ const mutationAdapter = require('./mutation-adapter'); const html = require('htmlparser2'); +/** + * (Copied from Blockly.utils) + * Legal characters for the unique ID. Should be all on a US keyboard. + * No characters that conflict with XML or JSON. Requests to remove additional + * 'problematic' characters from this soup will be denied. That's your failure + * to properly escape in your own environment. Issues #251, #625, #682, #1304. + * @private + */ +const genUidSoup_ = '!#$%()*+,-./:;=?@[]^_`{|}~' + + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + +/** + * (Copied from Blockly.utils) + * Generate a unique ID. This should be globally unique. + * 87 characters ^ 20 length > 128 bits (better than a UUID). + * @return {string} A globally unique ID string. + */ +const genUid_ = function () { + const length = 20; + const soupLength = genUidSoup_.length; + const id = []; + for (let i = 0; i < length; i++) { + id[i] = genUidSoup_.charAt(Math.random() * soupLength); + } + return id.join(''); +}; + /** * Convert and an individual block DOM to the representation tree. * Based on Blockly's `domToBlockHeadless_`. @@ -11,6 +38,10 @@ const html = require('htmlparser2'); * @return {undefined} */ const domToBlock = function (blockDOM, blocks, isTopBlock, parent) { + if (!blockDOM.attribs.id) { + blockDOM.attribs.id = genUid_(); + } + // Block skeleton. const block = { id: blockDOM.attribs.id, // Block ID diff --git a/src/engine/blocks.js b/src/engine/blocks.js index 2e62e2f3e..d19e6fc34 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -288,7 +288,7 @@ class Blocks { newCoordinate: e.newCoordinate }); break; - case 'drag': + case 'dragOutside': if (optRuntime) { optRuntime.emitBlockDragUpdate(e.isOutside); } diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 1e9de9ea1..72478e5b7 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -626,6 +626,7 @@ class VirtualMachine extends EventEmitter { this._hoveredSpriteId !== this.editingTarget.id) { const dragTarget = this.runtime.getTargetById(this._hoveredSpriteId); if (dragTarget) { + // TODO tell gui to tell blocks to undo, to cause the blocks to move back into place. const newBlocks = adapter(e); for (let i = 0; i < newBlocks.length; i++) { dragTarget.blocks.createBlock(newBlocks[i]);