This commit is contained in:
Rizky 2023-12-13 09:48:04 +07:00
parent a9df13dd93
commit 9bbc3a3d8f
14 changed files with 128 additions and 44 deletions

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.item.adv.js); meta.scope.def = parseJs(meta);
} }
}, },
}, },

View File

@ -1,13 +1,16 @@
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 = (code: string) => { export const parseJs = (meta: IMeta) => {
const code = meta.item.adv?.js;
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 }> = {};
const result = {} as { const result = {} as {
local: typeof local | undefined; local?: typeof local | undefined;
passprop: typeof passprop | undefined; passprop?: typeof passprop | undefined;
props: Record<string, { name: string; value: string }>; props?: Record<string, { name: string; value: string }>;
}; };
try { try {

View File

@ -2,6 +2,7 @@ import init from "wasm-gzip";
import { PG } from "./ed-global"; import { PG } from "./ed-global";
import { jscript } from "../../../utils/script/jscript"; import { jscript } from "../../../utils/script/jscript";
import { viLoadLegacy } from "../../vi/load/load-legacy"; import { viLoadLegacy } from "../../vi/load/load-legacy";
import { treeRebuild } from "./tree/build";
export const edInit = async (p: PG) => { export const edInit = async (p: PG) => {
p.status = "ready"; p.status = "ready";
@ -34,5 +35,7 @@ export const edInit = async (p: PG) => {
}); });
p.script.loaded = true; p.script.loaded = true;
treeRebuild(p);
p.render(); p.render();
}; };

View File

@ -56,6 +56,7 @@ export const reloadPage = async (p: PG, page_id: string, note: string) => {
} }
p.page.entry = remotePage.entry; p.page.entry = remotePage.entry;
p.page.smeta = remotePage.meta; p.page.smeta = remotePage.meta;
p.page.cur = remotePage; p.page.cur = remotePage;

View File

@ -1,4 +1,10 @@
import { IItem, MItem } from "../../../../utils/types/item"; import { IItem, MItem } from "../../../../utils/types/item";
import { viEvalScript } from "../../../vi/render/script";
import { viEvalProps } from "../../../vi/render/script/eval-prop";
import {
getScopeMeta,
getScopeValue,
} from "../../../vi/render/script/scope-meta";
import { genMeta } from "../../../view/logic/meta/meta"; import { genMeta } from "../../../view/logic/meta/meta";
import { IMeta, PG, active } from "../ed-global"; import { IMeta, PG, active } from "../ed-global";
import { pushTreeNode } from "./build/push-tree"; import { pushTreeNode } from "./build/push-tree";
@ -27,6 +33,7 @@ export const treeRebuild = async (p: PG, arg?: { note?: string }) => {
const meta: Record<string, IMeta> = {}; const meta: Record<string, IMeta> = {};
p.page.tree = []; p.page.tree = [];
for (const mitem of mitems) { for (const mitem of mitems) {
const item = mitem.toJSON() as IItem; const item = mitem.toJSON() as IItem;
if (item) { if (item) {
@ -40,10 +47,6 @@ export const treeRebuild = async (p: PG, arg?: { note?: string }) => {
if (!is_layout) { if (!is_layout) {
pushTreeNode(p, m, meta); pushTreeNode(p, m, meta);
} }
// if (m.item.adv?.jsBuilt) {
// viEvalScript({ meta: p.page.meta }, m);
// }
}, },
}, },
}, },

View File

@ -93,7 +93,7 @@ export const EdPropPopoverForm: FC<{ mprop: FMCompDef; name: string }> = ({
onBlur={() => { onBlur={() => {
if (local.name !== name) { if (local.name !== name) {
const keys = Object.keys(mprop.parent?.toJSON()); const keys = Object.keys(mprop.parent?.toJSON());
if ([...keys, ...keywords].includes(local.name)) { if ([...keys, ...invalidKeyword].includes(local.name)) {
alert(`Cannot use "${local.name}" as name`); alert(`Cannot use "${local.name}" as name`);
local.name = name; local.name = name;
local.render(); local.render();
@ -226,7 +226,7 @@ export const EdPropPopoverForm: FC<{ mprop: FMCompDef; name: string }> = ({
); );
}; };
const keywords = [ export const invalidKeyword = [
"await", "await",
"break", "break",
"case", "case",
@ -273,4 +273,5 @@ const keywords = [
"while", "while",
"with", "with",
"yield", "yield",
"key",
]; ];

View File

@ -35,15 +35,18 @@ export const viLoadLegacy = async (vi: {
} }
} }
let api_url = vi.site.api_url;
if (!api_url) api_url = ((site.config as any) || {}).api_url || "";
await initApi(site.config); await initApi(site.config);
const path = `/npm/site/${vi.site.id}/site.js`; const path = `/npm/site/${vi.site.id}/site.js`;
await importModule(path); await importModule(path);
if (!vi.site.db.get()) { if (!vi.site.db.get()) {
vi.site.db.set(createDB(vi.site.api_url)); vi.site.db.set(createDB(api_url));
} }
if (!vi.site.api.get()) { if (!vi.site.api.get()) {
vi.site.api.set(createAPI(vi.site.api_url)); vi.site.api.set(createAPI(api_url));
} }
const w = window as any; const w = window as any;

View File

@ -1,9 +0,0 @@
import { IMeta } from "../../ed/logic/ed-global";
import { VG } from "./global";
export const viEvalProps = async (ctx: VG, meta: IMeta) => {
if (meta.item.component?.props) {
for (const [k, v] of Object.entries(meta.item.component.props)) {
}
}
};

View File

@ -7,18 +7,30 @@ import { viParts } from "./parts";
import { ViRender } from "./render"; import { ViRender } from "./render";
import { createViLocal } from "./script/local"; import { createViLocal } from "./script/local";
import { createViPassProp } from "./script/passprop"; import { createViPassProp } from "./script/passprop";
import { getScope } from "./script/scope-meta"; import { getScopeMeta, getScopeValue } from "./script/scope-meta";
import { viEvalProps } from "./script/eval-prop";
export const ViScript: FC<{ meta: IMeta }> = ({ meta }) => { export const ViScript: FC<{ meta: IMeta }> = ({ meta }) => {
const vi = useGlobal(ViGlobal, "VI"); const vi = useGlobal(ViGlobal, "VI");
viEvalScript(vi, meta); const scope_meta = getScopeMeta(vi, meta);
const scope = getScopeValue(scope_meta);
if (meta.item.component?.id) {
viEvalProps(vi, meta, scope);
}
viEvalScript(vi, meta, scope);
if (meta.script) return meta.script.result; if (meta.script) return meta.script.result;
return null; return null;
}; };
export const viEvalScript = (vi: { meta: VG["meta"] }, meta: IMeta) => { export const viEvalScript = (
vi: { meta: VG["meta"] },
meta: IMeta,
scope: any
) => {
const childs = meta.item.childs; const childs = meta.item.childs;
const parts = viParts(meta); const parts = viParts(meta);
@ -31,8 +43,6 @@ export const viEvalScript = (vi: { meta: VG["meta"] }, meta: IMeta) => {
}); });
} }
const scope = getScope(vi, meta);
if (!meta.script) { if (!meta.script) {
meta.script = { meta.script = {
result: null, result: null,
@ -47,6 +57,7 @@ export const viEvalScript = (vi: { meta: VG["meta"] }, meta: IMeta) => {
useEffect, useEffect,
children, children,
props: parts.props, props: parts.props,
isEditor: true,
Local: script.Local, Local: script.Local,
PassProp: script?.PassProp, PassProp: script?.PassProp,
ErrorBox: ErrorBox, ErrorBox: ErrorBox,
@ -60,7 +71,7 @@ export const viEvalScript = (vi: { meta: VG["meta"] }, meta: IMeta) => {
const fn = new Function( const fn = new Function(
...Object.keys(arg), ...Object.keys(arg),
`// [${meta.item.type}] ${meta.item.name}: ${meta.item.id} `// ${meta.item.name}: ${meta.item.id}
${meta.item.adv?.jsBuilt || ""} ${meta.item.adv?.jsBuilt || ""}
` `
); );

View File

@ -0,0 +1,63 @@
import { IMeta } from "../../../ed/logic/ed-global";
import { invalidKeyword } from "../../../ed/panel/side/prop-master/prop-form";
import { VG } from "../global";
import { getScopeMeta, getScopeValue } from "./scope-meta";
export const viEvalProps = (
vi: { meta: VG["meta"] },
meta: IMeta,
scope: any
) => {
if (meta.item.component?.id) {
if (!meta.scope.def) {
meta.scope.def = {};
}
if (!meta.scope.val) {
meta.scope.val = {};
}
const exports = (window as any).exports;
const arg = {
...exports,
...scope,
isEditor: true,
};
meta.scope.def.props = {};
let fails = new Set<string>();
for (const [name, prop] of Object.entries(meta.item.component.props)) {
try {
const fn = new Function(
...Object.keys(arg),
`// [${meta.item.name}] ${name}: ${meta.item.id}
return ${prop.valueBuilt || ""}
`
);
meta.scope.def.props[name] = prop.value;
meta.scope.val[name] = fn(...Object.values(arg));
scope[name] = meta.scope.val[name];
arg[name] = scope[name];
} catch (e) {
fails.add(name);
}
}
if (fails.size > 0) {
for (const [name, prop] of Object.entries(meta.item.component.props)) {
if (fails.has(name) && !invalidKeyword.includes(name)) {
const fn = new Function(
...Object.keys(arg),
`// [${meta.item.name}] ${name}: ${meta.item.id}
return ${prop.valueBuilt || ""}
`
);
meta.scope.def.props[name] = prop.value;
meta.scope.val[name] = fn(...Object.values(arg));
scope[name] = meta.scope.val[name];
arg[name] = scope[name];
}
}
}
}
};

View File

@ -1,7 +1,11 @@
import { IMeta } from "../../../ed/logic/ed-global"; import { IMeta } from "../../../ed/logic/ed-global";
import { VG } from "../global"; import { VG } from "../global";
const getScopeMeta = (vi: { meta: VG["meta"] }, meta: IMeta) => { export const getScopeMeta = (
vi: { meta: VG["meta"] },
meta: IMeta,
debug?: boolean
) => {
let cur = meta; let cur = meta;
const scopes_meta: IMeta[] = []; const scopes_meta: IMeta[] = [];
@ -39,7 +43,7 @@ const getScopeMeta = (vi: { meta: VG["meta"] }, meta: IMeta) => {
return scope_meta; return scope_meta;
}; };
const getScopeValue = (scope_meta: ReturnType<typeof getScopeMeta>) => { export const getScopeValue = (scope_meta: ReturnType<typeof getScopeMeta>) => {
const scope: any = {}; const scope: any = {};
for (const [varname, s] of Object.entries(scope_meta)) { for (const [varname, s] of Object.entries(scope_meta)) {
@ -51,8 +55,6 @@ const getScopeValue = (scope_meta: ReturnType<typeof getScopeMeta>) => {
return scope; return scope;
}; };
export const getScope = (vi: { meta: VG["meta"] }, meta: IMeta) => { // export const getScope = (vi: { meta: VG["meta"] }, meta: IMeta) => {
const scope_meta = getScopeMeta(vi, meta);
return getScopeValue(scope_meta); // };
};

View File

@ -47,12 +47,17 @@ export const genComp = (p: GenMetaP, arg: GenMetaArg) => {
item: simplifyItemChild(item), item: simplifyItemChild(item),
parent: { parent: {
id: arg.parent.item.id, id: arg.parent.item.id,
comp_id: arg.parent?.comp?.id, comp_id: arg.parent?.comp?.component?.id,
instance_id: arg.parent?.instance_id,
}, },
instances, instances,
scope: {}, scope: {},
}; };
if (p.smeta?.[item.id]) {
meta.scope.def = p.smeta[item.id].scope;
}
if (item.id) { if (item.id) {
if (p.set_meta !== false) { if (p.set_meta !== false) {
p.meta[item.id] = meta; p.meta[item.id] = meta;
@ -102,7 +107,7 @@ export const genComp = (p: GenMetaP, arg: GenMetaArg) => {
} }
for (const child of Object.values(item.childs)) { for (const child of Object.values(item.childs)) {
if (child.name.startsWith('jsx:')) continue; if (child.name.startsWith("jsx:")) continue;
genMeta(p, { genMeta(p, {
item: child, item: child,
is_root: false, is_root: false,

View File

@ -17,8 +17,12 @@ export const instantiate = (arg: {
newitem.id = item.id; newitem.id = item.id;
} }
if (item.component) { if (newitem.component && item.component) {
newitem.component = item.component; for (const k of Object.keys(newitem.component.props)) {
if (item.component.props[k]) {
newitem.component.props[k] = item.component.props[k];
}
}
} }
for (const key of Object.keys(item)) { for (const key of Object.keys(item)) {

View File

@ -4,11 +4,6 @@ import { simplifyItemChild } from "./simplify";
import { GenMetaArg, GenMetaP, IMeta } from "./types"; import { GenMetaArg, GenMetaP, IMeta } from "./types";
export const genMeta = (p: GenMetaP, arg: GenMetaArg) => { export const genMeta = (p: GenMetaP, arg: GenMetaArg) => {
// let wrapper = (fn: any) => {
// fn();
// };
// wrapper(() => {
const item = arg.item as IItem; const item = arg.item as IItem;
if (item.type === "item" && item.component?.id) { if (item.type === "item" && item.component?.id) {
@ -64,5 +59,4 @@ export const genMeta = (p: GenMetaP, arg: GenMetaArg) => {
}); });
} }
} }
// });
}; };