From b8aa26bd898d1030a6c801cda7bcee75a7b99853 Mon Sep 17 00:00:00 2001 From: xiaozzzi <42293085+xiaozzzi@users.noreply.github.com> Date: Thu, 12 Oct 2023 19:45:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=BB=8F=E8=B4=B4=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/views/article/ArticleIndex.vue | 60 +++++++++++++++---- .../src/views/article/scripts/codemirror.ts | 10 +++- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/blossom-editor/src/renderer/src/views/article/ArticleIndex.vue b/blossom-editor/src/renderer/src/views/article/ArticleIndex.vue index aeb9fb6..7497a58 100644 --- a/blossom-editor/src/renderer/src/views/article/ArticleIndex.vue +++ b/blossom-editor/src/renderer/src/views/article/ArticleIndex.vue @@ -477,26 +477,60 @@ const editorLoading = ref(false) // eidtor loading let cmw: CmWrapper // codemirror editor wrapper /** - * 拖拽上传的回调 - * @param event + * 文件上传回调 + * @param event DragEvent | ClipboardEvent */ -const uploadFileCallback = async (event: DragEvent) => { +const uploadFileCallback = async (event: DragEvent | ClipboardEvent) => { if (!curIsArticle()) { return } - let data: DataTransfer | null = event.dataTransfer - if (data && data.files.length && data.files.length > 0) { - for (const file of data.files) { - const form = new FormData() - form.append('file', file) - form.append('name', file.name) - form.append('pid', curArticle.value!.pid.toString()) - uploadFileApi(form).then((resp) => { - cmw.insertBlockCommand(`\n![${file.name}](${resp.data})\n`) - }) + /** + * 拖拽上传 + */ + if (event instanceof DragEvent) { + let data: DataTransfer | null = event.dataTransfer + if (data && data.files.length && data.files.length > 0) { + for (const file of data.files) { + const form = new FormData() + form.append('file', file) + form.append('name', file.name) + form.append('pid', curArticle.value!.pid.toString()) + uploadFileApi(form).then((resp) => { + cmw.insertBlockCommand(`\n![${file.name}](${resp.data})\n`) + }) + } } } + + /** + * 黏贴上传 + */ + if (event instanceof ClipboardEvent) { + if (!event.clipboardData) { + return + } + const dataTransferItemList: DataTransferItemList = event.clipboardData?.items + // 过滤非图片类型 + const items: DataTransferItem[] = [].slice.call(dataTransferItemList).filter((item: DataTransferItem) => { + return item.type.indexOf('image') !== -1 + }) + if (items.length === 0) { + return + } + const dataTransferItem: DataTransferItem = items[0] + const file: File | null = dataTransferItem.getAsFile() + if (file == null) { + return + } + const form = new FormData() + form.append('file', file) + form.append('name', file.name) + form.append('pid', curArticle.value!.pid.toString()) + uploadFileApi(form).then((resp) => { + cmw.insertBlockCommand(`\n![${file.name}](${resp.data})\n`) + }) + } } /** diff --git a/blossom-editor/src/renderer/src/views/article/scripts/codemirror.ts b/blossom-editor/src/renderer/src/views/article/scripts/codemirror.ts index a5020b1..4699371 100644 --- a/blossom-editor/src/renderer/src/views/article/scripts/codemirror.ts +++ b/blossom-editor/src/renderer/src/views/article/scripts/codemirror.ts @@ -11,6 +11,7 @@ import * as prettier from 'prettier/standalone' import pluginMarkdown from 'prettier/plugins/markdown' import { isBlank } from '@renderer/assets/utils/obj' +import { log } from 'console' const { editorStyle } = useConfigStore() @@ -297,8 +298,13 @@ export class CmWrapper { } }), EditorView.domEventHandlers({ - drop(a: DragEvent) { - uploadFileCallback(a) + drop(event: DragEvent) { + console.log(event) + uploadFileCallback(event) + return + }, + paste(event: ClipboardEvent) { + uploadFileCallback(event) return } })