Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: https://github.com/siyuan-note/siyuan/issues/7860

Vanessae6304e1d249b
diff --git a/app/appearance/langs/en_US.json b/app/appearance/langs/en_US.json
index 420b91712a..03fcaed9f5 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 387ee62d4b..51615428f2 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 876714c4a0..a78a7e0ab1 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 27c7e878e4..416e66adbc 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 c6cf6aa978..0abb6f1c67 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 455e03b2cb..46b64c1a7c 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 5b28ce4a28..14fe2bc3fa 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 || `<div class="b3-list--empty">${window.siyuan.languages.emptyContent}</div>`;
+ element.querySelector("#searchList").innerHTML = resultHTML ||
+ `<div class="b3-list-item b3-list-item--focus" data-type="search-new">
+ <svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg>
+ <span class="b3-list-item__text">
+ ${window.siyuan.languages.newFile} <mark>${(element.querySelector("#searchInput") as HTMLInputElement).value}</mark>
+ </span>
+ <kbd class="b3-list-item__meta">${window.siyuan.languages.enterNew}</kbd>
+</div>
+<div class="search__empty">
+ ${window.siyuan.languages.enterNewTip}
+</div>`;
};
diff --git a/app/src/util/newFile.ts b/app/src/util/newFile.ts
index 37486562c8..b487f61777 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
+ 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;
+ }
+ }
+ });
if (!notebookId) {
- getAllModels().editor.find((item) => {
- const currentElement = item.parent.headElement;
- if (currentElement.classList.contains("item--focus")) {
- notebookId = item.editor.protyle.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 = item.editor.protyle.path;
+ currentPath = selectPath;
} else {
- currentPath = pathPosix().dirname(item.editor.protyle.path);
- }
- 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);
- }
+ 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 === "/") {