Skip to content

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

:art: Support child document creation in reference menus https://github.com/siyuan-note/siyuan/issues/18494

Daniel5b023b8d5cf0
diff --git a/app/src/protyle/hint/extend.ts b/app/src/protyle/hint/extend.ts
index 134eff1773..38234a9b88 100644
--- a/app/src/protyle/hint/extend.ts
+++ b/app/src/protyle/hint/extend.ts
@@ -503,14 +503,12 @@ export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IH
<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>
+ 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++;
- }
+ });
+ 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>`;
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index 19b426a125..0583e7b5c1 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -21,7 +21,8 @@ import {
getDocCreateTemplatePath,
newFileByRefHint,
newFileBySelectRange,
- newFileInProtyle
+ newFileInProtyle,
+ newSubDocByRefHint
} from "../../util/newFile";
import {isAbnormalItem, upDownHint} from "../../util/upDownHint";
import {setPosition} from "../../util/setPosition";
@@ -392,12 +393,10 @@ ${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>
+ 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;
@@ -502,11 +501,13 @@ ${genHintItemHTML(item)}
let tempElement = document.createElement("div");
tempElement.innerHTML = value.replace(/<mark>/g, "").replace(/<\/mark>/g, "");
tempElement = tempElement.firstElementChild as HTMLDivElement;
- if (value.startsWith("((newFile ") && value.endsWith(`${Lute.Caret}'))`)) {
- const fileNames = value.substring(11, value.length - 4).split(`"${Constants.ZWSP}'`);
+ const isNewSubDoc = value.startsWith("((newSubDoc ") && value.endsWith(`${Lute.Caret}'))`);
+ if ((value.startsWith("((newFile ") || isNewSubDoc) && value.endsWith(`${Lute.Caret}'))`)) {
+ const prefix = isNewSubDoc ? "((newSubDoc " : "((newFile ";
+ const fileNames = value.substring(prefix.length, value.length - 4).split(`"${Constants.ZWSP}'`);
const realFileName = fileNames.length === 1 ? fileNames[0] : fileNames[1];
const newID = Lute.NewNodeID();
- newFileByRefHint(protyle, realFileName, () => {
+ const bindNewDoc = () => {
transaction(protyle, [{
action: "replaceAttrViewBlock",
avID,
@@ -523,7 +524,12 @@ ${genHintItemHTML(item)}
blockID: nodeElement.dataset.nodeId,
context: {protyleID: protyle.id},
}]);
- }, newID);
+ };
+ if (isNewSubDoc) {
+ newSubDocByRefHint(protyle, realFileName, bindNewDoc, newID);
+ } else {
+ newFileByRefHint(protyle, realFileName, bindNewDoc, newID);
+ }
updateAttrViewCellAnimation(cellElement, {
type: "block",
isDetached: false,
diff --git a/app/src/protyle/render/av/action.ts b/app/src/protyle/render/av/action.ts
index b653545b1b..ef43119e54 100644
--- a/app/src/protyle/render/av/action.ts
+++ b/app/src/protyle/render/av/action.ts
@@ -998,30 +998,30 @@ export const avContextmenu = (protyle: IProtyle, rowElement: HTMLElement | undef
const detachedItemIDs = selectedItems.filter(item => item.isDetached).map(item => item.itemID);
if (detachedItemIDs.length > 0) {
menu.addItem({
- id: "createSubDocAndBind",
- label: window.siyuan.languages.createSubDocAndBind,
+ id: "createDocAndBind",
+ label: window.siyuan.languages.createDocAndBind,
icon: "iconFile",
- accelerator: window.siyuan.config.keymap.editor.general.newNameFile.custom,
+ accelerator: window.siyuan.config.keymap.editor.general.newNameSettingFile.custom,
click() {
createAttributeViewItemDocs({
protyle,
blockElement,
itemIDs: detachedItemIDs,
- saveMode: "subDoc",
+ saveMode: "template",
});
}
});
menu.addItem({
- id: "createDocAndBind",
- label: window.siyuan.languages.createDocAndBind,
+ id: "createSubDocAndBind",
+ label: window.siyuan.languages.createSubDocAndBind,
icon: "iconFile",
- accelerator: window.siyuan.config.keymap.editor.general.newNameSettingFile.custom,
+ accelerator: window.siyuan.config.keymap.editor.general.newNameFile.custom,
click() {
createAttributeViewItemDocs({
protyle,
blockElement,
itemIDs: detachedItemIDs,
- saveMode: "template",
+ saveMode: "subDoc",
});
}
});
diff --git a/app/src/util/newFile.ts b/app/src/util/newFile.ts
index 141002e88c..e0ff037aac 100644
--- a/app/src/util/newFile.ts
+++ b/app/src/util/newFile.ts
@@ -416,6 +416,22 @@ export const newFileByRefHint = (
});
};
+/** 在当前文档下新建子文档,创建后仅回调插入引用或绑定数据库条目 */
+export const newSubDocByRefHint = (
+ protyle: IProtyle,
+ name: string,
+ onCreated?: (id: string, title: string) => void,
+ presetId?: string,
+) => {
+ const target = getNewDocTargetFromTree({
+ templatePath: "",
+ currentNotebookId: protyle.notebookId,
+ currentPath: protyle.path,
+ name: replaceFileName(name.trim()),
+ });
+ createRefDocAsSubDoc(target, onCreated, presetId);
+};
+
function createRefDocByHPath(
protyle: IProtyle,
target: NewDocTargetByHPath,