-
Notifications
You must be signed in to change notification settings - Fork 0
/
stretch-font.min.js.map
1 lines (1 loc) · 8.86 KB
/
stretch-font.min.js.map
1
{"version":3,"file":"stretch-font.min.js","sources":["stretch-font.js"],"sourcesContent":["function useStretchFont(root = document, className = 'stretch-font') {\n let $tmpl\n\n const $weight = [0.99, 0.99, 0.99, 0.98, 0.98, 0.92, 0.92, 0.92, 0.92]\n const $class = 'stretch-font__tmpl'\n const $store = new Map()\n\n const resizeObserver = new ResizeObserver(entries => {\n self.requestAnimationFrame(() => entries.forEach(({ target, contentRect }) => formula(target, contentRect)))\n })\n\n const mutationObserver = new MutationObserver(entries => {\n const search = [...new Set(entries.map(x => x.target))]\n if (!search.length) return\n\n let nodes = []\n for (const target of search) {\n if (target.classList.contains(className)) {\n nodes.push(target)\n }\n nodes = [...nodes, ...getNodes(target)]\n }\n\n for (const node of nodes.values()) build(node)\n })\n\n function setDisplay(node) {\n const display = self.getComputedStyle(node).display\n if (display === 'inline') node.classList.add('stretch-font__inline')\n }\n\n function store(node, payload = {}) {\n if (!payload && !Object.keys(payload).length) return\n const o = $store.get(node)\n $store.set(node, { ...o, ...payload })\n }\n function setSize(node) {\n if ($store.get(node)?.size) return\n const size = +self.getComputedStyle(node).fontSize.slice(0, -2)\n\n store(node, { size })\n }\n function setWeight(node) {\n if ($store.get(node)?.weight) return\n const weight = self.getComputedStyle(node).fontWeight\n\n store(node, { weight })\n }\n function setMin(node) {\n if ($store.get(node)?.min) return\n let { stretchMin: min } = node.dataset\n\n store(node, { min: +min })\n }\n function setMax(node) {\n if ($store.get(node)?.max) return\n let { stretchMax: max } = node.dataset\n\n store(node, { max: +max })\n }\n function setParams(node) {\n const n = document.createElement('span')\n n.innerHTML = node.innerHTML\n n.style.fontSize = $store.get(node)?.size\n $tmpl.appendChild(n)\n\n let { freeze } = $store.get(node)\n let { width, height } = n.getBoundingClientRect()\n const isStretch = 'stretch' in node.dataset || 'stretchX' in node.dataset || 'stretchY' in node.dataset\n\n if (isStretch) {\n freeze = [0, 0]\n } else {\n freeze ??= [width, height]\n }\n\n store(node, { width, height, freeze })\n\n n.remove()\n }\n\n function hasTmpl() {\n let node = document.querySelector('.' + $class)\n\n if (!node) {\n node = document.createElement('div')\n node.classList.add($class)\n document.querySelector('body').appendChild(node)\n }\n\n return node\n }\n\n function formula(node, rect = null) {\n if (!rect) rect = node.getBoundingClientRect()\n const { size, min, max, width, height, freeze, weight } = $store.get(node)\n const V = $weight.at(weight / 100 - 1)\n\n const X = ((freeze[0] || rect.width) / width) * size * V\n const Y = ((freeze[1] || rect.height) / height) * size * V\n\n let fz = X > max || Y > max ? max : X < min || Y < min ? min : X < Y ? X : Y\n if ('stretchX' in node.dataset && !('stretchY' in node.dataset)) fz = X > max ? max : X < min ? min : X\n if ('stretchY' in node.dataset && !('stretchX' in node.dataset)) fz = Y > max ? max : Y < min ? min : Y\n\n node.style.fontSize = fz + 'px'\n }\n\n function getNodes(target) {\n return target.querySelectorAll('.' + className) || []\n }\n\n function saveNode(node) {\n setDisplay(node)\n if ('stretchMin' in node.dataset) setMin(node)\n if ('stretchMax' in node.dataset) setMax(node)\n setSize(node)\n setWeight(node)\n setParams(node)\n }\n\n function build(node) {\n saveNode(node)\n formula(node)\n resizeObserver.observe(node)\n }\n\n self.addEventListener('DOMContentLoaded', () => {\n $tmpl = hasTmpl()\n\n for (const node of getNodes(root).values()) build(node)\n\n mutationObserver.observe(root, { childList: true, subtree: true })\n })\n}\n\nexport default useStretchFont\n"],"names":["$tmpl","root","arguments","length","undefined","document","className","$weight","$class","$store","Map","resizeObserver","ResizeObserver","entries","self","requestAnimationFrame","forEach","_ref","formula","target","contentRect","mutationObserver","MutationObserver","search","_toConsumableArray","Set","map","x","_step","nodes","_iterator","_createForOfIteratorHelper","s","n","done","value","classList","contains","push","concat","getNodes","err","e","f","_step2","_iterator2","values","build","store","node","payload","Object","keys","o","get","set","_objectSpread","rect","getBoundingClientRect","_$store$get7","size","min","max","width","height","freeze","weight","V","at","X","Y","fz","dataset","style","fontSize","querySelectorAll","saveNode","getComputedStyle","display","add","setDisplay","_$store$get3","stretchMin","setMin","_$store$get4","stretchMax","setMax","_$store$get","slice","setSize","_$store$get2","fontWeight","setWeight","_$store$get5","createElement","innerHTML","appendChild","_freeze","_n$getBoundingClientR","remove","setParams","observe","addEventListener","querySelector","hasTmpl","_step3","_iterator3","childList","subtree"],"mappings":"26EAAA,WAAqE,IAC/DA,EADkBC,EAAIC,UAAAC,OAAA,QAAAC,IAAAF,UAAA,GAAAA,UAAA,GAAGG,SAAUC,EAASJ,UAAAC,OAAA,QAAAC,IAAAF,UAAA,GAAAA,UAAA,GAAG,eAG7CK,EAAU,CAAC,IAAM,IAAM,IAAM,IAAM,IAAM,IAAM,IAAM,IAAM,KAC3DC,EAAS,qBACTC,EAAS,IAAIC,IAEbC,EAAiB,IAAIC,gBAAe,SAAAC,GACxCC,KAAKC,uBAAsB,WAAA,OAAMF,EAAQG,SAAQ,SAAAC,GAAsB,OAAOC,EAApBD,EAANE,OAAmBF,EAAXG,kBAC9D,IAEMC,EAAmB,IAAIC,kBAAiB,SAAAT,GAC5C,IAAMU,EAAMC,EAAO,IAAIC,IAAIZ,EAAQa,KAAI,SAAAC,GAAC,OAAIA,EAAER,MAAM,MACpD,GAAKI,EAAOpB,OAAZ,CAEA,IAC2ByB,EADvBC,EAAQ,GAAEC,EAAAC,EACOR,GAAM,IAA3B,IAAAO,EAAAE,MAAAJ,EAAAE,EAAAG,KAAAC,MAA6B,CAAA,IAAlBf,EAAMS,EAAAO,MACXhB,EAAOiB,UAAUC,SAAS/B,IAC5BuB,EAAMS,KAAKnB,GAEbU,EAAKU,GAAAA,OAAAf,EAAOK,GAAKL,EAAKgB,EAASrB,IACjC,CAAC,CAAA,MAAAsB,GAAAX,EAAAY,EAAAD,EAAA,CAAA,QAAAX,EAAAa,GAAA,CAAA,IAEgCC,EAFhCC,EAAAd,EAEkBF,EAAMiB,UAAQ,IAAjC,IAAAD,EAAAb,MAAAY,EAAAC,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