2022-12-08 18:06:36 +08:00
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
|
|
|
|
export default function useMutationObserver() {
|
|
|
|
const instance = useRef<MutationObserver>();
|
|
|
|
|
|
|
|
const destroyObserver = () => {
|
|
|
|
if (instance.current) {
|
|
|
|
instance.current.takeRecords();
|
|
|
|
instance.current.disconnect();
|
|
|
|
instance.current = undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const createObserver = (target: Node, callback: MutationCallback) => {
|
|
|
|
if (MutationObserver) {
|
|
|
|
destroyObserver();
|
|
|
|
instance.current = new MutationObserver(callback);
|
|
|
|
instance.current.observe(target, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
attributeFilter: ['style', 'class'],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-08 22:32:37 +08:00
|
|
|
useEffect(() => destroyObserver, []);
|
2022-12-08 18:06:36 +08:00
|
|
|
|
2022-12-13 15:47:57 +08:00
|
|
|
const reRendering = (mutation: MutationRecord, watermarkElement?: HTMLElement) => {
|
|
|
|
let flag = false;
|
|
|
|
// Whether to delete the watermark node
|
|
|
|
if (mutation.removedNodes.length) {
|
|
|
|
flag = Array.from(mutation.removedNodes).some((node) => node === watermarkElement);
|
|
|
|
}
|
|
|
|
// Whether the watermark dom property value has been modified
|
|
|
|
if (mutation.type === 'attributes' && mutation.target === watermarkElement) {
|
|
|
|
flag = true;
|
|
|
|
}
|
|
|
|
return flag;
|
|
|
|
};
|
|
|
|
|
|
|
|
return { createObserver, destroyObserver, reRendering };
|
2022-12-08 18:06:36 +08:00
|
|
|
}
|