Fix tiptap selection sync and toolbar event loop

This commit is contained in:
lix-2026
2026-04-19 21:03:25 +08:00
parent 111a87d4fd
commit 394e2a155c
87 changed files with 17415 additions and 527 deletions
@@ -0,0 +1,78 @@
import { describe, expect, it } from "vitest";
import { normalizeDocumentContentResponse } from "@/lib/documents/page-subtree-response";
import type { PageSubtreeProjection } from "@/lib/documents/page-subtree";
describe("page subtree response helpers", () => {
it("保留服务端返回的 pageSubtree", () => {
const serverPageSubtree = {
projectionId: "server_projection",
projection: "page_tree",
rootNodeId: "doc_1",
rootNode: {
id: "doc_1",
parentNodeId: null,
nodeType: "page",
blockId: null,
anchorBlockId: null,
depth: 0,
metadata: {
title: "服务端标题",
textSnippet: null,
blockType: null,
headingLevel: null,
numbering: null,
childCount: 0,
order: 0,
path: ["doc_1"],
},
},
subtree: {
rootNodeId: "doc_1",
nodes: [],
},
outline: [],
evidence: [],
stats: {
blockCount: 0,
headingCount: 0,
evidenceCount: 0,
maxDepth: 0,
},
} satisfies PageSubtreeProjection;
const normalized = normalizeDocumentContentResponse({
documentId: "doc_1",
payload: {
content: null,
revision: 1,
conflictDetectionKey: "doc_1:1",
pageSubtree: serverPageSubtree,
},
});
expect(normalized.pageSubtree).toBe(serverPageSubtree);
});
it("在服务端缺失 pageSubtree 时保留空值", () => {
const normalized = normalizeDocumentContentResponse({
documentId: "doc_1",
title: "测试页面",
payload: {
content: [
{
id: "heading_1",
type: "heading",
props: { level: 1 },
content: [{ type: "text", text: "章节一" }],
},
],
revision: 3,
conflictDetectionKey: "doc_1:3",
},
});
expect(normalized.revision).toBe(3);
expect(normalized.conflictDetectionKey).toBe("doc_1:3");
expect(normalized.pageSubtree).toBeNull();
});
});
@@ -0,0 +1,48 @@
import {
type PageSubtreeProjection,
} from "@/lib/documents/page-subtree";
export type DocumentContentResponseLike = {
content?: unknown;
revision?: number | null;
conflict_detection_key?: string | null;
conflictDetectionKey?: string | null;
page_subtree?: PageSubtreeProjection | null;
pageSubtree?: PageSubtreeProjection | null;
title?: string | null;
};
export type NormalizedDocumentContentResponse = {
content: unknown;
revision: number;
conflictDetectionKey: string;
pageSubtree: PageSubtreeProjection | null;
};
export function normalizeDocumentContentResponse(input: {
documentId: string;
title?: string | null;
payload?: DocumentContentResponseLike | null;
}): NormalizedDocumentContentResponse {
const payload = input.payload ?? null;
const content = payload?.content ?? null;
const revision =
typeof payload?.revision === "number" && Number.isInteger(payload.revision)
? payload.revision
: 0;
const conflictDetectionKey = (
typeof payload?.conflictDetectionKey === "string" && payload.conflictDetectionKey.trim()
? payload.conflictDetectionKey
: typeof payload?.conflict_detection_key === "string" && payload.conflict_detection_key.trim()
? payload.conflict_detection_key
: `${input.documentId}:0`
);
const pageSubtree = payload?.pageSubtree ?? payload?.page_subtree ?? null;
return {
content,
revision,
conflictDetectionKey,
pageSubtree,
};
}