wip fix local

This commit is contained in:
Rizky 2023-12-19 16:05:51 +07:00
parent b0ef6bb6f5
commit 59ab04b6be
9 changed files with 78 additions and 53 deletions

View File

@ -3,12 +3,11 @@ import {
EComp, EComp,
EPage, EPage,
ESite, ESite,
IScopeComp,
PropFieldKind, PropFieldKind,
} from "../../../web/src/nova/ed/logic/ed-global"; } from "../../../web/src/nova/ed/logic/ed-global";
import { IItem } from "../../../web/src/utils/types/item"; import { IItem } from "../../../web/src/utils/types/item";
import { site_group } from "./actions/site_group"; import { site_group } from "./actions/site_group";
import { parseJs } from "./editor/parser/parse-js"; import { ParsedScope, parseJs } from "./editor/parser/parse-js";
/* /*
WARNING: WARNING:
@ -113,7 +112,7 @@ export const SyncActions = {
prop_kind: PropFieldKind; prop_kind: PropFieldKind;
value: Uint8Array; value: Uint8Array;
} }
) => ({}) as boolean, ) => ({}) as boolean | ParsedScope,
parse: async (code: string | Record<string, string>) => parse: async (code: string | Record<string, string>) =>
({}) as Record<string, ReturnType<typeof parseJs>>, ({}) as Record<string, ReturnType<typeof parseJs>>,
}, },

View File

@ -1,3 +1,5 @@
import { transform } from "esbuild";
import { g } from "utils/global";
import { Doc } from "yjs"; import { Doc } from "yjs";
import { MContent } from "../../../../web/src/utils/types/general"; import { MContent } from "../../../../web/src/utils/types/general";
import { MItem } from "../../../../web/src/utils/types/item"; import { MItem } from "../../../../web/src/utils/types/item";
@ -6,7 +8,7 @@ import { SAction } from "../actions";
import { docs } from "../entity/docs"; import { docs } from "../entity/docs";
import { gunzipAsync } from "../entity/zlib"; import { gunzipAsync } from "../entity/zlib";
import { SyncConnection } from "../type"; import { SyncConnection } from "../type";
import { transform } from "esbuild"; import { parseJs } from "../editor/parser/parse-js";
const decoder = new TextDecoder(); const decoder = new TextDecoder();
export const code_edit: SAction["code"]["edit"] = async function ( 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)); const src = decoder.decode(await gunzipAsync(arg.value));
if (arg.type === "adv") { 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 root = undefined as undefined | MRoot | MItem;
let doc = undefined as undefined | Doc; let doc = undefined as undefined | Doc;
@ -45,22 +47,30 @@ export const code_edit: SAction["code"]["edit"] = async function (
} }
if (adv) { if (adv) {
const res = await transform(`render(${src})`, { try {
jsx: "transform", const res = await transform(`render(${src})`, {
format: "cjs", jsx: "transform",
loader: "tsx", format: "cjs",
minify: true, loader: "tsx",
sourcemap: "inline", minify: true,
}); sourcemap: "inline",
});
doc?.transact(() => { doc?.transact(() => {
if (adv) { if (adv) {
adv.set(mode, src); adv.set(mode, src);
if (mode === "js") { if (mode === "js") {
adv.set("jsBuilt", res.code); 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 mprops = root.get("component")?.get("props");
const mprop = mprops?.get(prop_name); const mprop = mprops?.get(prop_name);
if (mprop) { if (mprop) {
const res = await transform(`return ${src}`, { try {
jsx: "transform", const res = await transform(`return ${src}`, {
format: "cjs", jsx: "transform",
loader: "tsx", format: "cjs",
}); loader: "tsx",
doc?.transact(() => { });
if (prop_kind === "value") { doc?.transact(() => {
mprop.set("value", src); if (prop_kind === "value") {
mprop.set("valueBuilt", res.code.substring(6)); mprop.set("value", src);
} else if (prop_kind === "gen") { mprop.set("valueBuilt", res.code.substring(6));
mprop.set("gen", src); } else if (prop_kind === "gen") {
mprop.set("genBuilt", res.code.substring(6)); mprop.set("gen", src);
} else if (prop_kind === "visible") { mprop.set("genBuilt", res.code.substring(6));
mprop.set("visible", src); } else if (prop_kind === "visible") {
} else if (prop_kind === "option") { mprop.set("visible", src);
const meta = mprop.get("meta"); } else if (prop_kind === "option") {
if (meta) { const meta = mprop.get("meta");
meta.set("options", src); if (meta) {
meta.set("optionsBuilt", res.code.substring(6)); meta.set("options", src);
meta.set("optionsBuilt", res.code.substring(6));
}
} }
} });
}); } catch (e) {
g.log.error(e);
}
} }
} }
} }

View File

@ -47,7 +47,7 @@ export const comp_load: SAction["comp"]["load"] = async function (
on: { on: {
visit(meta) { visit(meta) {
if (typeof meta.item.adv?.js === "string") { if (typeof meta.item.adv?.js === "string") {
meta.scope.def = parseJs(meta); meta.scope.def = parseJs(meta.item.adv?.js);
} }
}, },
}, },

View File

@ -222,7 +222,7 @@ const scanMeta = async (doc: DPage, sync: SyncConnection) => {
on: { on: {
visit(meta) { visit(meta) {
if (typeof meta.item.adv?.js === "string") { 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) { visit(meta) {
if (!meta.parent?.comp_id) { if (!meta.parent?.comp_id) {
if (typeof meta.item.adv?.js === "string") { if (typeof meta.item.adv?.js === "string") {
meta.scope.def = parseJs(meta); meta.scope.def = parseJs(meta.item.adv?.js);
} }
} }
}, },

View File

@ -1,9 +1,9 @@
import recast from "recast"; import recast from "recast";
import babel from "recast/parsers/babel-ts"; import babel from "recast/parsers/babel-ts";
import { IMeta } from "../../../../../web/src/nova/ed/logic/ed-global";
export const parseJs = (meta: IMeta) => { export type ParsedScope = Exclude<ReturnType<typeof parseJs>, undefined>;
const code = meta.item.adv?.js;
export const parseJs = (code?: string) => {
if (!code) return undefined; if (!code) return undefined;
const local = { name: "", value: "", index: 0 }; const local = { name: "", value: "", index: 0 };
const passprop: Record<string, { value: string; index: number }> = {}; const passprop: Record<string, { value: string; index: number }> = {};

View File

@ -1,9 +1,9 @@
import { compress, decompress } from "wasm-gzip"; import { compress, decompress } from "wasm-gzip";
import { loadCompSnapshot } from "./comp/load";
import { PG } from "./ed-global"; import { PG } from "./ed-global";
import { loadSite } from "./ed-site"; import { loadSite } from "./ed-site";
import { treeRebuild } from "./tree/build"; import { treeRebuild } from "./tree/build";
import { loadCompSnapshot } from "./comp/load";
import { produce } from "immer";
export const edRoute = async (p: PG) => { export const edRoute = async (p: PG) => {
if (p.status === "ready" || p.status === "init") { if (p.status === "ready" || p.status === "init") {
if (!p.site.domain && !p.site.name) { if (!p.site.domain && !p.site.name) {

View File

@ -11,6 +11,8 @@ import { getActiveMeta } from "../../../logic/active/get-meta";
import { EDGlobal, IMeta, active } from "../../../logic/ed-global"; import { EDGlobal, IMeta, active } from "../../../logic/ed-global";
import { edMonacoDefaultVal } from "./default-val"; import { edMonacoDefaultVal } from "./default-val";
import { declareScope } from "./scope"; import { declareScope } from "./scope";
import { ParsedScope } from "../../../../../../../srv/ws/sync/editor/parser/parse-js";
import { ISimpleMeta } from "../../../../vi/utils/types";
const scriptEdit = { const scriptEdit = {
timeout: null as any, timeout: null as any,
@ -188,7 +190,7 @@ export const EdScriptMonaco: FC<{}> = () => {
local.value = val || ""; local.value = val || "";
local.render(); local.render();
clearTimeout(scriptEdit.timeout); clearTimeout(scriptEdit.timeout);
scriptEdit.timeout = setTimeout(() => { scriptEdit.timeout = setTimeout(async () => {
const meta = getActiveMeta(p); const meta = getActiveMeta(p);
const type = p.ui.popup.script.mode; const type = p.ui.popup.script.mode;
if (meta && meta.mitem) { if (meta && meta.mitem) {
@ -199,6 +201,7 @@ export const EdScriptMonaco: FC<{}> = () => {
arg.page_id = p.page.cur.id; arg.page_id = p.page.cur.id;
} }
let scope: boolean | ParsedScope = false;
if (p.ui.popup.script.type === "prop-master") { if (p.ui.popup.script.type === "prop-master") {
p.sync.code.edit({ p.sync.code.edit({
type: "prop", type: "prop",
@ -208,7 +211,7 @@ export const EdScriptMonaco: FC<{}> = () => {
...arg, ...arg,
}); });
} else { } else {
p.sync.code.edit({ scope = await p.sync.code.edit({
type: "adv", type: "adv",
mode: type, mode: type,
item_id: active.item_id, item_id: active.item_id,
@ -216,6 +219,13 @@ export const EdScriptMonaco: FC<{}> = () => {
...arg, ...arg,
}); });
} }
if (typeof scope === "object") {
if (active.comp_id) {
} else {
p.page.smeta[active.item_id].scope = scope;
}
}
} }
}, 1000); }, 1000);
}} }}

View File

@ -51,8 +51,10 @@ export const declareScope = async (
}, },
source: `\ source: `\
export const {}; export const {};
const _local = ${def.local.value};
declare global { declare global {
const ${def.local.name} = ${def.local.value}; const ${def.local.name}: typeof _local & { render: () =>void };
}`, }`,
}); });
} else if (def.passprop) { } else if (def.passprop) {

View File

@ -48,7 +48,7 @@ export const EdTreeName = ({
const comp = p.comp.list[item.component.id]; const comp = p.comp.list[item.component.id];
mitem = comp?.doc.getMap("map").get("root"); mitem = comp?.doc.getMap("map").get("root");
} }
if (mitem) { if (mitem) {
mitem.doc?.transact(() => { mitem.doc?.transact(() => {
if (mitem) { if (mitem) {
@ -98,7 +98,7 @@ export const EdTreeName = ({
) : ( ) : (
<div className="flex flex-col"> <div className="flex flex-col">
<Name name={node.text} is_jsx_prop={is_jsx_prop} /> <Name name={node.text} is_jsx_prop={is_jsx_prop} />
<div className={"text-[9px] text-gray-500 -mt-1"}>{node.id} - {item.originalId}</div> {/* <div className={"text-[9px] text-gray-500 -mt-1"}>{node.id} - {item.originalId}</div> */}
</div> </div>
)} )}
</div> </div>