fix: update code demo to include JSON (#5444)

* fix: update code demo to include JSON

* fix: do property renames

* fix: change XML to place holder

* fix: PR comments
This commit is contained in:
Beka Westberg
2021-09-10 13:51:12 +00:00
committed by alschmiedt
parent 84514efb09
commit ee221520a9
46 changed files with 122 additions and 93 deletions

View File

@@ -234,14 +234,16 @@ Code.LANG = Code.getLang();
* List of tab names.
* @private
*/
Code.TABS_ = ['blocks', 'javascript', 'php', 'python', 'dart', 'lua', 'xml'];
Code.TABS_ = [
'blocks', 'javascript', 'php', 'python', 'dart', 'lua', 'xml', 'json'
];
/**
* List of tab names with casing, for display in the UI.
* @private
*/
Code.TABS_DISPLAY_ = [
'Blocks', 'JavaScript', 'PHP', 'Python', 'Dart', 'Lua', 'XML',
'Blocks', 'JavaScript', 'PHP', 'Python', 'Dart', 'Lua', 'XML', 'JSON'
];
Code.selected = 'blocks';
@@ -259,8 +261,8 @@ Code.tabClick = function(clickedName) {
try {
xmlDom = Blockly.Xml.textToDom(xmlText);
} catch (e) {
var q =
window.confirm(MSG['badXml'].replace('%1', e));
var q = window.confirm(
MSG['parseError'].replace(/%1/g, 'XML').replace('%2', e));
if (!q) {
// Leave the user on the XML tab.
return;
@@ -272,6 +274,25 @@ Code.tabClick = function(clickedName) {
}
}
if (document.getElementById('tab_json').classList.contains('tabon')) {
var jsonTextarea = document.getElementById('content_json');
var jsonText = jsonTextarea.value;
var json = null;
try {
json = JSON.parse(jsonText);
} catch (e) {
var q = window.confirm(
MSG['parseError'].replace(/%1/g, 'JSON').replace('%2', e));
if (!q) {
// Leave the user on the JSON tab.
return;
}
}
if (json) {
Blockly.serialization.workspaces.load(json, Code.workspace);
}
}
if (document.getElementById('tab_blocks').classList.contains('tabon')) {
Code.workspace.setVisible(false);
}
@@ -324,6 +345,11 @@ Code.renderContent = function() {
var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
xmlTextarea.value = xmlText;
xmlTextarea.focus();
} else if (content.id == 'content_json') {
var jsonTextarea = document.getElementById('content_json');
jsonTextarea.value = JSON.stringify(
Blockly.serialization.workspaces.save(Code.workspace), null, 2);
jsonTextarea.focus();
} else if (content.id == 'content_javascript') {
Code.attemptCodeGeneration(Blockly.JavaScript);
} else if (content.id == 'content_python') {
@@ -466,7 +492,7 @@ Code.init = function() {
BlocklyStorage['HTTPREQUEST_ERROR'] = MSG['httpRequestError'];
BlocklyStorage['LINK_ALERT'] = MSG['linkAlert'];
BlocklyStorage['HASH_ERROR'] = MSG['hashError'];
BlocklyStorage['XML_ERROR'] = MSG['xmlError'];
BlocklyStorage['XML_ERROR'] = MSG['loadError'];
Code.bindClick(linkButton,
function() {BlocklyStorage.link(Code.workspace);});
} else if (linkButton) {