wip component smeta

This commit is contained in:
Rizky 2023-12-19 05:31:10 +07:00
parent 4d186c8580
commit c11f01cdb7
10 changed files with 80 additions and 15 deletions

View File

@ -38,7 +38,11 @@ export const code_edit: SAction["code"]["edit"] = async function (
const mitem = findId(root, item_id); const mitem = findId(root, item_id);
if (mitem) { if (mitem) {
const adv = mitem.get("adv"); let adv = mitem.get("adv");
if (!adv) {
mitem.set("adv", new Y.Map() as any);
adv = mitem.get("adv");
}
if (adv) { if (adv) {
const res = await transform(`render(${src})`, { const res = await transform(`render(${src})`, {
@ -48,10 +52,13 @@ export const code_edit: SAction["code"]["edit"] = async function (
minify: true, minify: true,
sourcemap: "inline", sourcemap: "inline",
}); });
doc?.transact(() => { doc?.transact(() => {
adv.set(mode, src); if (adv) {
if (mode === "js") { adv.set(mode, src);
adv.set("jsBuilt", res.code); if (mode === "js") {
adv.set("jsBuilt", res.code);
}
} }
}); });
} }

View File

@ -1,7 +1,10 @@
import { EComp } from "../../../../web/src/nova/ed/logic/ed-global"; import { EComp } from "../../../../web/src/nova/ed/logic/ed-global";
import { genMeta } from "../../../../web/src/nova/vi/meta/meta";
import { IItem } from "../../../../web/src/utils/types/item";
import { DComp } from "../../../../web/src/utils/types/root"; import { DComp } from "../../../../web/src/utils/types/root";
import { SAction } from "../actions"; import { SAction } from "../actions";
import { loadComponent, userSyncComponent } from "../editor/load-component"; import { loadComponent, userSyncComponent } from "../editor/load-component";
import { parseJs } from "../editor/parser/parse-js";
import { docs } from "../entity/docs"; import { docs } from "../entity/docs";
import { snapshot } from "../entity/snapshot"; import { snapshot } from "../entity/snapshot";
import { gzipAsync } from "../entity/zlib"; import { gzipAsync } from "../entity/zlib";
@ -32,8 +35,30 @@ export const comp_load: SAction["comp"]["load"] = async function (
const snap = snapshot.get("comp", id); const snap = snapshot.get("comp", id);
if (snap) { if (snap) {
const meta = {};
const item = docs.comp[id].doc
.getMap("map")
.get("root")
?.toJSON() as IItem;
genMeta(
{
comps: {},
meta,
on: {
visit(meta) {
if (typeof meta.item.adv?.js === "string") {
meta.scope.def = parseJs(meta);
}
},
},
},
{ item }
);
result[id] = { result[id] = {
id, id,
meta,
snapshot: await gzipAsync(snap.bin), snapshot: await gzipAsync(snap.bin),
}; };
} }

View File

@ -229,8 +229,10 @@ const scanMeta = async (doc: DPage, sync: SyncConnection) => {
meta, meta,
on: { on: {
visit(meta) { visit(meta) {
if (typeof meta.item.adv?.js === "string") { if (!meta.parent?.comp_id) {
meta.scope.def = parseJs(meta); if (typeof meta.item.adv?.js === "string") {
meta.scope.def = parseJs(meta);
}
} }
}, },
}, },
@ -242,7 +244,23 @@ const scanMeta = async (doc: DPage, sync: SyncConnection) => {
const comps: EPage["comps"] = {}; const comps: EPage["comps"] = {};
for (const [id, snap] of Object.entries(msnap)) { for (const [id, snap] of Object.entries(msnap)) {
comps[id] = { id, snapshot: await gzipAsync(snap.bin) }; const meta = {};
genMeta(
{
comps: {},
meta,
on: {
visit(meta) {
if (typeof meta.item.adv?.js === "string") {
meta.scope.def = parseJs(meta);
}
},
},
},
{ item: mcomps[id].comp }
);
comps[id] = { id, meta, snapshot: await gzipAsync(snap.bin) };
} }
return { meta: simplifyMeta(meta), comps, entry }; return { meta: simplifyMeta(meta), comps, entry };

View File

@ -56,6 +56,7 @@ const EmptyPage = {
const EmptyComp = { const EmptyComp = {
id: "", id: "",
meta: {} as Record<string, ISimpleMeta>,
snapshot: null as null | Uint8Array, snapshot: null as null | Uint8Array,
}; };

View File

@ -25,7 +25,7 @@ export const mainPerItemVisit = (
meta: MPIVParam[0], meta: MPIVParam[0],
parts: MPIVParam[1] parts: MPIVParam[1]
) => { ) => {
if ((meta.item as IContent).type === "text" && !meta.item.adv?.jsBuilt) { if ((meta.item as IContent).type === "text" && !meta.item.adv?.js) {
parts.props.spellCheck = false; parts.props.spellCheck = false;
parts.props.contentEditable = true; parts.props.contentEditable = true;
if (meta.parent?.comp_id) { if (meta.parent?.comp_id) {

View File

@ -25,7 +25,6 @@ export const EdPopScript = () => {
if (script.type === "item") { if (script.type === "item") {
delete p.script.init_local_effect[active.item_id]; delete p.script.init_local_effect[active.item_id];
treeRebuild(p, { note: "script-close" });
} }
p.render(); p.render();

View File

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

View File

@ -25,7 +25,7 @@ export const viParts = (meta: IMeta, arg?: ViParts) => {
}; };
let shouldRenderChild = true; let shouldRenderChild = true;
if (content.type === "text" && !item.adv?.jsBuilt) { if (content.type === "text" && !item.adv?.js) {
props.dangerouslySetInnerHTML = { __html: item.html || "" }; props.dangerouslySetInnerHTML = { __html: item.html || "" };
shouldRenderChild = false; shouldRenderChild = false;
} }

View File

@ -34,7 +34,7 @@ export const viEvalScript = (
if (!meta.script) { if (!meta.script) {
meta.script = { meta.script = {
result: null, result: null,
Local: createViLocal(meta, scope, vi.script?.init_local_effect), Local: createViLocal(vi.meta, meta, scope, vi.script?.init_local_effect),
PassProp: createViPassProp(vi, meta, scope), PassProp: createViPassProp(vi, meta, scope),
}; };
} }

View File

@ -1,8 +1,9 @@
import { ReactNode, useEffect, useRef, useState } from "react"; import { ReactNode, useEffect, useRef } from "react";
import { IMeta } from "../../../ed/logic/ed-global"; import { IMeta } from "../../../ed/logic/ed-global";
import { updatePropScope } from "./eval-prop"; import { updatePropScope } from "./eval-prop";
export const createViLocal = ( export const createViLocal = (
metas: Record<string, IMeta>,
meta: IMeta, meta: IMeta,
scope: any, scope: any,
init_local_effect: any init_local_effect: any
@ -32,10 +33,24 @@ export const createViLocal = (
} }
useEffect(() => { useEffect(() => {
let should_run = !init_local_effect[meta.item.id]; let id = meta.item.id;
if (meta.parent?.instance_id) {
const parent_meta = metas[meta.parent?.instance_id];
if (parent_meta && parent_meta.instances) {
for (const [k, v] of Object.entries(
parent_meta.instances[meta.parent.instance_id]
)) {
if (v === meta.item.id) {
id = k;
break;
}
}
}
}
let should_run = !init_local_effect[id];
if (should_run) { if (should_run) {
if (typeof init_local_effect === "object") { if (typeof init_local_effect === "object") {
init_local_effect[meta.item.id] = true; init_local_effect[id] = true;
} }
const fn = async () => { const fn = async () => {
if (arg.effect) { if (arg.effect) {