Skip to content

Commit 4f15bbc

Browse files
authored
feat(onchange): callback now accepts custom event (#1791)
* feat(onchange): callback now accepts custom event * Delete block-added.ts * testd updated, changelog added * Update example-dev.html * indexes added to all events * block-removed dispatching on block replacing * Update example-dev.html
1 parent 1433c1a commit 4f15bbc

File tree

10 files changed

+216
-59
lines changed

10 files changed

+216
-59
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### 2.23.0
4+
5+
- `Improvement` — The `onChange` callback now accepts two arguments: EditorJS API and the CustomEvent with `type` and `detail` allowing to determine what happened with a Block
6+
37
### 2.22.3
48

59
- `Fix` — Tool config is passed to `prepare` method [editor-js/embed#68](https://github.com/editor-js/embed/issues/68)

docs/installation.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ var editor = new EditorJS({
192192
193193
/**
194194
* onChange callback
195+
* Accepts CustomEvent describing what happened
195196
*/
196-
onChange: (editorAPI, affectedBlockAPI) => {console.log('Now I know that Editor\'s content changed!')}
197+
onChange: (editorAPI, event) => {console.log('Now I know that Editor\'s content changed!')}
197198
});
198199
```
199200

example/example-dev.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@
318318
onReady: function(){
319319
saveButton.click();
320320
},
321-
onChange: function(api, block) {
322-
console.log('something changed', block);
321+
onChange: function(api, event) {
322+
console.log('something changed', event);
323323
},
324324
});
325325

example/example.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@
281281
onReady: function(){
282282
saveButton.click();
283283
},
284-
onChange: function(api, block) {
285-
console.log('something changed', block);
284+
onChange: function(api, event) {
285+
console.log('something changed', event);
286286
}
287287
});
288288

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/editorjs",
3-
"version": "2.22.3",
3+
"version": "2.23.0",
44
"description": "Editor.js — Native JS, based on API and Open Source",
55
"main": "dist/editor.js",
66
"types": "./types/index.d.ts",
@@ -29,7 +29,8 @@
2929
"_tools:build": "git submodule foreach yarn build",
3030
"_tools:make": "yarn _tools:yarn && yarn _tools:build",
3131
"tools:update": "yarn _tools:checkout && yarn _tools:pull && yarn _tools:make",
32-
"test:e2e": "yarn build && cypress run"
32+
"test:e2e": "yarn build && cypress run",
33+
"test:e2e:open": "yarn build && cypress open"
3334
},
3435
"author": "CodeX",
3536
"license": "Apache-2.0",

src/components/modules/blockManager.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Blocks from '../blocks';
1414
import { BlockToolData, PasteEvent } from '../../../types';
1515
import { BlockTuneData } from '../../../types/block-tunes/block-tune-data';
1616
import BlockAPI from '../block/api';
17+
import { BlockMutationType } from '../../../types/events/block/mutation-type';
1718

1819
/**
1920
* @typedef {BlockManager} BlockManager
@@ -289,12 +290,24 @@ export default class BlockManager extends Module {
289290
tunes,
290291
});
291292

293+
/**
294+
* In case of block replacing (Converting OR from Toolbox or Shortcut on empty block OR on-paste to empty block)
295+
* we need to dispatch the 'block-removing' event for the replacing block
296+
*/
297+
if (replace) {
298+
this.blockDidMutated(BlockMutationType.Removed, this.getBlockByIndex(newIndex), {
299+
index: newIndex,
300+
});
301+
}
302+
292303
this._blocks.insert(newIndex, block, replace);
293304

294305
/**
295306
* Force call of didMutated event on Block insertion
296307
*/
297-
this.blockDidMutated(block);
308+
this.blockDidMutated(BlockMutationType.Added, block, {
309+
index: newIndex,
310+
});
298311

299312
if (needToFocus) {
300313
this.currentBlockIndex = newIndex;
@@ -370,7 +383,9 @@ export default class BlockManager extends Module {
370383
/**
371384
* Force call of didMutated event on Block insertion
372385
*/
373-
this.blockDidMutated(block);
386+
this.blockDidMutated(BlockMutationType.Added, block, {
387+
index,
388+
});
374389

375390
if (needToFocus) {
376391
this.currentBlockIndex = index;
@@ -445,7 +460,9 @@ export default class BlockManager extends Module {
445460
/**
446461
* Force call of didMutated event on Block removal
447462
*/
448-
this.blockDidMutated(blockToRemove);
463+
this.blockDidMutated(BlockMutationType.Removed, blockToRemove, {
464+
index,
465+
});
449466

450467
if (this.currentBlockIndex >= index) {
451468
this.currentBlockIndex--;
@@ -721,7 +738,10 @@ export default class BlockManager extends Module {
721738
/**
722739
* Force call of didMutated event on Block movement
723740
*/
724-
this.blockDidMutated(this.currentBlock);
741+
this.blockDidMutated(BlockMutationType.Moved, this.currentBlock, {
742+
fromIndex,
743+
toIndex,
744+
});
725745
}
726746

727747
/**
@@ -788,7 +808,11 @@ export default class BlockManager extends Module {
788808
BlockEvents.dragLeave(event);
789809
});
790810

791-
block.on('didMutated', (affectedBlock: Block) => this.blockDidMutated(affectedBlock));
811+
block.on('didMutated', (affectedBlock: Block) => {
812+
return this.blockDidMutated(BlockMutationType.Changed, affectedBlock, {
813+
index: this.getBlockIndex(affectedBlock),
814+
});
815+
});
792816
}
793817

794818
/**
@@ -828,10 +852,19 @@ export default class BlockManager extends Module {
828852
/**
829853
* Block mutation callback
830854
*
855+
* @param mutationType - what happened with block
831856
* @param block - mutated block
832-
*/
833-
private blockDidMutated(block: Block): Block {
834-
this.Editor.ModificationsObserver.onChange(new BlockAPI(block));
857+
* @param details - additional data to pass with change event
858+
*/
859+
private blockDidMutated(mutationType: BlockMutationType, block: Block, details: Record<string, unknown> = {}): Block {
860+
const event = new CustomEvent(mutationType, {
861+
detail: {
862+
target: new BlockAPI(block),
863+
...details,
864+
},
865+
});
866+
867+
this.Editor.ModificationsObserver.onChange(event);
835868

836869
return block;
837870
}

src/components/modules/modificationsObserver.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Module from '../__module';
2-
import { BlockAPI } from '../../../types';
32
import * as _ from '../utils';
43

54
/**
@@ -28,13 +27,13 @@ export default class ModificationsObserver extends Module {
2827
/**
2928
* Call onChange event passed to Editor.js configuration
3029
*
31-
* @param block - changed Block
30+
* @param event - some of our custom change events
3231
*/
33-
public onChange(block: BlockAPI): void {
32+
public onChange(event: CustomEvent): void {
3433
if (this.disabled || !_.isFunction(this.config.onChange)) {
3534
return;
3635
}
3736

38-
this.config.onChange(this.Editor.API.methods, block);
37+
this.config.onChange(this.Editor.API.methods, event);
3938
}
4039
}

0 commit comments

Comments
 (0)