Skip to content

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

:art: Hide redundant document creation options https://github.com/siyuan-note/siyuan/issues/18506

Daniel79eab8eed48e
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index 4d625f6850..94c810be3c 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -291,7 +291,7 @@ const renderSlashMenu = (protyle: IProtyle, toolbarElement: Element) => {
${getSlashItem("{{", "iconSQL", window.siyuan.languages.blockEmbed, "true")}
${getSlashItem(Constants.ZWSP + 5, "iconSparkles", window.siyuan.languages.aiWriting)}
${getSlashItem('<div data-type="NodeAttributeView" data-av-type="table"></div>', "iconDatabase", window.siyuan.languages.database, "true")}
- ${getSlashItem(Constants.ZWSP + 4, "iconFile", window.siyuan.languages.newSubDocRef)}
+ ${getSlashItem(Constants.ZWSP + 6, "iconFile", window.siyuan.languages.newSubDocRef)}
</div>
<div class="keyboard__slash-title"></div>
<div class="keyboard__slash-block">
diff --git a/app/src/protyle/hint/extend.ts b/app/src/protyle/hint/extend.ts
index 38234a9b88..0ec006039a 100644
--- a/app/src/protyle/hint/extend.ts
+++ b/app/src/protyle/hint/extend.ts
@@ -30,7 +30,8 @@ const getHotkeyOrMarker = (hotkey: string, marker: string) => {
return "";
};
-export const hintSlash = (key: string, protyle: IProtyle) => {
+export const hintSlash = (key: string, protyle: IProtyle, sourceOrHideConfiguredCreate: THintSource | boolean = false) => {
+ const hideConfiguredCreate = typeof sourceOrHideConfiguredCreate === "boolean" && sourceOrHideConfiguredCreate;
const allList: IHintData[] = [{
filter: [window.siyuan.languages.template, "template", "模板", "moban", "muban", "mb"],
id: "template",
@@ -382,10 +383,11 @@ export const hintSlash = (key: string, protyle: IProtyle) => {
if (!hasPlugin) {
allList.pop();
}
+ const visibleList = hideConfiguredCreate ? allList.filter((item) => item.id !== "newFileRef") : allList;
if (key === "") {
- return allList;
+ return visibleList;
}
- return allList.filter((item) => {
+ return visibleList.filter((item) => {
if (!item.filter) {
return false;
}
@@ -472,6 +474,7 @@ export const genHintItemHTML = (item: IBlock) => {
export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IHintData[] => {
const nodeElement = hasClosestBlock(getEditorRange(protyle.wysiwyg.element).startContainer);
+ const createTarget = protyle.hint.prepareCreateTarget(protyle, "ref");
protyle.hint.genLoading(protyle);
let refParam: IObject;
if (protyle.lite) {
@@ -493,52 +496,59 @@ export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IH
refParam.notebook = protyle.notebookId;
}
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({
- value: `((newFile "${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>
+ createTarget.promise.then((hideConfiguredCreate) => {
+ if (!createTarget.isCurrent()) {
+ return;
+ }
+ const dataList: IHintData[] = [];
+ let createItemCount = 0;
+ if (response.data.newDoc) {
+ const newFileName = Lute.UnEscapeHTMLStr(replaceFileName(response.data.k));
+ if (!hideConfiguredCreate) {
+ dataList.push({
+ value: `((newFile "${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.newFile} <mark>${response.data.k}</mark></span></div>`,
- });
- createItemCount++;
- 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>
+ });
+ createItemCount++;
+ }
+ 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>`;
+ if (source === "search") {
+ value = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${key}${Constants.ZWSP}${item.name || item.refText.replace(new RegExp(Constants.ZWSP, "g"), "")}</span>`;
+ } else if (source === "av") {
+ let refText = item.name || item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
+ if (nodeElement) {
+ refText = item.ial["custom-sy-av-s-text-" + nodeElement.getAttribute("data-av-id")] || refText;
+ }
+ value = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${refText}</span>`;
+ }
+ dataList.push({
+ value,
+ html: genHintItemHTML(item),
+ });
});
- 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>`;
if (source === "search") {
- value = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${key}${Constants.ZWSP}${item.name || item.refText.replace(new RegExp(Constants.ZWSP, "g"), "")}</span>`;
- } else if (source === "av") {
- let refText = item.name || item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
- if (nodeElement) {
- refText = item.ial["custom-sy-av-s-text-" + nodeElement.getAttribute("data-av-id")] || refText;
- }
- value = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${refText}</span>`;
+ protyle.hint.splitChar = "((";
+ protyle.hint.lastIndex = -1;
}
- dataList.push({
- value,
- html: genHintItemHTML(item),
- });
+ if (dataList.length === 0) {
+ dataList.push({
+ value: "",
+ html: window.siyuan.languages.emptyContent,
+ });
+ } else if (createItemCount > 0 && dataList.length > createItemCount) {
+ dataList[createItemCount].focus = true;
+ }
+ protyle.hint.genHTML(dataList, protyle, true, source);
});
- if (source === "search") {
- protyle.hint.splitChar = "((";
- protyle.hint.lastIndex = -1;
- }
- if (dataList.length === 0) {
- dataList.push({
- value: "",
- html: window.siyuan.languages.emptyContent,
- });
- } else if (createItemCount > 0 && dataList.length > createItemCount) {
- dataList[createItemCount].focus = true;
- }
- protyle.hint.genHTML(dataList, protyle, true, source);
});
return [];
};
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index 0583e7b5c1..571edb0347 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -19,6 +19,7 @@ import {genHintItemHTML, hintEmbed, hintRef, hintSlash} from "./extend";
import {
getBlockRefAnchorText,
getDocCreateTemplatePath,
+ isConfiguredCreateTargetCurrentSubDoc,
newFileByRefHint,
newFileBySelectRange,
newFileInProtyle,
@@ -67,6 +68,21 @@ const genEmojiInsertHTML = (value: string) => {
return unicode2Emoji(value, kind === "dynamic" || kind === "network" ? "emoji" : "") + " ";
};
+type TCreateTargetType = "doc" | "ref";
+
+type TCreateTargetState = {
+ promise: Promise<boolean>;
+ result?: boolean;
+};
+
+type TCreateTargetSession = {
+ id: number;
+ notebookId: string;
+ path: string;
+ renderID: number;
+ targets: Partial<Record<TCreateTargetType, TCreateTargetState>>;
+};
+
export class Hint {
public timeId: number;
public element: HTMLDivElement;
@@ -76,6 +92,9 @@ export class Hint {
public splitChar = "";
public lastIndex = -1;
private source: THintSource;
+ private createTargetSessionID = 0;
+ private createTargetRenderID = 0;
+ private createTargetSession?: TCreateTargetSession;
constructor(protyle: IProtyle) {
this.element = document.createElement("div");
@@ -139,6 +158,44 @@ ${unicode2Emoji(emoji.unicode)}</button>`;
});
}
+ public prepareCreateTarget(protyle: IProtyle, type: TCreateTargetType) {
+ const notebookId = protyle.notebookId || "";
+ const path = protyle.path || "";
+ if (this.element.classList.contains("fn__none") || !this.createTargetSession ||
+ this.createTargetSession.notebookId !== notebookId || this.createTargetSession.path !== path) {
+ this.createTargetSession = {
+ id: ++this.createTargetSessionID,
+ notebookId,
+ path,
+ renderID: 0,
+ targets: {},
+ };
+ }
+ const session = this.createTargetSession;
+ const renderID = ++this.createTargetRenderID;
+ session.renderID = renderID;
+ let targetState = session.targets[type];
+ if (!targetState) {
+ const sessionID = session.id;
+ targetState = {
+ promise: isConfiguredCreateTargetCurrentSubDoc(protyle, type).catch(() => false),
+ };
+ session.targets[type] = targetState;
+ targetState.promise.then((result) => {
+ if (this.createTargetSession?.id === sessionID) {
+ targetState.result = result;
+ }
+ });
+ }
+ return {
+ result: targetState.result,
+ promise: targetState.promise,
+ isCurrent: () => this.createTargetSession === session && session.renderID === renderID &&
+ session.notebookId === protyle.notebookId && session.path === protyle.path &&
+ !this.element.classList.contains("fn__none"),
+ };
+ }
+
public render(protyle: IProtyle) {
if (!window.getSelection().focusNode) {
this.element.classList.add("fn__none");
@@ -217,7 +274,17 @@ ${unicode2Emoji(emoji.unicode)}</button>`;
} else {
const blockElement = hasClosestBlock(protyle.toolbar.range.startContainer);
if (this.enableSlash && !isMobile() && blockElement && !isInEmbedBlock(blockElement)) {
- this.genHTML(hintSlash(key, protyle), protyle, false, "hint");
+ const createTarget = this.prepareCreateTarget(protyle, "doc");
+ if (createTarget.result !== undefined) {
+ this.genHTML(hintSlash(key, protyle, createTarget.result), protyle, false, "hint");
+ } else {
+ this.genLoading(protyle);
+ createTarget.promise.then((isCurrentSubDoc) => {
+ if (createTarget.isCurrent()) {
+ this.genHTML(hintSlash(key, protyle, isCurrentSubDoc), protyle, false, "hint");
+ }
+ });
+ }
}
}
return;
@@ -255,7 +322,7 @@ ${unicode2Emoji(emoji.unicode)}</button>`;
setPosition(this.element, 0, 0);
/// #endif
}
- } else {
+ } else if (!this.element.querySelector(".fn__loading")) {
this.element.insertAdjacentHTML("beforeend", '<div class="fn__loading"><img width="64px" src="/stage/loading-pure.svg"></div>');
}
}
@@ -376,6 +443,7 @@ ${unicode2Emoji(emoji.unicode)}</button>`;
}
private genSearchHTML(protyle: IProtyle, searchElement: HTMLInputElement, nodeElement: false | HTMLElement, oldValue: string, source: THintSource) {
+ const createTarget = this.prepareCreateTarget(protyle, "ref");
this.element.lastElementChild.innerHTML = '<div class="ft__center"><img style="height:32px;width:32px;" src="/stage/loading-pure.svg"></div>';
const searchParam: IObject = {
k: searchElement.value,
@@ -388,37 +456,44 @@ ${unicode2Emoji(emoji.unicode)}</button>`;
searchParam.notebook = protyle.notebookId;
}
fetchPost("/api/search/searchRefBlock", searchParam, (response) => {
- let searchHTML = "";
- if (response.data.newDoc) {
- 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>
+ createTarget.promise.then((hideConfiguredCreate) => {
+ if (!createTarget.isCurrent()) {
+ return;
+ }
+ let searchHTML = "";
+ if (response.data.newDoc && !hideConfiguredCreate) {
+ 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>`;
- 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>
+ }
+ if (response.data.newDoc) {
+ 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${hideConfiguredCreate && response.data.blocks.length === 0 ? " b3-list-item--focus" : ""}" 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;
- if (source === "av") {
- // av 搜索时需要获取值 https://github.com/siyuan-note/siyuan/issues/12020
- let refText = item.name || item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
- if (nodeElement) {
- refText = item.ial["custom-sy-av-s-text-" + nodeElement.getAttribute("data-av-id")] || refText;
- }
- blockRefHTML = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${refText}</span>`;
- } else {
- blockRefHTML = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${oldValue}</span>`;
}
- searchHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two${index === 0 ? " b3-list-item--focus" : ""}" data-value="${encodeURIComponent(blockRefHTML)}">
+ response.data.blocks.forEach((item: IBlock, index: number) => {
+ let blockRefHTML;
+ if (source === "av") {
+ // av 搜索时需要获取值 https://github.com/siyuan-note/siyuan/issues/12020
+ let refText = item.name || item.refText.replace(new RegExp(Constants.ZWSP, "g"), "");
+ if (nodeElement) {
+ refText = item.ial["custom-sy-av-s-text-" + nodeElement.getAttribute("data-av-id")] || refText;
+ }
+ blockRefHTML = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${refText}</span>`;
+ } else {
+ blockRefHTML = `<span data-type="block-ref" data-id="${item.id}" data-subtype="s">${oldValue}</span>`;
+ }
+ searchHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two${index === 0 ? " b3-list-item--focus" : ""}" data-value="${encodeURIComponent(blockRefHTML)}">
${genHintItemHTML(item)}
</button>`;
+ });
+ if (searchHTML === "") {
+ searchHTML = `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two" data-value="">${window.siyuan.languages.emptyContent}</button>`;
+ }
+ this.element.lastElementChild.innerHTML = searchHTML;
+ setPosition(this.element, parseInt(this.element.style.left), parseInt(this.element.style.right));
});
- if (searchHTML === "") {
- searchHTML = `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two" data-value="">${window.siyuan.languages.emptyContent}</button>`;
- }
- this.element.lastElementChild.innerHTML = searchHTML;
- setPosition(this.element, parseInt(this.element.style.left), parseInt(this.element.style.right));
});
}
diff --git a/app/src/util/newFile.ts b/app/src/util/newFile.ts
index 47666079e4..1670e1d5da 100644
--- a/app/src/util/newFile.ts
+++ b/app/src/util/newFile.ts
@@ -6,14 +6,20 @@ import {Editor} from "../editor";
import {openFileById} from "../editor/util";
import {getActiveTab, getDockByType} from "../layout/tabUtil";
/// #endif
-import {fetchPost} from "./fetch";
+import {fetchPost, fetchSyncPost} from "./fetch";
import {getDisplayName, getOpenNotebookCount, pathPosix} from "./pathName";
import {Constants} from "../constants";
import {replaceFileName, validateName} from "../editor/rename";
import {hideElements} from "../protyle/ui/hideElements";
import {openMobileFileById} from "../mobile/editor";
import type {App} from "../index";
-import {NewDocTargetByHPath, NewDocTargetSubDoc, getNewDocTargetFromSavePath, getNewDocTargetFromTree} from "./parseNewDocTarget";
+import {
+ NewDocTargetByHPath,
+ NewDocTargetSubDoc,
+ getNewDocTargetFromSavePath,
+ getNewDocTargetFromTree,
+ isCurrentDocSubDocTarget
+} from "./parseNewDocTarget";
import {focusByRange, selectAll} from "../protyle/util/selection";
import {
createNewFileSelectionContext,
@@ -291,6 +297,55 @@ function getNewDocHPath(targetNotebookId: string, currentNotebookId: string, cur
});
}
+/** 判断配置的新建位置是否与在当前文档下直接创建子文档等价 */
+export const isConfiguredCreateTargetCurrentSubDoc = async (
+ protyle: IProtyle,
+ type: "doc" | "ref",
+) => {
+ if (!protyle.notebookId || !protyle.path) {
+ return false;
+ }
+ const savePathResponse = await fetchSyncPost(
+ type === "doc" ? "/api/filetree/getDocCreateSavePath" : "/api/filetree/getRefCreateSavePath",
+ {notebook: protyle.notebookId},
+ );
+ if (savePathResponse.code !== 0 || !savePathResponse.data) {
+ return false;
+ }
+ const targetNotebookId = savePathResponse.data.box as string;
+ if (targetNotebookId !== protyle.notebookId) {
+ return false;
+ }
+ const templatePath = (savePathResponse.data.path as string || "").trim();
+ if (!templatePath) {
+ return true;
+ }
+ const hPathResponse = await fetchSyncPost("/api/filetree/getHPathByPath", {
+ notebook: protyle.notebookId,
+ path: protyle.path,
+ });
+ if (hPathResponse.code !== 0 || typeof hPathResponse.data !== "string") {
+ return false;
+ }
+ const title = type === "ref" ? "__siyuan_ref_target__" : "";
+ const target = getNewDocTargetFromSavePath({
+ templatePath,
+ hPath: hPathResponse.data || "/",
+ targetNotebookId,
+ currentNotebookId: protyle.notebookId,
+ name: title || undefined,
+ hasFocusTarget: true,
+ currentPath: protyle.path,
+ });
+ return isCurrentDocSubDocTarget({
+ target,
+ currentNotebookId: protyle.notebookId,
+ currentPath: protyle.path,
+ currentHPath: hPathResponse.data || "/",
+ title,
+ });
+};
+
function createNewDoc(request: NewDocRequest, templatePath: string, docCreateTemplatePath: string,
targetNotebookId: string, hPath: string) {
const target = getNewDocTargetFromSavePath({
diff --git a/app/src/util/parseNewDocTarget.test.ts b/app/src/util/parseNewDocTarget.test.ts
index 169ffe4d37..bd0a31d200 100644
--- a/app/src/util/parseNewDocTarget.test.ts
+++ b/app/src/util/parseNewDocTarget.test.ts
@@ -1,6 +1,11 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {getNewDocTargetFromSavePath, getNewDocTargetFromTree, NewDocTarget} from "./parseNewDocTarget";
+import {
+ getNewDocTargetFromSavePath,
+ getNewDocTargetFromTree,
+ isCurrentDocSubDocTarget,
+ NewDocTarget
+} from "./parseNewDocTarget";
const assertSubDoc = (target: NewDocTarget, expected: {
targetNotebookId?: string;
@@ -289,6 +294,74 @@ describe("getNewDocTargetFromSavePath", () => {
});
});
+describe("isCurrentDocSubDocTarget", () => {
+ const currentNotebookId = "nb";
+ const currentPath = "/20260628041644-ndcuikw/20260628040939-kkaajwr.sy";
+ const currentHPath = "/parent1/parent2/docName";
+
+ const isCurrentSubDoc = (target: NewDocTarget, title: string) => isCurrentDocSubDocTarget({
+ target,
+ currentNotebookId,
+ currentPath,
+ currentHPath,
+ title,
+ });
+
+ it("直接创建当前文档的空标题子文档", () => {
+ assert.equal(isCurrentSubDoc({
+ kind: "subDoc",
+ targetNotebookId: currentNotebookId,
+ parentPath: currentPath,
+ title: "",
+ }, ""), true);
+ });
+
+ it("配置路径解析为当前文档下的命名子文档", () => {
+ assert.equal(isCurrentSubDoc({
+ kind: "hPath",
+ targetNotebookId: currentNotebookId,
+ hPath: currentHPath + "/target",
+ title: "target",
+ }, "target"), true);
+ });
+
+ it("空标题的容器路径解析为当前文档", () => {
+ assert.equal(isCurrentSubDoc({
+ kind: "hPath",
+ targetNotebookId: currentNotebookId,
+ hPath: currentHPath + "/",
+ title: "",
+ }, ""), true);
+ });
+
+ it("标题不同时不等价", () => {
+ assert.equal(isCurrentSubDoc({
+ kind: "hPath",
+ targetNotebookId: currentNotebookId,
+ hPath: currentHPath + "/configured-title",
+ title: "configured-title",
+ }, ""), false);
+ });
+
+ it("父路径不是当前文档时不等价", () => {
+ assert.equal(isCurrentSubDoc({
+ kind: "hPath",
+ targetNotebookId: currentNotebookId,
+ hPath: currentHPath + "/child/target",
+ title: "target",
+ }, "target"), false);
+ });
+
+ it("目标笔记本不同时不等价", () => {
+ assert.equal(isCurrentSubDoc({
+ kind: "hPath",
+ targetNotebookId: "other-nb",
+ hPath: currentHPath + "/target",
+ title: "target",
+ }, "target"), false);
+ });
+});
+
describe("getNewDocTargetFromTree", () => {
const notebookId = "nb";
// 文档树 data-path:带 .sy 的内核路径
diff --git a/app/src/util/parseNewDocTarget.ts b/app/src/util/parseNewDocTarget.ts
index 29a215ce1b..f0abaf00fd 100644
--- a/app/src/util/parseNewDocTarget.ts
+++ b/app/src/util/parseNewDocTarget.ts
@@ -34,6 +34,32 @@ export type NewDocTargetSubDoc = {
export type NewDocTarget = NewDocTargetByHPath | NewDocTargetSubDoc;
+/** 判断配置路径的解析结果是否与在当前文档下直接创建子文档等价 */
+export const isCurrentDocSubDocTarget = (request: {
+ target: NewDocTarget;
+ currentNotebookId: string;
+ currentPath: string;
+ currentHPath: string;
+ title: string;
+}) => {
+ const {target} = request;
+ if (target.targetNotebookId !== request.currentNotebookId || target.title !== request.title) {
+ return false;
+ }
+ if (target.kind === "subDoc") {
+ return target.parentPath === request.currentPath;
+ }
+
+ const segments = target.hPath.split("/").filter(Boolean);
+ if (target.title) {
+ segments.pop();
+ }
+ const parentHPath = segments.length === 0 ? "/" : "/" + segments.join("/");
+ const currentHPathSegments = mergePathSegments([], request.currentHPath.split("/").filter(Boolean));
+ const currentHPath = currentHPathSegments.length === 0 ? "/" : "/" + currentHPathSegments.join("/");
+ return parentHPath === currentHPath;
+};
+
/** 按保存路径配置解析新建目标 */
export const getNewDocTargetFromSavePath = (request: {
templatePath: string;