From 1f8ad5fd324a8cb22208c0c7cdfb27fefc2d8f0c Mon Sep 17 00:00:00 2001 From: Rizky Date: Mon, 25 Dec 2023 12:42:46 +0700 Subject: [PATCH] wip fix --- .../ed/panel/popup/script/scope/add-scope.tsx | 1 + .../scope/{scope-parent.tsx => map-exim.tsx} | 17 +---- .../popup/script/scope/scope-children.tsx | 37 ---------- .../ed/panel/popup/script/scope/scope.tsx | 73 +++++++++++++++++-- 4 files changed, 67 insertions(+), 61 deletions(-) rename app/web/src/nova/ed/panel/popup/script/scope/{scope-parent.tsx => map-exim.tsx} (74%) delete mode 100644 app/web/src/nova/ed/panel/popup/script/scope/scope-children.tsx diff --git a/app/web/src/nova/ed/panel/popup/script/scope/add-scope.tsx b/app/web/src/nova/ed/panel/popup/script/scope/add-scope.tsx index 7bca01de..336bfd03 100644 --- a/app/web/src/nova/ed/panel/popup/script/scope/add-scope.tsx +++ b/app/web/src/nova/ed/panel/popup/script/scope/add-scope.tsx @@ -21,6 +21,7 @@ export const addScope = ( ); model.onDidChangeContent((e) => { const text = model.getValue(); + console.log(filename, text); // const models = monaco.editor.getModels().filter((e) => { // return e.uri.toString().startsWith("ts:scope~"); // }); diff --git a/app/web/src/nova/ed/panel/popup/script/scope/scope-parent.tsx b/app/web/src/nova/ed/panel/popup/script/scope/map-exim.tsx similarity index 74% rename from app/web/src/nova/ed/panel/popup/script/scope/scope-parent.tsx rename to app/web/src/nova/ed/panel/popup/script/scope/map-exim.tsx index 2314462f..9b1e849e 100644 --- a/app/web/src/nova/ed/panel/popup/script/scope/scope-parent.tsx +++ b/app/web/src/nova/ed/panel/popup/script/scope/map-exim.tsx @@ -1,22 +1,7 @@ import { IMeta, PG, active } from "../../../../logic/ed-global"; import { extractExportImport } from "./extract-exim"; -export const defineScopeParent = (p: PG, meta: IMeta) => { - const metas = active.comp_id - ? p.comp.list[active.comp_id]?.meta - : p.page.meta; - - const parents: IMeta[] = []; - let cur = meta; - if (cur && cur.parent) { - while (cur && cur.parent && cur.parent.id) { - if (cur.mitem) { - parents.unshift(cur); - } - cur = metas[cur.parent.id]; - } - } - +export const scopeMapExportImport = (p: PG, meta: IMeta, parents: IMeta[]) => { let i = 0; let next_parent = parents[i + 1]; const imports = {} as Record; diff --git a/app/web/src/nova/ed/panel/popup/script/scope/scope-children.tsx b/app/web/src/nova/ed/panel/popup/script/scope/scope-children.tsx deleted file mode 100644 index 367b1218..00000000 --- a/app/web/src/nova/ed/panel/popup/script/scope/scope-children.tsx +++ /dev/null @@ -1,37 +0,0 @@ -import { IMeta, PG, active } from "../../../../logic/ed-global"; -import { extractExportImport } from "./extract-exim"; -import { defineScopeParent } from "./scope-parent"; - -export const defineScopeChildren = ( - p: PG, - meta: IMeta, - imports: Record -) => { - const metas = active.comp_id - ? p.comp.list[active.comp_id]?.meta - : p.page.meta; - - const result = {} as Record; - let cur_id = meta.item.id; - const childs = findChilds(metas, cur_id); - for (const child of childs) { - result[child.item.id] = `\ -${Object.entries(imports) - .map(([k, v]) => { - return `import { ${k} } from "${v.source_file}";`; - }) - .join("\n")} - `; - } - return result; -}; - -const findChilds = (metas: Record, id: string) => { - const childs = [] as IMeta[]; - for (const [k, v] of Object.entries(metas)) { - if (v.parent?.id === id) { - childs.push(metas[k]); - } - } - return childs; -}; diff --git a/app/web/src/nova/ed/panel/popup/script/scope/scope.tsx b/app/web/src/nova/ed/panel/popup/script/scope/scope.tsx index 93454d75..f52f4db4 100644 --- a/app/web/src/nova/ed/panel/popup/script/scope/scope.tsx +++ b/app/web/src/nova/ed/panel/popup/script/scope/scope.tsx @@ -1,8 +1,9 @@ import type { OnMount } from "@monaco-editor/react"; -import { IMeta, PG } from "../../../../logic/ed-global"; +import { IMeta, PG, active } from "../../../../logic/ed-global"; import { addScope } from "./add-scope"; -import { defineScopeChildren } from "./scope-children"; -import { defineScopeParent } from "./scope-parent"; +import { scopeMapExportImport } from "./map-exim"; +import { IItem } from "../../../../../../utils/types/item"; +import { IContent } from "../../../../../../utils/types/general"; type Monaco = Parameters[1]; export type MonacoEditor = Parameters[0]; @@ -13,12 +14,68 @@ export const declareScope = async ( editor: MonacoEditor, monaco: Monaco ) => { - const parent = defineScopeParent(p, meta); + const metas = active.comp_id + ? p.comp.list[active.comp_id]?.meta + : p.page.meta; + const parents: IMeta[] = []; + let cur = meta; + if (cur && cur.parent) { + while (cur && cur.parent && cur.parent.id) { + if (cur.mitem) { + parents.unshift(cur); + } + cur = metas[cur.parent.id]; + } + } - for (const [k, v] of Object.entries(parent.exports)) { - if (k !== meta.item.id) { - for (const [i, j] of Object.entries(v)) { - addScope(p, monaco, i, j); + const last_parent = parents[parents.length - 1]; + if ( + last_parent && + Array.isArray(last_parent.item.childs) && + last_parent.item.childs.length > 0 + ) { + const paths: IMeta[][] = []; + const childs = last_parent.item.childs; + + const map_childs = ( + childs: IContent[], + paths: IMeta[][], + parent?: IMeta + ) => { + for (const m of childs) { + const meta = metas[m.id]; + if (meta) { + let cur: null | IMeta[] = null; + for (const path of paths) { + if (path[path.length - 1] === parent) { + cur = path; + cur.push(meta); + break; + } + } + + if (!cur) { + paths.push([meta]); + cur = paths[paths.length - 1]; + } + + if (cur) { + if (Array.isArray(meta.item.childs)) { + map_childs(meta.item.childs, paths, meta); + } + } + } + } + }; + map_childs(childs, paths); + } else { + const exim = scopeMapExportImport(p, meta, parents); + + for (const [k, v] of Object.entries(exim.exports)) { + if (k !== meta.item.id) { + for (const [i, j] of Object.entries(v)) { + addScope(p, monaco, i, j); + } } } }