forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatConversionTestUtil.ts
More file actions
209 lines (194 loc) · 5.52 KB
/
formatConversionTestUtil.ts
File metadata and controls
209 lines (194 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// TODO: remove duplicate file
import {
Block,
BlockNoteSchema,
BlockSchema,
InlineContent,
InlineContentSchema,
isPartialLinkInlineContent,
isStyledTextInlineContent,
PartialBlock,
PartialInlineContent,
PartialTableCell,
StyledText,
StyleSchema,
TableCell,
TableContent,
UniqueID,
} from "@blocknote/core";
function textShorthandToStyledText(
content: string | StyledText<any>[] = "",
): StyledText<any>[] {
if (typeof content === "string") {
return [
{
type: "text",
text: content,
styles: {},
},
];
}
return content;
}
function partialContentToInlineContent(
content:
| PartialInlineContent<any, any>
| PartialTableCell<any, any>
| TableContent<any>
| undefined,
):
| InlineContent<any, any>[]
| TableContent<any>
| TableCell<any, any>
| undefined {
if (typeof content === "string") {
return textShorthandToStyledText(content);
}
if (Array.isArray(content)) {
return content.flatMap((partialContent) => {
if (typeof partialContent === "string") {
return textShorthandToStyledText(partialContent);
} else if (isPartialLinkInlineContent(partialContent)) {
return {
...partialContent,
content: textShorthandToStyledText(partialContent.content),
};
} else if (isStyledTextInlineContent(partialContent)) {
return partialContent;
} else {
// custom inline content
return {
props: {},
...partialContent,
content: partialContentToInlineContent(partialContent.content),
} as any;
}
});
} else if (content?.type === "tableContent") {
return {
type: "tableContent",
columnWidths: content.columnWidths,
headerRows: content.headerRows,
headerCols: content.headerCols,
rows: content.rows.map((row) => {
const cells: any[] = row.cells.map((cell) => {
if (!("type" in cell) || cell.type !== "tableCell") {
return partialContentToInlineContent({
type: "tableCell",
content: cell as any,
});
}
return partialContentToInlineContent(cell);
});
return {
...row,
cells,
};
}),
};
} else if (content?.type === "tableCell") {
return {
type: "tableCell",
content: partialContentToInlineContent(content.content) as any[],
props: {
backgroundColor: content.props?.backgroundColor ?? "default",
textColor: content.props?.textColor ?? "default",
textAlignment: content.props?.textAlignment ?? "left",
colspan: content.props?.colspan ?? 1,
rowspan: content.props?.rowspan ?? 1,
},
} satisfies TableCell<any, any>;
}
return content;
}
export function partialBlocksToBlocksForTesting<
BSchema extends BlockSchema,
I extends InlineContentSchema,
S extends StyleSchema,
>(
schema: BlockNoteSchema<BSchema, I, S>,
partialBlocks: Array<PartialBlock<BSchema, I, S>>,
): Array<Block<BSchema, I, S>> {
return partialBlocks.map((partialBlock) =>
partialBlockToBlockForTesting(schema.blockSchema, partialBlock),
);
}
export function partialBlockToBlockForTesting<
BSchema extends BlockSchema,
I extends InlineContentSchema,
S extends StyleSchema,
>(
schema: BSchema,
partialBlock: PartialBlock<BSchema, I, S>,
): Block<BSchema, I, S> {
const contentType: "inline" | "table" | "none" =
schema[partialBlock.type!].content;
const withDefaults: Block<BSchema, I, S> = {
id: "",
type: partialBlock.type!,
props: {} as any,
content:
contentType === "inline"
? []
: contentType === "table"
? {
type: "tableContent",
columnWidths: undefined,
headerRows: undefined,
headerCols: undefined,
rows: [],
}
: (undefined as any),
children: [] as any,
...partialBlock,
};
Object.entries(schema[partialBlock.type!].propSchema).forEach(
([propKey, propValue]) => {
if (
withDefaults.props[propKey] === undefined &&
propValue.default !== undefined
) {
(withDefaults.props as any)[propKey] = propValue.default;
}
},
);
if (contentType === "inline") {
const content = withDefaults.content as InlineContent<I, S>[] | undefined;
withDefaults.content = partialContentToInlineContent(content) as any;
} else if (contentType === "table") {
const content = withDefaults.content as TableContent<I, S> | undefined;
withDefaults.content = {
type: "tableContent",
columnWidths:
content?.columnWidths ||
content?.rows[0]?.cells.map(() => undefined) ||
[],
headerRows: content?.headerRows || undefined,
headerCols: content?.headerCols || undefined,
rows:
content?.rows.map((row) => ({
cells: row.cells.map((cell) => partialContentToInlineContent(cell)),
})) || [],
} as any;
}
return {
...withDefaults,
content: partialContentToInlineContent(withDefaults.content),
children: withDefaults.children.map((c) => {
return partialBlockToBlockForTesting(schema, c);
}),
} as any;
}
export function addIdsToBlock(block: PartialBlock<any, any, any>) {
if (!block.id) {
block.id = UniqueID.options.generateID();
}
if (block.children) {
addIdsToBlocks(block.children);
}
}
export function addIdsToBlocks(blocks: PartialBlock<any, any, any>[]) {
for (const block of blocks) {
addIdsToBlock(block);
}
}