From ab4125319286c471fe8ca65e9dbe71cad5181f25 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Tue, 27 Sep 2022 14:57:25 +0800 Subject: [PATCH] :bug: https://github.com/siyuan-note/siyuan/issues/5962 --- app/src/protyle/upload/index.ts | 5 +++-- app/src/protyle/util/editorCommonEvent.ts | 19 +------------------ app/src/protyle/util/paste.ts | 11 ++++------- 3 files changed, 8 insertions(+), 27 deletions(-) diff --git a/app/src/protyle/upload/index.ts b/app/src/protyle/upload/index.ts index 53b117519..09e7c4168 100644 --- a/app/src/protyle/upload/index.ts +++ b/app/src/protyle/upload/index.ts @@ -124,10 +124,11 @@ const genUploadedLabel = (responseText: string, protyle: IProtyle) => { insertHTML(protyle.lute.SpinBlockDOM(succFileText), protyle); }; -export const uploadLocalFiles = (files: string[], protyle: IProtyle) => { +export const uploadLocalFiles = (files: string[], protyle: IProtyle, isUpload:boolean) => { const msgId = showMessage(window.siyuan.languages.uploading, 0); fetchPost("/api/asset/insertLocalAssets", { assetPaths: files, + isUpload, id: protyle.block.rootID }, (response) => { hideMessage(msgId); @@ -145,7 +146,7 @@ export const uploadFiles = (protyle: IProtyle, files: FileList | DataTransferIte } if (0 === fileItem.size && "" === fileItem.type && -1 === fileItem.name.indexOf(".")) { // 文件夹 - document.execCommand("insertHTML", false, `[${fileItem.name}](file://${fileItem.path})`); + uploadLocalFiles([fileItem.path], protyle, false); } else { fileList.push(fileItem); } diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts index ac87431c8..e3325765f 100644 --- a/app/src/protyle/util/editorCommonEvent.ts +++ b/app/src/protyle/util/editorCommonEvent.ts @@ -706,27 +706,10 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => { focusByRange(document.caretRangeFromPoint(event.clientX, event.clientY)); if (event.dataTransfer.types[0] === "Files") { const files: string[] = []; - let isAllFile = true; for (let i = 0; i < event.dataTransfer.files.length; i++) { files.push(event.dataTransfer.files[i].path); - if (event.dataTransfer.files[i].type === "") { - isAllFile = false; - } - } - if (isAllFile) { - if (event.altKey) { - let fileText = ""; - files.forEach((item) => { - // 拖入文件名包含 `)` 或 `]` 的文件以 `file://` 插入后链接解析错误 https://github.com/siyuan-note/siyuan/issues/5786 - fileText += `[${path.basename(item).replace(/\]/g, "\\]").replace(/\[/g, "\\[")}](file://${item.replace(/\\/g, "\\\\").replace(/\)/g, "\\)").replace(/\(/g, "\\(")})\n`; - }); - insertHTML(protyle.lute.SpinBlockDOM(fileText), protyle); - } else { - uploadLocalFiles(files, protyle); - } - } else { - uploadLocalFiles(files, protyle); } + uploadLocalFiles(files, protyle, !event.altKey); } else { paste(protyle, event); } diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts index 4fd7fcd33..0dd2671ed 100644 --- a/app/src/protyle/util/paste.ts +++ b/app/src/protyle/util/paste.ts @@ -48,11 +48,8 @@ export const pasteAsPlainText = async (protyle:IProtyle) => { } } if (localFiles.length > 0) { - let fileText = ""; - localFiles.forEach((item) => { - fileText += `[${path.basename(item).replace(/\]/g, "\\]").replace(/\[/g, "\\[")}](file://${item.replace(/\\/g, "\\\\").replace(/\)/g, "\\)").replace(/\(/g, "\\(")})\n`; - }); - insertHTML(protyle.lute.SpinBlockDOM(fileText), protyle); + uploadLocalFiles(localFiles, protyle, false); + writeText(""); } else { insertHTML(protyle.lute.BlockDOM2Content(protyle.lute.InlineMd2BlockDOM(clipboard.readText())), protyle, false, false); } @@ -124,7 +121,7 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven localFiles.push(item.childNodes[0].nodeValue); }); if (localFiles.length > 0) { - uploadLocalFiles(localFiles, protyle); + uploadLocalFiles(localFiles, protyle, true); writeText(""); return; } @@ -132,7 +129,7 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven } else { const xmlString = await fetchSyncPost("/api/clipboard/readFilePaths", {}); if (xmlString.data.length > 0) { - uploadLocalFiles(xmlString.data, protyle); + uploadLocalFiles(xmlString.data, protyle, true); writeText(""); return; }