diff --git a/demos/code/code.js b/demos/code/code.js index b8602064966..9ae4f792c87 100644 --- a/demos/code/code.js +++ b/demos/code/code.js @@ -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) { diff --git a/demos/code/index.html b/demos/code/index.html index 9fd858aeffa..a3a69bb7db9 100644 --- a/demos/code/index.html +++ b/demos/code/index.html @@ -47,6 +47,8 @@

Blockly‏ > Dart   XML +   + JSON   @@ -78,6 +80,7 @@

Blockly‏ >

   

   
+