From 59ab04b6be8fb85f171f50499381945644475b08 Mon Sep 17 00:00:00 2001 From: Rizky Date: Tue, 19 Dec 2023 16:05:51 +0700 Subject: [PATCH] wip fix local --- app/srv/ws/sync/actions.ts | 5 +- app/srv/ws/sync/actions/code_edit.ts | 88 +++++++++++-------- app/srv/ws/sync/actions/comp_load.ts | 2 +- app/srv/ws/sync/actions/page_load.ts | 4 +- app/srv/ws/sync/editor/parser/parse-js.ts | 6 +- app/web/src/nova/ed/logic/ed-route.ts | 4 +- .../src/nova/ed/panel/popup/script/monaco.tsx | 14 ++- .../src/nova/ed/panel/popup/script/scope.tsx | 4 +- .../src/nova/ed/panel/tree/node/item/name.tsx | 4 +- 9 files changed, 78 insertions(+), 53 deletions(-) diff --git a/app/srv/ws/sync/actions.ts b/app/srv/ws/sync/actions.ts index 9a563332..cb143f1d 100644 --- a/app/srv/ws/sync/actions.ts +++ b/app/srv/ws/sync/actions.ts @@ -3,12 +3,11 @@ import { EComp, EPage, ESite, - IScopeComp, PropFieldKind, } from "../../../web/src/nova/ed/logic/ed-global"; import { IItem } from "../../../web/src/utils/types/item"; import { site_group } from "./actions/site_group"; -import { parseJs } from "./editor/parser/parse-js"; +import { ParsedScope, parseJs } from "./editor/parser/parse-js"; /* WARNING: @@ -113,7 +112,7 @@ export const SyncActions = { prop_kind: PropFieldKind; value: Uint8Array; } - ) => ({}) as boolean, + ) => ({}) as boolean | ParsedScope, parse: async (code: string | Record) => ({}) as Record>, }, diff --git a/app/srv/ws/sync/actions/code_edit.ts b/app/srv/ws/sync/actions/code_edit.ts index 02fade45..39d64631 100644 --- a/app/srv/ws/sync/actions/code_edit.ts +++ b/app/srv/ws/sync/actions/code_edit.ts @@ -1,3 +1,5 @@ +import { transform } from "esbuild"; +import { g } from "utils/global"; import { Doc } from "yjs"; import { MContent } from "../../../../web/src/utils/types/general"; import { MItem } from "../../../../web/src/utils/types/item"; @@ -6,7 +8,7 @@ import { SAction } from "../actions"; import { docs } from "../entity/docs"; import { gunzipAsync } from "../entity/zlib"; import { SyncConnection } from "../type"; -import { transform } from "esbuild"; +import { parseJs } from "../editor/parser/parse-js"; const decoder = new TextDecoder(); export const code_edit: SAction["code"]["edit"] = async function ( @@ -16,7 +18,7 @@ export const code_edit: SAction["code"]["edit"] = async function ( const src = decoder.decode(await gunzipAsync(arg.value)); if (arg.type === "adv") { - const { item_id, mode, comp_id, page_id, value } = arg; + const { item_id, mode, comp_id, page_id } = arg; let root = undefined as undefined | MRoot | MItem; let doc = undefined as undefined | Doc; @@ -45,22 +47,30 @@ export const code_edit: SAction["code"]["edit"] = async function ( } if (adv) { - const res = await transform(`render(${src})`, { - jsx: "transform", - format: "cjs", - loader: "tsx", - minify: true, - sourcemap: "inline", - }); + try { + const res = await transform(`render(${src})`, { + jsx: "transform", + format: "cjs", + loader: "tsx", + minify: true, + sourcemap: "inline", + }); - doc?.transact(() => { - if (adv) { - adv.set(mode, src); - if (mode === "js") { - adv.set("jsBuilt", res.code); + doc?.transact(() => { + if (adv) { + adv.set(mode, src); + if (mode === "js") { + adv.set("jsBuilt", res.code); + } } - } - }); + }); + } catch (e) { + g.log.error(e); + } + + if (mode === "js") { + return parseJs(adv.get("js")) || false; + } } } } @@ -75,28 +85,32 @@ export const code_edit: SAction["code"]["edit"] = async function ( const mprops = root.get("component")?.get("props"); const mprop = mprops?.get(prop_name); if (mprop) { - const res = await transform(`return ${src}`, { - jsx: "transform", - format: "cjs", - loader: "tsx", - }); - doc?.transact(() => { - if (prop_kind === "value") { - mprop.set("value", src); - mprop.set("valueBuilt", res.code.substring(6)); - } else if (prop_kind === "gen") { - mprop.set("gen", src); - mprop.set("genBuilt", res.code.substring(6)); - } else if (prop_kind === "visible") { - mprop.set("visible", src); - } else if (prop_kind === "option") { - const meta = mprop.get("meta"); - if (meta) { - meta.set("options", src); - meta.set("optionsBuilt", res.code.substring(6)); + try { + const res = await transform(`return ${src}`, { + jsx: "transform", + format: "cjs", + loader: "tsx", + }); + doc?.transact(() => { + if (prop_kind === "value") { + mprop.set("value", src); + mprop.set("valueBuilt", res.code.substring(6)); + } else if (prop_kind === "gen") { + mprop.set("gen", src); + mprop.set("genBuilt", res.code.substring(6)); + } else if (prop_kind === "visible") { + mprop.set("visible", src); + } else if (prop_kind === "option") { + const meta = mprop.get("meta"); + if (meta) { + meta.set("options", src); + meta.set("optionsBuilt", res.code.substring(6)); + } } - } - }); + }); + } catch (e) { + g.log.error(e); + } } } } diff --git a/app/srv/ws/sync/actions/comp_load.ts b/app/srv/ws/sync/actions/comp_load.ts index b34dfd4d..13ceaa2c 100644 --- a/app/srv/ws/sync/actions/comp_load.ts +++ b/app/srv/ws/sync/actions/comp_load.ts @@ -47,7 +47,7 @@ export const comp_load: SAction["comp"]["load"] = async function ( on: { visit(meta) { if (typeof meta.item.adv?.js === "string") { - meta.scope.def = parseJs(meta); + meta.scope.def = parseJs(meta.item.adv?.js); } }, }, diff --git a/app/srv/ws/sync/actions/page_load.ts b/app/srv/ws/sync/actions/page_load.ts index bdfce535..66323e3b 100644 --- a/app/srv/ws/sync/actions/page_load.ts +++ b/app/srv/ws/sync/actions/page_load.ts @@ -222,7 +222,7 @@ const scanMeta = async (doc: DPage, sync: SyncConnection) => { on: { visit(meta) { if (typeof meta.item.adv?.js === "string") { - meta.scope.def = parseJs(meta); + meta.scope.def = parseJs(meta.item.adv?.js); } }, }, @@ -247,7 +247,7 @@ const scanMeta = async (doc: DPage, sync: SyncConnection) => { visit(meta) { if (!meta.parent?.comp_id) { if (typeof meta.item.adv?.js === "string") { - meta.scope.def = parseJs(meta); + meta.scope.def = parseJs(meta.item.adv?.js); } } }, diff --git a/app/srv/ws/sync/editor/parser/parse-js.ts b/app/srv/ws/sync/editor/parser/parse-js.ts index d31e0a6d..51d3f7b0 100644 --- a/app/srv/ws/sync/editor/parser/parse-js.ts +++ b/app/srv/ws/sync/editor/parser/parse-js.ts @@ -1,9 +1,9 @@ import recast from "recast"; import babel from "recast/parsers/babel-ts"; -import { IMeta } from "../../../../../web/src/nova/ed/logic/ed-global"; -export const parseJs = (meta: IMeta) => { - const code = meta.item.adv?.js; +export type ParsedScope = Exclude, undefined>; + +export const parseJs = (code?: string) => { if (!code) return undefined; const local = { name: "", value: "", index: 0 }; const passprop: Record = {}; diff --git a/app/web/src/nova/ed/logic/ed-route.ts b/app/web/src/nova/ed/logic/ed-route.ts index 3160a6f5..49a73751 100644 --- a/app/web/src/nova/ed/logic/ed-route.ts +++ b/app/web/src/nova/ed/logic/ed-route.ts @@ -1,9 +1,9 @@ import { compress, decompress } from "wasm-gzip"; +import { loadCompSnapshot } from "./comp/load"; import { PG } from "./ed-global"; import { loadSite } from "./ed-site"; import { treeRebuild } from "./tree/build"; -import { loadCompSnapshot } from "./comp/load"; -import { produce } from "immer"; + export const edRoute = async (p: PG) => { if (p.status === "ready" || p.status === "init") { if (!p.site.domain && !p.site.name) { diff --git a/app/web/src/nova/ed/panel/popup/script/monaco.tsx b/app/web/src/nova/ed/panel/popup/script/monaco.tsx index 4d70195d..6b12ce49 100644 --- a/app/web/src/nova/ed/panel/popup/script/monaco.tsx +++ b/app/web/src/nova/ed/panel/popup/script/monaco.tsx @@ -11,6 +11,8 @@ import { getActiveMeta } from "../../../logic/active/get-meta"; import { EDGlobal, IMeta, active } from "../../../logic/ed-global"; import { edMonacoDefaultVal } from "./default-val"; import { declareScope } from "./scope"; +import { ParsedScope } from "../../../../../../../srv/ws/sync/editor/parser/parse-js"; +import { ISimpleMeta } from "../../../../vi/utils/types"; const scriptEdit = { timeout: null as any, @@ -188,7 +190,7 @@ export const EdScriptMonaco: FC<{}> = () => { local.value = val || ""; local.render(); clearTimeout(scriptEdit.timeout); - scriptEdit.timeout = setTimeout(() => { + scriptEdit.timeout = setTimeout(async () => { const meta = getActiveMeta(p); const type = p.ui.popup.script.mode; if (meta && meta.mitem) { @@ -199,6 +201,7 @@ export const EdScriptMonaco: FC<{}> = () => { arg.page_id = p.page.cur.id; } + let scope: boolean | ParsedScope = false; if (p.ui.popup.script.type === "prop-master") { p.sync.code.edit({ type: "prop", @@ -208,7 +211,7 @@ export const EdScriptMonaco: FC<{}> = () => { ...arg, }); } else { - p.sync.code.edit({ + scope = await p.sync.code.edit({ type: "adv", mode: type, item_id: active.item_id, @@ -216,6 +219,13 @@ export const EdScriptMonaco: FC<{}> = () => { ...arg, }); } + + if (typeof scope === "object") { + if (active.comp_id) { + } else { + p.page.smeta[active.item_id].scope = scope; + } + } } }, 1000); }} diff --git a/app/web/src/nova/ed/panel/popup/script/scope.tsx b/app/web/src/nova/ed/panel/popup/script/scope.tsx index e46590bf..c636397a 100644 --- a/app/web/src/nova/ed/panel/popup/script/scope.tsx +++ b/app/web/src/nova/ed/panel/popup/script/scope.tsx @@ -51,8 +51,10 @@ export const declareScope = async ( }, source: `\ export const {}; +const _local = ${def.local.value}; + declare global { - const ${def.local.name} = ${def.local.value}; + const ${def.local.name}: typeof _local & { render: () =>void }; }`, }); } else if (def.passprop) { diff --git a/app/web/src/nova/ed/panel/tree/node/item/name.tsx b/app/web/src/nova/ed/panel/tree/node/item/name.tsx index 75934ed3..1f1972a6 100644 --- a/app/web/src/nova/ed/panel/tree/node/item/name.tsx +++ b/app/web/src/nova/ed/panel/tree/node/item/name.tsx @@ -48,7 +48,7 @@ export const EdTreeName = ({ const comp = p.comp.list[item.component.id]; mitem = comp?.doc.getMap("map").get("root"); } - + if (mitem) { mitem.doc?.transact(() => { if (mitem) { @@ -98,7 +98,7 @@ export const EdTreeName = ({ ) : (
-
{node.id} - {item.originalId}
+ {/*
{node.id} - {item.originalId}
*/}
)}