Skip to content
File

Blob: tests/client/util/editor-fixtures.ts

typescript60 lines
1import type { JSONContent } from "@tiptap/core";
2import { createDetailsBlockNode, type DetailsBlockAttrs } from "@/client/components/editor/controllers/details-block";
3 
4interface DetailsNodeOptions extends DetailsBlockAttrs {
5 bid?: string | null;
6 contentText?: string;
7}
8 
9export function createParagraphNode(text: string, bid?: string | null): JSONContent {
10 return {
11 type: "paragraph",
12 ...(typeof bid === "undefined" ? {} : { attrs: { bid } }),
13 content: [{ type: "text", text }],
14 };
15}
16 
17export function createHeadingNode(text: string, level = 1): JSONContent {
18 return {
19 type: "heading",
20 attrs: { level },
21 content: [{ type: "text", text }],
22 };
23}
24 
25export function createDetailsNode(options: DetailsNodeOptions = {}): JSONContent {
26 const { bid, contentText, ...attrs } = options;
27 const node = createDetailsBlockNode(attrs);
28 
29 return {
30 ...node,
31 attrs:
32 typeof bid === "undefined"
33 ? node.attrs
34 : {
35 ...(node.attrs ?? {}),
36 bid,
37 },
38 content: node.content?.map((child) => {
39 if (child.type !== "detailsContent") {
40 return child;
41 }
42 
43 return {
44 ...child,
45 content: child.content?.map((contentChild) => {
46 if (contentChild.type !== "paragraph") {
47 return contentChild;
48 }
49 
50 return {
51 ...contentChild,
52 ...(typeof bid === "undefined" ? {} : { attrs: { bid: null } }),
53 ...(contentText ? { content: [{ type: "text", text: contentText }] } : {}),
54 };
55 }),
56 };
57 }),
58 };
59}