This commit is contained in:
Rizky 2024-06-24 17:22:57 +07:00
parent 964f0fe8ba
commit c64fe0e4d0
4 changed files with 79 additions and 70 deletions

File diff suppressed because one or more lines are too long

View File

@ -157,7 +157,7 @@ export const EDGlobal = {
script: { script: {
site_types: {} as Record<string, string>, site_types: {} as Record<string, string>,
loaded: false, loaded: false,
do_edit: async (newval: string, all?: boolean) => { }, do_edit: async (newval: string, all?: boolean) => {},
db: null as any, db: null as any,
api: null as any, api: null as any,
init_local_effect: {} as Record<string, boolean>, init_local_effect: {} as Record<string, boolean>,
@ -176,13 +176,14 @@ export const EDGlobal = {
page: EPage; page: EPage;
doc: DPage; doc: DPage;
on_update?: (bin: Uint8Array, origin: any) => Promise<void>; on_update?: (bin: Uint8Array, origin: any) => Promise<void>;
update_timeout: any;
} }
>, >,
building: false, building: false,
meta: {} as Record<string, IMeta>, meta: {} as Record<string, IMeta>,
entry: [] as string[], entry: [] as string[],
tree: [] as NodeModel<IMeta>[], tree: [] as NodeModel<IMeta>[],
render: () => { }, render: () => {},
}, },
comp: { comp: {
doc: null as null | DComp, doc: null as null | DComp,
@ -295,9 +296,9 @@ export const EDGlobal = {
type: "item" as "item" | "prop-master" | "prop-instance" | "comp-types", type: "item" as "item" | "prop-master" | "prop-instance" | "comp-types",
prop_kind: "" as PropFieldKind, prop_kind: "" as PropFieldKind,
prop_name: "", prop_name: "",
on_close: () => { }, on_close: () => {},
typings: { status: "ok" as "ok" | "loading" | "error", err_msg: "" }, typings: { status: "ok" as "ok" | "loading" | "error", err_msg: "" },
wb_render: () => { }, wb_render: () => {},
}, },
site: null as null | ((site_id: string) => void | Promise<void>), site: null as null | ((site_id: string) => void | Promise<void>),
site_form: null as null | { site_form: null as null | {

View File

@ -7,6 +7,7 @@ import { treeCacheBuild, treeRebuild } from "./tree/build";
import { get, set } from "idb-keyval"; import { get, set } from "idb-keyval";
import { nav } from "../../vi/render/script/extract-nav"; import { nav } from "../../vi/render/script/extract-nav";
const UPDATE_TIMEOUT = 200;
export const edRoute = async (p: PG) => { export const edRoute = async (p: PG) => {
if (p.sync && (p.status === "ready" || p.status === "init")) { if (p.sync && (p.status === "ready" || p.status === "init")) {
if (!p.site.domain && !p.site.name) { if (!p.site.domain && !p.site.name) {
@ -67,45 +68,48 @@ export const reloadLayout = async (p: PG, layout_id: string, note: string) => {
} }
page.on_update = async (bin: Uint8Array, origin: any) => { page.on_update = async (bin: Uint8Array, origin: any) => {
if (origin === "local" || !p.sync) return; clearTimeout(page.update_timeout);
page.update_timeout = setTimeout(async () => {
if (origin === "local" || !p.sync) return;
const res = await p.sync.yjs.sv_local( const res = await p.sync.yjs.sv_local(
"page",
layout_id,
Buffer.from(compress(bin))
);
if (res) {
const diff_local = Y.encodeStateAsUpdate(
doc as any,
decompress(res.sv)
);
Y.applyUpdate(doc as any, decompress(res.diff), "local");
if (!isTextEditing()) {
await treeRebuild(p, { note: note + " page-on-update" });
}
await p.sync.yjs.diff_local(
"page", "page",
p.page.cur.id, layout_id,
Buffer.from(compress(diff_local)) Buffer.from(compress(bin))
); );
p.preview.page_cache[layout_id] = { if (res) {
root, const diff_local = Y.encodeStateAsUpdate(
url: "~~@$#%^#@~LAYOUT~~@$#%^#@~", doc as any,
}; decompress(res.sv)
await treeCacheBuild(p, layout_id); );
p.render(); Y.applyUpdate(doc as any, decompress(res.diff), "local");
const meta_cache = p.preview.meta_cache[layout_id]; if (!isTextEditing()) {
if (meta_cache) { await treeRebuild(p, { note: note + " page-on-update" });
p.site.layout.meta = meta_cache.meta; }
p.site.layout.entry = meta_cache.entry;
savePageMetaCache(p, meta_cache.meta); await p.sync.yjs.diff_local(
"page",
p.page.cur.id,
Buffer.from(compress(diff_local))
);
p.preview.page_cache[layout_id] = {
root,
url: "~~@$#%^#@~LAYOUT~~@$#%^#@~",
};
await treeCacheBuild(p, layout_id);
p.render();
const meta_cache = p.preview.meta_cache[layout_id];
if (meta_cache) {
p.site.layout.meta = meta_cache.meta;
p.site.layout.entry = meta_cache.entry;
savePageMetaCache(p, meta_cache.meta);
}
} }
} }, UPDATE_TIMEOUT);
}; };
const root = (doc.getMap("map").get("root") as any)?.toJSON(); const root = (doc.getMap("map").get("root") as any)?.toJSON();
@ -170,46 +174,49 @@ export const reloadPage = async (
} }
page.on_update = async (bin: Uint8Array, origin: any) => { page.on_update = async (bin: Uint8Array, origin: any) => {
if (origin === "local" || !p.sync) return; clearTimeout(page.update_timeout);
page.update_timeout = setTimeout(async () => {
if (origin === "local" || !p.sync) return;
if (page.page.id !== remotePage.id) { if (page.page.id !== remotePage.id) {
alert("Page ID Mismatch!\n Refreshing to preventing data loss..."); alert("Page ID Mismatch!\n Refreshing to preventing data loss...");
location.reload(); location.reload();
return; return;
}
const res = await p.sync.yjs.sv_local(
"page",
p.page.cur.id,
Buffer.from(compress(bin))
);
if (res) {
const diff_local = Y.encodeStateAsUpdate(
doc as any,
decompress(res.sv)
);
Y.applyUpdate(doc as any, decompress(res.diff), "local");
if (!isTextEditing()) {
await treeRebuild(p, { note: note + " page-on-update" });
} }
await p.sync.yjs.diff_local( const res = await p.sync.yjs.sv_local(
"page", "page",
p.page.cur.id, p.page.cur.id,
Buffer.from(compress(diff_local)) Buffer.from(compress(bin))
); );
p.ui.syncing = false;
p.page.entry = (doc as any) if (res) {
.getMap("map") const diff_local = Y.encodeStateAsUpdate(
.get("root") doc as any,
?.get("childs") decompress(res.sv)
?.map((e: any) => e.get("id")) as string[]; );
Y.applyUpdate(doc as any, decompress(res.diff), "local");
if (active.should_render_main) p.render(); if (!isTextEditing()) {
} await treeRebuild(p, { note: note + " page-on-update" });
}
await p.sync.yjs.diff_local(
"page",
p.page.cur.id,
Buffer.from(compress(diff_local))
);
p.ui.syncing = false;
p.page.entry = (doc as any)
.getMap("map")
.get("root")
?.get("childs")
?.map((e: any) => e.get("id")) as string[];
if (active.should_render_main) p.render();
}
}, UPDATE_TIMEOUT);
}; };
doc.on("update", page.on_update); doc.on("update", page.on_update);

View File

@ -125,6 +125,7 @@ export const devItem = (
{ value: string; valueBuilt?: string } { value: string; valueBuilt?: string }
>; >;
console.log("committing", pedit);
if (mitem) { if (mitem) {
for (const [item_id, changes] of Object.entries(pedit)) { for (const [item_id, changes] of Object.entries(pedit)) {
const meta = metas[item_id]; const meta = metas[item_id];