浏览代码

:art: fix https://github.com/siyuan-note/siyuan/issues/10091

Vanessa 1 年之前
父节点
当前提交
c6d51b0362
共有 2 个文件被更改,包括 38 次插入42 次删除
  1. 12 42
      app/src/protyle/wysiwyg/keydown.ts
  2. 26 0
      app/src/util/newFile.ts

+ 12 - 42
app/src/protyle/wysiwyg/keydown.ts

@@ -63,9 +63,7 @@ import {countBlockWord} from "../../layout/status";
 import {moveToDown, moveToUp} from "./move";
 import {pasteAsPlainText} from "../util/paste";
 import {preventScroll} from "../scroll/preventScroll";
-import {getSavePath} from "../../util/newFile";
-import {escapeHtml} from "../../util/escape";
-import {insertHTML} from "../util/insertHTML";
+import {getSavePath, newFileBySelect} from "../../util/newFile";
 import {removeSearchMark} from "../toolbar/util";
 import {avKeydown} from "../render/av/keydown";
 import {checkFold} from "../../util/noRelyPCFunction";
@@ -1021,7 +1019,8 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
             return;
         }
 
-        if (matchHotKey(window.siyuan.config.keymap.editor.general.newNameFile.custom, event)) {
+        const isNewNameFile = matchHotKey(window.siyuan.config.keymap.editor.general.newNameFile.custom, event);
+        if (isNewNameFile || matchHotKey(window.siyuan.config.keymap.editor.general.newNameSettingFile.custom, event)) {
             if (!selectText.trim() && (nodeElement.querySelector("tr") || nodeElement.querySelector("span"))) {
                 // 没选中时,都是纯文本就创建子文档 https://ld246.com/article/1663073488381/comment/1664804353295#comments
             } else {
@@ -1030,47 +1029,18 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
                 ) {
                     selectAll(protyle, nodeElement, range);
                 }
-                const newFileName = replaceFileName(selectText.trim() ? selectText.trim() : protyle.lute.BlockDOM2Content(nodeElement.outerHTML).replace(/\n/g, "")) || "Untitled";
-                fetchPost("/api/filetree/getHPathByPath", {
-                    notebook: protyle.notebookId,
-                    path: protyle.path,
-                }, (response) => {
-                    fetchPost("/api/filetree/createDocWithMd", {
+                if (isNewNameFile) {
+                    fetchPost("/api/filetree/getHPathByPath", {
                         notebook: protyle.notebookId,
-                        path: pathPosix().join(response.data, newFileName),
-                        parentID: protyle.block.rootID,
-                        markdown: ""
-                    }, response => {
-                        insertHTML(`<span data-type="block-ref" data-id="${response.data}" data-subtype="d">${escapeHtml(newFileName.substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen))}</span>`,
-                            protyle, false, true);
-                        hideElements(["toolbar"], protyle);
+                        path: protyle.path,
+                    }, (response) => {
+                        newFileBySelect(protyle, selectText, nodeElement, response.data);
                     });
-                });
-            }
-            event.preventDefault();
-            event.stopPropagation();
-            return;
-        }
-
-        if (matchHotKey(window.siyuan.config.keymap.editor.general.newNameSettingFile.custom, event)) {
-            if (!selectText.trim() && (nodeElement.querySelector("tr") || nodeElement.querySelector("span"))) {
-                // 没选中时,都是纯文本就创建子文档 https://ld246.com/article/1663073488381/comment/1664804353295#comments
-            } else {
-                if (!selectText.trim() && getContenteditableElement(nodeElement).textContent) {
-                    selectAll(protyle, nodeElement, range);
-                }
-                getSavePath(protyle.path, protyle.notebookId, (pathString) => {
-                    const newFileName = replaceFileName(selectText.trim() ? selectText.trim() : protyle.lute.BlockDOM2Content(nodeElement.outerHTML).replace(/\n/g, "")) || "Untitled";
-                    fetchPost("/api/filetree/createDocWithMd", {
-                        notebook: protyle.notebookId,
-                        path: pathPosix().join(pathString, newFileName),
-                        parentID: protyle.block.rootID,
-                        markdown: ""
-                    }, response => {
-                        insertHTML(`<span data-type="block-ref" data-id="${response.data}" data-subtype="d">${escapeHtml(newFileName.substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen))}</span>`, protyle);
-                        hideElements(["toolbar"], protyle);
+                } else {
+                    getSavePath(protyle.path, protyle.notebookId, (pathString) => {
+                        newFileBySelect(protyle, selectText, nodeElement, pathString);
                     });
-                });
+                }
             }
             event.preventDefault();
             event.stopPropagation();

+ 26 - 0
app/src/util/newFile.ts

@@ -13,6 +13,8 @@ import {replaceFileName, validateName} from "../editor/rename";
 import {hideElements} from "../protyle/ui/hideElements";
 import {openMobileFileById} from "../mobile/editor";
 import {App} from "../index";
+import {insertHTML} from "../protyle/util/insertHTML";
+import {escapeHtml} from "./escape";
 
 export const getNewFilePath = (useSavePath: boolean) => {
     let notebookId = "";
@@ -192,3 +194,27 @@ export const newFileByName = (app: App, value: string) => {
         name: replaceFileName(value.trim()) || "Untitled"
     });
 };
+
+export const newFileBySelect = (protyle: IProtyle, selectText: string, nodeElement: HTMLElement, pathDir: string) => {
+    const newFileName = replaceFileName(selectText.trim() ? selectText.trim() : protyle.lute.BlockDOM2Content(nodeElement.outerHTML).replace(/\n/g, "")) || "Untitled";
+    const hPath = pathPosix().join(pathDir, newFileName);
+    fetchPost("/api/filetree/getIDsByHPath", {
+        path: hPath,
+        notebook: protyle.notebookId
+    }, (idResponse) => {
+        const refText = escapeHtml(newFileName.substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen));
+        if (idResponse.data && idResponse.data.length > 0) {
+            insertHTML(`<span data-type="block-ref" data-id="${idResponse.data[0]}" data-subtype="d">${refText}</span>`, protyle, false, true);
+        } else {
+            fetchPost("/api/filetree/createDocWithMd", {
+                notebook: protyle.notebookId,
+                path: hPath,
+                parentID: protyle.block.rootID,
+                markdown: ""
+            }, response => {
+                insertHTML(`<span data-type="block-ref" data-id="${response.data}" data-subtype="d">${refText}</span>`, protyle, false, true);
+            });
+        }
+        hideElements(["toolbar"], protyle);
+    });
+};