mirror of
https://github.com/codex-team/editor.js
synced 2026-03-18 00:19:53 +01:00
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
/**
|
|
* Check if passed mutation belongs to a passed element
|
|
*
|
|
* @param mutationRecord - mutation to check
|
|
* @param element - element that is expected to contain mutation
|
|
*/
|
|
export const isMutationBelongsToElement = (mutationRecord: MutationRecord, element: Element): boolean => {
|
|
const { type, target, addedNodes, removedNodes } = mutationRecord;
|
|
|
|
/**
|
|
* Skip own technical mutations, for example, data-empty attribute changes
|
|
*/
|
|
if (mutationRecord.type === 'attributes' && mutationRecord.attributeName === 'data-empty') {
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Covers all types of mutations happened to the element or it's descendants with the only one exception - removing/adding the element itself;
|
|
*/
|
|
if (element.contains(target)) {
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* In case of removing/adding the element itself, mutation type will be 'childList' and 'removedNodes'/'addedNodes' will contain the element.
|
|
*/
|
|
if (type === 'childList') {
|
|
const elementAddedItself = Array.from(addedNodes).some(node => node === element);
|
|
|
|
if (elementAddedItself) {
|
|
return true;
|
|
}
|
|
|
|
const elementRemovedItself = Array.from(removedNodes).some(node => node === element);
|
|
|
|
if (elementRemovedItself) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
};
|