From cbbedeec90250f1362e02d822804dfdf4a8aa523 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 23 Dec 2019 22:34:38 +0800 Subject: [PATCH] feat: add one-click paste and upload image function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加一键粘贴上传图片功能 --- assets/scripts/editor.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/assets/scripts/editor.js b/assets/scripts/editor.js index 07695dc..a31a513 100644 --- a/assets/scripts/editor.js +++ b/assets/scripts/editor.js @@ -72,10 +72,33 @@ let app = new Vue({ cm.showHint(e); } }); - this.editor.on("change", (cm, change) => { + this.editor.on("change", (cm, e) => { this.refresh(); this.saveEditorContent(this.editor, '__editor_content'); }); + + // 粘贴上传图片并插入 + this.editor.on("paste", (cm, e) => { + if (!(e.clipboardData && e.clipboardData.items)) { + return; + } + for (let i = 0, len = e.clipboardData.items.length; i < len; ++i) { + let item = e.clipboardData.items[i]; + if (item.kind === 'file') { + const pasteFile = item.getAsFile(); + let data = new FormData(); + data.append("file", pasteFile); + axios.post('https://imgkr.com/api/files/upload', data, { + headers: { + 'Content-Type': 'multipart/form-data' + } + }).then(resp => { + this.uploaded(resp.data) + }).catch(err => { + }) + } + } + }); this.cssEditor.on('update', (instance) => { this.cssChanged(); this.saveEditorContent(this.cssEditor, '__css_content');