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

patch projects with top-level shadow blocks #10177

Merged
merged 1 commit into from
Sep 12, 2024
Merged
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
80 changes: 51 additions & 29 deletions pxtblocks/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ function promoteShadow(shadow: Element) {
shadow.remove();
return undefined;
}
const newBlock = createBlockFromShadow(shadow);
shadow.parentElement.appendChild(newBlock);
shadow.remove();

return newBlock;
};

function createBlockFromShadow(shadow: Element) {
const newBlock = Blockly.utils.xml.createElement("block");

for (const attr of shadow.getAttributeNames()) {
Expand All @@ -392,45 +400,59 @@ function promoteShadow(shadow: Element) {
newBlock.appendChild(child.cloneNode(true));
}

shadow.parentElement.appendChild(newBlock);
shadow.remove();

return newBlock;
};
}

export function patchShadows(root: Element, inShadow: boolean) {
if (root.tagName === "shadow") {
const type = root.getAttribute("type");
let shouldPatch = false;

switch (type) {
case "variables_get_reporter":
case "argument_reporter_boolean":
case "argument_reporter_number":
case "argument_reporter_string":
case "argument_reporter_array":
case "argument_reporter_custom":
shouldPatch = true;
break;
if (root.parentElement?.tagName === "xml") {
console.warn(`Shadow block of type '${root.getAttribute("type")}' found at top level. Converting to non-shadow block`);
pxt.tickEvent(`blocks.import.topLevelShadow`, { blockId: root.getAttribute("type") });

const newBlock = createBlockFromShadow(root);
root.parentElement.insertBefore(newBlock, root);
root.remove();
root = newBlock;

const mutation = getDirectChildren(root, "mutation")[0];
if (mutation?.hasAttribute("dupliacteondrag")) {
mutation.removeAttribute("dupliacteondrag");
}
}
else {
const type = root.getAttribute("type");
let shouldPatch = false;

switch (type) {
case "variables_get_reporter":
case "argument_reporter_boolean":
case "argument_reporter_number":
case "argument_reporter_string":
case "argument_reporter_array":
case "argument_reporter_custom":
shouldPatch = true;
break;
}

if (shouldPatch) {
root = promoteShadow(root)
if (!root) return;
let mutation = getDirectChildren(root, "mutation")[0];
if (shouldPatch) {
root = promoteShadow(root)
if (!root) return;
let mutation = getDirectChildren(root, "mutation")[0];

if (mutation) {
mutation.setAttribute("duplicateondrag", "true");
if (mutation) {
mutation.setAttribute("duplicateondrag", "true");
}
else {
mutation = Blockly.utils.xml.createElement("mutation");
mutation.setAttribute("duplicateondrag", "true");
root.appendChild(mutation);
}
}
else {
mutation = Blockly.utils.xml.createElement("mutation");
mutation.setAttribute("duplicateondrag", "true");
root.appendChild(mutation);
else if (type === "variables_get" || hasNonShadowChild(root)) {
root = promoteShadow(root);
}
}
else if (type === "variables_get" || hasNonShadowChild(root)) {
root = promoteShadow(root);
}

}

if (!root) return;
Expand Down