Skip to content

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

:bug: Navigate through callout titles with arrow keys https://github.com/siyuan-note/siyuan/issues/19128

Danielf23b549d4874
diff --git a/app/src/protyle/wysiwyg/calloutCaret.test.ts b/app/src/protyle/wysiwyg/calloutCaret.test.ts
new file mode 100644
index 0000000000..943e5d4976
--- /dev/null
+++ b/app/src/protyle/wysiwyg/calloutCaret.test.ts
@@ -0,0 +1,126 @@
+import {before, describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+
+let getCalloutTitleNavigationTarget: typeof import("./calloutCaret").getCalloutTitleNavigationTarget;
+
+before(async () => {
+ Object.assign(globalThis, {
+ SIYUAN_VERSION: "test",
+ NODE_ENV: "test",
+ });
+ ({getCalloutTitleNavigationTarget} = await import("./calloutCaret"));
+});
+
+class TestElement {
+ nodeType = 1;
+ tagName = "DIV";
+ nodeName = "DIV";
+ parentElement: TestElement | null = null;
+ children: TestElement[] = [];
+ attributes = new Map<string, string>();
+ classes = new Set<string>();
+ classList = {
+ contains: (className: string) => this.classes.has(className),
+ };
+
+ append(...elements: TestElement[]) {
+ elements.forEach(element => {
+ element.parentElement = this;
+ this.children.push(element);
+ });
+ return this;
+ }
+
+ addClass(className: string) {
+ this.classes.add(className);
+ return this;
+ }
+
+ setAttribute(name: string, value: string) {
+ this.attributes.set(name, value);
+ return this;
+ }
+
+ getAttribute(name: string) {
+ return this.attributes.get(name) || null;
+ }
+
+ hasAttribute(name: string) {
+ return this.attributes.has(name);
+ }
+
+ querySelector(selector: string) {
+ if (selector !== ":scope > .callout-info > .callout-title") {
+ return null;
+ }
+ const infoElement = this.children.find(element => element.classes.has("callout-info"));
+ return infoElement?.children.find(element => element.classes.has("callout-title")) || null;
+ }
+
+ querySelectorAll(selector: string) {
+ if (selector !== "[data-node-id]") {
+ return [];
+ }
+ const matches: TestElement[] = [];
+ const visit = (element: TestElement) => {
+ element.children.forEach(child => {
+ if (child.hasAttribute("data-node-id")) {
+ matches.push(child);
+ }
+ visit(child);
+ });
+ };
+ visit(this);
+ return matches;
+ }
+}
+
+const asElement = (element: TestElement) => element as unknown as Element;
+
+const block = (id: string, type = "NodeParagraph") => new TestElement()
+ .setAttribute("data-node-id", id)
+ .setAttribute("data-type", type);
+
+const createCallout = () => {
+ const editor = new TestElement().addClass("protyle-wysiwyg");
+ const callout = block("callout", "NodeCallout").addClass("callout");
+ const info = new TestElement().addClass("callout-info");
+ const title = new TestElement().addClass("callout-title");
+ const content = new TestElement().addClass("callout-content");
+ const first = block("first");
+ const second = block("second");
+ editor.append(callout.append(info.append(title), content.append(first, second)));
+ return {editor, callout, title, first, second};
+};
+
+describe("getCalloutTitleNavigationTarget", () => {
+ it("moves upward from the first callout content block to the title", () => {
+ const {title, first} = createCallout();
+
+ assert.equal(getCalloutTitleNavigationTarget(asElement(first), false, "ArrowUp"), asElement(title));
+ });
+
+ it("keeps upward navigation between callout content blocks unchanged", () => {
+ const {second} = createCallout();
+
+ assert.equal(getCalloutTitleNavigationTarget(asElement(second), false, "ArrowUp"), undefined);
+ });
+
+ it("moves downward into the title of an adjacent callout", () => {
+ const {callout, title} = createCallout();
+ const previous = block("previous");
+
+ assert.equal(getCalloutTitleNavigationTarget(asElement(previous), asElement(callout), "ArrowDown"),
+ asElement(title));
+ });
+
+ it("finds a callout at the beginning of an adjacent container", () => {
+ const {callout, title} = createCallout();
+ const list = block("list", "NodeList").addClass("list");
+ const item = block("item", "NodeListItem").addClass("li");
+ list.append(item.append(callout));
+
+ assert.equal(getCalloutTitleNavigationTarget(asElement(block("previous")), asElement(list), "ArrowDown"),
+ asElement(title));
+ });
+});
diff --git a/app/src/protyle/wysiwyg/calloutCaret.ts b/app/src/protyle/wysiwyg/calloutCaret.ts
new file mode 100644
index 0000000000..802dbf7c75
--- /dev/null
+++ b/app/src/protyle/wysiwyg/calloutCaret.ts
@@ -0,0 +1,30 @@
+import {hasClosestByClassName} from "../util/hasClosest";
+import {getFirstBlock} from "./getBlock";
+
+const getCalloutTitleElement = (calloutElement: Element) =>
+ calloutElement.querySelector<HTMLElement>(":scope > .callout-info > .callout-title");
+
+export const getCalloutTitleNavigationTarget = (nodeElement: Element, adjacentElement: Element | false | undefined,
+ key: string) => {
+ if (key === "ArrowUp") {
+ const contentElement = hasClosestByClassName(nodeElement, "callout-content");
+ if (!contentElement) {
+ return;
+ }
+ const calloutElement = contentElement.parentElement;
+ if (!calloutElement?.classList.contains("callout") ||
+ getFirstBlock(contentElement) !== nodeElement) {
+ return;
+ }
+ return getCalloutTitleElement(calloutElement);
+ }
+
+ if (key === "ArrowDown" && adjacentElement) {
+ const firstElement = getFirstBlock(adjacentElement);
+ const calloutElement = adjacentElement.classList.contains("callout") ? adjacentElement :
+ firstElement?.classList.contains("callout") ? firstElement : undefined;
+ if (calloutElement) {
+ return getCalloutTitleElement(calloutElement);
+ }
+ }
+};
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index 2b88fd8a28..70ba2c25b5 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -133,6 +133,7 @@ import {isMobile} from "../../util/functions";
import {confirmBlockRef} from "../../util/checkBlockRef";
import {scheduleCaretScroll, scheduleOffscreenCaretScroll} from "./caretScroll";
import {scrollPage} from "../scroll/page";
+import {getCalloutTitleNavigationTarget} from "./calloutCaret";
import {
BLOCK_SELECTION_CLASS,
clearBlockSelectionMode,
@@ -1034,6 +1035,17 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
if (selectText === "" && range.collapsed && !nodeElement.classList.contains("av")) {
let adjacentElement = toPrevious ? getPreviousBlock(nodeElement) :
(toNext ? getNextBlock(nodeElement) : undefined);
+ const calloutTitleNavigationTarget =
+ (event.key === "ArrowUp" && toPrevious || event.key === "ArrowDown" && toNext) ?
+ getCalloutTitleNavigationTarget(nodeElement, adjacentElement, event.key) : undefined;
+ if (calloutTitleNavigationTarget) {
+ range.selectNodeContents(calloutTitleNavigationTarget);
+ range.collapse(event.key === "ArrowDown");
+ focusByRange(range);
+ event.stopPropagation();
+ event.preventDefault();
+ return;
+ }
if (adjacentElement) {
adjacentElement = toPrevious ? getLastBlock(adjacentElement) : getFirstBlock(adjacentElement);
// 显式聚焦空段落,避免浏览器跨越容器边界时跳过该块。