Skip to content

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

:sparkles: Support creating child documents from the text selection toolbar https://github.com/siyuan-note/siyuan/issues/18494

Danield1aa3f7243f7
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index 3bfa89b7c4..0b62936936 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -154,6 +154,7 @@ 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.ts
index 3bee202f29..2eec7810e9 100644
--- a/app/src/protyle/toolbar/util.ts
+++ b/app/src/protyle/toolbar/util.ts
@@ -4,6 +4,7 @@ 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) {
@@ -138,6 +139,15 @@ 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.ts
index 795f884727..3c9b9e9f2d 100644
--- a/app/src/protyle/util/Options.ts
+++ b/app/src/protyle/util/Options.ts
@@ -106,6 +106,7 @@ export class Options {
"inline-memo",
] : [
"block-ref",
+ "new-sub-doc-ref",
"a",
"|",
"text",
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index 4c4513bcd3..098dc49ce8 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -85,7 +85,7 @@ import {countBlockWord} from "../../layout/status";
import {moveToDown, moveToUp} from "./move";
import {beforePaste, pasteAsPlainText} from "../util/paste";
import {preventScroll} from "../scroll/preventScroll";
-import {getRefCreateSavePath, newFileBySelect} from "../../util/newFile";
+import {newFileBySelectRange} from "../../util/newFile";
import {removeSearchMark} from "../toolbar/util";
import {avKeydown} from "../render/av/keydown";
import {checkFold} from "../../util/noRelyPCFunction";
@@ -1424,48 +1424,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
const isNewNameFile = matchHotKey(window.siyuan.config.keymap.editor.general.newNameFile.custom, event);
if (isNewNameFile || matchHotKey(window.siyuan.config.keymap.editor.general.newNameSettingFile.custom, event)) {
- if (!selectText.trim() && (nodeElement.querySelector("tr") || nodeElement.querySelector("span"))) {
- // 没选中时,都是纯文本就创建子文档 https://ld246.com/article/1663073488381/comment/1664804353295#comments
- } else {
- if (!selectText.trim() &&
- getContenteditableElement(nodeElement).textContent // https://github.com/siyuan-note/siyuan/issues/8099
- ) {
- selectAll(protyle, nodeElement, range);
- }
- // 固定触发时的文档和选区,异步创建完成后仅修改原位置 https://github.com/siyuan-note/siyuan/issues/16972
- const selectionRange = range.cloneRange();
- const startBlockElement = hasClosestBlock(selectionRange.startContainer);
- const endBlockElement = hasClosestBlock(selectionRange.endContainer);
- if (startBlockElement && endBlockElement) {
- const sourceNotebookId = protyle.notebookId;
- const sourceRootID = protyle.block.rootID;
- const sourcePath = protyle.path;
- const newFileName = replaceFileName(selectText.trim() ? selectText.trim() :
- protyle.lute.BlockDOM2Content(nodeElement.outerHTML).replace(/\n/g, "").trim());
- const selectionContext = {
- range: selectionRange,
- notebookId: sourceNotebookId,
- rootID: sourceRootID,
- path: sourcePath,
- startBlockID: startBlockElement.getAttribute("data-node-id"),
- endBlockID: endBlockElement.getAttribute("data-node-id"),
- text: selectionRange.toString(),
- };
- if (isNewNameFile) {
- fetchPost("/api/filetree/getHPathByPath", {
- notebook: sourceNotebookId,
- path: sourcePath,
- }, (response) => {
- newFileBySelect(protyle, newFileName, selectionContext, response.data, sourceNotebookId);
- });
- } else {
- getRefCreateSavePath(sourceNotebookId, sourcePath, (targetNotebookId, hPath) => {
- newFileBySelect(protyle, newFileName, selectionContext, hPath, targetNotebookId);
- });
- }
- }
- hideElements(["toolbar"], protyle);
- }
+ newFileBySelectRange(protyle, range, isNewNameFile ? "subDoc" : "configured");
event.preventDefault();
event.stopPropagation();
return;
diff --git a/app/src/util/newFile.ts b/app/src/util/newFile.ts
index 38e67816d3..1084be13ac 100644
--- a/app/src/util/newFile.ts
+++ b/app/src/util/newFile.ts
@@ -1,5 +1,5 @@
import {showMessage} from "../dialog/message";
-import {hasTopClosestByTag} from "../protyle/util/hasClosest";
+import {hasClosestBlock, hasTopClosestByTag} from "../protyle/util/hasClosest";
/// #if !MOBILE
import {Files} from "../layout/dock/Files";
import {Editor} from "../editor";
@@ -14,13 +14,15 @@ import {hideElements} from "../protyle/ui/hideElements";
import {openMobileFileById} from "../mobile/editor";
import type {App} from "../index";
import {NewDocTargetByHPath, NewDocTargetSubDoc, getNewDocTargetFromSavePath, getNewDocTargetFromTree} from "./parseNewDocTarget";
-import {focusByRange} from "../protyle/util/selection";
+import {focusByRange, selectAll} from "../protyle/util/selection";
import {
+ createNewFileSelectionContext,
isNewFileSelectionValid,
isRangeInEditor,
isSameRange,
NewFileSelectionContext
} from "./newFileSelection";
+import {getContenteditableElement} from "../protyle/wysiwyg/getBlock";
export const getBlockRefAnchorText = (title: string) => {
const trimmed = (title || "").trim();
@@ -146,6 +148,44 @@ export const newFileBySelect = (protyle: IProtyle, newFileName: string, context:
});
};
+export const newFileBySelectRange = (protyle: IProtyle, range: Range, target: "subDoc" | "configured") => {
+ const nodeElement = hasClosestBlock(range.startContainer);
+ if (!nodeElement) {
+ return;
+ }
+ const selectText = range.toString();
+ if (!selectText.trim() && (nodeElement.querySelector("tr") || nodeElement.querySelector("span"))) {
+ // 没选中时,都是纯文本就创建子文档 https://ld246.com/article/1663073488381/comment/1664804353295#comments
+ return;
+ }
+ if (!selectText.trim() &&
+ getContenteditableElement(nodeElement).textContent // https://github.com/siyuan-note/siyuan/issues/8099
+ ) {
+ selectAll(protyle, nodeElement, range);
+ }
+ const selectionContext = createNewFileSelectionContext(protyle, range);
+ if (!selectionContext) {
+ return;
+ }
+ const sourceNotebookId = selectionContext.notebookId;
+ const sourcePath = selectionContext.path;
+ const newFileName = replaceFileName(selectText.trim() ? selectText.trim() :
+ protyle.lute.BlockDOM2Content(nodeElement.outerHTML).replace(/\n/g, "").trim());
+ if (target === "subDoc") {
+ fetchPost("/api/filetree/getHPathByPath", {
+ notebook: sourceNotebookId,
+ path: sourcePath,
+ }, (response) => {
+ newFileBySelect(protyle, newFileName, selectionContext, response.data, sourceNotebookId);
+ });
+ } else {
+ getRefCreateSavePath(sourceNotebookId, sourcePath, (targetNotebookId, hPath) => {
+ newFileBySelect(protyle, newFileName, selectionContext, hPath, targetNotebookId);
+ });
+ }
+ hideElements(["toolbar"], protyle);
+};
+
export const getRefCreateSavePath = (notebookId: string, currentPath: string, cb: (targetNotebookId: string, hPath: string) => void) => {
fetchPost("/api/filetree/getRefCreateSavePath", {
notebook: notebookId
diff --git a/app/src/util/newFileSelection.test.ts b/app/src/util/newFileSelection.test.ts
index 7816615df3..846eff0142 100644
--- a/app/src/util/newFileSelection.test.ts
+++ b/app/src/util/newFileSelection.test.ts
@@ -1,6 +1,7 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
+ createNewFileSelectionContext,
isNewFileSelectionValid,
isRangeInEditor,
isSameRange,
@@ -75,6 +76,7 @@ const createRange = (start: TestText, end: TestText, text = "selected") => ({
endContainer: asNode(end),
endOffset: text.length,
toString: () => text,
+ cloneRange: () => createRange(start, end, text),
}) as unknown as Range;
const createFixture = () => {
@@ -103,6 +105,20 @@ const createFixture = () => {
};
describe("new document selection context", () => {
+ it("captures the selected document and block boundaries", () => {
+ const {protyle, range} = createFixture();
+
+ const context = createNewFileSelectionContext(protyle, range);
+
+ assert.notEqual(context?.range, range);
+ assert.equal(context?.notebookId, "notebook");
+ assert.equal(context?.rootID, "root");
+ assert.equal(context?.path, "/root.sy");
+ assert.equal(context?.startBlockID, "start");
+ assert.equal(context?.endBlockID, "end");
+ assert.equal(context?.text, "selected");
+ });
+
it("keeps a connected selection in the original document valid", () => {
const {context, protyle} = createFixture();
diff --git a/app/src/util/newFileSelection.ts b/app/src/util/newFileSelection.ts
index f5784a951a..02d5fddd8e 100644
--- a/app/src/util/newFileSelection.ts
+++ b/app/src/util/newFileSelection.ts
@@ -10,6 +10,25 @@ export type NewFileSelectionContext = {
text: string;
};
+export const createNewFileSelectionContext = (protyle: IProtyle, range: Range) => {
+ // 固定触发时的文档和选区,异步创建完成后仅修改原位置 https://github.com/siyuan-note/siyuan/issues/16972
+ const selectionRange = range.cloneRange();
+ const startBlockElement = hasClosestBlock(selectionRange.startContainer);
+ const endBlockElement = hasClosestBlock(selectionRange.endContainer);
+ if (!startBlockElement || !endBlockElement) {
+ return;
+ }
+ return {
+ range: selectionRange,
+ notebookId: protyle.notebookId,
+ rootID: protyle.block.rootID,
+ path: protyle.path,
+ startBlockID: startBlockElement.getAttribute("data-node-id"),
+ endBlockID: endBlockElement.getAttribute("data-node-id"),
+ text: selectionRange.toString(),
+ } as NewFileSelectionContext;
+};
+
export const isSameRange = (range: Range, targetRange: Range) => range.startContainer === targetRange.startContainer &&
range.startOffset === targetRange.startOffset && range.endContainer === targetRange.endContainer &&
range.endOffset === targetRange.endOffset;