From 0f4cfc87f7ef6b4b86468d92883f66fd574a1830 Mon Sep 17 00:00:00 2001 From: Rizky Date: Wed, 10 Jan 2024 19:27:43 +0700 Subject: [PATCH] wip fix code --- app/srv/api/site-export.ts | 2 +- app/srv/ws/sync/actions/activity.ts | 1 + app/srv/ws/sync/actions/code_load.ts | 34 +++++++++++++++++++----- app/srv/ws/sync/editor/code/prep-code.ts | 4 ++- app/srv/ws/sync/entity/docs.ts | 2 +- app/web/src/nova/ed/panel/main/main.tsx | 1 + app/web/src/nova/vi/load/load-legacy.tsx | 1 + 7 files changed, 36 insertions(+), 9 deletions(-) diff --git a/app/srv/api/site-export.ts b/app/srv/api/site-export.ts index a34648cd..0734adee 100644 --- a/app/srv/api/site-export.ts +++ b/app/srv/api/site-export.ts @@ -111,7 +111,7 @@ export const _ = { }, }; -function readDirectoryRecursively( +export function readDirectoryRecursively( dirPath: string, baseDir?: string[] ): Record { diff --git a/app/srv/ws/sync/actions/activity.ts b/app/srv/ws/sync/actions/activity.ts index 8a102173..335366e4 100644 --- a/app/srv/ws/sync/actions/activity.ts +++ b/app/srv/ws/sync/actions/activity.ts @@ -3,6 +3,7 @@ import { prepCode } from "../editor/code/prep-code"; import { startCodeWatcher, stopCodeWatcher } from "../editor/code/watcher"; import { activity as a } from "../entity/activity"; import { SyncConnection } from "../type"; + export const activity: SAction["activity"] = async function ( this: SyncConnection, name, diff --git a/app/srv/ws/sync/actions/code_load.ts b/app/srv/ws/sync/actions/code_load.ts index 2208814b..b698df40 100644 --- a/app/srv/ws/sync/actions/code_load.ts +++ b/app/srv/ws/sync/actions/code_load.ts @@ -1,19 +1,41 @@ +import { dir } from "dir"; +import { g } from "utils/global"; import { DCode } from "../../../../web/src/utils/types/root"; +import { readDirectoryRecursively } from "../../../api/site-export"; import { SAction } from "../actions"; import { docs } from "../entity/docs"; import { SyncConnection } from "../type"; +import { getCode } from "../editor/code/prep-code"; export const code_load: SAction["code"]["load"] = async function ( this: SyncConnection, - id, + site_id, type ) { let result = null as unknown as Awaited>; - if (!docs.code[id]) { - const src_doc = new Y.Doc() as DCode; - const built_doc = new Y.Doc() as DCode; - docs.code[id] = { id, src: src_doc, built: built_doc }; + const code = await getCode(site_id, "site"); + + if (code) { + if (!docs.code[site_id]) { + docs.code[site_id] = { + id: site_id, + src: loadFolderAsDCode(dir.path(`${g.datadir}/site/code/${code.id}`)), + build: loadFolderAsDCode( + dir.path(`${g.datadir}/site/build/${code.id}`) + ), + }; + } + + return result; } - return result; + return { id: site_id, snapshot: null }; +}; + +const loadFolderAsDCode = (path: string) => { + const doc = new Y.Doc() as DCode; + + const dirs = readDirectoryRecursively(path); + + return doc; }; diff --git a/app/srv/ws/sync/editor/code/prep-code.ts b/app/srv/ws/sync/editor/code/prep-code.ts index 98c88ee9..bb4e6e5b 100644 --- a/app/srv/ws/sync/editor/code/prep-code.ts +++ b/app/srv/ws/sync/editor/code/prep-code.ts @@ -1,6 +1,7 @@ import { dir } from "dir"; import { g } from "utils/global"; import { dirAsync } from "fs-jetpack"; +import { docs } from "../../entity/docs"; export type DBCode = Exclude>, null>; export const prepCode = async (site_id: string, name: string) => { @@ -61,10 +62,11 @@ export const hello_world = () => { }); code = await getCode(site_id); + return code as DBCode; }; -const getCode = async (site_id: string, name?: string) => { +export const getCode = async (site_id: string, name?: string) => { return await db.code.findFirst({ where: name ? { id_site: site_id, name } diff --git a/app/srv/ws/sync/entity/docs.ts b/app/srv/ws/sync/entity/docs.ts index 8faaf75c..e4743032 100644 --- a/app/srv/ws/sync/entity/docs.ts +++ b/app/srv/ws/sync/entity/docs.ts @@ -27,5 +27,5 @@ export const docs = { um: Y.UndoManager; } >, - code: {} as Record, + code: {} as Record, }; diff --git a/app/web/src/nova/ed/panel/main/main.tsx b/app/web/src/nova/ed/panel/main/main.tsx index 0bf90c48..73964468 100644 --- a/app/web/src/nova/ed/panel/main/main.tsx +++ b/app/web/src/nova/ed/panel/main/main.tsx @@ -12,6 +12,7 @@ export const EdMain = () => { const meta = active.comp_id ? p.comp.list[active.comp_id].meta[active.item_id] : p.page.meta[active.item_id]; + return (