Skip to content

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

:sparkles: Improve jumping to the parent block to focus the document title https://github.com/siyuan-note/siyuan/issues/18835

Daniela0ee40666246
diff --git a/app/src/block/jumpToParent.test.ts b/app/src/block/jumpToParent.test.ts
index a45d2cec54..026afb768e 100644
--- a/app/src/block/jumpToParent.test.ts
+++ b/app/src/block/jumpToParent.test.ts
@@ -1,6 +1,6 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {shouldFocusJumpTarget} from "./jumpToParent";
+import {shouldFocusJumpTarget, shouldFocusParentDocumentTitle} from "./jumpToParent";
describe("shouldFocusJumpTarget", () => {
it("focuses a target hidden by folding", () => {
@@ -39,3 +39,34 @@ describe("shouldFocusJumpTarget", () => {
}), false);
});
});
+
+describe("shouldFocusParentDocumentTitle", () => {
+ it("focuses the title when the parent is the root document", () => {
+ assert.equal(shouldFocusParentDocumentTitle({
+ isRoot: true,
+ hasTitle: true,
+ isBacklink: false,
+ }), true);
+ });
+
+ it("keeps block navigation for non-root parents", () => {
+ assert.equal(shouldFocusParentDocumentTitle({
+ isRoot: false,
+ hasTitle: true,
+ isBacklink: false,
+ }), false);
+ });
+
+ it("keeps block navigation when the editor has no usable title", () => {
+ assert.equal(shouldFocusParentDocumentTitle({
+ isRoot: true,
+ hasTitle: false,
+ isBacklink: false,
+ }), false);
+ assert.equal(shouldFocusParentDocumentTitle({
+ isRoot: true,
+ hasTitle: true,
+ isBacklink: true,
+ }), false);
+ });
+});
diff --git a/app/src/block/jumpToParent.ts b/app/src/block/jumpToParent.ts
index 24157e16eb..e7929e2b09 100644
--- a/app/src/block/jumpToParent.ts
+++ b/app/src/block/jumpToParent.ts
@@ -4,3 +4,9 @@ export const shouldFocusJumpTarget = (options: {
isFolded: boolean,
isHidden: boolean,
}) => !options.isRoot && (options.showAll || (options.isFolded && options.isHidden));
+
+export const shouldFocusParentDocumentTitle = (options: {
+ isRoot: boolean,
+ hasTitle: boolean,
+ isBacklink: boolean,
+}) => options.isRoot && options.hasTitle && !options.isBacklink;
diff --git a/app/src/block/util.ts b/app/src/block/util.ts
index 822454105a..16efe344b8 100644
--- a/app/src/block/util.ts
+++ b/app/src/block/util.ts
@@ -1,4 +1,4 @@
-import {focusByWbr, getEditorRange, getUndoFocusContext} from "../protyle/util/selection";
+import {focusByRange, focusByWbr, getEditorRange, getUndoFocusContext} from "../protyle/util/selection";
import {hasClosestBlock, hasClosestByClassName, isInEmbedBlock} from "../protyle/util/hasClosest";
import {
getContenteditableElement,
@@ -18,7 +18,9 @@ import {openFileById} from "../editor/util";
import {openMobileFileById} from "../mobile/editor";
import {mathRender} from "../protyle/render/mathRender";
import {buildCancelSuperBlockOperations} from "./cancelSuperBlock";
-import {shouldFocusJumpTarget} from "./jumpToParent";
+import {shouldFocusJumpTarget, shouldFocusParentDocumentTitle} from "./jumpToParent";
+import {zoomOut} from "../menus/protyle";
+import {pushBack} from "../util/backForward";
export const cancelSB = async (protyle: IProtyle, nodeElement: Element, range?: Range) => {
let previousId = getPreviousBlockSibling(nodeElement)?.getAttribute("data-node-id");
@@ -196,6 +198,7 @@ export const refreshSbAndPersistWidth = (sbElement: Element,
};
export const jumpToParent = (protyle: IProtyle, nodeElement: Element, type: "parent" | "next" | "previous") => {
+ const previousRange = getEditorRange(nodeElement);
fetchPost("/api/block/getBlockSiblingID", {
id: nodeElement.getAttribute("data-node-id"),
notebook: protyle.notebookId,
@@ -204,6 +207,34 @@ export const jumpToParent = (protyle: IProtyle, nodeElement: Element, type: "par
if (!targetId) {
return;
}
+ const titleElement = protyle.title?.editElement;
+ if (shouldFocusParentDocumentTitle({
+ isRoot: targetId === protyle.block.rootID,
+ hasTitle: Boolean(titleElement),
+ isBacklink: Boolean(protyle.options.backlinkData),
+ })) {
+ const focusTitle = () => {
+ const range = titleElement.ownerDocument.createRange();
+ range.selectNodeContents(titleElement);
+ range.collapse(false);
+ focusByRange(range);
+ protyle.toolbar.range = range;
+ protyle.contentElement.scrollTop = 0;
+ pushBack(protyle, range, titleElement);
+ };
+ pushBack(protyle, previousRange);
+ if (protyle.block.showAll) {
+ zoomOut({
+ protyle,
+ id: protyle.block.rootID,
+ callback: focusTitle,
+ reload: true,
+ });
+ } else {
+ focusTitle();
+ }
+ return;
+ }
fetchPost("/api/block/checkBlockFold", {
id: targetId,
notebook: protyle.notebookId,