mutationObserver.mjs.map 3.1 KB

1
  1. {"version":3,"file":"mutationObserver.mjs","names":["onBeforeUnmount","onMounted","ref","watch","refElement","useMutationObserver","handler","options","mutationRef","once","immediate","optionKeys","defaultValue","Object","keys","length","observer","MutationObserver","mutations","disconnect","newValue","oldValue","el","observe","attributes","attr","characterData","char","childList","child","subtree","sub","flush"],"sources":["../../src/composables/mutationObserver.ts"],"sourcesContent":["// Utilities\nimport { onBeforeUnmount, onMounted, ref, watch } from 'vue'\nimport { refElement } from '@/util'\n\n// Types\nimport type { ComponentPublicInstance } from 'vue'\n\nexport interface MutationOptions {\n attr?: boolean\n char?: boolean\n child?: boolean\n sub?: boolean\n once?: boolean\n immediate?: boolean\n}\n\nexport function useMutationObserver (\n handler?: MutationCallback,\n options?: MutationOptions,\n) {\n const mutationRef = ref<ComponentPublicInstance | HTMLElement>()\n const { once, immediate, ...optionKeys } = options || {}\n const defaultValue = !Object.keys(optionKeys).length\n\n const observer = new MutationObserver((\n mutations: MutationRecord[],\n observer: MutationObserver\n ) => {\n handler?.(mutations, observer)\n\n if (options?.once) observer.disconnect()\n })\n\n onMounted(() => {\n if (!options?.immediate) return\n\n handler?.([], observer)\n })\n\n onBeforeUnmount(() => {\n observer.disconnect()\n })\n\n watch(mutationRef, (newValue, oldValue) => {\n if (oldValue) observer.disconnect()\n\n const el = refElement(newValue)\n\n if (!el) return\n\n observer.observe(el, {\n attributes: options?.attr ?? defaultValue,\n characterData: options?.char ?? defaultValue,\n childList: options?.child ?? defaultValue,\n subtree: options?.sub ?? defaultValue,\n })\n }, {\n flush: 'post',\n })\n\n return { mutationRef }\n}\n"],"mappings":"AAAA;AACA,SAASA,eAAe,EAAEC,SAAS,EAAEC,GAAG,EAAEC,KAAK,QAAQ,KAAK;AAAA,SACnDC,UAAU,6BAEnB;AAYA,OAAO,SAASC,mBAAmBA,CACjCC,OAA0B,EAC1BC,OAAyB,EACzB;EACA,MAAMC,WAAW,GAAGN,GAAG,EAAyC;EAChE,MAAM;I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