From 33dfa0de4e6c32712db0f283f316a7167b2d3ccd Mon Sep 17 00:00:00 2001 From: Vanessa Date: Fri, 20 Oct 2023 11:45:39 +0800 Subject: [PATCH] :heavy_plus_sign: fix https://github.com/siyuan-note/siyuan/issues/9452 --- app/src/plugin/EventBus.ts | 2 +- app/src/protyle/util/paste.ts | 39 +++++++++++++++++++++++++++++++---- app/src/types/index.d.ts | 1 + 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/app/src/plugin/EventBus.ts b/app/src/plugin/EventBus.ts index 86a0051bb..09e2a956c 100644 --- a/app/src/plugin/EventBus.ts +++ b/app/src/plugin/EventBus.ts @@ -20,7 +20,7 @@ export class EventBus { } emit(type: TEventBus, detail?: DetailType) { - return this.eventTarget.dispatchEvent(new CustomEvent(type, {detail})); + return this.eventTarget.dispatchEvent(new CustomEvent(type, {detail, cancelable: true})); } } diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts index 6a1fefeed..1cb1cbed8 100644 --- a/app/src/protyle/util/paste.ts +++ b/app/src/protyle/util/paste.ts @@ -125,10 +125,10 @@ export const pasteText = (protyle: IProtyle, textPlain: string, nodeElement: Ele export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEvent) & { target: HTMLElement }) => { event.stopPropagation(); event.preventDefault(); - let textHTML; - let textPlain; - let siyuanHTML; - let files; + let textHTML: string; + let textPlain: string; + let siyuanHTML: string; + let files: FileList | DataTransferItemList; if ("clipboardData" in event) { textHTML = event.clipboardData.getData("text/html"); textPlain = event.clipboardData.getData("text/plain"); @@ -194,6 +194,37 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven textHTML = Lute.Sanitize(textHTML); } + if (protyle && protyle.app && protyle.app.plugins) { + for (let i = 0; i < protyle.app.plugins.length; i++) { + const response: IObject & { files: FileList } = await new Promise((resolve) => { + const emitResult = protyle.app.plugins[i].eventBus.emit("paste", { + protyle, + resolve, + textHTML, + textPlain, + siyuanHTML, + files + }); + if (emitResult) { + resolve(undefined); + } + }); + + if (response?.textHTML) { + textHTML = response.textHTML; + } + if (response?.textPlain) { + textPlain = response.textPlain; + } + if (response?.siyuanHTML) { + siyuanHTML = response.siyuanHTML; + } + if (response?.files) { + files = response.files as FileList; + } + } + } + const nodeElement = hasClosestBlock(event.target); if (!nodeElement) { if (files && files.length > 0) { diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index 99c245866..db6f3cb37 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -49,6 +49,7 @@ type TEventBus = "ws-main" | "open-menu-blockref" | "open-menu-fileannotationref" | "open-menu-tag" | "open-menu-link" | "open-menu-image" | "open-menu-av" | "open-menu-content" | "open-menu-breadcrumbmore" | "open-menu-doctree" | "open-siyuan-url-plugin" | "open-siyuan-url-block" | + "paste" | "input-search" | "loaded-protyle" | "loaded-protyle-dynamic" | "destroy-protyle"