Skip to content

Commit eecbca5

Browse files
authored
test: add test case for exporting custom block (#2150)
1 parent f1d6e89 commit eecbca5

File tree

7 files changed

+92
-23
lines changed

7 files changed

+92
-23
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div class="bn-block-group" data-node-type="blockGroup">
2+
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
3+
<div class="bn-block" data-node-type="blockContainer" data-id="1">
4+
<div class="bn-block-content" data-content-type="simpleCustomParagraph">
5+
<p class="bn-inline-content simple-custom-paragraph">Simple Custom Paragraph</p>
6+
</div>
7+
</div>
8+
</div>
9+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p class="simple-custom-paragraph">Simple Custom Paragraph</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Simple Custom Paragraph
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
{
3+
"attrs": {
4+
"id": "1",
5+
},
6+
"content": [
7+
{
8+
"attrs": {
9+
"backgroundColor": "default",
10+
"textAlignment": "left",
11+
"textColor": "default",
12+
},
13+
"content": [
14+
{
15+
"text": "Simple Custom Paragraph",
16+
"type": "text",
17+
},
18+
],
19+
"type": "simpleCustomParagraph",
20+
},
21+
],
22+
"type": "blockContainer",
23+
},
24+
]

tests/src/unit/core/formatConversion/export/exportTestInstances.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,18 @@ export const exportTestInstancesBlockNoteHTML: TestInstance<
17171717
},
17181718
executeTest: testExportBlockNoteHTML,
17191719
},
1720+
{
1721+
testCase: {
1722+
name: "custom-blocks/simpleCustomParagraph",
1723+
content: [
1724+
{
1725+
type: "simpleCustomParagraph",
1726+
content: "Simple Custom Paragraph",
1727+
},
1728+
],
1729+
},
1730+
executeTest: testExportBlockNoteHTML,
1731+
},
17201732
];
17211733

17221734
export const exportTestInstancesHTML: TestInstance<

tests/src/unit/core/schema/__snapshots__/blocks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@
242242
"extensions": undefined,
243243
"implementation": {
244244
"node": null,
245+
"parse": [Function],
245246
"render": [Function],
246247
"toExternalHTML": [Function],
247248
},

tests/src/unit/core/testSchema.ts

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
import {
22
BlockNoteSchema,
3-
addNodeAndExtensionsToSpec,
3+
createBlockConfig,
4+
createBlockSpec,
45
createImageBlockConfig,
56
createImageBlockSpec,
67
createInlineContentSpec,
78
createPageBreakBlockSpec,
89
createStyleSpec,
910
defaultProps,
11+
parseDefaultProps,
1012
} from "@blocknote/core";
1113

1214
// BLOCKS ----------------------------------------------------------------------
1315

1416
// This is a modified version of the default image block that does not implement
1517
// a `toExternalHTML` function. It's used to test if the custom serializer by
1618
// default serializes custom blocks using their `render` function.
17-
const SimpleImage = addNodeAndExtensionsToSpec(
18-
{
19-
type: "simpleImage",
20-
propSchema: createImageBlockConfig({}).propSchema,
21-
content: "none",
22-
},
19+
const SimpleImage = createBlockSpec(
20+
createBlockConfig(
21+
() =>
22+
({
23+
type: "simpleImage",
24+
propSchema: createImageBlockConfig({}).propSchema,
25+
content: "none",
26+
}) as const,
27+
),
2328
{
2429
render(block, editor) {
2530
return createImageBlockSpec().implementation.render.call(
@@ -31,13 +36,27 @@ const SimpleImage = addNodeAndExtensionsToSpec(
3136
},
3237
);
3338

34-
const CustomParagraph = addNodeAndExtensionsToSpec(
35-
{
36-
type: "customParagraph",
37-
propSchema: defaultProps,
38-
content: "inline",
39-
},
39+
const CustomParagraph = createBlockSpec(
40+
createBlockConfig(
41+
() =>
42+
({
43+
type: "customParagraph",
44+
propSchema: defaultProps,
45+
content: "inline",
46+
}) as const,
47+
),
4048
{
49+
parse: (e) => {
50+
if (e.tagName !== "P") {
51+
return undefined;
52+
}
53+
54+
if (e.classList.contains("custom-paragraph")) {
55+
return parseDefaultProps(e);
56+
}
57+
58+
return undefined;
59+
},
4160
render: () => {
4261
const paragraph = document.createElement("p");
4362
paragraph.className = "custom-paragraph";
@@ -50,7 +69,6 @@ const CustomParagraph = addNodeAndExtensionsToSpec(
5069
toExternalHTML: () => {
5170
const paragraph = document.createElement("p");
5271
paragraph.className = "custom-paragraph";
53-
paragraph.innerHTML = "Hello World";
5472

5573
return {
5674
dom: paragraph,
@@ -59,12 +77,15 @@ const CustomParagraph = addNodeAndExtensionsToSpec(
5977
},
6078
);
6179

62-
const SimpleCustomParagraph = addNodeAndExtensionsToSpec(
63-
{
64-
type: "simpleCustomParagraph",
65-
propSchema: defaultProps,
66-
content: "inline",
67-
},
80+
const SimpleCustomParagraph = createBlockSpec(
81+
createBlockConfig(
82+
() =>
83+
({
84+
type: "simpleCustomParagraph",
85+
propSchema: defaultProps,
86+
content: "inline",
87+
}) as const,
88+
),
6889
{
6990
render: () => {
7091
const paragraph = document.createElement("p");
@@ -198,9 +219,9 @@ const FontSize = createStyleSpec(
198219
export const testSchema = BlockNoteSchema.create().extend({
199220
blockSpecs: {
200221
pageBreak: createPageBreakBlockSpec(),
201-
customParagraph: CustomParagraph,
202-
simpleCustomParagraph: SimpleCustomParagraph,
203-
simpleImage: SimpleImage,
222+
customParagraph: CustomParagraph(),
223+
simpleCustomParagraph: SimpleCustomParagraph(),
224+
simpleImage: SimpleImage(),
204225
},
205226
inlineContentSpecs: {
206227
mention: Mention,

0 commit comments

Comments
 (0)