Browse Source

:bug: https://github.com/siyuan-note/siyuan/issues/5962

Vanessa 2 years ago
parent
commit
ab41253192

+ 3 - 2
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);
         }

+ 1 - 18
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);
             }

+ 4 - 7
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;
             }