forked from HeyPuter/puter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviselect.min.js
3 lines (3 loc) · 12.2 KB
/
viselect.min.js
1
2
3
/*! @viselect/vanilla v3.2.4 MIT | https://github.com/Simonwep/selection/tree/master/packages/vanilla */
(function(E,g){typeof exports=="object"&&typeof module<"u"?module.exports=g():typeof define=="function"&&define.amd?define(g):(E=typeof globalThis<"u"?globalThis:E||self,E.SelectionArea=g())})(this,function(){"use strict";var Y=Object.defineProperty;var $=(E,g,v)=>g in E?Y(E,g,{enumerable:!0,configurable:!0,writable:!0,value:v}):E[g]=v;var u=(E,g,v)=>($(E,typeof g!="symbol"?g+"":g,v),v);class E{constructor(){u(this,"_listeners",new Map);u(this,"on",this.addEventListener);u(this,"off",this.removeEventListener);u(this,"emit",this.dispatchEvent)}addEventListener(n,e){const t=this._listeners.get(n)||new Set;return this._listeners.set(n,t),t.add(e),this}removeEventListener(n,e){var t;return(t=this._listeners.get(n))==null||t.delete(e),this}dispatchEvent(n,...e){let t=!0;for(const s of this._listeners.get(n)||[])t=s(...e)!==!1&&t;return t}unbindAllListeners(){this._listeners.clear()}}const g=(r,n="px")=>typeof r=="number"?r+n:r;function v({style:r},n,e){if(typeof n=="object")for(const[t,s]of Object.entries(n))s!==void 0&&(r[t]=g(s));else e!==void 0&&(r[n]=g(e))}function C(r){return(n,e,t,s={})=>{n instanceof HTMLCollection||n instanceof NodeList?n=Array.from(n):Array.isArray(n)||(n=[n]),Array.isArray(e)||(e=[e]);for(const o of n)for(const i of e)o[r](i,t,{capture:!1,...s});return[n,e,t,s]}}const A=C("addEventListener"),b=C("removeEventListener"),w=r=>{const{clientX:n,clientY:e,target:t}=r.touches&&r.touches[0]||r;return{x:n,y:e,target:t}};function M(r,n,e="touch"){switch(e){case"center":{const t=n.left+n.width/2,s=n.top+n.height/2;return t>=r.left&&t<=r.right&&s>=r.top&&s<=r.bottom}case"cover":return n.left>=r.left&&n.top>=r.top&&n.right<=r.right&&n.bottom<=r.bottom;case"touch":return r.right>=n.left&&r.left<=n.right&&r.bottom>=n.top&&r.top<=n.bottom}}function T(r,n=document){const e=Array.isArray(r)?r:[r];let t=[];for(let s=0,o=e.length;s<o;s++){const i=e[s];typeof i=="string"?t=t.concat(Array.from(n.querySelectorAll(i))):i instanceof Element&&t.push(i)}return t}const H=()=>matchMedia("(hover: none), (pointer: coarse)").matches,q=()=>"safari"in window,R=(r,n)=>{for(const[e,t]of Object.entries(r)){const s=n[e];r[e]=s===void 0?r[e]:typeof s=="object"&&typeof t=="object"&&t!==null&&!Array.isArray(t)?R(t,s):s}return r},F=r=>{let n,e=-1,t=!1;return{next(...s){n=s,t||(t=!0,e=requestAnimationFrame(()=>{r(...n),t=!1}))},cancel(){cancelAnimationFrame(e),t=!1}}},{abs:S,max:k,min:D,ceil:j}=Math;class B extends E{constructor(e){super();u(this,"_options");u(this,"_selection",{stored:[],selected:[],touched:[],changed:{added:[],removed:[]}});u(this,"_area");u(this,"_clippingElement");u(this,"_targetElement");u(this,"_targetRect");u(this,"_selectables",[]);u(this,"_latestElement");u(this,"_areaRect",new DOMRect);u(this,"_areaLocation",{y1:0,x2:0,y2:0,x1:0});u(this,"_singleClick",!0);u(this,"_frame");u(this,"_scrollAvailable",!0);u(this,"_scrollingActive",!1);u(this,"_scrollSpeed",{x:0,y:0});u(this,"_scrollDelta",{x:0,y:0});u(this,"disable",this._bindStartEvents.bind(this,!1));u(this,"enable",this._bindStartEvents);this._options=R({selectionAreaClass:"selection-area",selectionContainerClass:void 0,selectables:[],document:window.document,behaviour:{overlap:"invert",intersect:"touch",startThreshold:{x:10,y:10},scrolling:{speedDivider:10,manualSpeed:750,startScrollMargins:{x:0,y:0}}},features:{range:!0,touch:!0,singleTap:{allow:!0,intersect:"native"}},startAreas:["html"],boundaries:["html"],container:"body"},e);for(const i of Object.getOwnPropertyNames(Object.getPrototypeOf(this)))typeof this[i]=="function"&&(this[i]=this[i].bind(this));const{document:t,selectionAreaClass:s,selectionContainerClass:o}=this._options;this._area=t.createElement("div"),this._clippingElement=t.createElement("div"),this._clippingElement.appendChild(this._area),this._area.classList.add(s),o&&this._clippingElement.classList.add(o),v(this._area,{willChange:"top, left, bottom, right, width, height",top:0,left:0,position:"fixed"}),v(this._clippingElement,{overflow:"hidden",position:"fixed",transform:"translate3d(0, 0, 0)",pointerEvents:"none",zIndex:"1"}),this._frame=F(i=>{this._recalculateSelectionAreaRect(),this._updateElementSelection(),this._emitEvent("move",i),this._redrawSelectionArea()}),this.enable()}_bindStartEvents(e=!0){const{document:t,features:s}=this._options,o=e?A:b;o(t,"mousedown",this._onTapStart),s.touch&&o(t,"touchstart",this._onTapStart,{passive:!1})}_onTapStart(e,t=!1){const{x:s,y:o,target:i}=w(e),{_options:l}=this,{document:c}=this._options,d=i.getBoundingClientRect(),_=T(l.startAreas,l.document),m=T(l.boundaries,l.document);this._targetElement=m.find(y=>M(y.getBoundingClientRect(),d));const f=e.composedPath();if(!this._targetElement||!_.find(y=>f.includes(y))||!m.find(y=>f.includes(y))||!t&&this._emitEvent("beforestart",e)===!1)return;this._areaLocation={x1:s,y1:o,x2:0,y2:0};const a=c.scrollingElement||c.body;this._scrollDelta={x:a.scrollLeft,y:a.scrollTop},this._singleClick=!0,this.clearSelection(!1,!0),A(c,["touchmove","mousemove"],this._delayedTapMove,{passive:!1}),A(c,["mouseup","touchcancel","touchend"],this._onTapStop),A(c,"scroll",this._onScroll)}_onSingleTap(e){const{singleTap:{intersect:t},range:s}=this._options.features,o=w(e);let i;if(t==="native")i=o.target;else if(t==="touch"){this.resolveSelectables();const{x:c,y:d}=o;i=this._selectables.find(_=>{const{right:m,left:f,top:a,bottom:y}=_.getBoundingClientRect();return c<m&&c>f&&d<y&&d>a})}if(!i)return;for(this.resolveSelectables();!this._selectables.includes(i);){if(!i.parentElement)return;i=i.parentElement}const{stored:l}=this._selection;if(this._emitEvent("start",e),e.shiftKey&&l.length&&s){const c=this._latestElement??l[0],[d,_]=c.compareDocumentPosition(i)&4?[i,c]:[c,i],m=[...this._selectables.filter(f=>f.compareDocumentPosition(d)&4&&f.compareDocumentPosition(_)&2),d,_];this.select(m)}else l.includes(i)&&(l.length===1||e.ctrlKey||l.every(c=>this._selection.stored.includes(c)))?this.deselect(i):(this._latestElement=i,this.select(i));this._emitEvent("stop",e)}_delayedTapMove(e){const{container:t,document:s,behaviour:{startThreshold:o}}=this._options,{x1:i,y1:l}=this._areaLocation,{x:c,y:d}=w(e),_=typeof o;if(_==="number"&&S(c+d-(i+l))>=o||_==="object"&&S(c-i)>=o.x||S(d-l)>=o.y){if(b(s,["mousemove","touchmove"],this._delayedTapMove,{passive:!1}),this._emitEvent("beforedrag",e)===!1){b(s,["mouseup","touchcancel","touchend"],this._onTapStop);return}A(s,["mousemove","touchmove"],this._onTapMove,{passive:!1}),v(this._area,"display","block"),T(t,s)[0].appendChild(this._clippingElement),this.resolveSelectables(),this._singleClick=!1,this._targetRect=this._targetElement.getBoundingClientRect(),this._scrollAvailable=this._targetElement.scrollHeight!==this._targetElement.clientHeight||this._targetElement.scrollWidth!==this._targetElement.clientWidth,this._scrollAvailable&&(A(s,"wheel",this._manualScroll,{passive:!1}),this._selectables=this._selectables.filter(m=>this._targetElement.contains(m))),this._setupSelectionArea(),this._emitEvent("start",e),this._onTapMove(e)}this._handleMoveEvent(e)}_setupSelectionArea(){const{_clippingElement:e,_targetElement:t,_area:s}=this,o=this._targetRect=t.getBoundingClientRect();this._scrollAvailable?(v(e,{top:o.top,left:o.left,width:o.width,height:o.height}),v(s,{marginTop:-o.top,marginLeft:-o.left})):(v(e,{top:0,left:0,width:"100%",height:"100%"}),v(s,{marginTop:0,marginLeft:0}))}_onTapMove(e){const{x:t,y:s}=w(e),{_scrollSpeed:o,_areaLocation:i,_options:l,_frame:c}=this,{speedDivider:d}=l.behaviour.scrolling,_=this._targetElement;if(i.x2=t,i.y2=s,this._scrollAvailable&&!this._scrollingActive&&(o.y||o.x)){this._scrollingActive=!0;const m=()=>{if(!o.x&&!o.y){this._scrollingActive=!1;return}const{scrollTop:f,scrollLeft:a}=_;o.y&&(_.scrollTop+=j(o.y/d),i.y1-=_.scrollTop-f),o.x&&(_.scrollLeft+=j(o.x/d),i.x1-=_.scrollLeft-a),c.next(e),requestAnimationFrame(m)};requestAnimationFrame(m)}else c.next(e);this._handleMoveEvent(e)}_handleMoveEvent(e){const{features:t}=this._options;(t.touch&&H()||this._scrollAvailable&&q())&&e.preventDefault()}_onScroll(){const{_scrollDelta:e,_options:{document:t}}=this,{scrollTop:s,scrollLeft:o}=t.scrollingElement||t.body;this._areaLocation.x1+=e.x-o,this._areaLocation.y1+=e.y-s,e.x=o,e.y=s,this._setupSelectionArea(),this._frame.next(null)}_manualScroll(e){const{manualSpeed:t}=this._options.behaviour.scrolling,s=e.deltaY?e.deltaY>0?1:-1:0,o=e.deltaX?e.deltaX>0?1:-1:0;this._scrollSpeed.y+=s*t,this._scrollSpeed.x+=o*t,this._onTapMove(e),e.preventDefault()}_recalculateSelectionAreaRect(){const{_scrollSpeed:e,_areaLocation:t,_areaRect:s,_targetElement:o,_options:i}=this,{scrollTop:l,scrollHeight:c,clientHeight:d,scrollLeft:_,scrollWidth:m,clientWidth:f}=o,a=this._targetRect,{x1:y,y1:L}=t;let{x2:p,y2:h}=t;const{behaviour:{scrolling:{startScrollMargins:x}}}=i;p<a.left+x.x?(e.x=_?-S(a.left-p+x.x):0,p=p<a.left?a.left:p):p>a.right-x.x?(e.x=m-_-f?S(a.left+a.width-p-x.x):0,p=p>a.right?a.right:p):e.x=0,h<a.top+x.y?(e.y=l?-S(a.top-h+x.y):0,h=h<a.top?a.top:h):h>a.bottom-x.y?(e.y=c-l-d?S(a.top+a.height-h-x.y):0,h=h>a.bottom?a.bottom:h):e.y=0;const O=D(y,p),P=D(L,h),W=k(y,p),X=k(L,h);s.x=O,s.y=P,s.width=W-O,s.height=X-P}_redrawSelectionArea(){const{x:e,y:t,width:s,height:o}=this._areaRect,{style:i}=this._area;i.left=`${e}px`,i.top=`${t}px`,i.width=`${s}px`,i.height=`${o}px`}_onTapStop(e,t){var l;const{document:s,features:o}=this._options,{_singleClick:i}=this;b(s,["mousemove","touchmove"],this._delayedTapMove),b(s,["touchmove","mousemove"],this._onTapMove),b(s,["mouseup","touchcancel","touchend"],this._onTapStop),b(s,"scroll",this._onScroll),this._keepSelection(),e&&i&&o.singleTap.allow?this._onSingleTap(e):!i&&!t&&(this._updateElementSelection(),this._emitEvent("stop",e)),this._scrollSpeed.x=0,this._scrollSpeed.y=0,this._scrollAvailable&&b(s,"wheel",this._manualScroll,{passive:!0}),this._clippingElement.remove(),(l=this._frame)==null||l.cancel(),v(this._area,"display","none")}_updateElementSelection(){const{_selectables:e,_options:t,_selection:s,_areaRect:o}=this,{stored:i,selected:l,touched:c}=s,{intersect:d,overlap:_}=t.behaviour,m=_==="invert",f=[],a=[],y=[];for(let p=0;p<e.length;p++){const h=e[p];if(M(o,h.getBoundingClientRect(),d)){if(l.includes(h))i.includes(h)&&!c.includes(h)&&c.push(h);else if(m&&i.includes(h)){y.push(h);continue}else a.push(h);f.push(h)}}m&&a.push(...i.filter(p=>!l.includes(p)));const L=_==="keep";for(let p=0;p<l.length;p++){const h=l[p];!f.includes(h)&&!(L&&i.includes(h))&&y.push(h)}s.selected=f,s.changed={added:a,removed:y},this._latestElement=f[f.length-1]}_emitEvent(e,t){return this.emit(e,{event:t,store:this._selection,selection:this})}_keepSelection(){const{_options:e,_selection:t}=this,{selected:s,changed:o,touched:i,stored:l}=t,c=s.filter(d=>!l.includes(d));switch(e.behaviour.overlap){case"drop":{t.stored=[...c,...l.filter(d=>!i.includes(d))];break}case"invert":{t.stored=[...c,...l.filter(d=>!o.removed.includes(d))];break}case"keep":{t.stored=[...l,...s.filter(d=>!l.includes(d))];break}}}resolveSelectables(){this._selectables=T(this._options.selectables,this._options.document)}clearSelection(e=!0,t=!1){const{selected:s,stored:o,changed:i}=this._selection;i.added=[],i.removed.push(...s,...e?o:[]),t||(this._emitEvent("move",null),this._emitEvent("stop",null)),this._latestElement=void 0,this._selection={stored:e?[]:o,selected:[],touched:[],changed:{added:[],removed:[]}}}getSelection(){return this._selection.stored}getSelectionArea(){return this._area}cancel(e=!1){this._onTapStop(null,!e)}destroy(){this.cancel(),this.disable(),this._clippingElement.remove(),super.unbindAllListeners()}select(e,t=!1){const{changed:s,selected:o,stored:i}=this._selection,l=T(e,this._options.document).filter(c=>!o.includes(c)&&!i.includes(c));return i.push(...l),o.push(...l),s.added.push(...l),s.removed=[],this._latestElement=void 0,t||(this._emitEvent("move",null),this._emitEvent("stop",null)),l}deselect(e,t=!1){const{selected:s,stored:o,changed:i}=this._selection,l=T(e,this._options.document).filter(c=>s.includes(c)||o.includes(c));l.length&&(this._selection.stored=o.filter(c=>!l.includes(c)),this._selection.selected=s.filter(c=>!l.includes(c)),this._selection.changed.added=[],this._selection.changed.removed.push(...l.filter(c=>!i.removed.includes(c))),this._latestElement=void 0,t||(this._emitEvent("move",null),this._emitEvent("stop",null)))}}return u(B,"version","3.2.4"),B});
//# sourceMappingURL=viselect.umd.js.map