Skip to content

Commit ecddc65

Browse files
fix: Block update removing children when setting content (#1103)
* Fixed block update removing children when setting content * Added tests
1 parent 19e5e5b commit ecddc65

File tree

3 files changed

+294
-1
lines changed

3 files changed

+294
-1
lines changed

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

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,3 +712,210 @@ Line2",
712712
},
713713
]
714714
`;
715+
716+
exports[`Update block cases > Update children only 1`] = `
717+
[
718+
{
719+
"children": [
720+
{
721+
"children": [],
722+
"content": [
723+
{
724+
"styles": {},
725+
"text": "Heading",
726+
"type": "text",
727+
},
728+
],
729+
"id": "3",
730+
"props": {
731+
"backgroundColor": "default",
732+
"level": 1,
733+
"textAlignment": "left",
734+
"textColor": "default",
735+
},
736+
"type": "heading",
737+
},
738+
],
739+
"content": [
740+
{
741+
"styles": {},
742+
"text": "Paragraph",
743+
"type": "text",
744+
},
745+
],
746+
"id": "1",
747+
"props": {
748+
"backgroundColor": "default",
749+
"textAlignment": "left",
750+
"textColor": "default",
751+
},
752+
"type": "paragraph",
753+
},
754+
{
755+
"children": [],
756+
"content": [],
757+
"id": "0",
758+
"props": {
759+
"backgroundColor": "default",
760+
"textAlignment": "left",
761+
"textColor": "default",
762+
},
763+
"type": "paragraph",
764+
},
765+
]
766+
`;
767+
768+
exports[`Update block cases > Update content and children 1`] = `
769+
[
770+
{
771+
"children": [
772+
{
773+
"children": [],
774+
"content": [
775+
{
776+
"styles": {},
777+
"text": "Heading",
778+
"type": "text",
779+
},
780+
],
781+
"id": "3",
782+
"props": {
783+
"backgroundColor": "default",
784+
"level": 1,
785+
"textAlignment": "left",
786+
"textColor": "default",
787+
},
788+
"type": "heading",
789+
},
790+
],
791+
"content": [
792+
{
793+
"styles": {},
794+
"text": "Updated Paragraph",
795+
"type": "text",
796+
},
797+
],
798+
"id": "1",
799+
"props": {
800+
"backgroundColor": "default",
801+
"textAlignment": "left",
802+
"textColor": "default",
803+
},
804+
"type": "paragraph",
805+
},
806+
{
807+
"children": [],
808+
"content": [],
809+
"id": "0",
810+
"props": {
811+
"backgroundColor": "default",
812+
"textAlignment": "left",
813+
"textColor": "default",
814+
},
815+
"type": "paragraph",
816+
},
817+
]
818+
`;
819+
820+
exports[`Update block cases > Update content only 1`] = `
821+
[
822+
{
823+
"children": [
824+
{
825+
"children": [],
826+
"content": [
827+
{
828+
"styles": {},
829+
"text": "Nested Paragraph",
830+
"type": "text",
831+
},
832+
],
833+
"id": "2",
834+
"props": {
835+
"backgroundColor": "default",
836+
"textAlignment": "left",
837+
"textColor": "default",
838+
},
839+
"type": "paragraph",
840+
},
841+
],
842+
"content": [
843+
{
844+
"styles": {},
845+
"text": "Updated Paragraph",
846+
"type": "text",
847+
},
848+
],
849+
"id": "1",
850+
"props": {
851+
"backgroundColor": "default",
852+
"textAlignment": "left",
853+
"textColor": "default",
854+
},
855+
"type": "paragraph",
856+
},
857+
{
858+
"children": [],
859+
"content": [],
860+
"id": "0",
861+
"props": {
862+
"backgroundColor": "default",
863+
"textAlignment": "left",
864+
"textColor": "default",
865+
},
866+
"type": "paragraph",
867+
},
868+
]
869+
`;
870+
871+
exports[`Update block cases > Update type only 1`] = `
872+
[
873+
{
874+
"children": [
875+
{
876+
"children": [],
877+
"content": [
878+
{
879+
"styles": {},
880+
"text": "Nested Paragraph",
881+
"type": "text",
882+
},
883+
],
884+
"id": "2",
885+
"props": {
886+
"backgroundColor": "default",
887+
"textAlignment": "left",
888+
"textColor": "default",
889+
},
890+
"type": "paragraph",
891+
},
892+
],
893+
"content": [
894+
{
895+
"styles": {},
896+
"text": "Paragraph",
897+
"type": "text",
898+
},
899+
],
900+
"id": "1",
901+
"props": {
902+
"backgroundColor": "default",
903+
"level": 1,
904+
"textAlignment": "left",
905+
"textColor": "default",
906+
},
907+
"type": "heading",
908+
},
909+
{
910+
"children": [],
911+
"content": [],
912+
"id": "0",
913+
"props": {
914+
"backgroundColor": "default",
915+
"textAlignment": "left",
916+
"textColor": "default",
917+
},
918+
"type": "paragraph",
919+
},
920+
]
921+
`;

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ let singleBlock: PartialBlock<
4545
DefaultStyleSchema
4646
>;
4747

48+
let singleBlockWithChildren: PartialBlock<
49+
typeof schema.blockSchema,
50+
DefaultInlineContentSchema,
51+
DefaultStyleSchema
52+
>;
53+
4854
let multipleBlocks: PartialBlock<
4955
typeof schema.blockSchema,
5056
DefaultInlineContentSchema,
@@ -77,6 +83,17 @@ beforeEach(() => {
7783
content: "Paragraph",
7884
};
7985

86+
singleBlockWithChildren = {
87+
type: "paragraph",
88+
content: "Paragraph",
89+
children: [
90+
{
91+
type: "paragraph",
92+
content: "Nested Paragraph",
93+
},
94+
],
95+
};
96+
8097
multipleBlocks = [
8198
{
8299
type: "heading",
@@ -264,6 +281,75 @@ describe("Insert, Update, & Delete Blocks", () => {
264281
});
265282
});
266283

284+
describe("Update block cases", () => {
285+
it("Update type only", async () => {
286+
await waitForEditor();
287+
288+
const existingBlock = editor.document[0];
289+
editor.insertBlocks([singleBlockWithChildren], existingBlock);
290+
291+
const newBlock = editor.document[0];
292+
editor.updateBlock(newBlock, {
293+
type: "heading",
294+
});
295+
296+
expect(editor.document).toMatchSnapshot();
297+
});
298+
299+
it("Update content only", async () => {
300+
await waitForEditor();
301+
302+
const existingBlock = editor.document[0];
303+
editor.insertBlocks([singleBlockWithChildren], existingBlock);
304+
305+
const newBlock = editor.document[0];
306+
editor.updateBlock(newBlock, {
307+
content: "Updated Paragraph",
308+
});
309+
310+
expect(editor.document).toMatchSnapshot();
311+
});
312+
313+
it("Update children only", async () => {
314+
await waitForEditor();
315+
316+
const existingBlock = editor.document[0];
317+
editor.insertBlocks([singleBlockWithChildren], existingBlock);
318+
319+
const newBlock = editor.document[0];
320+
editor.updateBlock(newBlock, {
321+
children: [
322+
{
323+
type: "heading",
324+
content: "Heading",
325+
},
326+
],
327+
});
328+
329+
expect(editor.document).toMatchSnapshot();
330+
});
331+
332+
it("Update content and children", async () => {
333+
await waitForEditor();
334+
335+
const existingBlock = editor.document[0];
336+
editor.insertBlocks([singleBlockWithChildren], existingBlock);
337+
338+
const newBlock = editor.document[0];
339+
editor.updateBlock(newBlock, {
340+
content: "Updated Paragraph",
341+
children: [
342+
{
343+
type: "heading",
344+
content: "Heading",
345+
},
346+
],
347+
});
348+
349+
expect(editor.document).toMatchSnapshot();
350+
});
351+
});
352+
267353
describe("Update Line Breaks", () => {
268354
it("Update paragraph with line break", () => {
269355
const existingBlock = editor.document[0];

packages/core/src/pm-nodes/BlockContainer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export const BlockContainer = Node.create<{
286286
state.tr
287287
.replaceWith(
288288
startPos,
289-
endPos,
289+
startPos + contentNode.nodeSize,
290290
state.schema.nodes[newType].create(
291291
{
292292
...contentNode.attrs,

0 commit comments

Comments
 (0)