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,
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<string, string>) =>
({}) 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 { 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,6 +47,7 @@ export const code_edit: SAction["code"]["edit"] = async function (
}
if (adv) {
try {
const res = await transform(`render(${src})`, {
jsx: "transform",
format: "cjs",
@ -61,6 +64,13 @@ export const code_edit: SAction["code"]["edit"] = async function (
}
}
});
} catch (e) {
g.log.error(e);
}
if (mode === "js") {
return parseJs(adv.get("js")) || false;
}
}
}
}
@ -75,6 +85,7 @@ export const code_edit: SAction["code"]["edit"] = async function (
const mprops = root.get("component")?.get("props");
const mprop = mprops?.get(prop_name);
if (mprop) {
try {
const res = await transform(`return ${src}`, {
jsx: "transform",
format: "cjs",
@ -97,6 +108,9 @@ export const code_edit: SAction["code"]["edit"] = async function (
}
}
});
} catch (e) {
g.log.error(e);
}
}
}
}

View File

@ -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);
}
},
},

View File

@ -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);
}
}
},

View File

@ -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<ReturnType<typeof parseJs>, undefined>;
export const parseJs = (code?: string) => {
if (!code) return undefined;
const local = { name: "", value: "", index: 0 };
const passprop: Record<string, { value: string; index: number }> = {};

View File

@ -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) {

View File

@ -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);
}}

View File

@ -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) {

View File

@ -98,7 +98,7 @@ export const EdTreeName = ({
) : (
<div className="flex flex-col">
<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>