wip fix component

This commit is contained in:
Rizky 2023-12-27 16:24:20 +07:00
parent 3367d15fa8
commit 3d57ce6c97
7 changed files with 49 additions and 16 deletions

View File

@ -123,7 +123,7 @@ export const page_load: SAction["page"]["load"] = async function (
url: page.url,
name: page.name,
snapshot: await gzipAsync(bin),
comps: await prepareComponentForPage(id),
comps: await prepareComponentForPage(id, this),
};
}
} else if (snap && !ydoc) {
@ -156,7 +156,7 @@ export const page_load: SAction["page"]["load"] = async function (
url: snap.url,
name: snap.name,
snapshot: await gzipAsync(snap.bin),
comps: await prepareComponentForPage(id),
comps: await prepareComponentForPage(id, this),
};
} else if (snap && ydoc) {
await setActivityPage(snap.id_site, id);
@ -174,7 +174,7 @@ export const page_load: SAction["page"]["load"] = async function (
url: snap.url,
name: snap.name,
snapshot: await gzipAsync(snap.bin),
comps: await prepareComponentForPage(id),
comps: await prepareComponentForPage(id, this),
};
}
};

View File

@ -3,14 +3,22 @@ import { IRoot } from "../../../../web/src/utils/types/root";
import { docs } from "../entity/docs";
import { snapshot } from "../entity/snapshot";
import { gzipAsync } from "../entity/zlib";
import { SyncConnection } from "../type";
import { loadComponent } from "./load-component";
export const prepareComponentForPage = async (page_id: string) => {
export const prepareComponentForPage = async (
page_id: string,
sync: SyncConnection
) => {
const doc = docs.page[page_id].doc;
const root = doc.getMap("map").get("root")?.toJSON() as IRoot;
const result = {} as Record<string, EComp>;
if (root.component_ids) {
for (const id of root.component_ids) {
if (!docs.comp[id]) {
await loadComponent(id, sync);
}
const snap = snapshot.get("comp", id);
if (snap) {
result[id] = { id, snapshot: await gzipAsync(snap.bin) };

View File

@ -1,13 +1,15 @@
import { IItem, MItem } from "../../../../utils/types/item";
import { IMeta } from "../ed-global";
import { IMeta, PG } from "../ed-global";
export const assignMitem = (arg: {
p: PG;
m: IMeta;
item: IItem;
mitem: MItem;
meta: Record<string, IMeta>;
}) => {
const { m, item, mitem, meta } = arg;
const { p, m, item, mitem, meta } = arg;
if (m.parent) {
if (m.parent.id === "root") {
if (m.item.id === item.id) {
@ -51,4 +53,21 @@ export const assignMitem = (arg: {
}
}
}
if (m.parent?.instance_id && m.parent?.instance_id === m.parent?.id) {
const parent = meta[m.parent?.instance_id];
if (parent.item.component?.id) {
const lcomp = p.comp.list[parent.item.component.id];
if (lcomp) {
const mcomp = lcomp.doc.getMap("map").get("root");
if (mcomp) {
mcomp.get("childs")?.forEach((e) => {
if (e.get("id") === m.item.originalId) {
m.mitem = e;
}
});
}
}
}
}
};

View File

@ -44,7 +44,7 @@ export const treeRebuild = async (p: PG, arg?: { note?: string }) => {
pushTreeNode(p, m, meta, p.page.tree);
}
assignMitem({ m, item, mitem, meta });
assignMitem({ p, m, item, mitem, meta });
}
},
},

View File

@ -55,14 +55,21 @@ export const mainPerItemVisit = (
prop.ref = (el) => {
if (el && text_edit.caret) {
if (text_edit.id === meta.item.id) {
setCaret(el, text_edit.caret);
text_edit.caret = null;
if (
text_edit.id === meta.item.id ||
text_edit.id === meta.item.originalId
) {
if (document.activeElement !== el) {
setCaret(el, text_edit.caret);
text_edit.caret = null;
}
}
}
};
prop.onKeyDown = (e) => {
text_edit.caret = getCaret(e.currentTarget);
if (typeof text_edit.del_key_id === "string") {
if (e.key === "Backspace" || e.key === "Delete") {
e.currentTarget.blur();
@ -77,14 +84,14 @@ export const mainPerItemVisit = (
prop.onInput = (e) => {
e.stopPropagation();
e.preventDefault();
const val = e.currentTarget.innerHTML;
const el = e.currentTarget;
const val = e.currentTarget.innerHTML;
clearTimeout(text_edit.timeout);
text_edit.timeout = setTimeout(() => {
text_edit.id = meta.item.id;
text_edit.caret = getCaret(el);
text_edit.id = meta.item.originalId || meta.item.id;
text_edit.timeout = setTimeout(() => {
text_edit.caret = getCaret(el);
if (active.comp_id && meta.parent?.comp_id === active.comp_id) {
const comp = p.comp.list[active.comp_id];
const m = comp.meta[meta.item.originalId || meta.item.id];

View File

@ -47,7 +47,6 @@ export const genComp = (p: GenMetaP, arg: GenMetaArg) => {
instance_id: arg.parent?.instance_id,
},
};
meta.item.childs = [];
if (item.id) {
if (p.set_meta !== false) {
@ -102,6 +101,7 @@ export const genComp = (p: GenMetaP, arg: GenMetaArg) => {
item,
instance_id: item.id,
root_instances: instances,
comp: item_comp,
},
}
);

View File

@ -13,7 +13,6 @@ export const ViRender: FC<{
}> = ({ meta, children, passprop }) => {
if (!meta) return null;
if (meta.item.hidden) return null;
if (meta.item.adv?.js || meta.item.component?.id) {