Skip to content
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
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- `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` - Invoke tool `reset()` methods on `Tools.destroy()`, catching and logging any errors as warnings
- `Fix` - `blocks.insertMany()` now triggers `onChange` with a `block-added` event for each inserted block

### 2.31.7

Expand Down
7 changes: 6 additions & 1 deletion src/components/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,15 @@
this.validate();
this.init();
await this.start();
await this.render();

const { BlockManager, Caret, UI, ModificationsObserver } = this.moduleInstances;

/**
* Disable modifications observer during initial rendering
*/
ModificationsObserver.disable();
await this.render();

UI.checkEmptiness();
ModificationsObserver.enable();

Expand Down Expand Up @@ -105,8 +110,8 @@
/**
* If holderId is preset, assign him to holder property and work next only with holder
*/
_.deprecationAssert(!!this.config.holderId, 'config.holderId', 'config.holder');

Check warning on line 113 in src/components/core.ts

View workflow job for this annotation

GitHub Actions / ESlint

Unexpected value in conditional. A boolean expression is required
if (this.config.holderId && !this.config.holder) {

Check warning on line 114 in src/components/core.ts

View workflow job for this annotation

GitHub Actions / ESlint

Unexpected value in conditional. A boolean expression is required

Check warning on line 114 in src/components/core.ts

View workflow job for this annotation

GitHub Actions / ESlint

Unexpected value in conditional. A boolean expression is required
this.config.holder = this.config.holderId;
this.config.holderId = null;
}
Expand All @@ -118,7 +123,7 @@
this.config.holder = 'editorjs';
}

if (!this.config.logLevel) {

Check warning on line 126 in src/components/core.ts

View workflow job for this annotation

GitHub Actions / ESlint

Unexpected value in conditional. A boolean expression is required
this.config.logLevel = _.LogLevels.VERBOSE;
}

Expand All @@ -128,7 +133,7 @@
* If default Block's Tool was not passed, use the Paragraph Tool
*/
_.deprecationAssert(Boolean(this.config.initialBlock), 'config.initialBlock', 'config.defaultBlock');
this.config.defaultBlock = this.config.defaultBlock || this.config.initialBlock || 'paragraph';

Check warning on line 136 in src/components/core.ts

View workflow job for this annotation

GitHub Actions / ESlint

Unexpected nullable string value in conditional. Please handle the nullish/empty cases explicitly

/**
* Height of Editor's bottom area that allows to set focus on the last Block
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
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
Loading