An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Move child document creation into the reference menu https://github.com/siyuan-note/siyuan/issues/18494
Danielbcc2b1fdd0ac
diff --git a/app/src/protyle/hint/extend.ts b/app/src/protyle/hint/extend.tsindex 6b77623e4a..134eff1773 100644--- a/app/src/protyle/hint/extend.ts+++ b/app/src/protyle/hint/extend.ts@@ -494,6 +494,7 @@ export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IH}fetchPost("/api/search/searchRefBlock", refParam, (response) => {const dataList: IHintData[] = [];+ let createItemCount = 0;if (response.data.newDoc) {const newFileName = Lute.UnEscapeHTMLStr(replaceFileName(response.data.k));dataList.push({@@ -501,6 +502,15 @@ export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IHhtml: `<div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg><span class="b3-list-item__text">${window.siyuan.languages.newFile} <mark>${response.data.k}</mark></span></div>`,});+ createItemCount++;+ if (source === "search") {+ dataList.push({+ value: `((newSubDoc "${newFileName}"${Constants.ZWSP}'${newFileName}${Lute.Caret}'))`,+ html: `<div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg>+<span class="b3-list-item__text">${window.siyuan.languages.newSubDoc} <mark>${response.data.k}</mark></span></div>`,+ });+ createItemCount++;+ }}response.data.blocks.forEach((item: IBlock) => {let value = `<span data-type="block-ref" data-id="${item.id}" data-subtype="d">${item.name || item.refText.replace(new RegExp(Constants.ZWSP, "g"), "")}</span>`;@@ -527,8 +537,8 @@ export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IHvalue: "",html: window.siyuan.languages.emptyContent,});- } else if (response.data.newDoc && dataList.length > 1) {- dataList[1].focus = true;+ } else if (createItemCount > 0 && dataList.length > createItemCount) {+ dataList[createItemCount].focus = true;}protyle.hint.genHTML(dataList, protyle, true, source);});diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.tsindex 11c6cc4fa4..19b426a125 100644--- a/app/src/protyle/hint/index.ts+++ b/app/src/protyle/hint/index.ts@@ -20,6 +20,7 @@ import {getBlockRefAnchorText,getDocCreateTemplatePath,newFileByRefHint,+ newFileBySelectRange,newFileInProtyle} from "../../util/newFile";import {isAbnormalItem, upDownHint} from "../../util/upDownHint";@@ -280,13 +281,10 @@ ${unicode2Emoji(emoji.unicode)}</button>`;if (this.source !== "hint") {hintsHTML = '<input style="margin:0 8px 4px 8px" class="b3-text-field"><div style="flex: 1;overflow:auto;">';}+ const focusIndex = data.findIndex(item => item.focus);data.forEach((hintData, i) => {// https://github.com/siyuan-note/siyuan/issues/1229 提示时,新建文件不应默认选中- let focusClass = "";- if ((i === 1 && data[i].focus) ||- (i === 0 && (data.length === 1 || !data[1].focus))) {- focusClass = " b3-list-item--focus";- }+ const focusClass = i === (focusIndex > -1 ? focusIndex : 0) ? " b3-list-item--focus" : "";if (hintData.html === "separator") {hintsHTML += `<button data-id="${hintData.id || ""}" class="b3-menu__separator"></button>`;} else {@@ -394,6 +392,12 @@ ${unicode2Emoji(emoji.unicode)}</button>`;const blockRefText = `((newFile "${oldValue}"${Constants.ZWSP}'${response.data.k}${Lute.Caret}'))`;searchHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two${response.data.blocks.length === 0 ? " b3-list-item--focus" : ""}" data-value="${encodeURIComponent(blockRefText)}"><div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg><span class="b3-list-item__text">${window.siyuan.languages.newFile} <mark>${response.data.k}</mark></span></div></button>`;+ if (source === "search") {+ const newFileName = Lute.UnEscapeHTMLStr(response.data.k);+ const subDocRefText = `((newSubDoc "${oldValue}"${Constants.ZWSP}'${newFileName}${Lute.Caret}'))`;+ searchHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two" data-value="${encodeURIComponent(subDocRefText)}"><div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg>+<span class="b3-list-item__text">${window.siyuan.languages.newSubDoc} <mark>${response.data.k}</mark></span></div></button>`;+ }}response.data.blocks.forEach((item: IBlock, index: number) => {let blockRefHTML;@@ -594,6 +598,14 @@ ${genHintItemHTML(item)}range.setStart(range.startContainer, this.lastIndex);focusByRange(range);}+ if (Constants.BLOCK_HINT_KEYS.includes(this.splitChar) && value.startsWith("((newSubDoc ") &&+ value.endsWith(`${Lute.Caret}'))`)) {+ const prefix = "((newSubDoc ";+ const fileNames = value.substring(prefix.length, value.length - 4).split(`"${Constants.ZWSP}'`);+ const realFileName = fileNames.length === 1 ? fileNames[0] : fileNames[1];+ newFileBySelectRange(protyle, range, "subDoc", refIsS ? "s" : "d", realFileName);+ return;+ }// 新建文件if (Constants.BLOCK_HINT_KEYS.includes(this.splitChar) && value.startsWith("((newFile ") && value.endsWith(`${Lute.Caret}'))`)) {const fileNames = value.substring(11, value.length - 4).split(`"${Constants.ZWSP}'`);diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.tsindex 0b62936936..3bfa89b7c4 100644--- a/app/src/protyle/toolbar/index.ts+++ b/app/src/protyle/toolbar/index.ts@@ -154,7 +154,6 @@ export class Toolbar {"inline-memo",] : ["block-ref",- "new-sub-doc-ref","a","|","text",diff --git a/app/src/protyle/toolbar/util.ts b/app/src/protyle/toolbar/util.tsindex 2eec7810e9..3bee202f29 100644--- a/app/src/protyle/toolbar/util.ts+++ b/app/src/protyle/toolbar/util.ts@@ -4,7 +4,6 @@ import {focusByRange, focusByWbr} from "../util/selection";import {writeText} from "../util/compatibility";import {isArrayEqual} from "../../util/functions";import {hasSameTextStyle} from "./Font";-import {newFileBySelectRange} from "../../util/newFile";export const previewTemplate = (pathString: string, element: Element, parentId: string) => {if (!pathString) {@@ -139,15 +138,6 @@ export const toolbarKeyToMenu = (toolbar: Array<string | IMenuItem>) => {lang: "ref",icon: "iconRef",tipPosition: "ne",- }, {- name: "new-sub-doc-ref",- hotkey: window.siyuan.config.keymap.editor.general.newNameFile.custom,- lang: "newSubDocRef",- icon: "iconFile",- tipPosition: "n",- click: (protyle) => {- newFileBySelectRange(protyle.protyle, protyle.protyle.toolbar.range, "subDoc");- },}, {name: "a",hotkey: window.siyuan.config.keymap.editor.insert.link.custom,diff --git a/app/src/protyle/util/Options.ts b/app/src/protyle/util/Options.tsindex 3c9b9e9f2d..795f884727 100644--- a/app/src/protyle/util/Options.ts+++ b/app/src/protyle/util/Options.ts@@ -106,7 +106,6 @@ export class Options {"inline-memo",] : ["block-ref",- "new-sub-doc-ref","a","|","text",diff --git a/app/src/util/newFile.ts b/app/src/util/newFile.tsindex 1084be13ac..141002e88c 100644--- a/app/src/util/newFile.ts+++ b/app/src/util/newFile.ts@@ -91,7 +91,8 @@ export const newFileInTree = (app: App, notebookId: string, currentPath: string,});};-const insertNewFileRef = (protyle: IProtyle, context: NewFileSelectionContext, id: string, refText: string) => {+const insertNewFileRef = (protyle: IProtyle, context: NewFileSelectionContext, id: string, refText: string,+ refSubtype: "d" | "s") => {if (!isNewFileSelectionValid(protyle, context)) {return;}@@ -99,9 +100,10 @@ const insertNewFileRef = (protyle: IProtyle, context: NewFileSelectionContext, iconst currentRange = selection?.rangeCount ? selection.getRangeAt(0).cloneRange() : undefined;const selectionMoved = currentRange && !isSameRange(currentRange, context.range);protyle.toolbar.range = context.range;+ const anchorText = refSubtype === "s" ? getBlockRefAnchorText(context.text) : refText;const refElements = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {type: "id",- color: `${id}${Constants.ZWSP}d${Constants.ZWSP}${refText}`+ color: `${id}${Constants.ZWSP}${refSubtype}${Constants.ZWSP}${anchorText}`}, false);if (!refElements?.[0]) {return;@@ -119,7 +121,7 @@ const insertNewFileRef = (protyle: IProtyle, context: NewFileSelectionContext, i};export const newFileBySelect = (protyle: IProtyle, newFileName: string, context: NewFileSelectionContext,- pathDir: string, targetNotebookId: string) => {+ pathDir: string, targetNotebookId: string, refSubtype: "d" | "s" = "d") => {if (!isNewFileSelectionValid(protyle, context)) {return;}@@ -130,7 +132,7 @@ export const newFileBySelect = (protyle: IProtyle, newFileName: string, context:}, (idResponse) => {const refText = getBlockRefAnchorText(newFileName);if (idResponse.data && idResponse.data.length > 0) {- insertNewFileRef(protyle, context, idResponse.data[0], refText);+ insertNewFileRef(protyle, context, idResponse.data[0], refText, refSubtype);} else {if (!isNewFileSelectionValid(protyle, context)) {return;@@ -142,13 +144,14 @@ export const newFileBySelect = (protyle: IProtyle, newFileName: string, context:markdown: "",titleEmpty: newFileName === "",}, response => {- insertNewFileRef(protyle, context, response.data, refText);+ insertNewFileRef(protyle, context, response.data, refText, refSubtype);});}});};-export const newFileBySelectRange = (protyle: IProtyle, range: Range, target: "subDoc" | "configured") => {+export const newFileBySelectRange = (protyle: IProtyle, range: Range, target: "subDoc" | "configured",+ refSubtype: "d" | "s" = "d", name?: string) => {const nodeElement = hasClosestBlock(range.startContainer);if (!nodeElement) {return;@@ -169,18 +172,19 @@ export const newFileBySelectRange = (protyle: IProtyle, range: Range, target: "s}const sourceNotebookId = selectionContext.notebookId;const sourcePath = selectionContext.path;- const newFileName = replaceFileName(selectText.trim() ? selectText.trim() :- protyle.lute.BlockDOM2Content(nodeElement.outerHTML).replace(/\n/g, "").trim());+ const fileName = name === undefined ? (selectText.trim() ||+ protyle.lute.BlockDOM2Content(nodeElement.outerHTML).replace(/\n/g, "").trim()) : name.trim();+ const newFileName = replaceFileName(fileName);if (target === "subDoc") {fetchPost("/api/filetree/getHPathByPath", {notebook: sourceNotebookId,path: sourcePath,}, (response) => {- newFileBySelect(protyle, newFileName, selectionContext, response.data, sourceNotebookId);+ newFileBySelect(protyle, newFileName, selectionContext, response.data, sourceNotebookId, refSubtype);});} else {getRefCreateSavePath(sourceNotebookId, sourcePath, (targetNotebookId, hPath) => {- newFileBySelect(protyle, newFileName, selectionContext, hPath, targetNotebookId);+ newFileBySelect(protyle, newFileName, selectionContext, hPath, targetNotebookId, refSubtype);});}hideElements(["toolbar"], protyle);