From e6304e1d249b19c736cc38c7594cbe78aaacdf5f Mon Sep 17 00:00:00 2001 From: Vanessa Date: Mon, 3 Apr 2023 18:07:22 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/siyuan/issues/7860 --- app/appearance/langs/en_US.json | 2 + app/appearance/langs/es_ES.json | 2 + app/appearance/langs/fr_FR.json | 2 + app/appearance/langs/zh_CHT.json | 2 + app/appearance/langs/zh_CN.json | 2 + app/src/assets/scss/business/_search.scss | 7 ++ app/src/search/util.ts | 78 +++++++++++++++++++---- app/src/util/newFile.ts | 77 ++++++++++++---------- 8 files changed, 124 insertions(+), 48 deletions(-) diff --git a/app/appearance/langs/en_US.json b/app/appearance/langs/en_US.json index 420b91712..03fcaed9f 100644 --- a/app/appearance/langs/en_US.json +++ b/app/appearance/langs/en_US.json @@ -1,4 +1,6 @@ { + "enterNew": "Enter to create", + "enterNewTip": "No documents found, Enter to create a new document.", "searchTip1": "to navigate", "searchTip2": "to open", "searchTip3": "to switch to the next hit", diff --git a/app/appearance/langs/es_ES.json b/app/appearance/langs/es_ES.json index 387ee62d4..51615428f 100644 --- a/app/appearance/langs/es_ES.json +++ b/app/appearance/langs/es_ES.json @@ -1,4 +1,6 @@ { + "enterNew": "Ingresar para crear", + "enterNewTip": "No se encontraron documentos, ingrese para crear un nuevo documento.", "searchTip1": "para navegar", "searchTip2": "para abrir", "searchTip3": "para cambiar al siguiente resultado", diff --git a/app/appearance/langs/fr_FR.json b/app/appearance/langs/fr_FR.json index 876714c4a..a78a7e0ab 100644 --- a/app/appearance/langs/fr_FR.json +++ b/app/appearance/langs/fr_FR.json @@ -1,4 +1,6 @@ { + "enterNew": "Entrez pour créer", + "enterNewTip": "Aucun document trouvé, entrez pour créer un nouveau document.", "searchTip1": "pour naviguer", "searchTip2": "pour ouvrir", "searchTip3": "pour passer au résultat suivant", diff --git a/app/appearance/langs/zh_CHT.json b/app/appearance/langs/zh_CHT.json index 27c7e878e..416e66adb 100644 --- a/app/appearance/langs/zh_CHT.json +++ b/app/appearance/langs/zh_CHT.json @@ -1,4 +1,6 @@ { + "enterNew": "回車創建", + "enterNewTip": "搜索結果為空,回車創建新文檔", "searchTip1": "導航", "searchTip2": "打開", "searchTip3": "切換到下一個命中", diff --git a/app/appearance/langs/zh_CN.json b/app/appearance/langs/zh_CN.json index c6cf6aa97..0abb6f1c6 100644 --- a/app/appearance/langs/zh_CN.json +++ b/app/appearance/langs/zh_CN.json @@ -1,4 +1,6 @@ { + "enterNew": "回车创建", + "enterNewTip": "搜索结果为空,回车创建新文档", "searchTip1": "导航", "searchTip2": "打开", "searchTip3": "切换到下一个命中", diff --git a/app/src/assets/scss/business/_search.scss b/app/src/assets/scss/business/_search.scss index 455e03b2c..46b64c1a7 100644 --- a/app/src/assets/scss/business/_search.scss +++ b/app/src/assets/scss/business/_search.scss @@ -185,4 +185,11 @@ margin: 2px 4px 0 16px; } } + + &__empty { + text-align: center; + font-size: 17px; + padding: 32px; + color: var(--b3-theme-on-surface); + } } diff --git a/app/src/search/util.ts b/app/src/search/util.ts index 5b28ce4a2..14fe2bc3f 100644 --- a/app/src/search/util.ts +++ b/app/src/search/util.ts @@ -19,6 +19,10 @@ import {unicode2Emoji} from "../emoji"; import {Dialog} from "../dialog"; import {hasClosestByClassName} from "../protyle/util/hasClosest"; import {setStorageVal, updateHotkeyTip} from "../protyle/util/compatibility"; +import {replaceFileName} from "../editor/rename"; +import {hideElements} from "../protyle/ui/hideElements"; +import {getNewFilePath} from "../util/newFile"; +import {matchHotKey} from "../protyle/util/hotKey"; const appendCriteria = (element: HTMLElement, data: ISearchOption[]) => { fetchPost("/api/storage/getCriteria", {}, (response) => { @@ -92,6 +96,24 @@ export const openGlobalSearch = (text: string, replace: boolean) => { wnd.split("lr").addTab(tab); setPanelFocus(tab.panelElement); }; + +const newEmptyFileByInput = (value: string) => { + const newData = getNewFilePath(true) + fetchPost("/api/filetree/getHPathByPath", { + notebook: newData.notebookId, + path: newData.currentPath, + }, (responsePath) => { + fetchPost("/api/filetree/createDocWithMd", { + notebook: newData.notebookId, + path: pathPosix().join(responsePath.data, replaceFileName(value.trim()) || "Untitled"), + markdown: "" + }, response => { + hideElements(["dialog"]); + openFileById({id: response.data, action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]}); + }); + }); +}; + // closeCB 不存在为页签搜索 export const genSearch = (config: ISearchOption, element: Element, closeCB?: () => void) => { let methodText = window.siyuan.languages.keyword; @@ -529,6 +551,8 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () } else if (target.parentElement.id === "replaceHistoryList") { replaceInputElement.value = target.textContent; replaceHistoryElement.classList.add("fn__none"); + } else if (target.getAttribute("data-type") === "search-new") { + newEmptyFileByInput(searchInputElement.value) } else if (target.getAttribute("data-type") === "search-item") { if (event.detail === 1) { clickTimeout = window.setTimeout(() => { @@ -611,6 +635,35 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () if (!currentList || event.isComposing) { return; } + const focusIsNew = currentList.getAttribute("data-type") === "search-new" + if (focusIsNew && matchHotKey(window.siyuan.config.keymap.general.newFile.custom, event)) { + newEmptyFileByInput(searchInputElement.value); + event.preventDefault(); + event.stopPropagation(); + return; + } + if (event.key === "Enter") { + if (focusIsNew) { + newEmptyFileByInput(searchInputElement.value) + } else { + const id = currentList.getAttribute("data-node-id"); + fetchPost("/api/block/checkBlockFold", {id}, (foldResponse) => { + openFileById({ + id, + action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT], + zoomIn: foldResponse.data + }); + if (closeCB) { + closeCB(); + } + }); + } + event.preventDefault(); + } + + if (focusIsNew) { + return; + } if (event.key === "ArrowDown") { currentList.classList.remove("b3-list-item--focus"); if (!currentList.nextElementSibling) { @@ -663,19 +716,6 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () edit }); event.preventDefault(); - } else if (event.key === "Enter") { - const id = currentList.getAttribute("data-node-id"); - fetchPost("/api/block/checkBlockFold", {id}, (foldResponse) => { - openFileById({ - id, - action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT], - zoomIn: foldResponse.data - }); - if (closeCB) { - closeCB(); - } - }); - event.preventDefault(); } }); replaceInputElement.addEventListener("keydown", (event: KeyboardEvent) => { @@ -1413,5 +1453,15 @@ ${unicode2Emoji(item.ial.icon, false, "b3-list-item__graphic", true)} edit.protyle.element.classList.add("fn__none"); element.querySelector(".search__drag").classList.add("fn__none"); } - element.querySelector("#searchList").innerHTML = resultHTML || `
${window.siyuan.languages.emptyContent}
`; + element.querySelector("#searchList").innerHTML = resultHTML || + `
+ + + ${window.siyuan.languages.newFile} ${(element.querySelector("#searchInput") as HTMLInputElement).value} + + ${window.siyuan.languages.enterNew} +
+
+ ${window.siyuan.languages.enterNewTip} +
`; }; diff --git a/app/src/util/newFile.ts b/app/src/util/newFile.ts index 37486562c..b487f6177 100644 --- a/app/src/util/newFile.ts +++ b/app/src/util/newFile.ts @@ -11,42 +11,38 @@ import {getDisplayName, getOpenNotebookCount, pathPosix} from "./pathName"; import {Constants} from "../constants"; import {validateName} from "../editor/rename"; -export const newFile = (notebookId?: string, currentPath?: string, paths?: string[], useSavePath = false) => { - if (getOpenNotebookCount() === 0) { - showMessage(window.siyuan.languages.newFileTip); - return; - } +export const getNewFilePath = (useSavePath: boolean) => { + let notebookId = "" + let currentPath = "" /// #if !MOBILE - if (!notebookId) { - getAllModels().editor.find((item) => { - const currentElement = item.parent.headElement; - if (currentElement.classList.contains("item--focus")) { - notebookId = item.editor.protyle.notebookId; - if (useSavePath) { - currentPath = item.editor.protyle.path; - } else { - currentPath = pathPosix().dirname(item.editor.protyle.path); - } - if (hasClosestByClassName(currentElement, "layout__wnd--active")) { - return true; - } + getAllModels().editor.find((item) => { + const currentElement = item.parent.headElement; + if (currentElement.classList.contains("item--focus")) { + notebookId = item.editor.protyle.notebookId; + if (useSavePath) { + currentPath = item.editor.protyle.path; + } else { + currentPath = pathPosix().dirname(item.editor.protyle.path); } - }); - if (!notebookId) { - const fileModel = getDockByType("file").data.file; - if (fileModel instanceof Files) { - const currentElement = fileModel.element.querySelector(".b3-list-item--focus"); - if (currentElement) { - const topElement = hasTopClosestByTag(currentElement, "UL"); - if (topElement) { - notebookId = topElement.getAttribute("data-url"); - } - const selectPath = currentElement.getAttribute("data-path"); - if (useSavePath) { - currentPath = selectPath; - } else { - currentPath = pathPosix().dirname(selectPath); - } + if (hasClosestByClassName(currentElement, "layout__wnd--active")) { + return true; + } + } + }); + if (!notebookId) { + const fileModel = getDockByType("file").data.file; + if (fileModel instanceof Files) { + const currentElement = fileModel.element.querySelector(".b3-list-item--focus"); + if (currentElement) { + const topElement = hasTopClosestByTag(currentElement, "UL"); + if (topElement) { + notebookId = topElement.getAttribute("data-url"); + } + const selectPath = currentElement.getAttribute("data-path"); + if (useSavePath) { + currentPath = selectPath; + } else { + currentPath = pathPosix().dirname(selectPath); } } } @@ -61,6 +57,19 @@ export const newFile = (notebookId?: string, currentPath?: string, paths?: strin } }); } + return {notebookId, currentPath} +} + +export const newFile = (notebookId?: string, currentPath?: string, paths?: string[], useSavePath = false) => { + if (getOpenNotebookCount() === 0) { + showMessage(window.siyuan.languages.newFileTip); + return; + } + if (!notebookId) { + const resultData = getNewFilePath(useSavePath) + notebookId = resultData.notebookId; + currentPath = resultData.currentPath; + } fetchPost("/api/filetree/getDocCreateSavePath", {notebook: notebookId}, (data) => { if (data.data.path.indexOf("/") > -1 && useSavePath) { if (data.data.path.startsWith("/") || currentPath === "/") {