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(store): deleteBlock should delete children recursively by default #5224

Merged
merged 6 commits into from
Nov 8, 2023
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 @@ -47,7 +47,9 @@ export function convertToList(
children: model.children,
...otherProperties,
};
page.deleteBlock(model);
page.deleteBlock(model, {
deleteChildren: false,
});

const id = page.addBlock('affine:list', blockProps, parent, index);
asyncFocusRichText(page, id);
Expand Down Expand Up @@ -78,7 +80,9 @@ export function convertToParagraph(
text: model.text?.clone(),
children: model.children,
};
page.deleteBlock(model);
page.deleteBlock(model, {
deleteChildren: false,
});

const id = page.addBlock('affine:paragraph', blockProps, parent, index);
asyncFocusRichText(page, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,9 @@ function handleListBlockBackspace(page: Page, model: ExtendedModel) {
children: model.children,
};
page.captureSync();
page.deleteBlock(model);
page.deleteBlock(model, {
deleteChildren: false,
});
const id = page.addBlock('affine:paragraph', blockProps, parent, index);
asyncFocusRichText(page, id);
return true;
Expand Down
8 changes: 6 additions & 2 deletions packages/blocks/src/_legacy/service/json2block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ export async function json2block(
assertExists(lastMergedModel);

lastMergedModel.text?.join(splitSuffixModel.text as Text);
page.deleteBlock(splitSuffixModel);
page.deleteBlock(splitSuffixModel, {
deleteChildren: false,
});
}

/**
Expand Down Expand Up @@ -201,7 +203,9 @@ export async function json2block(
}

if (isFocusedBlockEmpty && !shouldMergeFirstBlock) {
page.deleteBlock(focusedBlockModel);
page.deleteBlock(focusedBlockModel, {
deleteChildren: false,
});
}
}

Expand Down
25 changes: 12 additions & 13 deletions packages/lit/src/utils/range-synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,18 @@ export class RangeSynchronizer {
endText.delete(0, to.length);
startText.join(endText);
}
});
// make each delete operation in one transaction to ensure
// `deleteBlock` works correctly
// For example:
// aaa
// bbb
// In this case, if we delete `aaa` firstly, then delete `bbb`,
// the `deleteBlock` will fail when it delete `bbb` because `aaa` is already deleted
// but `deleteBlock` still try to get the parent of `bbb` which is `aaa`
blocks.slice(1).forEach(block => {
this.root.page.transact(() => {
this.root.page.deleteBlock(block.model);
});

blocks
.slice(1)
// delete from lowest to highest
.reverse()
.forEach(block => {
const parent = this.root.page.getParent(block.model);
assertExists(parent);
this.root.page.deleteBlock(block.model, {
bringChildrenTo: parent,
});
});
});

const newSelection = this._selectionManager.getInstance('text', {
Expand Down
247 changes: 242 additions & 5 deletions packages/store/src/__tests__/workspace.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,23 +435,260 @@ describe('addBlock', () => {
});

describe('deleteBlock', () => {
it('can delete single model', async () => {
it('delete children recursively by default', async () => {
const page = await createTestPage();

page.addBlock('affine:page', {
title: new page.Text(),
const pageId = page.addBlock('affine:page', {});
const noteId = page.addBlock('affine:note', {}, pageId);
page.addBlock('affine:paragraph', {}, noteId);
page.addBlock('affine:paragraph', {}, noteId);
assert.deepEqual(serializeWorkspace(page.doc).spaces[spaceId].blocks, {
'0': {
'prop:title': '',
'sys:children': ['1'],
'sys:flavour': 'affine:page',
'sys:id': '0',
},
'1': {
'prop:background': '--affine-background-secondary-color',
'prop:hidden': false,
'prop:index': 'a0',
'prop:xywh': '[0,0,800,95]',
'sys:children': ['2', '3'],
'sys:flavour': 'affine:note',
'sys:id': '1',
},
'2': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '2',
},
'3': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '3',
},
});

const deletedModel = page.getBlockById('1') as BaseBlockModel;
page.deleteBlock(deletedModel);

assert.deepEqual(serializeWorkspace(page.doc).spaces[spaceId].blocks, {
'0': {
'prop:title': '',
'sys:children': [],
'sys:flavour': 'affine:page',
'sys:id': '0',
},
});
});

it('bring children to parent', async () => {
const page = await createTestPage();

const pageId = page.addBlock('affine:page', {});
const noteId = page.addBlock('affine:note', {}, pageId);
const p1 = page.addBlock('affine:paragraph', {}, noteId);
page.addBlock('affine:paragraph', {}, p1);
page.addBlock('affine:paragraph', {}, p1);

assert.deepEqual(serializeWorkspace(page.doc).spaces[spaceId].blocks, {
'0': {
'prop:title': '',
'sys:children': ['1'],
'sys:flavour': 'affine:page',
'sys:id': '0',
},
'1': {
'prop:background': '--affine-background-secondary-color',
'prop:hidden': false,
'prop:index': 'a0',
'prop:xywh': '[0,0,800,95]',
'sys:children': ['2'],
'sys:flavour': 'affine:note',
'sys:id': '1',
},
'2': {
'prop:text': '',
'prop:type': 'text',
'sys:children': ['3', '4'],
'sys:flavour': 'affine:paragraph',
'sys:id': '2',
},
'3': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '3',
},
'4': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '4',
},
});

page.deleteBlock(page.root as BaseBlockModel);
assert.deepEqual(serializeWorkspace(page.doc).spaces[spaceId].blocks, {});
const deletedModel = page.getBlockById('2') as BaseBlockModel;
const deletedModelParent = page.getBlockById('1') as BaseBlockModel;
page.deleteBlock(deletedModel, {
bringChildrenTo: deletedModelParent,
});

assert.deepEqual(serializeWorkspace(page.doc).spaces[spaceId].blocks, {
'0': {
'prop:title': '',
'sys:children': ['1'],
'sys:flavour': 'affine:page',
'sys:id': '0',
},
'1': {
'prop:background': '--affine-background-secondary-color',
'prop:hidden': false,
'prop:index': 'a0',
'prop:xywh': '[0,0,800,95]',
'sys:children': ['3', '4'],
'sys:flavour': 'affine:note',
'sys:id': '1',
},
'3': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '3',
},
'4': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '4',
},
});
});

it('bring children to other block', async () => {
const page = await createTestPage();

const pageId = page.addBlock('affine:page', {});
const noteId = page.addBlock('affine:note', {}, pageId);
const p1 = page.addBlock('affine:paragraph', {}, noteId);
const p2 = page.addBlock('affine:paragraph', {}, noteId);
page.addBlock('affine:paragraph', {}, p1);
page.addBlock('affine:paragraph', {}, p1);
page.addBlock('affine:paragraph', {}, p2);

assert.deepEqual(serializeWorkspace(page.doc).spaces[spaceId].blocks, {
'0': {
'prop:title': '',
'sys:children': ['1'],
'sys:flavour': 'affine:page',
'sys:id': '0',
},
'1': {
'prop:background': '--affine-background-secondary-color',
'prop:hidden': false,
'prop:index': 'a0',
'prop:xywh': '[0,0,800,95]',
'sys:children': ['2', '3'],
'sys:flavour': 'affine:note',
'sys:id': '1',
},
'2': {
'prop:text': '',
'prop:type': 'text',
'sys:children': ['4', '5'],
'sys:flavour': 'affine:paragraph',
'sys:id': '2',
},
'3': {
'prop:text': '',
'prop:type': 'text',
'sys:children': ['6'],
'sys:flavour': 'affine:paragraph',
'sys:id': '3',
},
'4': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '4',
},
'5': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '5',
},
'6': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '6',
},
});

const deletedModel = page.getBlockById('2') as BaseBlockModel;
const moveToModel = page.getBlockById('3') as BaseBlockModel;
page.deleteBlock(deletedModel, {
bringChildrenTo: moveToModel,
});

assert.deepEqual(serializeWorkspace(page.doc).spaces[spaceId].blocks, {
'0': {
'prop:title': '',
'sys:children': ['1'],
'sys:flavour': 'affine:page',
'sys:id': '0',
},
'1': {
'prop:background': '--affine-background-secondary-color',
'prop:hidden': false,
'prop:index': 'a0',
'prop:xywh': '[0,0,800,95]',
'sys:children': ['3'],
'sys:flavour': 'affine:note',
'sys:id': '1',
},
'3': {
'prop:text': '',
'prop:type': 'text',
'sys:children': ['6', '4', '5'],
'sys:flavour': 'affine:paragraph',
'sys:id': '3',
},
'4': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '4',
},
'5': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '5',
},
'6': {
'prop:text': '',
'prop:type': 'text',
'sys:children': [],
'sys:flavour': 'affine:paragraph',
'sys:id': '6',
},
});
});

it('can delete model with parent', async () => {
Expand Down
Loading
Loading