Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
feat: save and sync
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-para committed Aug 4, 2023
1 parent 3d891b1 commit e5f9587
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 92 deletions.
79 changes: 42 additions & 37 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[][]
Expand Down Expand Up @@ -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<string, Record<string, unknown>> = {}

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}/`)
})
66 changes: 13 additions & 53 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<string[]>([])
Expand Down Expand Up @@ -87,27 +61,6 @@ const treeHeight = computed(() => {
</script>

<template>
<NModal v-model:show="showEdit">
<NCard style="width: 80vw" role="dialog">
<div class="h-full flex flex-col gap-2">
<NInput
class="flex-1"
type="textarea"
:autosize="{
minRows: 20,
maxRows: 30
}"
v-model:value="cacheEdit"
></NInput>
<div class="flex justify-center gap-2">
<NButton @click="tryReplace">替换</NButton>
<NButton @click="tryAppend">追加</NButton>
<NButton @click="doReset">重置</NButton>
</div>
</div>
</NCard>
</NModal>

<div class="flex flex-col gap-2 flex-1 min-h-0">
<div class="flex gap-2">
<NButton :disabled="!canUndo" @click="selectedKeys = historyUndo()">
Expand All @@ -124,10 +77,17 @@ const treeHeight = computed(() => {
</NIcon>
</template>
</NButton>
<NButton @click="popupEdit">
<NButton @click="syncData">
<template #icon>
<NIcon>
<SaveAltOutlined></SaveAltOutlined>
</NIcon>
</template>
</NButton>
<NButton @click="loadData">
<template #icon>
<NIcon>
<EditOutlined></EditOutlined>
<SyncOutlined></SyncOutlined>
</NIcon>
</template>
</NButton>
Expand Down
7 changes: 5 additions & 2 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -37,6 +37,9 @@ export async function loadData() {
}
}
}

taskData.data = mergeData
}

export async function syncData() {
await axios.post('/api/sync', taskData.data)
}

0 comments on commit e5f9587

Please sign in to comment.