Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Block update removing children when setting content #1103

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
]
`;
86 changes: 86 additions & 0 deletions packages/core/src/api/blockManipulation/blockManipulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ let singleBlock: PartialBlock<
DefaultStyleSchema
>;

let singleBlockWithChildren: PartialBlock<
typeof schema.blockSchema,
DefaultInlineContentSchema,
DefaultStyleSchema
>;

let multipleBlocks: PartialBlock<
typeof schema.blockSchema,
DefaultInlineContentSchema,
Expand Down Expand Up @@ -88,6 +94,17 @@ beforeEach(() => {
content: "Paragraph",
};

singleBlockWithChildren = {
type: "paragraph",
content: "Paragraph",
children: [
{
type: "paragraph",
content: "Nested Paragraph",
},
],
};

multipleBlocks = [
{
type: "heading",
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/pm-nodes/BlockContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export const BlockContainer = Node.create<{
state.tr
.replaceWith(
startPos,
endPos,
startPos + contentNode.nodeSize,
state.schema.nodes[newType].create(
{
...contentNode.attrs,
Expand Down
Loading