diff --git a/src/api/file.js b/src/api/file.js index c68d946..397c16b 100644 --- a/src/api/file.js +++ b/src/api/file.js @@ -1,5 +1,7 @@ import fetch from './fetch'; -import { v4 as uuidv4 } from 'uuid'; +import { + v4 as uuidv4 +} from 'uuid'; const fileUploadConfig = { username: 'filess', repo: 'images', @@ -15,32 +17,41 @@ const fileUploadConfig = { ] } +function getConfiguration() { + const imgHost = localStorage.getItem("ImgHost") || 'default' + + // default + let token = fileUploadConfig.accessToken[Math.floor(Math.random() * fileUploadConfig.accessToken.length)].replace('doocsmd', '') + let username = fileUploadConfig.username + let repo = fileUploadConfig.repo + + // GitHub + if (imgHost === 'github' && localStorage.getItem("GitHubConfig")) { + const githubConfg = JSON.parse(localStorage.getItem("GitHubConfig")) + const repoUrl = githubConfg.repo.replace("https://github.com/", "").replace("http://github.com/", "").replace("github.com/", "").split("/") + token = githubConfg.accessToken + username = repoUrl[0] + repo = repoUrl[1] + } + return { + username, + repo, + token + } +} + 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 dateFilename = new Date().getTime() + '-' + uuid + '.' + filename.split('.')[1]; - 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}`; + const { + username, + repo, + token + } = getConfiguration() + const url = `https://api.github.com/repos/${username}/${repo}/contents/${dir}/${dateFilename}` return fetch({ url, @@ -58,4 +69,4 @@ function fileUpload(content, filename) { export default { fileUpload -}; \ No newline at end of file +}; diff --git a/src/assets/scripts/util.js b/src/assets/scripts/util.js index 18339cf..9273229 100644 --- a/src/assets/scripts/util.js +++ b/src/assets/scripts/util.js @@ -179,7 +179,6 @@ export function css2json(css) { */ export function saveEditorContent(editor, name) { const content = editor.getValue(0) - if (content) { localStorage.setItem(name, content) } else { @@ -198,7 +197,6 @@ export function formatDoc(content) { export function fixCodeWhiteSpace(value = 'pre') { const preDomList = document.getElementsByClassName('code__pre'); - if (preDomList.length > 0) { preDomList.forEach(pre => { pre.style.whiteSpace = value; @@ -217,4 +215,4 @@ export function downLoadMD(doc) { document.body.appendChild(downLink); downLink.click(); document.body.removeChild(downLink); -} \ No newline at end of file +} diff --git a/src/components/CodemirrorEditor/uploadImgDialog.vue b/src/components/CodemirrorEditor/uploadImgDialog.vue index 767dd24..d4cbb45 100644 --- a/src/components/CodemirrorEditor/uploadImgDialog.vue +++ b/src/components/CodemirrorEditor/uploadImgDialog.vue @@ -26,7 +26,6 @@ 将文件拖到此处,或 点击上传 - @@ -35,21 +34,15 @@ ref="form" :model="formGitHub" label-position="right" - label-width="80px" + label-width="100px" > - - - - - + + 如何获取 GitHub token? - 保存配置 @@ -72,10 +65,8 @@ export default { data() { return { formGitHub: { - username: "", repo: "", accessToken: "", - // useCDN: true, }, options: [ { @@ -105,8 +96,17 @@ export default { localStorage.setItem("ImgHost", this.imgHost); }, onSubmit() { - console.log("submit github params:", this.formGitHub); + if (!(this.formGitHub.repo && this.formGitHub.accessToken)) { + const blankElement = this.formGitHub.repo ? "token" : "GitHub 仓库" + this.$message({ + showClose: true, + message: `参数「​${blankElement}」不能为空`, + type: "error", + }); + return; + } localStorage.setItem("GitHubConfig", JSON.stringify(this.formGitHub)); + console.log("submit github params:", this.formGitHub); this.$message({ message: "保存成功", type: "success", @@ -137,8 +137,8 @@ export default { validateConfig() { switch (this.imgHost) { case "github": - const { username, repo, accessToken } = this.formGitHub; - if (!username || !repo || !accessToken) { + const { repo, accessToken } = this.formGitHub; + if (!repo || !accessToken) { this.$message.error("未配置 GitHub 参数"); return false; }