From afbb50cd173509664b6e6434bcce14712dd5a119 Mon Sep 17 00:00:00 2001 From: purocean Date: Mon, 6 Aug 2018 19:10:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=96=B0=E5=A2=9E=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E6=94=AF=E6=8C=81=E4=BB=93=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/file.js | 8 ++++---- backend/main.js | 4 ++-- frontend/src/components/Tree.vue | 11 +++++++---- frontend/src/components/TreeNode.vue | 2 +- frontend/src/file.js | 4 ++-- frontend/src/pages/Home.vue | 2 +- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/backend/file.js b/backend/file.js index 1fcfddb5..f2dacb51 100644 --- a/backend/file.js +++ b/backend/file.js @@ -95,8 +95,8 @@ exports.read = (repo, p) => { return fs.readFileSync(resolvePath(p, repo)) } -exports.write = (p, content) => { - p = resolvePath(p) +exports.write = (repo, p, content) => { + p = resolvePath(p, repo) mkdirPSync(path.dirname(p)) @@ -132,8 +132,8 @@ exports.mv = (oldPath, newPath) => { } } -exports.exists = p => { - return fs.existsSync(resolvePath(p)) +exports.exists = (repo, p) => { + return fs.existsSync(resolvePath(p, repo)) } exports.upload = (file, path) => { diff --git a/backend/main.js b/backend/main.js index e01d1092..24a94d7e 100644 --- a/backend/main.js +++ b/backend/main.js @@ -19,11 +19,11 @@ const fileContent = async (ctx, next) => { if (ctx.method === 'GET') { ctx.body = result('ok', '获取成功', file.read(ctx.query.repo, ctx.query.path).toString()) } else if (ctx.method === 'POST') { - if (ctx.request.body.is_new && file.exists(ctx.request.body.path)) { + if (ctx.request.body.is_new && file.exists(ctx.request.body.repo, ctx.request.body.path)) { throw new Error('文件已经存在') } - file.write(ctx.request.body.path, ctx.request.body.content) + file.write(ctx.request.body.repo, ctx.request.body.path, ctx.request.body.content) ctx.body = result() } else if (ctx.method === 'DELETE') { file.rm(ctx.query.path) diff --git a/frontend/src/components/Tree.vue b/frontend/src/components/Tree.vue index b65358a9..c9322b1d 100644 --- a/frontend/src/components/Tree.vue +++ b/frontend/src/components/Tree.vue @@ -30,6 +30,7 @@ export default { components: { TreeNode, XFilter }, data () { return { + repo: null, tree: null, file: null, showFilter: false @@ -46,13 +47,15 @@ export default { this.$bus.off('switch-repository', this.init) }, methods: { - init (repo) { + init (repo = null) { if (repo) { + this.repo = repo this.tree = null - File.tree(repo, tree => { - this.tree = tree - }) } + + File.tree(this.repo, tree => { + this.tree = tree + }) }, onDelete (path) { // 删除了正在编辑的文件或者其父目录 diff --git a/frontend/src/components/TreeNode.vue b/frontend/src/components/TreeNode.vue index 0c6364f5..0d76ead6 100644 --- a/frontend/src/components/TreeNode.vue +++ b/frontend/src/components/TreeNode.vue @@ -83,7 +83,7 @@ export default { } const path = this.item.path + '/' + filename - File.write(path, `# ${filename.replace(/\.md$/i, '')}\n`, () => { + File.write(this.item.repo, path, `# ${filename.replace(/\.md$/i, '')}\n`, () => { this.$emit('change', path) }, e => { alert(e.message) diff --git a/frontend/src/file.js b/frontend/src/file.js index 14877575..d77b4e00 100644 --- a/frontend/src/file.js +++ b/frontend/src/file.js @@ -72,7 +72,7 @@ export default { }) }) }, - write: (path, content, call, ecall, isNew = false) => { + write: (repo, path, content, call, ecall, isNew = false) => { try { if (path.endsWith('.c.md')) { content = encrypt(content) @@ -88,7 +88,7 @@ export default { fetch('/api/file', { method: 'POST', headers: {'Content-Type': 'application/json'}, - body: JSON.stringify({path, content, is_new: isNew}) + body: JSON.stringify({repo, path, content, is_new: isNew}) }).then(response => { response.json().then(result => { if (result.status === 'ok') { diff --git a/frontend/src/pages/Home.vue b/frontend/src/pages/Home.vue index ad99a269..20a23fdf 100644 --- a/frontend/src/pages/Home.vue +++ b/frontend/src/pages/Home.vue @@ -103,7 +103,7 @@ export default { const content = this.value this.lastSaveContent = content - File.write(file.path, content, () => { + File.write(file.repo, file.path, content, () => { this.status = '保存于:' + (new Date()).toLocaleString() }, e => { this.file = null