2018-09-04 23:34:11 +02:00
|
|
|
/**
|
|
|
|
* @module ModificationsObserver
|
|
|
|
*
|
|
|
|
* Handles any mutations
|
|
|
|
* and gives opportunity to handle outside
|
|
|
|
*/
|
|
|
|
|
2018-11-23 17:11:50 +01:00
|
|
|
import Module from '../__module';
|
2019-11-30 21:42:39 +01:00
|
|
|
import * as _ from '../utils';
|
2018-12-07 09:33:43 +01:00
|
|
|
import Block from '../block';
|
2018-09-04 23:34:11 +02:00
|
|
|
|
|
|
|
export default class ModificationsObserver extends Module {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Debounce Timer
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
public static readonly DebounceTimer = 450;
|
|
|
|
|
2018-12-07 09:33:43 +01:00
|
|
|
/**
|
|
|
|
* MutationObserver instance
|
|
|
|
*/
|
|
|
|
private observer: MutationObserver;
|
|
|
|
|
2018-12-20 21:47:27 +01:00
|
|
|
/**
|
|
|
|
* Allows to temporary disable mutations handling
|
|
|
|
*/
|
|
|
|
private disabled: boolean;
|
|
|
|
|
2018-09-04 23:34:11 +02:00
|
|
|
/**
|
|
|
|
* Used to prevent several mutation callback execution
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
|
|
|
private mutationDebouncer = _.debounce( () => {
|
2019-07-01 13:10:30 +02:00
|
|
|
this.updateNativeInputs();
|
2018-11-23 17:11:50 +01:00
|
|
|
this.config.onChange();
|
2018-09-04 23:34:11 +02:00
|
|
|
}, ModificationsObserver.DebounceTimer);
|
|
|
|
|
2019-07-01 13:10:30 +02:00
|
|
|
/**
|
|
|
|
* Array of native inputs in Blocks.
|
|
|
|
* Changes in native inputs are not handled by modification observer, so we need to set change event listeners on them
|
|
|
|
*/
|
|
|
|
private nativeInputs: HTMLElement[] = [];
|
|
|
|
|
2018-09-04 23:34:11 +02:00
|
|
|
/**
|
|
|
|
* Clear timeout and set null to mutationDebouncer property
|
|
|
|
*/
|
|
|
|
public destroy() {
|
|
|
|
this.mutationDebouncer = null;
|
2019-09-09 12:03:18 +02:00
|
|
|
if (this.observer) {
|
|
|
|
this.observer.disconnect();
|
|
|
|
}
|
2018-12-07 09:33:43 +01:00
|
|
|
this.observer = null;
|
2019-07-01 13:10:30 +02:00
|
|
|
this.nativeInputs.forEach((input) => this.Editor.Listeners.off(input, 'input', this.mutationDebouncer));
|
2018-09-04 23:34:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preparation method
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
|
|
|
public async prepare(): Promise<void> {
|
|
|
|
/**
|
|
|
|
* wait till Browser render Editor's Blocks
|
|
|
|
*/
|
|
|
|
window.setTimeout( () => {
|
|
|
|
this.setObserver();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
2018-12-20 21:47:27 +01:00
|
|
|
/**
|
|
|
|
* Allows to disable observer,
|
|
|
|
* for example when Editor wants to stealthy mutate DOM
|
|
|
|
*/
|
|
|
|
public disable() {
|
|
|
|
this.disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables mutation handling
|
|
|
|
* Should be called after .disable()
|
|
|
|
*/
|
|
|
|
public enable() {
|
|
|
|
this.disabled = false;
|
|
|
|
}
|
|
|
|
|
2018-09-04 23:34:11 +02:00
|
|
|
/**
|
|
|
|
* setObserver
|
|
|
|
*
|
|
|
|
* sets 'DOMSubtreeModified' listener on Editor's UI.nodes.redactor
|
|
|
|
* so that User can handle outside from API
|
|
|
|
*/
|
|
|
|
private setObserver(): void {
|
2018-12-07 09:33:43 +01:00
|
|
|
const {UI} = this.Editor;
|
|
|
|
const observerOptions = {
|
|
|
|
childList: true,
|
|
|
|
attributes: true,
|
|
|
|
subtree: true,
|
|
|
|
characterData: true,
|
|
|
|
characterDataOldValue: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.observer = new MutationObserver((mutationList, observer) => {
|
|
|
|
this.mutationHandler(mutationList, observer);
|
|
|
|
});
|
|
|
|
this.observer.observe(UI.nodes.redactor, observerOptions);
|
|
|
|
}
|
2018-09-04 23:34:11 +02:00
|
|
|
|
2018-12-07 09:33:43 +01:00
|
|
|
/**
|
|
|
|
* MutationObserver events handler
|
|
|
|
* @param mutationList
|
|
|
|
* @param observer
|
|
|
|
*/
|
|
|
|
private mutationHandler(mutationList, observer) {
|
2018-12-20 21:47:27 +01:00
|
|
|
/**
|
|
|
|
* Skip mutations in stealth mode
|
|
|
|
*/
|
|
|
|
if (this.disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-04 23:34:11 +02:00
|
|
|
/**
|
2018-12-07 09:33:43 +01:00
|
|
|
* We divide two Mutation types:
|
2018-12-20 21:47:27 +01:00
|
|
|
* 1) mutations that concerns client changes: settings changes, symbol added, deletion, insertions and so on
|
2018-12-07 09:33:43 +01:00
|
|
|
* 2) functional changes. On each client actions we set functional identifiers to interact with user
|
2018-09-04 23:34:11 +02:00
|
|
|
*/
|
2018-12-07 09:33:43 +01:00
|
|
|
let contentMutated = false;
|
|
|
|
|
|
|
|
mutationList.forEach((mutation) => {
|
|
|
|
switch (mutation.type) {
|
|
|
|
case 'childList':
|
|
|
|
case 'subtree':
|
|
|
|
case 'characterData':
|
|
|
|
case 'characterDataOldValue':
|
|
|
|
contentMutated = true;
|
|
|
|
break;
|
|
|
|
case 'attributes':
|
|
|
|
const mutatedTarget = mutation.target as Element;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes on Element.ce-block usually is functional
|
|
|
|
*/
|
|
|
|
if (!mutatedTarget.classList.contains(Block.CSS.wrapper)) {
|
|
|
|
contentMutated = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/** call once */
|
|
|
|
if (contentMutated) {
|
2018-09-04 23:34:11 +02:00
|
|
|
this.mutationDebouncer();
|
2018-12-07 09:33:43 +01:00
|
|
|
}
|
2018-09-04 23:34:11 +02:00
|
|
|
}
|
2019-07-01 13:10:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets native inputs and set oninput event handler
|
|
|
|
*/
|
|
|
|
private updateNativeInputs(): void {
|
|
|
|
if (this.nativeInputs) {
|
|
|
|
this.nativeInputs.forEach((input) => {
|
|
|
|
this.Editor.Listeners.off(input, 'input');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.nativeInputs = Array.from(this.Editor.UI.nodes.redactor.querySelectorAll('textarea, input, select'));
|
|
|
|
|
|
|
|
this.nativeInputs.forEach((input) => this.Editor.Listeners.on(input, 'input', this.mutationDebouncer));
|
|
|
|
}
|
2018-09-04 23:34:11 +02:00
|
|
|
}
|