diff --git a/app/src/protyle/header/Title.ts b/app/src/protyle/header/Title.ts index d4e7e0dc0..87f913f40 100644 --- a/app/src/protyle/header/Title.ts +++ b/app/src/protyle/header/Title.ts @@ -91,7 +91,7 @@ export class Title { event.stopPropagation(); event.preventDefault(); }); - this.editElement.addEventListener("keydown", (event: KeyboardEvent) => { + this.editElement.addEventListener("keydown", async (event: KeyboardEvent) => { if (event.isComposing) { return; } @@ -100,7 +100,10 @@ export class Title { return true; } if (matchHotKey("⇧⌘V", event)) { - navigator.clipboard.readText().then(textPlain => { + event.preventDefault(); + event.stopPropagation(); + let textPlain = await readText() || ""; + if (textPlain) { // 对 HTML 标签进行内部转义,避免被 Lute 解析以后变为小写 https://github.com/siyuan-note/siyuan/issues/10620 textPlain = textPlain.replace(//g, ";;;gt;;;"); enableLuteMarkdownSyntax(protyle); @@ -110,9 +113,8 @@ export class Title { content = content.replace(/;;;lt;;;[^;]+;;;gt;;;/g, ""); document.execCommand("insertText", false, replaceFileName(content)); this.rename(protyle); - }); - event.preventDefault(); - event.stopPropagation(); + } + return; } if (matchHotKey(window.siyuan.config.keymap.general.enterBack.custom, event)) { const ids = protyle.path.split("/"); @@ -255,7 +257,7 @@ export class Title { document.execCommand("paste"); } else { try { - const text = await readText(); + const text = await readText() || ""; document.execCommand("insertText", false, replaceFileName(text)); this.rename(protyle); } catch (e) { @@ -269,16 +271,15 @@ export class Title { label: window.siyuan.languages.pasteAsPlainText, accelerator: "⇧⌘V", click: async () => { - navigator.clipboard.readText().then(textPlain => { - textPlain = textPlain.replace(//g, ";;;gt;;;"); - enableLuteMarkdownSyntax(protyle); - let content = protyle.lute.BlockDOM2EscapeMarkerContent(protyle.lute.Md2BlockDOM(textPlain)); - restoreLuteMarkdownSyntax(protyle); - // 移除 ;;;lt;;; 和 ;;;gt;;; 转义及其包裹的内容 - content = content.replace(/;;;lt;;;[^;]+;;;gt;;;/g, ""); - document.execCommand("insertText", false, replaceFileName(content)); - this.rename(protyle); - }); + let textPlain = await readText() || ""; + textPlain = textPlain.replace(//g, ";;;gt;;;"); + enableLuteMarkdownSyntax(protyle); + let content = protyle.lute.BlockDOM2EscapeMarkerContent(protyle.lute.Md2BlockDOM(textPlain)); + restoreLuteMarkdownSyntax(protyle); + // 移除 ;;;lt;;; 和 ;;;gt;;; 转义及其包裹的内容 + content = content.replace(/;;;lt;;;[^;]+;;;gt;;;/g, ""); + document.execCommand("insertText", false, replaceFileName(content)); + this.rename(protyle); } }).element); window.siyuan.menus.menu.append(new MenuItem({ diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts index 75bd37c02..6ccd02d1f 100644 --- a/app/src/protyle/util/compatibility.ts +++ b/app/src/protyle/util/compatibility.ts @@ -98,7 +98,9 @@ export const readText = () => { } else if (isInHarmony()) { return window.JSHarmony.readClipboard(); } - return navigator.clipboard.readText(); + return navigator.clipboard.readText().catch((err) => { + alert(err); + }) || ""; }; /// #if !BROWSER diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts index c3f3549e6..3c5a65c08 100644 --- a/app/src/protyle/util/paste.ts +++ b/app/src/protyle/util/paste.ts @@ -100,7 +100,7 @@ export const getPlainText = (blockElement: HTMLElement, isNested = false) => { export const pasteEscaped = async (protyle: IProtyle, nodeElement: Element) => { try { - let clipText = await readText(); + let clipText = await readText() || ""; // 删掉 text 标签,只保留文本 clipText = clipText.replace(/(.*?)<\/span>/g, "$1"); @@ -150,7 +150,7 @@ export const pasteAsPlainText = async (protyle: IProtyle) => { /// #endif if (localFiles.length === 0) { // Inline-level elements support pasted as plain text https://github.com/siyuan-note/siyuan/issues/8010 - let textPlain = await readText(); + let textPlain = await readText() || ""; if (getSelection().rangeCount > 0) { const range = getSelection().getRangeAt(0); if (hasClosestByAttribute(range.startContainer, "data-type", "code") || hasClosestByClassName(range.startContainer, "hljs")) {