47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
|
// Types
|
||
|
|
||
|
function mounted(el, binding) {
|
||
|
const modifiers = binding.modifiers || {};
|
||
|
const value = binding.value;
|
||
|
const {
|
||
|
once,
|
||
|
immediate,
|
||
|
...modifierKeys
|
||
|
} = modifiers;
|
||
|
const defaultValue = !Object.keys(modifierKeys).length;
|
||
|
const {
|
||
|
handler,
|
||
|
options
|
||
|
} = typeof value === 'object' ? value : {
|
||
|
handler: value,
|
||
|
options: {
|
||
|
attributes: modifierKeys?.attr ?? defaultValue,
|
||
|
characterData: modifierKeys?.char ?? defaultValue,
|
||
|
childList: modifierKeys?.child ?? defaultValue,
|
||
|
subtree: modifierKeys?.sub ?? defaultValue
|
||
|
}
|
||
|
};
|
||
|
const observer = new MutationObserver(function () {
|
||
|
let mutations = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
||
|
let observer = arguments.length > 1 ? arguments[1] : undefined;
|
||
|
handler?.(mutations, observer);
|
||
|
if (once) unmounted(el, binding);
|
||
|
});
|
||
|
if (immediate) handler?.([], observer);
|
||
|
el._mutate = Object(el._mutate);
|
||
|
el._mutate[binding.instance.$.uid] = {
|
||
|
observer
|
||
|
};
|
||
|
observer.observe(el, options);
|
||
|
}
|
||
|
function unmounted(el, binding) {
|
||
|
if (!el._mutate?.[binding.instance.$.uid]) return;
|
||
|
el._mutate[binding.instance.$.uid].observer.disconnect();
|
||
|
delete el._mutate[binding.instance.$.uid];
|
||
|
}
|
||
|
export const Mutate = {
|
||
|
mounted,
|
||
|
unmounted
|
||
|
};
|
||
|
export default Mutate;
|
||
|
//# sourceMappingURL=index.mjs.map
|