diff --git a/editor/extension.ts b/editor/extension.ts index a1d4969b..974fa2b6 100644 --- a/editor/extension.ts +++ b/editor/extension.ts @@ -383,6 +383,141 @@ namespace pxt.editor { }) } + /** + * + FALSE + FALSE + FALSE + FALSE + FALSE + FALSE + FALSE + FALSE + TRUE + FALSE + FALSE + FALSE + FALSE + FALSE + FALSE + FALSE + TRUE + FALSE + FALSE + FALSE + FALSE + FALSE + FALSE + FALSE + FALSE + + + to + + ` + # # # # # + . . . . # + . . . . . + . . . . # + . . . . # + ` + + + */ + + function patchBlocks(pkgTargetVersion: string, dom: Element) { + // is this a old script? + if (pxt.semver.majorCmp(pkgTargetVersion || "0.0.0", "1.0.0") >= 0) return; + + // showleds + const nodes = U.toArray(dom.querySelectorAll("block[type=device_show_leds]")) + .concat(U.toArray(dom.querySelectorAll("block[type=device_build_image]"))) + .concat(U.toArray(dom.querySelectorAll("block[type=device_build_big_image]"))) + nodes.forEach(node => { + const leds: string[][] = [[], [], [], [], []]; + U.toArray(node.querySelectorAll("field[name^=LED]")) + .forEach(lednode => { + let n = lednode.getAttribute("name"); + let col = parseInt(n[3]); + let row = parseInt(n[4]); + leds[row][col] = lednode.innerHTML == "TRUE" ? "#" : "."; + }); + node.innerHTML = ""; + const f = node.ownerDocument.createElement("field"); + f.setAttribute("name", "LEDS"); + const s = '`\n' + leds.map(row => row.join('')).join('\n') + '\n`'; + f.appendChild(node.ownerDocument.createTextNode(s)); + node.appendChild(f); + }); + + // radio + /* + + + receivedNumber + + + + name + value + + + + receivedString + + + converts to + + + receivedNumber + + + name + value + + + receivedString + + */ + const varids: pxt.Map = {}; + + function addField(node: Element, name: string) { + const f = node.ownerDocument.createElement("field"); + f.setAttribute("name","HANDLER_" + name) + f.setAttribute("id", varids[name]); + f.appendChild(node.ownerDocument.createTextNode(name)); + node.appendChild(f); + } + + U.toArray(dom.querySelectorAll("variable")).forEach(node => varids[node.innerHTML] = node.getAttribute("id")); + U.toArray(dom.querySelectorAll("block[type=radio_on_packet]")) + .forEach(node => { + const mutation = node.querySelector("mutation"); + if (!mutation) return; + const renameMap = JSON.parse(node.getAttribute("renamemap") || "{}"); + switch (mutation.getAttribute("callbackproperties")) { + case "receivedNumber": + node.setAttribute("type", "radio_on_number"); + node.removeChild(node.querySelector("field[name=receivedNumber]")); + addField(node, "receivedNumber"); + break; + case "receivedString,receivedNumber": + node.setAttribute("type", "radio_on_value"); + node.removeChild(node.querySelector("field[name=receivedNumber]")); + node.removeChild(node.querySelector("field[name=receivedString]")); + addField(node, "name"); + addField(node, "value"); + break; + case "receivedString": + node.setAttribute("type", "radio_on_string"); + node.removeChild(node.querySelector("field[name=receivedString]")); + addField(node, "receivedString"); + break; + } + node.removeChild(mutation); + }) + } + initExtensionsAsync = function (opts: pxt.editor.ExtensionOptions): Promise { pxt.debug('loading microbit target extensions...') @@ -418,6 +553,8 @@ namespace pxt.editor { if (canHID()) pxt.commands.deployCoreAsync = deployCoreAsync; + + res.blocklyPatch = patchBlocks; return Promise.resolve(res); } diff --git a/package.json b/package.json index 81edd9aa..d7afac49 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,6 @@ "@types/node": "8.0.53" }, "dependencies": { - "pxt-core": "3.14.4" + "pxt-core": "3.14.9" } }