@@ -14,6 +14,7 @@ import Blocks from '../blocks';
1414import { BlockToolData , PasteEvent } from '../../../types' ;
1515import { BlockTuneData } from '../../../types/block-tunes/block-tune-data' ;
1616import 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 }
0 commit comments