From 7ba2c91dc02d2456d513cef7ad692f3fef56ec03 Mon Sep 17 00:00:00 2001 From: Rizky Date: Sat, 21 Oct 2023 20:01:20 +0700 Subject: [PATCH] fix route --- app/web/src/render/ed/logic/ed-global.ts | 14 ++++- app/web/src/render/ed/logic/ed-route.ts | 6 +- app/web/src/render/ed/logic/tree/build.tsx | 71 ++++++++++++++++++++-- 3 files changed, 82 insertions(+), 9 deletions(-) diff --git a/app/web/src/render/ed/logic/ed-global.ts b/app/web/src/render/ed/logic/ed-global.ts index 058af517..18acc464 100644 --- a/app/web/src/render/ed/logic/ed-global.ts +++ b/app/web/src/render/ed/logic/ed-global.ts @@ -1,6 +1,10 @@ +import { NodeModel } from "@minoru/react-dnd-treeview"; import { clientStartSync } from "../../../utils/sync/client"; -import { IItem } from "../../../utils/types/item"; +import { IContent, MContent } from "../../../utils/types/general"; +import { IItem, MItem } from "../../../utils/types/item"; import { DPage, IRoot } from "../../../utils/types/root"; +import { IText, MText } from "../../../utils/types/text"; +import { NodeMeta } from "../../editor/logic/global"; const EmptySite = { id: "", @@ -20,6 +24,11 @@ const EmptyPage = { snapshot: null as null | Uint8Array, }; +export type EdMeta = { + item: IItem | IText; + mitem?: MItem | MText; +}; + export const EDGlobal = { status: "init" as | "init" @@ -33,6 +42,9 @@ export const EDGlobal = { current: EmptyPage, doc: null as null | DPage, root: null as null | IRoot, + entry: [] as string[], + tree: [] as NodeModel[], + meta: {} as Record, }, }; diff --git a/app/web/src/render/ed/logic/ed-route.ts b/app/web/src/render/ed/logic/ed-route.ts index 23a585e3..9175e5a8 100644 --- a/app/web/src/render/ed/logic/ed-route.ts +++ b/app/web/src/render/ed/logic/ed-route.ts @@ -1,6 +1,7 @@ import { IRoot } from "../../../utils/types/root"; import { PG } from "./ed-global"; import { produce } from "immer"; +import { treeRebuild } from "./tree/build"; export const edRoute = async (p: PG) => { if (p.status === "ready") { if (!p.site.domain && !p.site.name) { @@ -32,10 +33,7 @@ export const edRoute = async (p: PG) => { p.page.doc = doc as any; if (p.page.doc) { - const root = p.page.doc.getMap("map").get("root")?.toJSON() as IRoot; - if (root) { - p.page.root = produce(root, () => {}); - } + treeRebuild(p); } } p.status = "ready"; diff --git a/app/web/src/render/ed/logic/tree/build.tsx b/app/web/src/render/ed/logic/tree/build.tsx index 754dfd6b..af5f8359 100644 --- a/app/web/src/render/ed/logic/tree/build.tsx +++ b/app/web/src/render/ed/logic/tree/build.tsx @@ -1,5 +1,68 @@ -import { IItem } from "../../../../utils/types/item"; -import { ISection } from "../../../../utils/types/section"; -import { PG } from "../ed-global"; +import { IItem, MItem } from "../../../../utils/types/item"; +import { MRoot } from "../../../../utils/types/root"; +import { MSection } from "../../../../utils/types/section"; +import { walk } from "../../../editor/logic/tree-logic"; +import { EdMeta, PG } from "../ed-global"; -export const treeRebuild = (items: (ISection | IItem)[]) => {}; +export const treeRebuild = async (p: PG) => { + const root = p.page.doc?.getMap("map").get("root"); + if (root) { + p.page.entry = []; + p.page.tree = []; + p.page.meta = {}; + + const sections = root.get("childs"); + if (sections) { + await Promise.all( + sections.map(async (e) => { + p.page.entry.push(e.get("id")); + await walkMap(p, { mitem: e, tree_parent_id: "root" }); + }) + ); + console.log(p.page); + } + } +}; + +const walkMap = async ( + p: PG, + arg: { mitem: MItem | MSection; tree_parent_id: string } +) => { + const { mitem, tree_parent_id } = arg; + + const item = {} as unknown as IItem; + mitem.forEach((e, k) => { + if (k !== "childs") { + let val = e; + if (typeof e === "object" && e) { + if ((e as any).toJSON) { + val = e.toJSON() as any; + } + } + (item as any)[k] = val; + } else { + item[k] = []; + } + }); + + const meta: EdMeta = { + item, + mitem: mitem as MItem, + }; + + p.page.meta[item.id] = meta; + + p.page.tree.push({ + id: item.id, + parent: tree_parent_id, + text: item.name, + data: meta, + }); + + await Promise.all( + mitem.get("childs")?.map(async (e, k) => { + item.childs.push(e.get("id")); + await walkMap(p, { mitem: e, tree_parent_id: item.id }); + }) || [] + ); +};