diff --git a/packages/core/src/api/blockManipulation/__snapshots__/blockManipulation.test.ts.snap b/packages/core/src/api/blockManipulation/__snapshots__/blockManipulation.test.ts.snap index 9a49952d7a..ecc3057eda 100644 --- a/packages/core/src/api/blockManipulation/__snapshots__/blockManipulation.test.ts.snap +++ b/packages/core/src/api/blockManipulation/__snapshots__/blockManipulation.test.ts.snap @@ -712,3 +712,210 @@ Line2", }, ] `; + +exports[`Update block cases > Update children only 1`] = ` +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Heading", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "level": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, + ], + "content": [ + { + "styles": {}, + "text": "Paragraph", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [], + "id": "0", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] +`; + +exports[`Update block cases > Update content and children 1`] = ` +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Heading", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "level": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, + ], + "content": [ + { + "styles": {}, + "text": "Updated Paragraph", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [], + "id": "0", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] +`; + +exports[`Update block cases > Update content only 1`] = ` +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested Paragraph", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Updated Paragraph", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [], + "id": "0", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] +`; + +exports[`Update block cases > Update type only 1`] = ` +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Nested Paragraph", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Paragraph", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "level": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, + { + "children": [], + "content": [], + "id": "0", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] +`; diff --git a/packages/core/src/api/blockManipulation/blockManipulation.test.ts b/packages/core/src/api/blockManipulation/blockManipulation.test.ts index 6bef807c80..58bd244a85 100644 --- a/packages/core/src/api/blockManipulation/blockManipulation.test.ts +++ b/packages/core/src/api/blockManipulation/blockManipulation.test.ts @@ -56,6 +56,12 @@ let singleBlock: PartialBlock< DefaultStyleSchema >; +let singleBlockWithChildren: PartialBlock< + typeof schema.blockSchema, + DefaultInlineContentSchema, + DefaultStyleSchema +>; + let multipleBlocks: PartialBlock< typeof schema.blockSchema, DefaultInlineContentSchema, @@ -88,6 +94,17 @@ beforeEach(() => { content: "Paragraph", }; + singleBlockWithChildren = { + type: "paragraph", + content: "Paragraph", + children: [ + { + type: "paragraph", + content: "Nested Paragraph", + }, + ], + }; + multipleBlocks = [ { type: "heading", @@ -285,6 +302,75 @@ describe("Insert, Update, & Delete Blocks", () => { }); }); +describe("Update block cases", () => { + it("Update type only", async () => { + await waitForEditor(); + + const existingBlock = editor.document[0]; + editor.insertBlocks([singleBlockWithChildren], existingBlock); + + const newBlock = editor.document[0]; + editor.updateBlock(newBlock, { + type: "heading", + }); + + expect(editor.document).toMatchSnapshot(); + }); + + it("Update content only", async () => { + await waitForEditor(); + + const existingBlock = editor.document[0]; + editor.insertBlocks([singleBlockWithChildren], existingBlock); + + const newBlock = editor.document[0]; + editor.updateBlock(newBlock, { + content: "Updated Paragraph", + }); + + expect(editor.document).toMatchSnapshot(); + }); + + it("Update children only", async () => { + await waitForEditor(); + + const existingBlock = editor.document[0]; + editor.insertBlocks([singleBlockWithChildren], existingBlock); + + const newBlock = editor.document[0]; + editor.updateBlock(newBlock, { + children: [ + { + type: "heading", + content: "Heading", + }, + ], + }); + + expect(editor.document).toMatchSnapshot(); + }); + + it("Update content and children", async () => { + await waitForEditor(); + + const existingBlock = editor.document[0]; + editor.insertBlocks([singleBlockWithChildren], existingBlock); + + const newBlock = editor.document[0]; + editor.updateBlock(newBlock, { + content: "Updated Paragraph", + children: [ + { + type: "heading", + content: "Heading", + }, + ], + }); + + expect(editor.document).toMatchSnapshot(); + }); +}); + describe("Update Line Breaks", () => { it("Update paragraph with line break", async () => { await waitForEditor(); diff --git a/packages/core/src/pm-nodes/BlockContainer.ts b/packages/core/src/pm-nodes/BlockContainer.ts index aa476f2ea5..6ead8b3ded 100644 --- a/packages/core/src/pm-nodes/BlockContainer.ts +++ b/packages/core/src/pm-nodes/BlockContainer.ts @@ -286,7 +286,7 @@ export const BlockContainer = Node.create<{ state.tr .replaceWith( startPos, - endPos, + startPos + contentNode.nodeSize, state.schema.nodes[newType].create( { ...contentNode.attrs,