Skip to content

Commit 18feb06

Browse files
authored
Fix OnChange Event Invocation (codex-team#2007)
* fix: call onchange event after block insert * changelog updated * patch version updated * removed the modification observer from saver * only changelog version added * delimiter added * feat: test case added for save inside the onchange
1 parent 32dcd3f commit 18feb06

5 files changed

Lines changed: 85 additions & 10 deletions

File tree

docs/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### 2.23.3
44

55
- `Improvement`*Dev Example Page* - Server added to allow opening example page on other devices in network.
6+
- `Fix``OnChange` event invocation after block insertion. [#1997](https://github.com/codex-team/editor.js/issues/1997)
67

78
### 2.23.2
89

@@ -434,4 +435,4 @@ See a whole [Changelog](/docs/)
434435
- `New` New [Editor.js PHP](http://github.com/codex-team/codex.editor.backend) — example of server-side implementation with HTML purifying and data validation.
435436
- `Improvements` - Improvements of Toolbar's position calculation.
436437
- `Improvements` — Improved zero-configuration initialization.
437-
- and many little improvements.
438+
- and many little improvements.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/editorjs",
3-
"version": "2.23.2",
3+
"version": "2.23.3",
44
"description": "Editor.js — Native JS, based on API and Open Source",
55
"main": "dist/editor.js",
66
"types": "./types/index.d.ts",
@@ -52,6 +52,7 @@
5252
"@cypress/code-coverage": "^3.9.2",
5353
"@cypress/webpack-preprocessor": "^5.6.0",
5454
"@editorjs/code": "^2.7.0",
55+
"@editorjs/delimiter": "^1.2.0",
5556
"@editorjs/header": "^2.6.1",
5657
"@editorjs/simple-image": "^1.4.1",
5758
"@types/node": "^14.14.35",

src/components/modules/saver.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,11 @@ export default class Saver extends Module {
2828
* @returns {OutputData}
2929
*/
3030
public async save(): Promise<OutputData> {
31-
const { BlockManager, Tools, ModificationsObserver } = this.Editor;
31+
const { BlockManager, Tools } = this.Editor;
3232
const blocks = BlockManager.blocks,
3333
chainData = [];
3434

3535
try {
36-
/**
37-
* Disable onChange callback on save to not to spam those events
38-
*/
39-
ModificationsObserver.disable();
4036

4137
blocks.forEach((block: Block) => {
4238
chainData.push(this.getSavedData(block));
@@ -50,9 +46,7 @@ export default class Saver extends Module {
5046
return this.makeOutput(sanitizedData);
5147
} catch (e) {
5248
_.logLabeled(`Saving failed due to the Error %o`, 'error', e);
53-
} finally {
54-
ModificationsObserver.enable();
55-
}
49+
}
5650
}
5751

5852
/**

test/cypress/tests/onchange.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Header from '@editorjs/header';
22
import Code from '@editorjs/code';
3+
import Delimiter from '@editorjs/delimiter';
34
import { BlockMutationType } from '../../../types/events/block/mutation-type';
45

56
/**
@@ -32,6 +33,32 @@ describe('onChange callback', () => {
3233
cy.createEditor(config).as('editorInstance');
3334
}
3435

36+
/**
37+
* Creates Editor instance with save inside the onChange event.
38+
*
39+
* @param blocks - list of blocks to prefill the editor
40+
*/
41+
function createEditorWithSave(blocks = null): void {
42+
const config = {
43+
tools: {
44+
header: Header,
45+
code: Code,
46+
delimiter: Delimiter,
47+
},
48+
onChange: (api, event): void => {
49+
console.log('something changed', api, event);
50+
api.saver.save();
51+
},
52+
data: blocks ? {
53+
blocks,
54+
} : null,
55+
};
56+
57+
cy.spy(config, 'onChange').as('onChange');
58+
59+
cy.createEditor(config).as('editorInstance');
60+
}
61+
3562
/**
3663
* EditorJS API is passed as the first parameter of the onChange callback
3764
*/
@@ -92,6 +119,53 @@ describe('onChange callback', () => {
92119
}));
93120
});
94121

122+
it('should fire onChange callback on block insertion with save inside onChange', () => {
123+
createEditorWithSave();
124+
125+
cy.get('[data-cy=editorjs]')
126+
.get('div.ce-block')
127+
.click();
128+
129+
cy.get('[data-cy=editorjs]')
130+
.get('div.ce-toolbar__plus')
131+
.click();
132+
133+
cy.get('[data-cy=editorjs]')
134+
.get('li.ce-toolbox__button[data-tool=delimiter]')
135+
.click();
136+
137+
cy.get('@onChange').should('be.calledThrice');
138+
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
139+
type: BlockMutationType.Removed,
140+
detail: {
141+
index: 0,
142+
target: {
143+
name: 'paragraph',
144+
},
145+
},
146+
}));
147+
148+
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
149+
type: BlockMutationType.Added,
150+
detail: {
151+
index: 0,
152+
target: {
153+
name: 'delimiter',
154+
},
155+
},
156+
}));
157+
158+
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
159+
type: BlockMutationType.Added,
160+
detail: {
161+
index: 1,
162+
target: {
163+
name: 'paragraph',
164+
},
165+
},
166+
}));
167+
});
168+
95169
it('should fire onChange callback on block replacement for both of blocks', () => {
96170
createEditor();
97171

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,11 @@
14281428
resolved "https://registry.yarnpkg.com/@editorjs/code/-/code-2.7.0.tgz#0a21de9ac15e4533605ffcc80969513ab2142ac5"
14291429
integrity sha512-gXtTce915fHp3H9i4IqhTxEDbbkT2heFfYiW/bhFHsCmZDpyGzfZxi94kmrEqDmbxXjV49ZZ6GZbR26If13KJw==
14301430

1431+
"@editorjs/delimiter@^1.2.0":
1432+
version "1.2.0"
1433+
resolved "https://registry.yarnpkg.com/@editorjs/delimiter/-/delimiter-1.2.0.tgz#5075f1a3e68765cfb6aec8694b316d81e2b41607"
1434+
integrity sha512-GKsCFPk85vH5FuCuVQ48NTLc9hk0T3DsBH9zABaicTYIJayFcUa8N4/Y+L3i4tduzDqqyvoxkv+5n43GmC5gEA==
1435+
14311436
"@editorjs/header@^2.6.1":
14321437
version "2.6.1"
14331438
resolved "https://registry.yarnpkg.com/@editorjs/header/-/header-2.6.1.tgz#454a46e4dbb32ae3aa1db4d22b0ddf2cc36c3134"

0 commit comments

Comments
 (0)