Skip to content

Commit cbbedee

Browse files
committed
feat: add one-click paste and upload image function
添加一键粘贴上传图片功能
1 parent b81e6a4 commit cbbedee

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

assets/scripts/editor.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,33 @@ let app = new Vue({
7272
cm.showHint(e);
7373
}
7474
});
75-
this.editor.on("change", (cm, change) => {
75+
this.editor.on("change", (cm, e) => {
7676
this.refresh();
7777
this.saveEditorContent(this.editor, '__editor_content');
7878
});
79+
80+
// 粘贴上传图片并插入
81+
this.editor.on("paste", (cm, e) => {
82+
if (!(e.clipboardData && e.clipboardData.items)) {
83+
return;
84+
}
85+
for (let i = 0, len = e.clipboardData.items.length; i < len; ++i) {
86+
let item = e.clipboardData.items[i];
87+
if (item.kind === 'file') {
88+
const pasteFile = item.getAsFile();
89+
let data = new FormData();
90+
data.append("file", pasteFile);
91+
axios.post('https://imgkr.com/api/files/upload', data, {
92+
headers: {
93+
'Content-Type': 'multipart/form-data'
94+
}
95+
}).then(resp => {
96+
this.uploaded(resp.data)
97+
}).catch(err => {
98+
})
99+
}
100+
}
101+
});
79102
this.cssEditor.on('update', (instance) => {
80103
this.cssChanged();
81104
this.saveEditorContent(this.cssEditor, '__css_content');

0 commit comments

Comments
 (0)