From 865a2e544ab56fb6f240d1da90e08468890acd30 Mon Sep 17 00:00:00 2001 From: Liu Date: Sun, 12 May 2024 23:21:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(ui-loader):=20=E7=BA=A0=E6=AD=A3=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=20ident?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + lib/compiler/ui-loader.js | 77 ++++++++++++++++++++------------------- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 37ea88d..42afbf6 100644 --- a/.gitignore +++ b/.gitignore @@ -64,3 +64,4 @@ test/fixtures/*/project *.zip .vscode lib/app-plugin +*.d.ts \ No newline at end of file diff --git a/lib/compiler/ui-loader.js b/lib/compiler/ui-loader.js index 6dbbbaa..0bfbc09 100644 --- a/lib/compiler/ui-loader.js +++ b/lib/compiler/ui-loader.js @@ -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}`); @@ -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": @@ -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) { @@ -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":