Skip to content

Commit c75ac27

Browse files
jkcsmatthewlipski
andauthored
fix: node conversion (#800)
* fix: node conversion * fix \n * test node conversions * fix: command `BNUpdateBlock` * Add a test for customBlock with line breaks. * add test for BNUpdateBlock with line break * fix type error * Refactored unit tests --------- Co-authored-by: Matthew Lipski <[email protected]>
1 parent dd886d0 commit c75ac27

File tree

22 files changed

+835
-13
lines changed

22 files changed

+835
-13
lines changed

examples/05-custom-schema/01-alert-block/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
align-items: center;
55
flex-grow: 1;
66
border-radius: 4px;
7-
height: 48px;
7+
min-height: 48px;
88
padding: 4px;
99
}
1010

packages/core/src/api/blockManipulation/__snapshots__/blockManipulation.test.ts.snap

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,3 +614,101 @@ exports[`Inserting Blocks with Different Placements > Insert nested inside exist
614614
},
615615
]
616616
`;
617+
618+
exports[`Update Line Breaks > Update custom block with line break 1`] = `
619+
[
620+
{
621+
"children": [],
622+
"content": [
623+
{
624+
"styles": {},
625+
"text": "Line1
626+
Line2",
627+
"type": "text",
628+
},
629+
],
630+
"id": "1",
631+
"props": {
632+
"backgroundColor": "default",
633+
"textAlignment": "left",
634+
"textColor": "default",
635+
},
636+
"type": "paragraph",
637+
},
638+
{
639+
"children": [],
640+
"content": [
641+
{
642+
"styles": {},
643+
"text": "Updated Custom Block with
644+
line
645+
break",
646+
"type": "text",
647+
},
648+
],
649+
"id": "2",
650+
"props": {},
651+
"type": "customBlock",
652+
},
653+
{
654+
"children": [],
655+
"content": [],
656+
"id": "0",
657+
"props": {
658+
"backgroundColor": "default",
659+
"textAlignment": "left",
660+
"textColor": "default",
661+
},
662+
"type": "paragraph",
663+
},
664+
]
665+
`;
666+
667+
exports[`Update Line Breaks > Update paragraph with line break 1`] = `
668+
[
669+
{
670+
"children": [],
671+
"content": [
672+
{
673+
"styles": {},
674+
"text": "Updated Custom Block with
675+
line
676+
break",
677+
"type": "text",
678+
},
679+
],
680+
"id": "1",
681+
"props": {
682+
"backgroundColor": "default",
683+
"textAlignment": "left",
684+
"textColor": "default",
685+
},
686+
"type": "paragraph",
687+
},
688+
{
689+
"children": [],
690+
"content": [
691+
{
692+
"styles": {},
693+
"text": "Line1
694+
Line2",
695+
"type": "text",
696+
},
697+
],
698+
"id": "2",
699+
"props": {},
700+
"type": "customBlock",
701+
},
702+
{
703+
"children": [],
704+
"content": [],
705+
"id": "0",
706+
"props": {
707+
"backgroundColor": "default",
708+
"textAlignment": "left",
709+
"textColor": "default",
710+
},
711+
"type": "paragraph",
712+
},
713+
]
714+
`;

packages/core/src/api/blockManipulation/blockManipulation.test.ts

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
11
import { afterEach, beforeEach, describe, expect, it } from "vitest";
22
import {
33
Block,
4-
DefaultBlockSchema,
4+
defaultBlockSpecs,
55
DefaultInlineContentSchema,
66
DefaultStyleSchema,
77
PartialBlock,
88
} from "../../blocks/defaultBlocks";
99
import { BlockNoteEditor } from "../../editor/BlockNoteEditor";
10+
import { createBlockSpec } from "../../schema";
11+
import { BlockNoteSchema } from "../../editor/BlockNoteSchema";
12+
13+
const CustomBlock = createBlockSpec(
14+
{
15+
type: "customBlock",
16+
propSchema: {},
17+
content: "inline",
18+
} as const,
19+
{
20+
render: () => {
21+
const dom = document.createElement("div");
22+
dom.className = "custom-block";
23+
24+
return {
25+
dom: dom,
26+
contentDOM: dom,
27+
};
28+
},
29+
}
30+
);
31+
32+
const schema = BlockNoteSchema.create({
33+
blockSpecs: {
34+
...defaultBlockSpecs,
35+
customBlock: CustomBlock,
36+
},
37+
});
1038

11-
let editor: BlockNoteEditor;
39+
let editor: BlockNoteEditor<typeof schema.blockSchema>;
1240
const div = document.createElement("div");
1341

1442
function waitForEditor() {
@@ -23,27 +51,36 @@ function waitForEditor() {
2351
}
2452

2553
let singleBlock: PartialBlock<
26-
DefaultBlockSchema,
54+
typeof schema.blockSchema,
2755
DefaultInlineContentSchema,
2856
DefaultStyleSchema
2957
>;
3058

3159
let multipleBlocks: PartialBlock<
32-
DefaultBlockSchema,
60+
typeof schema.blockSchema,
61+
DefaultInlineContentSchema,
62+
DefaultStyleSchema
63+
>[];
64+
65+
let blocksWithLineBreaks: PartialBlock<
66+
typeof schema.blockSchema,
3367
DefaultInlineContentSchema,
3468
DefaultStyleSchema
3569
>[];
3670

3771
let insert: (
3872
placement: "before" | "nested" | "after"
3973
) => Block<
40-
DefaultBlockSchema,
74+
typeof schema.blockSchema,
4175
DefaultInlineContentSchema,
4276
DefaultStyleSchema
4377
>[];
4478

4579
beforeEach(() => {
46-
editor = BlockNoteEditor.create();
80+
editor = BlockNoteEditor.create<typeof schema.blockSchema>({
81+
schema: schema,
82+
});
83+
4784
editor.mount(div);
4885

4986
singleBlock = {
@@ -86,6 +123,17 @@ beforeEach(() => {
86123
},
87124
];
88125

126+
blocksWithLineBreaks = [
127+
{
128+
type: "paragraph",
129+
content: "Line1\nLine2",
130+
},
131+
{
132+
type: "customBlock",
133+
content: "Line1\nLine2",
134+
},
135+
];
136+
89137
insert = (placement) => {
90138
const existingBlock = editor.document[0];
91139
editor.insertBlocks(multipleBlocks, existingBlock, placement);
@@ -122,7 +170,7 @@ describe("Test strong typing", () => {
122170
{
123171
type: "paragraph",
124172
props: {
125-
// @ts-expect-error level not suitable for paragraph
173+
// @ts-expect-error invalid type
126174
level: 1,
127175
},
128176
}
@@ -236,3 +284,34 @@ describe("Insert, Update, & Delete Blocks", () => {
236284
expect(editor.document).toMatchSnapshot();
237285
});
238286
});
287+
288+
describe("Update Line Breaks", () => {
289+
it("Update paragraph with line break", async () => {
290+
await waitForEditor();
291+
292+
const existingBlock = editor.document[0];
293+
editor.insertBlocks(blocksWithLineBreaks, existingBlock);
294+
295+
const newBlock = editor.document[0];
296+
editor.updateBlock(newBlock, {
297+
type: "paragraph",
298+
content: "Updated Custom Block with \nline \nbreak",
299+
});
300+
301+
expect(editor.document).toMatchSnapshot();
302+
});
303+
it("Update custom block with line break", async () => {
304+
await waitForEditor();
305+
306+
const existingBlock = editor.document[0];
307+
editor.insertBlocks(blocksWithLineBreaks, existingBlock);
308+
309+
const newBlock = editor.document[1];
310+
editor.updateBlock(newBlock, {
311+
type: "customBlock",
312+
content: "Updated Custom Block with \nline \nbreak",
313+
});
314+
315+
expect(editor.document).toMatchSnapshot();
316+
});
317+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bn-inline-content custom-block" data-editable="">Custom customBlock</div><div class="bn-inline-content custom-block" data-editable="">Custom customBlock <br>with <br>line breaks</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="1"><div class="bn-block" data-node-type="blockContainer" data-id="1"><div class="bn-block-content" data-content-type="customBlock"><div class="bn-inline-content custom-block" data-editable="">Custom customBlock</div></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="2"><div class="bn-block" data-node-type="blockContainer" data-id="2"><div class="bn-block-content" data-content-type="customBlock"><div class="bn-inline-content custom-block" data-editable="">Custom customBlock <br>with <br>line breaks</div></div></div></div></div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p class="custom-paragraph">Hello World</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="1"><div class="bn-block" data-node-type="blockContainer" data-id="1"><div class="bn-block-content" data-content-type="customParagraph"><p class="bn-inline-content custom-paragraph" data-editable="">Line 1<br>Line 2</p></div></div></div></div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p class="bn-inline-content">Line 1<br>Line 2</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="1"><div class="bn-block" data-node-type="blockContainer" data-id="1"><div class="bn-block-content" data-content-type="paragraph"><p class="bn-inline-content">Line 1<br>Line 2</p></div></div></div></div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Custom customBlock
2+
3+
Custom customBlock\
4+
with\
5+
line breaks
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Line 1\
2+
Line 2

0 commit comments

Comments
 (0)