Skip to content

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

:bug: Align mobile outline heading highlights https://github.com/siyuan-note/siyuan/issues/20224

Daniel27b16d8857c3
diff --git a/app/src/editor/outlineContainers.test.ts b/app/src/editor/outlineContainers.test.ts
new file mode 100644
index 0000000000..0f2af4be76
--- /dev/null
+++ b/app/src/editor/outlineContainers.test.ts
@@ -0,0 +1,51 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isClassDeclaration, ScriptTarget, transpileModule} from "typescript";
+
+for (const path of ["src/layout/dock/Outline.ts", "src/mobile/dock/MobileOutline.ts"]) {
+ const source = createSourceFile(path, readFileSync(path, "utf8"), ScriptTarget.ES2021, true);
+ const method = source.statements.find(isClassDeclaration).members.find(member =>
+ member.name?.getText(source) === "setCurrent");
+ const compiled = transpileModule(`class Harness {
+ currentRequestID = 0; blockId = "doc"; highlights = [];
+ setCurrentById(id) { this.highlights.push(id); }
+ ${method.getText(source)}
+ }
+ globalThis.Harness = Harness;`, {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+
+ for (const container of ["bq", "callout-content"]) {
+ for (const caret of ["heading", "paragraph"]) {
+ test(`${path}: ${caret} inside ${container} highlights the preceding outer heading`, async () => {
+ const outer = {id: "outer", type: "NodeHeading", container: "", previous: undefined as {id: string} | undefined};
+ const inner = {id: "inner", type: "NodeHeading", container, previous: outer};
+ const paragraph = {id: "paragraph", type: "NodeParagraph", container, previous: inner};
+ const nodes = [outer, inner, paragraph].map(node => ({
+ ...node,
+ getAttribute: (name: string) => name === "data-type" ? node.type : node.id,
+ }));
+ const context: any = {
+ hasClosestByClassName: (node: typeof inner, className: string) => node.container === className,
+ getPreviousBlock: (node: typeof inner) => nodes.find(item => item.id === node.previous?.id),
+ fetchPost: () => assert.fail("an available outer heading must not require a breadcrumb request"),
+ };
+ runInNewContext(compiled, context);
+ const outline = new context.Harness();
+ await outline.setCurrent(nodes[caret === "heading" ? 1 : 2]);
+ assert.deepEqual(Array.from(outline.highlights), ["outer"]);
+ });
+ }
+ }
+
+ test(`${path}: headings outside excluded containers retain direct highlighting`, async () => {
+ const context: any = {
+ hasClosestByClassName: () => false,
+ getPreviousBlock: () => assert.fail("a regular heading must be highlighted directly"),
+ };
+ runInNewContext(compiled, context);
+ const outline = new context.Harness();
+ await outline.setCurrent({getAttribute: (name: string) => name === "data-type" ? "NodeHeading" : "heading"});
+ assert.deepEqual(Array.from(outline.highlights), ["heading"]);
+ });
+}
diff --git a/app/src/mobile/dock/MobileOutline.ts b/app/src/mobile/dock/MobileOutline.ts
index 0b97e9f9d7..a5b16fe7f7 100644
--- a/app/src/mobile/dock/MobileOutline.ts
+++ b/app/src/mobile/dock/MobileOutline.ts
@@ -438,7 +438,9 @@ export class MobileOutline extends Model {
} else {
let previousElement = getPreviousBlock(nodeElement);
while (previousElement) {
- if (previousElement.getAttribute("data-type") === "NodeHeading") {
+ if (previousElement.getAttribute("data-type") === "NodeHeading" &&
+ !hasClosestByClassName(previousElement, "bq") &&
+ !hasClosestByClassName(previousElement, "callout-content")) {
break;
} else {
previousElement = getPreviousBlock(previousElement);