Fix performance (#3931)

* Speed up performance for converting the xml to json
This commit is contained in:
alschmiedt
2020-06-05 09:22:52 -07:00
committed by GitHub
parent 26e84fef11
commit b77a30450e
2 changed files with 22 additions and 15 deletions

View File

@@ -612,18 +612,23 @@ Blockly.Flyout.prototype.createBlock_ = function(blockXml) {
* @private
*/
Blockly.Flyout.prototype.getBlockXml_ = function(blockInfo) {
var blockXml = null;
// All blockInfo will have type, so check for blockxml first.
if (blockInfo['blockxml']) {
blockXml = Blockly.Xml.textToDom(blockInfo['blockxml']);
var blockElement = null;
var blockXml = blockInfo['blockxml'];
if (blockXml && typeof blockXml != 'string') {
blockElement = blockXml;
} else if (blockXml && typeof blockXml == 'string') {
blockElement = Blockly.Xml.textToDom(blockXml);
} else if (blockInfo['type']) {
blockXml = Blockly.utils.xml.createElement('xml');
blockXml.setAttribute('type', blockInfo['type']);
blockXml.setAttribute('disabled', blockInfo['disabled']);
} else {
blockElement = Blockly.utils.xml.createElement('xml');
blockElement.setAttribute('type', blockInfo['type']);
blockElement.setAttribute('disabled', blockInfo['disabled']);
}
if (!blockElement) {
throw Error('Error: Invalid block definition. Block definition must have blockxml or type.');
}
return blockXml;
return blockElement;
};
/**

View File

@@ -126,15 +126,17 @@ Blockly.utils.toolbox.toolboxXmlToJson_ = function(toolboxDef) {
continue;
}
var obj = {};
obj['kind'] = child.tagName.toUpperCase();
var tagName = child.tagName.toUpperCase();
obj['kind'] = tagName;
// Store the xml for a block
if (child.tagName.toUpperCase() == 'BLOCK') {
obj['blockxml'] = Blockly.utils.xml.domToText(child);
}
// Get the contents for a category.
if (child.tagName.toUpperCase() == 'CATEGORY') {
if (tagName == 'BLOCK') {
obj['blockxml'] = child;
} else if (tagName == 'CATEGORY') {
// Get the contents of a category
obj['contents'] = Blockly.utils.toolbox.toolboxXmlToJson_(child);
}
// Add xml attributes to object
for (var j = 0; j < child.attributes.length; j++) {
var attr = child.attributes[j];