Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- `Improvement` - Removed "Narrow" ("Thin") mode. On non-mobile viewports, Plus and Block Tunes controls now use a responsive action rail beside the editor content.
- `Improvement` - `paste` config supports filter functions for tag-based substitution
- `Fix` - "Convert to" is not shown in the Inline Toolbar when there are no tools to convert to
- `Fix` - `blocks.insertMany()` now triggers `onChange` with a `block-added` event for each inserted block

### 2.31.7

Expand Down
8 changes: 8 additions & 0 deletions src/components/modules/blockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ export default class BlockManager extends Module {
*/
public insertMany(blocks: Block[], index = 0): void {
this._blocks.insertMany(blocks, index);

const firstBlockIndex = this.getBlockIndex(blocks[0]);

blocks.forEach((block, blockIndex) => {
this.blockDidMutated(BlockAddedMutationType, block, {
index: firstBlockIndex + blockIndex,
});
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/components/modules/modificationsObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class ModificationsObserver extends Module {
/**
* Flag shows onChange event is disabled
*/
private disabled = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems unexpected. I'd suggest explicitly call disable/enable on initial render.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure it is covered by tests

private disabled = true;

/**
* Blocks wrapper mutation observer instance
Expand Down
55 changes: 55 additions & 0 deletions test/cypress/tests/onchange.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,61 @@ describe('onChange callback', () => {
}));
});

it('should batch block-added events with their targets and indices on insertMany', () => {
cy.createEditor({
onChange: cy.stub().as('onChange'),
data: {
blocks: [
{
type: 'paragraph',
data: { text: 'First block' },
},
{
type: 'paragraph',
data: { text: 'Last block' },
},
],
},
}).as('editorInstance');

cy.wait(modificationsObserverBatchTimeout);
cy.get('@onChange').should('not.be.called');

cy.get<EditorJS>('@editorInstance').then(editor => {
editor.blocks.insertMany([
{
id: 'bulk-first',
type: 'paragraph',
data: { text: 'Inserted first' },
},
{
id: 'bulk-second',
type: 'paragraph',
data: { text: 'Inserted second' },
},
], 1);
});

cy.get('@onChange').should('be.calledWithBatchedEvents', [
{
type: BlockAddedMutationType,
detail: {
index: 1,
target: { id: 'bulk-first' },
},
},
{
type: BlockAddedMutationType,
detail: {
index: 2,
target: { id: 'bulk-second' },
},
},
]);
cy.get('@onChange').its('firstCall.args.1')
.should('have.length', 2);
});

it('should not be called when editor is initialized with readOnly mode', () => {
const config = {
readOnly: true,
Expand Down