From c6d51b0362eb2738a921d4d1befe03330441d4ff Mon Sep 17 00:00:00 2001 From: Vanessa Date: Fri, 12 Jan 2024 09:43:20 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/10091 --- app/src/protyle/wysiwyg/keydown.ts | 54 +++++++----------------------- app/src/util/newFile.ts | 26 ++++++++++++++ 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts index b80bdd0bc..66992d6ff 100644 --- a/app/src/protyle/wysiwyg/keydown.ts +++ b/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(`${escapeHtml(newFileName.substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen))}`, - protyle, false, true); - hideElements(["toolbar"], protyle); + path: protyle.path, + }, (response) => { + newFileBySelect(protyle, selectText, nodeElement, response.data); + }); + } else { + getSavePath(protyle.path, protyle.notebookId, (pathString) => { + newFileBySelect(protyle, selectText, nodeElement, pathString); }); - }); - } - 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(`${escapeHtml(newFileName.substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen))}`, protyle); - hideElements(["toolbar"], protyle); - }); - }); } event.preventDefault(); event.stopPropagation(); diff --git a/app/src/util/newFile.ts b/app/src/util/newFile.ts index cf5ad2717..db99efc6d 100644 --- a/app/src/util/newFile.ts +++ b/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(`${refText}`, protyle, false, true); + } else { + fetchPost("/api/filetree/createDocWithMd", { + notebook: protyle.notebookId, + path: hPath, + parentID: protyle.block.rootID, + markdown: "" + }, response => { + insertHTML(`${refText}`, protyle, false, true); + }); + } + hideElements(["toolbar"], protyle); + }); +};