Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"gh-pages": "^0.12.0",
"highlightjs": "^9.8.0",
"htmlparser2": "3.9.2",
"jsdom": "^9.11.0",
"json": "^9.0.4",
"lodash.defaultsdeep": "4.6.0",
"minilog": "3.1.0",
Expand Down
5 changes: 5 additions & 0 deletions src/playground/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ window.onload = function () {
});
window.workspace = workspace;

// Filter available blocks
var toolbox = vm.filterToolbox(workspace.options.languageTree);
// var toolbox = workspace.options.languageTree;
workspace.updateToolbox(toolbox);

// Attach scratch-blocks events to VM.
workspace.addChangeListener(vm.blockListener);
var flyoutWorkspace = workspace.getFlyout().getWorkspace();
Expand Down
49 changes: 49 additions & 0 deletions src/util/filter-toolbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Filter Blockly toolbox XML node containing blocks to only those with
* valid opcodes. Return a copy of the node with valid blocks.
* @param {HTMLElement} node Blockly toolbox XML node
* @param {Array.<string>} opcodes Valid opcodes. Blocks producing other opcodes
* will be filtered.
* @returns {HTMLElement} filtered toolbox XML node
*/
var filterToolboxNode = function (node, opcodes) {
var filteredCategory = node.cloneNode();
for (var block = node.firstElementChild; block; block = block.nextElementSibling) {
if (block.nodeName.toLowerCase() !== 'block') continue;
var opcode = block.getAttribute('type').toLowerCase();
if (opcodes.indexOf(opcode) !== -1) {
filteredCategory.appendChild(block.cloneNode(true));
}
}
return filteredCategory;
};

/**
* Filter Blockly toolbox XML and return a copy which only contains blocks with
* existent opcodes. Categories with no valid children will be removed.
* @param {HTMLElement} toolbox Blockly toolbox XML node
* @param {Array.<string>} opcodes Valid opcodes. Blocks producing other opcodes
* will be filtered.
* @returns {HTMLElement} filtered toolbox XML node
*/
var filterToolbox = function (toolbox, opcodes) {
if (!toolbox.hasChildNodes()) return toolbox;
var filteredToolbox;
if (toolbox.firstElementChild.nodeName.toLowerCase() === 'category') {
filteredToolbox = toolbox.cloneNode();
for (
var category = toolbox.firstElementChild;
category;
category = category.nextElementSibling
) {
if (category.nodeName.toLowerCase() !== 'category') continue;
var filteredCategory = filterToolboxNode(category, opcodes);
if (filteredCategory.hasChildNodes()) filteredToolbox.appendChild(filteredCategory);
}
} else {
filteredToolbox = filterToolboxNode(toolbox, opcodes);
}
return filteredToolbox;
};

module.exports = filterToolbox;
14 changes: 14 additions & 0 deletions src/virtual-machine.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var EventEmitter = require('events');
var util = require('util');

var filterToolbox = require('./util/filter-toolbox');
var Runtime = require('./engine/runtime');
var sb2import = require('./import/sb2import');

Expand Down Expand Up @@ -336,4 +337,17 @@ VirtualMachine.prototype.postSpriteInfo = function (data) {
this.editingTarget.postSpriteInfo(data);
};


/**
* Filter Blockly toolbox XML and return a copy which only contains blocks with
* existent opcodes. Categories with no valid children will be removed.
* @param {HTMLElement} toolbox Blockly toolbox XML node
* @returns {HTMLElement} filtered toolbox XML node
*/
VirtualMachine.prototype.filterToolbox = function (toolbox) {
var opcodes = Object.keys(this.runtime._primitives)
.concat(Object.keys(this.runtime._hats));
return filterToolbox(toolbox, opcodes);
};

module.exports = VirtualMachine;
5 changes: 5 additions & 0 deletions test/fixtures/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'max-len': [0]
}
};
Loading