mirror of
https://github.com/codex-team/editor.js
synced 2026-03-18 08:29:52 +01:00
Mutation callback (#444)
* modification observer initials * add debouncer to callback execution * change feature name * update * code improvements * tslint fixes * use debouncer from utils * add types * upgrade * fix * update
This commit is contained in:
parent
2c0d747035
commit
7acf321454
11 changed files with 176 additions and 38 deletions
73
src/components/modules/modificationsObserver.ts
Normal file
73
src/components/modules/modificationsObserver.ts
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/**
|
||||
* @module ModificationsObserver
|
||||
*
|
||||
* Handles any mutations
|
||||
* and gives opportunity to handle outside
|
||||
*/
|
||||
|
||||
import IEditorConfig from '../interfaces/editor-config';
|
||||
|
||||
declare const Module: any;
|
||||
declare const _: any;
|
||||
|
||||
export default class ModificationsObserver extends Module {
|
||||
|
||||
/**
|
||||
* Debounce Timer
|
||||
* @type {number}
|
||||
*/
|
||||
public static readonly DebounceTimer = 450;
|
||||
|
||||
/**
|
||||
* Used to prevent several mutation callback execution
|
||||
* @type {Function}
|
||||
*/
|
||||
private mutationDebouncer = _.debounce( () => {
|
||||
this.config.onChange.call();
|
||||
}, ModificationsObserver.DebounceTimer);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param {IEditorConfig} config
|
||||
*/
|
||||
constructor({config}) {
|
||||
super({config});
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear timeout and set null to mutationDebouncer property
|
||||
*/
|
||||
public destroy() {
|
||||
this.mutationDebouncer = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Preparation method
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async prepare(): Promise<void> {
|
||||
/**
|
||||
* wait till Browser render Editor's Blocks
|
||||
*/
|
||||
window.setTimeout( () => {
|
||||
this.setObserver();
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* setObserver
|
||||
*
|
||||
* sets 'DOMSubtreeModified' listener on Editor's UI.nodes.redactor
|
||||
* so that User can handle outside from API
|
||||
*/
|
||||
private setObserver(): void {
|
||||
const {Listeners, UI} = this.Editor;
|
||||
|
||||
/**
|
||||
* Set Listener to the Editor <div> element that holds only Blocks
|
||||
*/
|
||||
Listeners.on(UI.nodes.redactor, 'DOMSubtreeModified', () => {
|
||||
this.mutationDebouncer();
|
||||
}, false);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue