@dflex/utils
Version:
Utility package for DFlex
2 lines (1 loc) • 12.5 kB
JavaScript
class t{constructor(t,s){this.x=t,this.y=s}}class s extends t{setAxes(t,s){this.x=t,this.y=s}clone(t){this.setAxes(t.x,t.y)}getInstance(){return{x:this.x,y:this.y}}isInstanceEqual(t){return this.x===t.x&&this.y===t.y}isEqual(t,s){return this.x===t&&this.y===s}isNotEqual(t,s){return this.x!==t||this.y!==s}}class i{constructor(t,s,i,e){this.top=t,this.right=s,this.bottom=i,this.left=e}}class e extends i{clone(t){this.top=t.top,this.right=t.right,this.bottom=t.bottom,this.left=t.left}setBox(t,s,i,e){return this.top=t,this.right=s,this.bottom=i,this.left=e,this}getBox(){return{top:this.top,right:this.right,bottom:this.bottom,left:this.left}}setByAxis(t,s,i){"x"===t?(this.left=s,this.right=i):(this.top=s,this.bottom=i)}setOne(t,s,i){"x"===t?-1===s?this.left=i:this.right=i:-1===s?this.top=i:this.bottom=i}getOne(t,s){return"x"===t?-1===s?this.left:this.right:-1===s?this.top:this.bottom}setPositionInstance(t){this.top=t.y,this.left=t.x}setPosition(t,s){this.top=s,this.left=t}hasEqualPosition(t,s){return this.top===s||this.left===t}getPosition(){return{x:this.left,y:this.top}}}class n extends e{constructor(t,s,i,e){super(t,s,i,e)}setFalsy(){return this.setBox(!1,!1,!1,!1),this}isTruthyByAxis(t){return"x"===t?this.left||this.right:this.top||this.bottom}isTruthyOnSide(t,s){return"x"===t?1===s?this.right:this.left:1===s?this.bottom:this.top}isTruthy(){return this.left||this.right||this.top||this.bottom}}class h extends e{t(t){return this.top>=t.bottom}i(t){return this.bottom<=t.top}h(t){return this.right<=t.left}o(t){return this.left>=t.right}u(t){return this.top<t.top}l(t){return this.right>t.right}p(t){return this.bottom>t.bottom}m(t){return this.left<t.left}_(t){return this.top>=t.top}T(t){return this.right<=t.right}M(t){return this.bottom<=t.bottom}O(t){return this.left>=t.left}isBoxIntersect(t){return!(this.i(t)||this.o(t)||this.t(t)||this.h(t))}isOutThreshold(t,s,i){if(s.setBox(!1,!1,!1,!1),i){if("y"===i){const i=this.u(t),e=this.p(t);return i&&(s.top=!0),e&&(s.bottom=!0),i||e}const e=this.m(t),n=this.l(t);return e&&(s.left=!0),n&&(s.right=!0),e||n}return this.u(t)?(s.top=!0,!0):this.l(t)?(s.right=!0,!0):this.p(t)?(s.bottom=!0,!0):!!this.m(t)&&(s.left=!0,!0)}isInsideThreshold(t){return this._(t)&&this.T(t)&&this.M(t)&&this.O(t)}getSurroundingBox(t){return{left:Math.min(t.left,this.left),top:Math.min(t.top,this.top),right:Math.max(t.right,this.right),bottom:Math.max(t.bottom,this.bottom)}}isPositionedY(t){return this.t(t)||this.i(t)}assignBiggestBox(t){const{top:s,left:i,right:e,bottom:n}=t;i<this.left&&(this.left=i),s<this.top&&(this.top=s),e>this.right&&(this.right=e),n>this.bottom&&(this.bottom=n)}}function r(t,s){return`${t}_${s}`}const o={};function u(t,...s){o[t]||(o[t]=!0,console.warn(...s))}let c=!1;function l(t,s){if(c)return;const i=t.getBoundingClientRect();Object.keys(s).forEach((t=>{if(Object.prototype.hasOwnProperty.call(i,t)&&i[t]!==s[t])throw c=!0,new Error(`Element position assertion failed. Expected: ${i[t]} found: ${s[t]}`)}))}function a(t){const s={easing:"ease-in",duration:"dynamic"};return void 0===t?s:null===t?null:{...s,...t}}function f(){}const d=new Set,g="height",p="width",m="min-height",x="position",w="opacity";function b(t){return"x"===t?p:g}function y(t){return"x"===t?"left":"top"}function _(t){return"x"===t?"right":"bottom"}function T(t){return"x"===t?"y":"x"}class M extends h{constructor(t,s,i,e){super(t,s,i,e),this.width=s-e,this.height=i-t}setByPointAndDimensions(t,s,i,e){this.top=t,this.left=s,this.width=e,this.height=i,this.right=s+e,this.bottom=t+i}setAxes(t,s){this.left=t,this.right=this.width+t,this.top=s,this.bottom=this.height+s}getInstance(){const{top:t,left:s,bottom:i,right:e,width:n,height:h}=this;return{top:t,left:s,bottom:i,right:e,width:n,height:h}}getViewportPos(t,s,i){const e=t,n=s+this.width,r=t+this.height,o=s;return i?new h(e,n,r,o):{top:e,right:n,bottom:r,left:o,height:this.height,width:this.width}}getDimensionDiff(t,s){const i=b(t);return this[i]-s[i]}getPositionDiff(t,s){return this[y(t)]-s[t]}}class O extends s{increase(t){return this.x+=t.x,this.y+=t.y,this}composeBox(t,s){const{top:i,left:e,bottom:n,right:r}=t;return s?new h(i+this.y,r-this.x,n-this.y,e+this.x):new h(i-this.y,r+this.x,n+this.y,e-this.x)}onSameAxis(t,s){return"y"===t?s.x===this.x:s.y===this.y}}class B extends s{isOneTruthy(){return this.x||this.y}isAllFalsy(){return!(this.x||this.y)}setFalsy(){this.x=!1,this.y=!1}}class S{static containerKey(t,s){return r(t,s)}static depthKey(t){return r(t,"dp")}constructor(t){this.B=t,this.thresholds={},this.isOut={}}S({width:t,height:s}){const i=Math.round(this.B.horizontal*t/100),e=Math.round(this.B.vertical*s/100);this.v=new O(i,e)}A(t,s,i){this.thresholds[t]=this.v.composeBox(s,i),this.isOut[t]=new n(!1,!1,!1,!1)}setMainThreshold(t,s,i){this.S(s),this.A(t,s,i)}updateMainThreshold(t,s,i){this.thresholds[t]=this.v.composeBox(s,i),this.isOut[t].setFalsy()}getElmMainThreshold(t){return this.v.composeBox(t,!1)}setContainerThreshold(t,s,i,e){this.A(t,i,!1);const{top:n,left:h}=i,{height:r,width:o}=e,u=S.containerKey(s,t),c=S.depthKey(s);this.A(u,{left:h,top:n,right:h+o,bottom:n+r},!1),this.thresholds[c]?this.thresholds[c].assignBiggestBox(this.thresholds[u]):this.A(c,this.thresholds[u],!1)}isOutThreshold(t,s,i){return s.isOutThreshold(this.thresholds[t],this.isOut[t],i)}destroy(){Object.keys(this.thresholds).forEach((t=>{delete this.thresholds[t]})),Object.keys(this.isOut).forEach((t=>{delete this.isOut[t]}))}}const v={x:null,y:null};class A{constructor(){this.D=new h(0,0,0,0),this.P={...v}}setZone(t,s,i,e){const n=i.getSurroundingBox(e);this.D.clone(n),this.P[t]=s}isInside(t,s,i){return!!i.isInsideThreshold(this.D)&&s===this.P[t]}clear(){this.D.setBox(0,0,0,0),this.P={...v}}}var E=new class{constructor(){this.q={}}newTravel(t){return void 0===this.q[t]?this.q[t]=0:this.q[t]+=1,`${t}${this.q[t]}`}};const D="dflex_cycle_",P="dflex_id_",q="dflex_ky_";function F(){return window.getSelection()}function $(t,s){let i=0,e=t;try{do{if(i+=1,i>1&&s(e))return i=0,e;e=e.parentElement}while(null!==e&&!e.isSameNode(document.body))}catch(t){}finally{i=0}return null}function R(){return"undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement}function I(t,s){t.dataset.devX=`${s.x}`,t.dataset.devY=`${s.y}`}function k(t,s,i,e=!0,n="true"){const h=e?`data-${s}`:s;i?t.removeAttribute(h):t.setAttribute(h,n)}function K(t,s){k(t,"index",!1,!0,`${s}`)}function L(t,s,i){const{left:e,top:n,right:h,bottom:r,height:o,width:u}=t.getBoundingClientRect(),c=new M(n,h,r,e);return 0===s&&0===i||c.setByPointAndDimensions(n+i,e+s,o,u),c}const j=[];function Q(t){cancelAnimationFrame(t)}function C(){let t,s=!0;function i(){s=!0}function e(){Q(t),j.splice(j.indexOf(t),1)}return[function(n,h){h&&e();try{s=!1;const e=function(t,s){return i=>{t(i),s()}}(n,i);t=requestAnimationFrame(e),j.push(t)}catch(t){}},e,function(){return s}]}function N(){j.forEach(Q),j.length=0}const V=[];function U(t){let s=null,i=!1;function e(){s&&(clearTimeout(s),s=null)}return V.push(e),[function(n,h){const r=n||f;i=!0,h&&e(),s=setTimeout((()=>{i=!1,r()}),t)},e,function(){return i}]}function W(){V.forEach((t=>{t()})),V.length=0}const[Y,Z]=U(0);class z{constructor(){this.F={},this.$=new Set}R(t){Array.isArray(this.F[t])||(this.F[t]=[])}I(t,s,i){this.F[s].push(t),i&&this.$.add(i)}k(t){return!Array.isArray(this.F[t])||0===this.F[t].length}hasElm(t){return this.$.has(t)}enqueueBeforeLast(t,s,i,e){this.R(i);const{length:n}=this.F[i];0===n?this.F[i].push(s):this.F[i][n-1]=s,this.I(t,i,e)}enqueue(t,s,i){this.R(s),this.I(t,s,i)}executeQueue(t){const s=[];if(this.k(t))return s;try{this.F[t].forEach((t=>{const i=t();s.push(i)}))}catch(t){}finally{Z(),delete this.F[t]}return s}K(t){Y((()=>{const[s,i]=t;this.executeQueue(s),i&&queueMicrotask((()=>this.executeQueue(i))),this.$.clear()}),!0)}scheduleNextTask(t){this.K(t)}cancelQueuedTask(){Z()}clear(){Z(),this.F={},this.$.clear()}}function G(t,s=!1,i=200){const[e,n]=U(i),[h,r]=C();let o=performance.now(),u=!1;const c=()=>{if(u)return;const n=performance.now();s||n-o>=i?(h(t,!0),o=n):e(c,!0)};return c.isPaused=()=>u,c.pause=()=>{u||(u=!0,r(),n())},c.resume=()=>{u&&(u=!1,c())},c}class H{constructor(t,s,i,e,n){this.index=t,this.SK=i,this.id=s,this.cycleID=e,this.hasScroll=n,this.reconciledIDs=new Set,this.numberOfTransformedELm=0,this.marginBottom=null,this.marginTop=null}}class J{constructor(t,s,i,e,n){const h=new H(t,s,i,e,n);this.L=[h],this.SKs=[i],this.complete(),this.isActive=!0}latest(){return this.L[this.L.length-1]}prev(){return this.L[this.L.length-2]}getAll(){return this.L}filter(t,s){return this.L.filter(s?s=>t.find((t=>t===s.cycleID)):s=>t.find((t=>t===s.id)))}j(t){this.SKs=this.SKs.filter((s=>!t.has(s)))}flush(t){const s=new Set;this.L=this.L.filter((i=>void 0===t.find((t=>t===i.SK&&(s.add(i.SK),!0)))||(s.has(i.SK)&&s.delete(i.SK),!1))),this.j(s)}pruneSKFromMigration(t){this.L=this.L.filter((s=>s.SK!==t)),this.j(new Set([t]))}setIndex(t){this.latest().index=t,this.latest().numberOfTransformedELm+=1}preserveVerticalMargin(t,s){this.latest()["bottom"===t?"marginBottom":"marginTop"]=s}clearMargin(){this.latest().marginBottom=null,this.latest().marginTop=null}add(t,s,i,e,n,h){this.L.push(new H(t,s,i,n,h)),e?this.SKs.unshift(i):this.SKs.push(i)}updateReconciledIDs(t,s){const i=this.L.find((s=>s.SK===t));i&&(i.reconciledIDs.clear(),s.forEach((t=>i.reconciledIDs.add(t))))}getMigrationBySK(t){return this.L.find((s=>s.SK===t))}getReconciledIDsBySK(t){const s=this.L.find((s=>s.SK===t));return s?s.reconciledIDs:d}start(){this.isTransitioning=!0}complete(){this.isTransitioning=!1,this.preserveVerticalMargin("top",null),this.preserveVerticalMargin("bottom",null)}clear(){this.L=[],this.SKs=[],this.isActive=!1}}const X=Object.freeze(["x","y"]);let tt=new WeakMap;function st(t,s,i){t.style.setProperty(s,i)}function it(t,s){t.style.removeProperty(s)}function et(t,s,i){const e=function(t){if(tt.has(t))return tt.get(t);const s={computedStyle:getComputedStyle(t),parsedProperties:new Map};return tt.set(t,s),s}(t),{parsedProperties:n,computedStyle:h}=e,r=n.get(s);if(void 0===r){const t=h.getPropertyValue(s),e=i?function(t){const s=parseFloat(t);return Number.isNaN(s)?0:s}(t):t;return n.set(s,e),e}return r}function nt(){tt=new WeakMap}function ht(t){return{width:et(t,p,!0),height:et(t,g,!0)}}function rt(t){return function(t){if(!/matrix\([^)]+\)/.test(t))return null;const s=t.match(/matrix\(\s*([^,]+),\s*([^,]+),\s*([^,]+),\s*([^,]+),\s*([^,]+),\s*([^)]+)\)/);return s?[parseFloat(s[5]),parseFloat(s[6])]:null}(et(t,"transform",!1))}function ot(t){const{height:s,width:i}=ht(t);st(t,g,`${s}px`),st(t,p,`${i}px`)}const ut=/absolute|fixed/;function ct(t){return et(t,x,!1)}function lt(t,s){return et(t,s,!1)}function at(t){const s=ct(t);ut.test(s)&&st(t,x,"relative")}function ft(t){1!==et(t,w,!0)&&st(t,w,"1")}function dt(t){const s=s=>et(t,s,!1),i=s(g),e=s(p),n=s(m),h="auto"!==s(m);"auto"!==n||st(t,m,i),h||st(t,"min-width",e)}function gt(t){const s=et(t,"transition",!1);return"none"!==s&&""!==s.trim()}function pt(t,s){if(!t.hasAttribute(s))return;const i=t.getAttribute(s);i&&""===i.trim()&&t.removeAttribute(s)}var mt={__proto__:null,enableCommit:!1,enableMechanismDebugger:!1,enableMutationDebugger:!1,enablePositionAssertion:!1,enableReconcileDebugger:!1,enableRegisterDebugger:!1,enableScrollDebugger:!1,enableUndoSiblingsDebugger:!1,enableVisibilityDebugger:!1};export{i as AbstractBox,t as AxesPoint,X as BOTH_AXIS,e as Box,n as BoxBool,h as BoxNum,M as BoxRect,C as DFlexCreateRAF,U as DFlexCreateTimeout,J as DFlexCycle,D as PREFIX_TRACKER_CYCLE,P as PREFIX_TRACKER_ID,q as PREFIX_TRACKER_KY,s as Point,B as PointBool,O as PointNum,z as TaskQueue,S as Threshold,A as ThresholdDeadZone,l as assertElmPos,N as autoCleanupAllRAFs,W as autoCleanupAllTimeouts,R as canUseDOM,nt as clearComputedStyleCache,r as combineKeys,G as eventDebounce,mt as featureFlags,a as getAnimationOptions,et as getCachedComputedStyleProperty,b as getDimensionTypeByAxis,L as getElmBoxRect,ht as getElmDimensions,lt as getElmOverflow,ct as getElmPos,_ as getEndingPointByAxis,T as getOppositeAxis,$ as getParentElm,rt as getParsedElmTransform,F as getSelection,y as getStartingPointByAxis,gt as hasCSSTransition,f as noop,d as noopSet,ft as removeOpacity,it as removeStyleProperty,pt as rmEmptyAttr,ot as setFixedDimensions,dt as setParentDimensions,at as setRelativePosition,st as setStyleProperty,E as tracker,k as updateDOMAttr,I as updateElmDatasetGrid,K as updateIndexAttr,u as warnOnce};