From ee221520a9d03ed5cbb55eb266f83145db93a3a6 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Fri, 10 Sep 2021 13:51:12 +0000 Subject: [PATCH] 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 --- demos/code/code.js | 36 +++++++++++++++++++++++++++++++----- demos/code/index.html | 3 +++ demos/code/msg/ar.js | 4 ++-- demos/code/msg/be-tarask.js | 4 ++-- demos/code/msg/br.js | 4 ++-- demos/code/msg/ca.js | 4 ++-- demos/code/msg/cs.js | 4 ++-- demos/code/msg/da.js | 4 ++-- demos/code/msg/de.js | 4 ++-- demos/code/msg/el.js | 4 ++-- demos/code/msg/en.js | 4 ++-- demos/code/msg/es.js | 4 ++-- demos/code/msg/et.js | 4 ++-- demos/code/msg/fa.js | 4 ++-- demos/code/msg/fr.js | 4 ++-- demos/code/msg/he.js | 4 ++-- demos/code/msg/hrx.js | 4 ++-- demos/code/msg/hu.js | 4 ++-- demos/code/msg/ia.js | 4 ++-- demos/code/msg/is.js | 4 ++-- demos/code/msg/it.js | 4 ++-- demos/code/msg/ja.js | 4 ++-- demos/code/msg/kab.js | 4 ++-- demos/code/msg/ko.js | 4 ++-- demos/code/msg/mk.js | 4 ++-- demos/code/msg/ms.js | 4 ++-- demos/code/msg/nb.js | 4 ++-- demos/code/msg/nl.js | 4 ++-- demos/code/msg/oc.js | 4 ++-- demos/code/msg/pl.js | 4 ++-- demos/code/msg/pms.js | 4 ++-- demos/code/msg/pt-br.js | 4 ++-- demos/code/msg/ro.js | 4 ++-- demos/code/msg/ru.js | 4 ++-- demos/code/msg/sc.js | 4 ++-- demos/code/msg/sk.js | 4 ++-- demos/code/msg/sr.js | 4 ++-- demos/code/msg/sv.js | 4 ++-- demos/code/msg/ta.js | 4 ++-- demos/code/msg/th.js | 4 ++-- demos/code/msg/tlh.js | 4 ++-- demos/code/msg/tr.js | 4 ++-- demos/code/msg/uk.js | 4 ++-- demos/code/msg/vi.js | 4 ++-- demos/code/msg/zh-hans.js | 4 ++-- demos/code/msg/zh-hant.js | 4 ++-- 46 files changed, 122 insertions(+), 93 deletions(-) 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‏ >

   

   
+