From e5f9587b1633321b1789ad7bfb8f50202ae2cdd6 Mon Sep 17 00:00:00 2001 From: nekosu Date: Fri, 4 Aug 2023 15:59:21 +0800 Subject: [PATCH] feat: save and sync --- server/src/index.ts | 79 ++++++++++++++++++++++++--------------------- src/App.vue | 66 ++++++++----------------------------- src/loader.ts | 7 ++-- 3 files changed, 60 insertions(+), 92 deletions(-) diff --git a/server/src/index.ts b/server/src/index.ts index 77413e3..d80fd0f 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -2,51 +2,17 @@ import express, { json } from 'express' import path from 'path' import fs from 'fs/promises' -type ListEntry = { - name: string - dir: ListEntry[] - file: string[] -} - const app = express() const webDir = path.resolve('./web') const resDir = path.resolve('./res') +const port = 8080 app.use(express.static(webDir)) app.use('/res', express.static(resDir)) app.use(json()) -app.get('/api/list', async (req, res) => { - const enumDir = async (dir: string) => { - const res: ListEntry = { - name: path.basename(dir), - dir: [], - file: [] - } - for (const filename of await fs.readdir(dir)) { - const file = path.join(dir, filename) - if ((await fs.stat(file)).isDirectory()) { - res.dir.push(await enumDir(file)) - } else { - if (['.json', '.png'].includes(path.extname(filename))) { - res.file.push(filename) - } - } - } - return res - } - - const info = await enumDir(resDir) - res.send({ - success: true, - data: { - info - } - }) -}) - -app.get('/api/list_flat', async (req, res) => { +app.post('/api/list', async (req, res) => { const info: { dir: string[][] file: string[][] @@ -88,4 +54,43 @@ app.get('/api/list_flat', async (req, res) => { }) }) -app.listen(8080) +app.post('/api/sync', async (req, res) => { + const makePath = (p: string) => { + return path.join(...p.split('.')) + '.json' + } + + const data = req.body as { + [task: string]: { + editor_info: { + path: string + } + } + } + + const result: Record> = {} + + for (const name in data) { + const task = data[name] + const file = makePath(task.editor_info.path) + result[file] = result[file] ?? {} + result[file][name] = task + } + + for (const file in result) { + await fs.writeFile( + path.join(resDir, file), + JSON.stringify(result[file], null, 4) + ) + } + + res.send({ + success: true, + data: { + result + } + }) +}) + +app.listen(port, () => { + console.log(`server started: http://localhost:${port}/`) +}) diff --git a/src/App.vue b/src/App.vue index f5e6b22..bec588e 100644 --- a/src/App.vue +++ b/src/App.vue @@ -5,7 +5,9 @@ import { EditOutlined, NavigateBeforeOutlined, NavigateNextOutlined, - SearchOutlined + SearchOutlined, + SaveAltOutlined, + SyncOutlined } from '@vicons/material' import { taskData, taskTree } from './data' import TaskEdit from '@/components/TaskEdit.vue' @@ -16,35 +18,7 @@ import { canUndo, canRedo } from './history' - -const showEdit = ref(false) -const cacheEdit = ref('') - -function popupEdit() { - cacheEdit.value = JSON.stringify(taskData.data, null, 2) - showEdit.value = true -} - -function tryReplace() { - try { - taskData.data = JSON.parse(cacheEdit.value) - showEdit.value = false - } catch (_err) {} -} - -function tryAppend() { - try { - taskData.data = { - ...taskData.data, - ...JSON.parse(cacheEdit.value) - } - showEdit.value = false - } catch (_err) {} -} - -function doReset() { - cacheEdit.value = JSON.stringify(taskData.data, null, 2) -} +import { loadData, syncData } from './loader' const searchText = ref('') const selectedKeys = ref([]) @@ -87,27 +61,6 @@ const treeHeight = computed(() => { - + + + + diff --git a/src/loader.ts b/src/loader.ts index 974be3d..5b16c3e 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -12,7 +12,7 @@ function appendPath(data: TaskData, path: string) { } export async function loadData() { - const entry = (await axios('/api/list_flat')).data as { + const entry = (await axios.post('/api/list')).data as { success: boolean data: { info: { @@ -37,6 +37,9 @@ export async function loadData() { } } } - taskData.data = mergeData } + +export async function syncData() { + await axios.post('/api/sync', taskData.data) +}