From f9c7d14a3fbbbd595558ee0fab918f44663c1390 Mon Sep 17 00:00:00 2001 From: hlw Date: Sun, 30 Aug 2020 20:21:15 +0800 Subject: [PATCH] feat: allow custom img host --- src/api/file.js | 24 ++- src/components/CodemirrorEditor/header.vue | 25 +-- .../CodemirrorEditor/rightClickMenu.vue | 29 +-- .../CodemirrorEditor/uploadImgDialog.vue | 177 ++++++++++++++++++ src/view/CodemirrorEditor.vue | 12 +- 5 files changed, 213 insertions(+), 54 deletions(-) create mode 100644 src/components/CodemirrorEditor/uploadImgDialog.vue diff --git a/src/api/file.js b/src/api/file.js index 250ecd1..c68d946 100644 --- a/src/api/file.js +++ b/src/api/file.js @@ -19,10 +19,28 @@ const fileUploadConfig = { function fileUpload(content, filename) { const date = new Date(); const dir = date.getFullYear() + '/' + (date.getMonth() + 1).toString().padStart(2, '0') + '/' + date.getDate().toString().padStart(2, '0'); - const uuid = uuidv4(); - const token = fileUploadConfig.accessToken[Math.floor(Math.random() * fileUploadConfig.accessToken.length)].replace('doocsmd', ''); + const uuid = uuidv4(); const dateFilename = new Date().getTime() + '-' + uuid + '.' + filename.split('.')[1]; - const url = `https://api.github.com/repos/${fileUploadConfig.username}/${fileUploadConfig.repo}/contents/${dir}/${dateFilename}`; + const imgHost = localStorage.getItem("ImgHost") || 'default' + + let token = '' + let username = '' + let repo = '' + + if (imgHost === 'default') { + token = fileUploadConfig.accessToken[Math.floor(Math.random() * fileUploadConfig.accessToken.length)].replace('doocsmd', ''); + username = fileUploadConfig.username + repo = fileUploadConfig.repo + } + + if (imgHost === 'github' && localStorage.getItem("GitHubConfig")) { + const githubConfg = JSON.parse(localStorage.getItem("GitHubConfig")); + token = githubConfg.accessToken + username = githubConfg.username + repo = githubConfg.repo + } + + const url = `https://api.github.com/repos/${username}/${repo}/contents/${dir}/${dateFilename}`; return fetch({ url, diff --git a/src/components/CodemirrorEditor/header.vue b/src/components/CodemirrorEditor/header.vue index bccd457..24082c8 100644 --- a/src/components/CodemirrorEditor/header.vue +++ b/src/components/CodemirrorEditor/header.vue @@ -1,14 +1,9 @@