1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
import { useFlicker } from '@/useFlicker'
const mapStore = new Map() const write = { async bind (el, { arg, modifiers, value }) { if (!value) return const delay = Object .entries(modifiers).map(v => +v[0]) .reduce((count, num) => count + num, 0) const duration = (arg === undefined) ? 150 : +arg const flickerHandler = useFlicker(el) flickerHandler.startFlicker() let delayTime await new Promise(res => delayTime = setTimeout(res, delay)) const innerHTMLArr = value.trim().split('') const lastIndex = innerHTMLArr.length - 1 let activeIndex = 0 const textInterval = setInterval(() => { flickerHandler.pushText({ value: innerHTMLArr[activeIndex++], next: activeIndex <= lastIndex }) if (activeIndex > lastIndex) clearInterval(textInterval) }, duration) const handlerEvent = { flickerHandler, textInterval, delayTime } const mapKey = 'map-key' + (Math.random() * 1000).toFixed(5) mapStore.set(mapKey, handlerEvent) el.setAttribute('map-key', mapKey) }, update (el, binding) { const { value, oldValue } = binding if (value === oldValue) return write.unbind(el) write.bind(el, binding) }, unbind (el) { const mapKey = el.getAttribute('map-key') const handlerEvent = mapStore.get(mapKey) handlerEvent.flickerHandler.closeFlicker() clearInterval(handlerEvent.textInterval) clearTimeout(handlerEvent.delayTime) mapStore.delete(mapKey) el.removeAttribute('map-key') } } export default write
|