Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small blockconfig improvement #9169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 35 additions & 28 deletions webapp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down