Skip to content

Commit

Permalink
fix(ui-loader): 纠正组件 ident
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed May 12, 2024
1 parent 8f899cb commit 865a2e5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ test/fixtures/*/project
*.zip
.vscode
lib/app-plugin
*.d.ts
77 changes: 40 additions & 37 deletions lib/compiler/ui-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
break;
case "template":
currentSchema.template = child;
await compileUINodeChildren(child, parentIdent);
await compileUINode(child);
break;
default:
throw SyntaxError(`Unknown node: ${child.name}`);
Expand All @@ -188,50 +188,54 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
schemas[currentSchema.name] = currentSchema;
}

async function compileUINodeChildren(node, ident) {
async function compileWidgetNodeChildren(node, ident) {
if (!Array.isArray(node.children)) {
return;
}
(await Promise.all(node.children.map(compileUINode))).forEach(
(childIdent) => {
if (childIdent) {
currentSchema.templateLines.push(
`ui_widget_append(${ident}, ${childIdent});`
);
}
}
const identList = await Promise.all(
node.children.map(async (node) => {
const childIdent = allocWidgetNodeIdent(node);
await compileWidgetNode(node, childIdent);
return childIdent;
})
);
identList.forEach((childIdent) => {
currentSchema.templateLines.push(
`ui_widget_append(${ident}, ${childIdent});`
);
});
}

async function compileUINode(node) {
function allocWidgetNodeIdent(node) {
let ident;
let widgetType;
const attrs = node.attributes || {};
const widgetType = ["w", "widget"].includes(node.name)
? attrs.type
: node.name;

if (["w", "widget"].includes(node.name)) {
widgetType = attrs.type;
if (attrs.ref && typeof attrs.ref === "string") {
ident = toIdent(attrs.ref);
currentSchema.refs.push(ident);
ident = `refs->${ident}`;
} else {
widgetType = node.name;
}
if (
currentSchema.template.children.length == 1 &&
currentSchema.template.children[0] === node
) {
ident = parentIdent;
} else {
if (attrs.ref && typeof attrs.ref === "string") {
ident = toIdent(attrs.ref);
currentSchema.refs.push(ident);
ident = `refs->${ident}`;
} else {
ident = `w[${count++}]`;
}
currentSchema.templateLines.push(
`${ident} = ui_create_widget(${
widgetType ? `"${widgetType}"` : "NULL"
});`
);
ident = `w[${count++}]`;
}
currentSchema.templateLines.push(
`${ident} = ui_create_widget(${widgetType ? `"${widgetType}"` : "NULL"});`
);
return ident;
}

async function compileUINode(node) {
return compileWidgetNode(
node.children.length == 1 ? node.children[0] : node,
parentIdent
);
}

async function compileWidgetNode(node, ident) {
const attrs = node.attributes || {};

Object.keys(attrs).forEach((attrName) => {
switch (attrName) {
case "ref":
Expand Down Expand Up @@ -267,8 +271,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
`ui_widget_set_text(${ident}, (const char*)${allocTextVar(node.text)});`
);
}
compileUINodeChildren(node, ident);
return ident;
compileWidgetNodeChildren(node, ident);
}

function compileNode(node) {
Expand All @@ -279,7 +282,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
}
state = stateEnum.PARSE_UI;
currentSchema.body = node;
return compileUINodeChildren(node, parentIdent);
return compileUINode(node);
case "lcui-app":
break;
case "resource":
Expand Down

0 comments on commit 865a2e5

Please sign in to comment.