From 64bdcb5f505e1a359b4c7f044646ed7d67bdb94c Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Wed, 2 Nov 2022 14:06:44 -0700 Subject: [PATCH] small blockconfig improvement --- webapp/src/app.tsx | 63 +++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/webapp/src/app.tsx b/webapp/src/app.tsx index 4d69abb19cc4..45883b15e020 100644 --- a/webapp/src/app.tsx +++ b/webapp/src/app.tsx @@ -1789,38 +1789,45 @@ export class ProjectView blockConfig.blocks = []; const blocks: Element[] = []; try { - // Decompile block markdown to xml - const decomp = await compiler.decompileBlocksSnippetAsync(blockConfig.md, undefined, { - snippetMode: true, - generateSourceMap: false - }); - const xml = decomp.outfiles[pxt.MAIN_BLOCKS]; - pxt.debug(`decompiled ${blockConfig.md} to ${xml}`); - // Get all top-level blocks - (() => { - const dom = Blockly.Xml.textToDom(xml); - const children = Array.from(dom.children); - for (const child of children) { - if (child.nodeName === "block") { - blocks.push(child); + const xmlToBlockConfig = (xml: string) => { + // Get all top-level blocks + (() => { + const dom = Blockly.Xml.textToDom(xml); + const children = Array.from(dom.children); + for (const child of children) { + if (child.nodeName === "block") { + blocks.push(child); + } } - } - })(); - // Extract child blocks from blocks of type "next", and discard the "next" block - (() => { - for (const block of blocks) { - const children = Array.from(block.children); - for (const child1 of children) { - if (child1.nodeName === "next") { - for (const child2 of Array.from(child1.children)) { - // Grab the blocks embedded in the "next" block - blocks.push(child2); + })(); + // Extract child blocks from blocks of type "next", discard the "next" block, and flatten + (() => { + loop: while (true) { + for (const block of blocks.slice()) { + const children = Array.from(block.children); + for (const child of children) { + if (child.nodeName === "next") { + const childBlock = Array.from(child.children)?.find(c => c.nodeName === "block"); + if (childBlock) blocks.push(childBlock); + block.removeChild(child); + continue loop; + } } - block.removeChild(child1); } + break; } - } - })(); + })(); + } + + // Decompile block markdown to xml + const decomp = await compiler.decompileBlocksSnippetAsync(blockConfig.md, undefined, { + snippetMode: true, + generateSourceMap: false, + }); + const mainXml = decomp.outfiles[pxt.MAIN_BLOCKS]; + pxt.debug(`decompiled ${blockConfig.md} to ${mainXml}`); + + xmlToBlockConfig(mainXml); } catch (e) { // Failed to decompile, don't propagate exception console.error(`Failed to resolve blockconfig for tutorial: ${header.tutorial.tutorialName}, ${e.message}. md:${blockConfig.md}`);