Skip to content

Commit 865a2e5

Browse files
committed
fix(ui-loader): 纠正组件 ident
1 parent 8f899cb commit 865a2e5

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ test/fixtures/*/project
6464
*.zip
6565
.vscode
6666
lib/app-plugin
67+
*.d.ts

lib/compiler/ui-loader.js

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
175175
break;
176176
case "template":
177177
currentSchema.template = child;
178-
await compileUINodeChildren(child, parentIdent);
178+
await compileUINode(child);
179179
break;
180180
default:
181181
throw SyntaxError(`Unknown node: ${child.name}`);
@@ -188,50 +188,54 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
188188
schemas[currentSchema.name] = currentSchema;
189189
}
190190

191-
async function compileUINodeChildren(node, ident) {
191+
async function compileWidgetNodeChildren(node, ident) {
192192
if (!Array.isArray(node.children)) {
193193
return;
194194
}
195-
(await Promise.all(node.children.map(compileUINode))).forEach(
196-
(childIdent) => {
197-
if (childIdent) {
198-
currentSchema.templateLines.push(
199-
`ui_widget_append(${ident}, ${childIdent});`
200-
);
201-
}
202-
}
195+
const identList = await Promise.all(
196+
node.children.map(async (node) => {
197+
const childIdent = allocWidgetNodeIdent(node);
198+
await compileWidgetNode(node, childIdent);
199+
return childIdent;
200+
})
203201
);
202+
identList.forEach((childIdent) => {
203+
currentSchema.templateLines.push(
204+
`ui_widget_append(${ident}, ${childIdent});`
205+
);
206+
});
204207
}
205208

206-
async function compileUINode(node) {
209+
function allocWidgetNodeIdent(node) {
207210
let ident;
208-
let widgetType;
209211
const attrs = node.attributes || {};
212+
const widgetType = ["w", "widget"].includes(node.name)
213+
? attrs.type
214+
: node.name;
210215

211-
if (["w", "widget"].includes(node.name)) {
212-
widgetType = attrs.type;
216+
if (attrs.ref && typeof attrs.ref === "string") {
217+
ident = toIdent(attrs.ref);
218+
currentSchema.refs.push(ident);
219+
ident = `refs->${ident}`;
213220
} else {
214-
widgetType = node.name;
215-
}
216-
if (
217-
currentSchema.template.children.length == 1 &&
218-
currentSchema.template.children[0] === node
219-
) {
220-
ident = parentIdent;
221-
} else {
222-
if (attrs.ref && typeof attrs.ref === "string") {
223-
ident = toIdent(attrs.ref);
224-
currentSchema.refs.push(ident);
225-
ident = `refs->${ident}`;
226-
} else {
227-
ident = `w[${count++}]`;
228-
}
229-
currentSchema.templateLines.push(
230-
`${ident} = ui_create_widget(${
231-
widgetType ? `"${widgetType}"` : "NULL"
232-
});`
233-
);
221+
ident = `w[${count++}]`;
234222
}
223+
currentSchema.templateLines.push(
224+
`${ident} = ui_create_widget(${widgetType ? `"${widgetType}"` : "NULL"});`
225+
);
226+
return ident;
227+
}
228+
229+
async function compileUINode(node) {
230+
return compileWidgetNode(
231+
node.children.length == 1 ? node.children[0] : node,
232+
parentIdent
233+
);
234+
}
235+
236+
async function compileWidgetNode(node, ident) {
237+
const attrs = node.attributes || {};
238+
235239
Object.keys(attrs).forEach((attrName) => {
236240
switch (attrName) {
237241
case "ref":
@@ -267,8 +271,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
267271
`ui_widget_set_text(${ident}, (const char*)${allocTextVar(node.text)});`
268272
);
269273
}
270-
compileUINodeChildren(node, ident);
271-
return ident;
274+
compileWidgetNodeChildren(node, ident);
272275
}
273276

274277
function compileNode(node) {
@@ -279,7 +282,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
279282
}
280283
state = stateEnum.PARSE_UI;
281284
currentSchema.body = node;
282-
return compileUINodeChildren(node, parentIdent);
285+
return compileUINode(node);
283286
case "lcui-app":
284287
break;
285288
case "resource":

0 commit comments

Comments
 (0)