Skip to content

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

:bug: Navigate through empty headings with arrow keys https://github.com/siyuan-note/siyuan/issues/19129

Danielc37512eb3f67
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 80a318ed8c..5b266470b8 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -30,11 +30,11 @@ import {
turnsIntoOneTransaction,
turnsIntoTransaction,
turnsOneInto,
- isEmptyParagraph,
turnEmptyParagraphsIntoTransaction,
updateBatchTransaction,
updateTransaction
} from "../wysiwyg/transaction";
+import {isEmptyParagraph} from "../wysiwyg/emptyTextBlock";
import {removeBlockPreservingSelectionMode} from "../wysiwyg/remove";
import {focusBlock, focusByRange, getBlockElementsByRange, getEditorRange, selectBlocksByRange} from "../util/selection";
import {hideElements} from "../ui/hideElements";
diff --git a/app/src/protyle/wysiwyg/emptyTextBlock.test.ts b/app/src/protyle/wysiwyg/emptyTextBlock.test.ts
new file mode 100644
index 0000000000..322a9b661c
--- /dev/null
+++ b/app/src/protyle/wysiwyg/emptyTextBlock.test.ts
@@ -0,0 +1,78 @@
+import {before, describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+
+let isEmptyParagraph: typeof import("./emptyTextBlock").isEmptyParagraph;
+let isEmptyTextBlock: typeof import("./emptyTextBlock").isEmptyTextBlock;
+
+before(async () => {
+ Object.assign(globalThis, {
+ SIYUAN_VERSION: "test",
+ NODE_ENV: "test",
+ });
+ ({isEmptyParagraph, isEmptyTextBlock} = await import("./emptyTextBlock"));
+});
+
+class TestEditableElement {
+ constructor(public textContent: string, public childTags: string[] = []) {
+ }
+
+ get childElementCount() {
+ return this.childTags.length;
+ }
+
+ cloneNode() {
+ return new TestEditableElement(this.textContent, [...this.childTags]);
+ }
+
+ querySelectorAll(selector: string) {
+ if (selector !== "br, wbr") {
+ return [];
+ }
+ return this.childTags.filter(tag => tag === "br" || tag === "wbr").map(tag => ({
+ remove: () => {
+ const index = this.childTags.indexOf(tag);
+ if (index > -1) {
+ this.childTags.splice(index, 1);
+ }
+ },
+ }));
+ }
+}
+
+class TestBlockElement {
+ classList = {
+ contains: () => false,
+ };
+
+ constructor(public type: string, public firstElementChild: TestEditableElement) {
+ }
+
+ getAttribute(name: string) {
+ if (name === "data-type") {
+ return this.type;
+ }
+ return name === "data-node-id" ? "20260904180000-test" : null;
+ }
+}
+
+const block = (type: string, text = "", childTags: string[] = []) =>
+ new TestBlockElement(type, new TestEditableElement(text, childTags)) as unknown as Element;
+
+describe("empty text blocks", () => {
+ it("recognizes empty paragraphs without changing paragraph semantics", () => {
+ assert.equal(isEmptyParagraph(block("NodeParagraph", "", ["wbr"])), true);
+ assert.equal(isEmptyTextBlock(block("NodeParagraph", "", ["wbr"])), true);
+ assert.equal(isEmptyParagraph(block("NodeHeading", "", ["wbr"])), false);
+ });
+
+ it("recognizes empty headings as caret navigation targets", () => {
+ assert.equal(isEmptyTextBlock(block("NodeHeading", "", ["wbr"])), true);
+ assert.equal(isEmptyTextBlock(block("NodeHeading", "\u200b")), true);
+ });
+
+ it("does not treat content or other block types as empty text blocks", () => {
+ assert.equal(isEmptyTextBlock(block("NodeHeading", "heading")), false);
+ assert.equal(isEmptyTextBlock(block("NodeHeading", "", ["span"])), false);
+ assert.equal(isEmptyTextBlock(block("NodeCodeBlock")), false);
+ });
+});
diff --git a/app/src/protyle/wysiwyg/emptyTextBlock.ts b/app/src/protyle/wysiwyg/emptyTextBlock.ts
new file mode 100644
index 0000000000..844def3c1e
--- /dev/null
+++ b/app/src/protyle/wysiwyg/emptyTextBlock.ts
@@ -0,0 +1,19 @@
+import {Constants} from "../../constants";
+import {getContenteditableElement} from "./getBlock";
+
+const isEmptyEditableBlock = (element: Element) => {
+ const editableElement = getContenteditableElement(element);
+ if (!editableElement) {
+ return false;
+ }
+ const contentElement = editableElement.cloneNode(true) as Element;
+ contentElement.querySelectorAll("br, wbr").forEach(item => item.remove());
+ const text = contentElement.textContent.replace(new RegExp(Constants.ZWSP, "g"), "").trim();
+ return text === "" && contentElement.childElementCount === 0;
+};
+
+export const isEmptyParagraph = (element: Element) =>
+ element.getAttribute("data-type") === "NodeParagraph" && isEmptyEditableBlock(element);
+
+export const isEmptyTextBlock = (element: Element) =>
+ ["NodeParagraph", "NodeHeading"].includes(element.getAttribute("data-type")) && isEmptyEditableBlock(element);
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index 70ba2c25b5..c3c5ee3aff 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -65,7 +65,6 @@ import {clearTableCell, fixTable, isIncludeCell} from "../util/table";
import {
transaction,
insertEmptyBlockquote,
- isEmptyParagraph,
turnEmptyParagraphsIntoTransaction,
turnsIntoGroupsTransaction,
turnsIntoOneTransaction,
@@ -74,6 +73,7 @@ import {
updateBatchTransaction,
updateTransaction
} from "./transaction";
+import {isEmptyParagraph, isEmptyTextBlock} from "./emptyTextBlock";
import {getBlockquoteContext, shouldCancelBlockquote} from "./blockquote";
import {fontEvent} from "../toolbar/Font";
import {applyTableCellStyleHotkey} from "../toolbar/tableCell";
@@ -1048,9 +1048,10 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}
if (adjacentElement) {
adjacentElement = toPrevious ? getLastBlock(adjacentElement) : getFirstBlock(adjacentElement);
- // 显式聚焦空段落,避免浏览器跨越容器边界时跳过该块。
+ // 显式聚焦空文本块,避免浏览器跨越容器边界时跳过该块。
// https://github.com/siyuan-note/siyuan/issues/18862
- if ((event.key === "ArrowUp" || event.key === "ArrowDown") && isEmptyParagraph(adjacentElement)) {
+ // https://github.com/siyuan-note/siyuan/issues/19129
+ if ((event.key === "ArrowUp" || event.key === "ArrowDown") && isEmptyTextBlock(adjacentElement)) {
focusBlock(adjacentElement, undefined, event.key === "ArrowDown");
event.stopPropagation();
event.preventDefault();
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 9ed488f87a..19b5e8e764 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -66,6 +66,7 @@ import {
getBlockSelectionStatusIDs,
restoreBlockSelectionModeState
} from "./blockSelection";
+import {isEmptyParagraph} from "./emptyTextBlock";
import {
invalidateTrackedRangesByOperations,
type ITrackedRangeInsertion,
@@ -2404,20 +2405,6 @@ const getEmptyParagraphTargetMarkdown = (type: TEmptyParagraphTarget) => {
return type === "line" ? "---" : "$$";
};
-export const isEmptyParagraph = (element: Element) => {
- if (element.getAttribute("data-type") !== "NodeParagraph") {
- return false;
- }
- const editableElement = getContenteditableElement(element);
- if (!editableElement) {
- return false;
- }
- const contentElement = editableElement.cloneNode(true) as Element;
- contentElement.querySelectorAll("br, wbr").forEach(item => item.remove());
- const text = contentElement.textContent.replace(new RegExp(Constants.ZWSP, "g"), "").trim();
- return text === "" && contentElement.childElementCount === 0;
-};
-
export const turnEmptyParagraphsIntoTransaction = (options: {
protyle: IProtyle,
nodeElements: Element[],