wip fix
This commit is contained in:
parent
5cf979c882
commit
cb1be7d1b7
|
|
@ -1,6 +1,4 @@
|
|||
import { IItem, MItem } from "../../../../utils/types/item";
|
||||
import { viEvalProps } from "../../../vi/render/comp";
|
||||
import { viEvalScript } from "../../../vi/render/script";
|
||||
import { genMeta } from "../../../view/logic/meta/meta";
|
||||
import { PG, active } from "../ed-global";
|
||||
import { pushTreeNode } from "./build/push-tree";
|
||||
|
|
@ -44,18 +42,18 @@ export const treeRebuild = async (p: PG, arg?: { note?: string }) => {
|
|||
if (!is_layout) {
|
||||
pushTreeNode(p, meta);
|
||||
|
||||
if (meta.item.component?.props) {
|
||||
viEvalProps({ meta: p.page.meta, tick: 0 }, meta);
|
||||
}
|
||||
// if (meta.item.component?.props) {
|
||||
// viEvalProps({ meta: p.page.meta, tick: 0 }, meta);
|
||||
// }
|
||||
|
||||
if (meta.item.adv?.jsBuilt) {
|
||||
viEvalScript({ meta: p.page.meta, tick: 0 }, meta);
|
||||
}
|
||||
// if (meta.item.adv?.jsBuilt) {
|
||||
// viEvalScript({ meta: p.page.meta, tick: 0 }, meta);
|
||||
// }
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
{ item, mitem }
|
||||
{ item }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { EDGlobal } from "../../logic/ed-global";
|
|||
|
||||
export const EdMain = () => {
|
||||
const p = useGlobal(EDGlobal, "EDITOR");
|
||||
return <Vi ctx={{ meta: p.page.meta }} entry={p.page.entry} />;
|
||||
return <Vi ctx={{ meta: p.page.meta, tick: 0 }} entry={p.page.entry} />;
|
||||
|
||||
// const local = useLocal({
|
||||
// el: null as ReactNode,
|
||||
|
|
|
|||
|
|
@ -5,9 +5,7 @@ import { ErrorBox } from "./utils/error-box";
|
|||
export const Vi: typeof ViRoot = (props) => {
|
||||
return (
|
||||
<ErrorBox>
|
||||
<Suspense>
|
||||
<ViRoot {...props} />
|
||||
</Suspense>
|
||||
<Suspense>{/* <ViRoot {...props} /> */}</Suspense>
|
||||
</ErrorBox>
|
||||
);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
import { MItem } from "../../../../utils/types/item";
|
||||
import { evalPropVis } from "./comp/eval-prop-vis";
|
||||
import { instantiate } from "./comp/instantiate";
|
||||
import { walkProp } from "./comp/walk-prop";
|
||||
import { genMeta } from "./meta";
|
||||
|
|
@ -13,7 +11,6 @@ export const genComp = (
|
|||
r: ReturnType<typeof applyRefIds>
|
||||
) => {
|
||||
const { item } = arg;
|
||||
const mitem = arg.mitem as MItem | undefined;
|
||||
if (item.type === "item" && item.component?.id && arg.parent?.item.id) {
|
||||
let pcomp = p.comps[item.component.id];
|
||||
if (p.on?.visit_component) {
|
||||
|
|
@ -26,18 +23,11 @@ export const genComp = (
|
|||
|
||||
if (pcomp) {
|
||||
const ref_ids = r?.ref_ids || item.component?.ref_ids || {};
|
||||
let mref_ids = r?.mref_ids || mitem?.get("component")?.get("ref_ids");
|
||||
|
||||
if (!mref_ids && mitem) {
|
||||
mitem.get("component")?.set("ref_ids", new Y.Map() as any);
|
||||
mref_ids = mitem.get("component")?.get("ref_ids");
|
||||
}
|
||||
|
||||
instantiate(item, pcomp.comp, ref_ids, mref_ids);
|
||||
instantiate(item, pcomp.comp, ref_ids);
|
||||
|
||||
const meta: IMeta = {
|
||||
item: simplifyItemChild(item),
|
||||
mitem,
|
||||
parent: {
|
||||
id: arg.parent.item.id,
|
||||
instance_id: arg.parent?.instance?.id,
|
||||
|
|
@ -50,26 +40,16 @@ export const genComp = (
|
|||
},
|
||||
};
|
||||
|
||||
const props = {} as Record<string, { value: any; visible?: string }>;
|
||||
|
||||
walkProp({
|
||||
item,
|
||||
mitem: mitem,
|
||||
pcomp,
|
||||
each(name, prop, mprop) {
|
||||
each(name, prop) {
|
||||
if (meta.scope.def?.props) {
|
||||
meta.scope.def.props[name] = {
|
||||
value: prop.valueBuilt,
|
||||
type: {
|
||||
"": "text",
|
||||
"content-element": "jsx",
|
||||
option: "opt",
|
||||
text: "text",
|
||||
}[prop.meta?.type || ""] as any,
|
||||
visible: false,
|
||||
};
|
||||
}
|
||||
props[name] = { value: prop.valueBuilt, visible: prop.visible };
|
||||
const comp_id = item.component?.id;
|
||||
if (
|
||||
prop.meta?.type === "content-element" &&
|
||||
|
|
@ -78,7 +58,6 @@ export const genComp = (
|
|||
) {
|
||||
genMeta(p, {
|
||||
item: prop.content,
|
||||
mitem: mprop?.get("content"),
|
||||
is_root: false,
|
||||
jsx_prop: {
|
||||
is_root: true,
|
||||
|
|
@ -87,26 +66,14 @@ export const genComp = (
|
|||
},
|
||||
parent: {
|
||||
item,
|
||||
mitem: mitem,
|
||||
comp: pcomp.comp,
|
||||
mcomp: pcomp.mcomp,
|
||||
instance: arg.parent?.instance || item,
|
||||
minstance: arg.parent?.minstance || mitem,
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const vis = evalPropVis(props);
|
||||
if (vis && meta.scope.def?.props) {
|
||||
for (const [k, v] of Object.entries(vis)) {
|
||||
if (meta.scope.def.props[k]) {
|
||||
meta.scope.def.props[k].visible = v === false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (p.on) {
|
||||
if (p.on.item_exists && p.meta[item.id]) {
|
||||
p.on.item_exists({ old: p.meta[item.id], new: meta });
|
||||
|
|
@ -126,18 +93,13 @@ export const genComp = (
|
|||
}
|
||||
|
||||
for (const [k, v] of Object.entries(item.childs)) {
|
||||
const mchild = mitem?.get("childs")?.get(k as unknown as number);
|
||||
genMeta(p, {
|
||||
item: v,
|
||||
mitem: mchild,
|
||||
is_root: false,
|
||||
parent: {
|
||||
item,
|
||||
mitem: mitem,
|
||||
comp: pcomp.comp,
|
||||
mcomp: pcomp.mcomp,
|
||||
instance: arg.parent?.instance || item,
|
||||
minstance: arg.parent?.minstance || mitem,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +0,0 @@
|
|||
export const evalPropVis = (
|
||||
props: Record<string, { value: string; visible?: string }>
|
||||
) => {
|
||||
try {
|
||||
const src = `
|
||||
${Object.entries(props)
|
||||
.map(([k, v]) => `const ${k} = ${v.value}`)
|
||||
.join("\n")}
|
||||
|
||||
return {
|
||||
${Object.entries(props)
|
||||
.filter(([_, v]) => v.visible)
|
||||
.map(([k, v]) => `${k}: ${v.visible},`)
|
||||
.join("\n")}
|
||||
}
|
||||
`;
|
||||
const fn = new Function(src);
|
||||
|
||||
return fn() as Record<string, boolean>;
|
||||
} catch (e) {}
|
||||
};
|
||||
|
|
@ -1,12 +1,10 @@
|
|||
import { createId } from "@paralleldrive/cuid2";
|
||||
import { TypedMap } from "yjs-types";
|
||||
import { IItem } from "../../../../../utils/types/item";
|
||||
|
||||
export const instantiate = (
|
||||
item: IItem,
|
||||
comp: IItem,
|
||||
ref_ids: Record<string, string>,
|
||||
mref_ids?: Record<string, string> & TypedMap<Record<string, string>>
|
||||
ref_ids: Record<string, string>
|
||||
) => {
|
||||
const newitem = structuredClone(comp);
|
||||
if (newitem.component) {
|
||||
|
|
@ -16,29 +14,24 @@ export const instantiate = (
|
|||
if (item.id) {
|
||||
newitem.id = item.id;
|
||||
}
|
||||
|
||||
|
||||
if (item.component) {
|
||||
newitem.component = item.component;
|
||||
}
|
||||
|
||||
walkChild(newitem, ref_ids, mref_ids);
|
||||
walkChild(newitem, ref_ids);
|
||||
};
|
||||
|
||||
const walkChild = (
|
||||
item: IItem,
|
||||
ref_ids: Record<string, string>,
|
||||
mref_ids?: Record<string, string> & TypedMap<Record<string, string>>
|
||||
) => {
|
||||
const walkChild = (item: IItem, ref_ids: Record<string, string>) => {
|
||||
if (!ref_ids[item.id]) {
|
||||
ref_ids[item.id] = createId();
|
||||
mref_ids?.set(item.id, ref_ids[item.id]);
|
||||
}
|
||||
|
||||
item.id = ref_ids[item.id];
|
||||
|
||||
if (item.childs) {
|
||||
for (const child of item.childs) {
|
||||
walkChild(child as IItem, ref_ids, mref_ids);
|
||||
walkChild(child as IItem, ref_ids);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,21 +1,12 @@
|
|||
import { IItem, MItem } from "../../../../../utils/types/item";
|
||||
import { FMCompDef, FNCompDef } from "../../../../../utils/types/meta-fn";
|
||||
import { IItem } from "../../../../../utils/types/item";
|
||||
import { FNCompDef } from "../../../../../utils/types/meta-fn";
|
||||
|
||||
export const walkProp = (arg: {
|
||||
item: IItem;
|
||||
mitem?: MItem;
|
||||
pcomp: { comp: IItem; mcomp?: MItem };
|
||||
each: (name: string, prop: FNCompDef, mprop?: FMCompDef) => void;
|
||||
pcomp: { comp: IItem };
|
||||
each: (name: string, prop: FNCompDef) => void;
|
||||
}) => {
|
||||
for (const [k, v] of Object.entries(arg.pcomp.comp.component?.props || {})) {
|
||||
let mprop = arg.mitem?.get("component")?.get("props")?.get(k);
|
||||
if (!mprop) {
|
||||
const map = new Y.Map() as any;
|
||||
syncronize(map, v);
|
||||
arg.mitem?.get("component")?.get("props")?.set(k, map);
|
||||
mprop = arg.mitem?.get("component")?.get("props")?.get(k);
|
||||
}
|
||||
|
||||
const props = arg.item.component?.props;
|
||||
let prop = props?.[k];
|
||||
if (props) {
|
||||
|
|
@ -26,7 +17,7 @@ export const walkProp = (arg: {
|
|||
}
|
||||
|
||||
if (prop) {
|
||||
arg.each(k, prop, mprop);
|
||||
arg.each(k, prop);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,39 +0,0 @@
|
|||
import { IItem, MItem } from "../../../../utils/types/item";
|
||||
import { IRoot, MRoot } from "../../../../utils/types/root";
|
||||
import { initLoadComp } from "./comp/init-load-comp";
|
||||
import { genMeta } from "./meta";
|
||||
import { GenMetaP } from "./types";
|
||||
|
||||
export const hydrateRoot = async (
|
||||
p: GenMetaP,
|
||||
mroot: MRoot | MItem,
|
||||
loadComponents: (comp_ids: string[]) => Promise<void>
|
||||
) => {
|
||||
const root = mroot.toJSON() as IRoot;
|
||||
if (root.type === "root") {
|
||||
const mitems: MItem[] = [];
|
||||
(mroot as MRoot).get("childs")?.forEach(async (mitem) => {
|
||||
mitems.push(mitem);
|
||||
});
|
||||
|
||||
await Promise.all(mitems.map((mitem) => hydrate(p, mitem, loadComponents)));
|
||||
} else {
|
||||
await hydrate(p, mroot as MItem, loadComponents);
|
||||
}
|
||||
};
|
||||
|
||||
const hydrate = async (
|
||||
p: GenMetaP,
|
||||
mitem: MItem,
|
||||
loadComponents: (comp_ids: string[]) => Promise<void>
|
||||
) => {
|
||||
const item = mitem.toJSON() as IItem;
|
||||
await initLoadComp(p, item, loadComponents);
|
||||
|
||||
genMeta(
|
||||
{
|
||||
...p,
|
||||
},
|
||||
{ item, mitem }
|
||||
);
|
||||
};
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { IItem, MItem } from "../../../../utils/types/item";
|
||||
import { IItem } from "../../../../utils/types/item";
|
||||
import { genComp } from "./comp";
|
||||
import { applyRefIds } from "./ref-ids";
|
||||
import { simplifyItemChild } from "./simplify";
|
||||
|
|
@ -9,19 +9,11 @@ export const genMeta = (p: GenMetaP, arg: GenMetaArg) => {
|
|||
fn();
|
||||
};
|
||||
|
||||
if (arg.is_root && arg.mitem) {
|
||||
const transact = arg.mitem.doc?.transact;
|
||||
if (transact) {
|
||||
wrapper = transact;
|
||||
}
|
||||
}
|
||||
|
||||
wrapper(() => {
|
||||
const { parent } = arg;
|
||||
const item = arg.item as IItem;
|
||||
const mitem = arg.mitem as MItem | undefined;
|
||||
|
||||
const r = applyRefIds(item, mitem, parent, p.smeta);
|
||||
const r = applyRefIds(item, parent);
|
||||
if (item.type === "item" && item.component?.id) {
|
||||
genComp(p, arg, r);
|
||||
return;
|
||||
|
|
@ -38,10 +30,6 @@ export const genMeta = (p: GenMetaP, arg: GenMetaArg) => {
|
|||
scope: {},
|
||||
};
|
||||
|
||||
if (p.set_mitem !== false && mitem) {
|
||||
meta.mitem = mitem;
|
||||
}
|
||||
|
||||
if (p.on?.visit) {
|
||||
p.on.visit(meta);
|
||||
}
|
||||
|
|
@ -60,19 +48,14 @@ export const genMeta = (p: GenMetaP, arg: GenMetaArg) => {
|
|||
}
|
||||
|
||||
if (item.childs) {
|
||||
for (const [k, v] of Object.entries(item.childs)) {
|
||||
const mchild = mitem?.get("childs")?.get(k as unknown as number);
|
||||
for (const [_, v] of Object.entries(item.childs)) {
|
||||
genMeta(p, {
|
||||
item: v,
|
||||
mitem: mchild,
|
||||
is_root: false,
|
||||
parent: {
|
||||
item,
|
||||
mitem: mitem,
|
||||
comp: arg.parent?.comp,
|
||||
mcomp: arg.parent?.mcomp,
|
||||
instance: arg.parent?.instance,
|
||||
minstance: arg.parent?.minstance,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,41 +1,16 @@
|
|||
import { createId } from "@paralleldrive/cuid2";
|
||||
import { IContent, MContent } from "../../../../utils/types/general";
|
||||
import { IItem } from "../../../../utils/types/item";
|
||||
import { ISimpleMeta } from "./types";
|
||||
|
||||
const timeouts = {} as Record<string, any>;
|
||||
|
||||
export const applyRefIds = (
|
||||
item: IContent,
|
||||
mitem?: MContent,
|
||||
parent?: { instance?: IItem; minstance?: MContent },
|
||||
smeta?: Record<string, ISimpleMeta>
|
||||
) => {
|
||||
export const applyRefIds = (item: IContent, parent?: { instance?: IItem }) => {
|
||||
const instance = parent?.instance;
|
||||
|
||||
if (instance && instance.component) {
|
||||
const parentcomp = parent?.minstance?.get("component");
|
||||
let mref_ids = parentcomp?.get("ref_ids");
|
||||
|
||||
const ref_ids = instance.component.ref_ids;
|
||||
|
||||
if (ref_ids) {
|
||||
if (!ref_ids[item.id]) {
|
||||
ref_ids[item.id] = createId();
|
||||
|
||||
// if (parentcomp) {
|
||||
// clearTimeout(timeouts[instance.id]);
|
||||
// timeouts[instance.id] = setTimeout(() => {
|
||||
// if (mref_ids) {
|
||||
// const mref_ids = new Y.Map() as any;
|
||||
// syncronize(mref_ids, ref_ids);
|
||||
// const parentcomp = parent?.minstance?.get("component");
|
||||
// if (parentcomp) {
|
||||
// parentcomp.set("ref_ids", mref_ids);
|
||||
// }
|
||||
// }
|
||||
// }, 50);
|
||||
// }
|
||||
}
|
||||
|
||||
if (ref_ids[item.id]) {
|
||||
|
|
@ -44,6 +19,6 @@ export const applyRefIds = (
|
|||
}
|
||||
}
|
||||
|
||||
return { ref_ids, mref_ids };
|
||||
return { ref_ids };
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
import { FC, ReactNode } from "react";
|
||||
import { IContent, MContent } from "../../../../utils/types/general";
|
||||
import { IItem, MItem } from "../../../../utils/types/item";
|
||||
import { ViContext } from "../../../vi/render/parts";
|
||||
import { ViRender } from "../../../vi/render/render";
|
||||
import { ReactNode } from "react";
|
||||
import { IContent } from "../../../../utils/types/general";
|
||||
import { IItem } from "../../../../utils/types/item";
|
||||
|
||||
export type GenMetaP = {
|
||||
meta: Record<string, IMeta>;
|
||||
comps: Record<string, { comp: IItem; mcomp?: MItem }>;
|
||||
comps: Record<string, { comp: IItem }>;
|
||||
on?: {
|
||||
visit_component?: (id: string) => void;
|
||||
visit?: (meta: IMeta) => void;
|
||||
|
|
@ -14,22 +12,17 @@ export type GenMetaP = {
|
|||
item_new?: (arg: { new: IMeta }) => void;
|
||||
};
|
||||
smeta?: Record<string, ISimpleMeta>;
|
||||
set_mitem?: boolean;
|
||||
set_meta?: boolean;
|
||||
};
|
||||
|
||||
export type GenMetaArg = {
|
||||
item: IContent;
|
||||
is_root?: boolean;
|
||||
mitem?: MContent;
|
||||
jsx_prop?: IMeta["jsx_prop"];
|
||||
parent?: {
|
||||
item: IItem;
|
||||
mitem?: MItem;
|
||||
comp?: IItem;
|
||||
mcomp?: MItem;
|
||||
instance?: IItem;
|
||||
minstance?: MItem;
|
||||
};
|
||||
};
|
||||
|
||||
|
|
@ -49,13 +42,10 @@ export type ISimpleMeta = {
|
|||
|
||||
export type IMeta = {
|
||||
item: IItem;
|
||||
mitem?: MItem;
|
||||
parent?: {
|
||||
id: string;
|
||||
instance_id?: string;
|
||||
comp_id?: string;
|
||||
minstance?: MItem;
|
||||
mcomp?: MItem;
|
||||
};
|
||||
jsx_prop?: {
|
||||
name: string;
|
||||
|
|
@ -68,10 +58,7 @@ export type IMeta = {
|
|||
scope: {
|
||||
val?: any;
|
||||
def?: {
|
||||
props?: Record<
|
||||
string,
|
||||
{ value: string; type: "jsx" | "text" | "opt"; visible: boolean }
|
||||
>;
|
||||
props?: Record<string, { value: string; visible: boolean }>;
|
||||
local?: {
|
||||
name: string;
|
||||
idx: number;
|
||||
|
|
|
|||
Loading…
Reference in New Issue