@viselect/vanilla
Advanced tools
Comparing version 3.2.5 to 3.2.6
@@ -1,3 +0,3 @@ | ||
declare type AnyFunction = (...args: any[]) => any; | ||
declare type EventMap = Record<string, AnyFunction>; | ||
type AnyFunction = (...args: any[]) => any; | ||
type EventMap = Record<string, AnyFunction>; | ||
export declare class EventTarget<Events extends EventMap> { | ||
@@ -4,0 +4,0 @@ private readonly _listeners; |
import type SelectionArea from './index'; | ||
import type { Intersection } from './utils'; | ||
export declare type Quantify<T> = Readonly<T[]> | T; | ||
export type DeepPartial<T> = T extends (infer U)[] ? T : T extends HTMLElement ? T : { | ||
[P in keyof T]?: DeepPartial<T[P]>; | ||
}; | ||
export type Quantify<T> = T[] | T; | ||
export interface ScrollEvent extends MouseEvent { | ||
@@ -23,3 +26,3 @@ deltaY: number; | ||
} | ||
export declare type SelectionEvents = { | ||
export type SelectionEvents = { | ||
beforestart: (e: SelectionEvent) => boolean | void; | ||
@@ -31,3 +34,3 @@ beforedrag: (e: SelectionEvent) => boolean | void; | ||
}; | ||
export declare type AreaLocation = { | ||
export type AreaLocation = { | ||
x1: number; | ||
@@ -42,4 +45,4 @@ y1: number; | ||
} | ||
export declare type TapMode = 'touch' | 'native'; | ||
export declare type OverlapMode = 'keep' | 'drop' | 'invert'; | ||
export type TapMode = 'touch' | 'native'; | ||
export type OverlapMode = 'keep' | 'drop' | 'invert'; | ||
export interface Scrolling { | ||
@@ -79,5 +82,4 @@ speedDivider: number; | ||
} | ||
export declare type PartialSelectionOptions = Partial<Omit<SelectionOptions, 'behaviour' | 'features'>> & { | ||
behaviour?: Partial<Behaviour>; | ||
features?: Partial<Features>; | ||
export type PartialSelectionOptions = DeepPartial<Omit<SelectionOptions, 'document'>> & { | ||
document?: Document; | ||
}; |
@@ -1,3 +0,3 @@ | ||
declare type AnyFunction = (...arg: any) => any; | ||
export declare type EventBindingArgs = [ | ||
type AnyFunction = (...arg: any) => any; | ||
export type EventBindingArgs = [ | ||
EventTarget | EventTarget[], | ||
@@ -4,0 +4,0 @@ string | string[], |
@@ -1,2 +0,2 @@ | ||
declare type AnyFunction = (...args: any[]) => void; | ||
type AnyFunction = (...args: any[]) => void; | ||
export interface Frames<F extends AnyFunction = AnyFunction> { | ||
@@ -3,0 +3,0 @@ next(...args: Parameters<F>): void; |
@@ -6,3 +6,2 @@ export * from './css'; | ||
export * from './constants'; | ||
export * from './deepAssign'; | ||
export * from './frames'; |
@@ -1,2 +0,2 @@ | ||
export declare type Intersection = 'center' | 'cover' | 'touch'; | ||
export type Intersection = 'center' | 'cover' | 'touch'; | ||
/** | ||
@@ -3,0 +3,0 @@ * Check if two DOM-Elements intersects each other. |
@@ -1,2 +0,2 @@ | ||
export declare type SelectAllSelectors = readonly (string | Element)[] | string | Element; | ||
export type SelectAllSelectors = readonly (string | Element)[] | string | Element; | ||
/** | ||
@@ -3,0 +3,0 @@ * Takes a selector (or array of selectors) and returns the matched nodes. |
@@ -1,3 +0,3 @@ | ||
/*! @viselect/vanilla v3.2.5 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}}}trigger(e,t=!0){this._onTapStart(e,t)}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.5"),B}); | ||
/*! @viselect/vanilla v3.2.6 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 X=Object.defineProperty;var Y=(E,g,v)=>g in E?X(E,g,{enumerable:!0,configurable:!0,writable:!0,value:v}):E[g]=v;var u=(E,g,v)=>(Y(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(l,e){const t=this._listeners.get(l)||new Set;return this._listeners.set(l,t),t.add(e),this}removeEventListener(l,e){var t;return(t=this._listeners.get(l))==null||t.delete(e),this}dispatchEvent(l,...e){let t=!0;for(const s of this._listeners.get(l)||[])t=s(...e)!==!1&&t;return t}unbindAllListeners(){this._listeners.clear()}}const g=(c,l="px")=>typeof c=="number"?c+l:c;function v({style:c},l,e){if(typeof l=="object")for(const[t,s]of Object.entries(l))s!==void 0&&(c[t]=g(s));else e!==void 0&&(c[l]=g(e))}function C(c){return(l,e,t,s={})=>{l instanceof HTMLCollection||l instanceof NodeList?l=Array.from(l):Array.isArray(l)||(l=[l]),Array.isArray(e)||(e=[e]);for(const i of l)for(const n of e)i[c](n,t,{capture:!1,...s});return[l,e,t,s]}}const T=C("addEventListener"),b=C("removeEventListener"),w=c=>{const{clientX:l,clientY:e,target:t}=c.touches&&c.touches[0]||c;return{x:l,y:e,target:t}};function M(c,l,e="touch"){switch(e){case"center":{const t=l.left+l.width/2,s=l.top+l.height/2;return t>=c.left&&t<=c.right&&s>=c.top&&s<=c.bottom}case"cover":return l.left>=c.left&&l.top>=c.top&&l.right<=c.right&&l.bottom<=c.bottom;case"touch":return c.right>=l.left&&c.left<=l.right&&c.bottom>=l.top&&c.top<=l.bottom}}function A(c,l=document){const e=Array.isArray(c)?c:[c];let t=[];for(let s=0,i=e.length;s<i;s++){const n=e[s];typeof n=="string"?t=t.concat(Array.from(l.querySelectorAll(n))):n instanceof Element&&t.push(n)}return t}const O=()=>matchMedia("(hover: none), (pointer: coarse)").matches,H=()=>"safari"in window,q=c=>{let l,e=-1,t=!1;return{next(...s){l=s,t||(t=!0,e=requestAnimationFrame(()=>{c(...l),t=!1}))},cancel(){cancelAnimationFrame(e),t=!1}}},{abs:S,max:R,min:k,ceil:D}=Math;class B extends E{constructor(e){var n,o,r,h,_;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={selectionAreaClass:"selection-area",selectionContainerClass:void 0,selectables:[],document:window.document,startAreas:["html"],boundaries:["html"],container:"body",...e,behaviour:{overlap:"invert",intersect:"touch",...e.behaviour,startThreshold:(n=e.behaviour)!=null&&n.startThreshold?typeof e.behaviour.startThreshold=="number"?e.behaviour.startThreshold:{x:10,y:10,...e.behaviour.startThreshold}:{x:10,y:10},scrolling:{speedDivider:10,manualSpeed:750,...(o=e.behaviour)==null?void 0:o.scrolling,startScrollMargins:{x:0,y:0,...(h=(r=e.behaviour)==null?void 0:r.scrolling)==null?void 0:h.startScrollMargins}}},features:{range:!0,touch:!0,...e.features,singleTap:{allow:!0,intersect:"native",...(_=e.features)==null?void 0:_.singleTap}}};for(const f of Object.getOwnPropertyNames(Object.getPrototypeOf(this)))typeof this[f]=="function"&&(this[f]=this[f].bind(this));const{document:t,selectionAreaClass:s,selectionContainerClass:i}=this._options;this._area=t.createElement("div"),this._clippingElement=t.createElement("div"),this._clippingElement.appendChild(this._area),this._area.classList.add(s),i&&this._clippingElement.classList.add(i),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=q(f=>{this._recalculateSelectionAreaRect(),this._updateElementSelection(),this._emitEvent("move",f),this._redrawSelectionArea()}),this.enable()}_bindStartEvents(e=!0){const{document:t,features:s}=this._options,i=e?T:b;i(t,"mousedown",this._onTapStart),s.touch&&i(t,"touchstart",this._onTapStart,{passive:!1})}_onTapStart(e,t=!1){const{x:s,y:i,target:n}=w(e),{_options:o}=this,{document:r}=this._options,h=n.getBoundingClientRect(),_=A(o.startAreas,o.document),f=A(o.boundaries,o.document);this._targetElement=f.find(y=>M(y.getBoundingClientRect(),h));const m=e.composedPath();if(!this._targetElement||!_.find(y=>m.includes(y))||!f.find(y=>m.includes(y))||!t&&this._emitEvent("beforestart",e)===!1)return;this._areaLocation={x1:s,y1:i,x2:0,y2:0};const a=r.scrollingElement||r.body;this._scrollDelta={x:a.scrollLeft,y:a.scrollTop},this._singleClick=!0,this.clearSelection(!1,!0),T(r,["touchmove","mousemove"],this._delayedTapMove,{passive:!1}),T(r,["mouseup","touchcancel","touchend"],this._onTapStop),T(r,"scroll",this._onScroll)}_onSingleTap(e){const{singleTap:{intersect:t},range:s}=this._options.features,i=w(e);let n;if(t==="native")n=i.target;else if(t==="touch"){this.resolveSelectables();const{x:r,y:h}=i;n=this._selectables.find(_=>{const{right:f,left:m,top:a,bottom:y}=_.getBoundingClientRect();return r<f&&r>m&&h<y&&h>a})}if(!n)return;for(this.resolveSelectables();!this._selectables.includes(n);){if(!n.parentElement)return;n=n.parentElement}const{stored:o}=this._selection;if(this._emitEvent("start",e),e.shiftKey&&o.length&&s){const r=this._latestElement??o[0],[h,_]=r.compareDocumentPosition(n)&4?[n,r]:[r,n],f=[...this._selectables.filter(m=>m.compareDocumentPosition(h)&4&&m.compareDocumentPosition(_)&2),h,_];this.select(f)}else o.includes(n)&&(o.length===1||e.ctrlKey||o.every(r=>this._selection.stored.includes(r)))?this.deselect(n):(this._latestElement=n,this.select(n));this._emitEvent("stop",e)}_delayedTapMove(e){const{container:t,document:s,behaviour:{startThreshold:i}}=this._options,{x1:n,y1:o}=this._areaLocation,{x:r,y:h}=w(e);if(typeof i=="number"&&S(r+h-(n+o))>=i||typeof i=="object"&&S(r-n)>=i.x||S(h-o)>=i.y){if(b(s,["mousemove","touchmove"],this._delayedTapMove,{passive:!1}),this._emitEvent("beforedrag",e)===!1){b(s,["mouseup","touchcancel","touchend"],this._onTapStop);return}T(s,["mousemove","touchmove"],this._onTapMove,{passive:!1}),v(this._area,"display","block"),A(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&&(T(s,"wheel",this._manualScroll,{passive:!1}),this._selectables=this._selectables.filter(_=>this._targetElement.contains(_))),this._setupSelectionArea(),this._emitEvent("start",e),this._onTapMove(e)}this._handleMoveEvent(e)}_setupSelectionArea(){const{_clippingElement:e,_targetElement:t,_area:s}=this,i=this._targetRect=t.getBoundingClientRect();this._scrollAvailable?(v(e,{top:i.top,left:i.left,width:i.width,height:i.height}),v(s,{marginTop:-i.top,marginLeft:-i.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:i,_areaLocation:n,_options:o,_frame:r}=this,{speedDivider:h}=o.behaviour.scrolling,_=this._targetElement;if(n.x2=t,n.y2=s,this._scrollAvailable&&!this._scrollingActive&&(i.y||i.x)){this._scrollingActive=!0;const f=()=>{if(!i.x&&!i.y){this._scrollingActive=!1;return}const{scrollTop:m,scrollLeft:a}=_;i.y&&(_.scrollTop+=D(i.y/h),n.y1-=_.scrollTop-m),i.x&&(_.scrollLeft+=D(i.x/h),n.x1-=_.scrollLeft-a),r.next(e),requestAnimationFrame(f)};requestAnimationFrame(f)}else r.next(e);this._handleMoveEvent(e)}_handleMoveEvent(e){const{features:t}=this._options;(t.touch&&O()||this._scrollAvailable&&H())&&e.preventDefault()}_onScroll(){const{_scrollDelta:e,_options:{document:t}}=this,{scrollTop:s,scrollLeft:i}=t.scrollingElement||t.body;this._areaLocation.x1+=e.x-i,this._areaLocation.y1+=e.y-s,e.x=i,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,i=e.deltaX?e.deltaX>0?1:-1:0;this._scrollSpeed.y+=s*t,this._scrollSpeed.x+=i*t,this._onTapMove(e),e.preventDefault()}_recalculateSelectionAreaRect(){const{_scrollSpeed:e,_areaLocation:t,_areaRect:s,_targetElement:i,_options:n}=this,{scrollTop:o,scrollHeight:r,clientHeight:h,scrollLeft:_,scrollWidth:f,clientWidth:m}=i,a=this._targetRect,{x1:y,y1:L}=t;let{x2:p,y2:d}=t;const{behaviour:{scrolling:{startScrollMargins:x}}}=n;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=f-_-m?S(a.left+a.width-p-x.x):0,p=p>a.right?a.right:p):e.x=0,d<a.top+x.y?(e.y=o?-S(a.top-d+x.y):0,d=d<a.top?a.top:d):d>a.bottom-x.y?(e.y=r-o-h?S(a.top+a.height-d-x.y):0,d=d>a.bottom?a.bottom:d):e.y=0;const P=k(y,p),j=k(L,d),F=R(y,p),W=R(L,d);s.x=P,s.y=j,s.width=F-P,s.height=W-j}_redrawSelectionArea(){const{x:e,y:t,width:s,height:i}=this._areaRect,{style:n}=this._area;n.left=`${e}px`,n.top=`${t}px`,n.width=`${s}px`,n.height=`${i}px`}_onTapStop(e,t){var o;const{document:s,features:i}=this._options,{_singleClick:n}=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&&n&&i.singleTap.allow?this._onSingleTap(e):!n&&!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(),(o=this._frame)==null||o.cancel(),v(this._area,"display","none")}_updateElementSelection(){const{_selectables:e,_options:t,_selection:s,_areaRect:i}=this,{stored:n,selected:o,touched:r}=s,{intersect:h,overlap:_}=t.behaviour,f=_==="invert",m=[],a=[],y=[];for(let p=0;p<e.length;p++){const d=e[p];if(M(i,d.getBoundingClientRect(),h)){if(o.includes(d))n.includes(d)&&!r.includes(d)&&r.push(d);else if(f&&n.includes(d)){y.push(d);continue}else a.push(d);m.push(d)}}f&&a.push(...n.filter(p=>!o.includes(p)));const L=_==="keep";for(let p=0;p<o.length;p++){const d=o[p];!m.includes(d)&&!(L&&n.includes(d))&&y.push(d)}s.selected=m,s.changed={added:a,removed:y},this._latestElement=m[m.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:i,touched:n,stored:o}=t,r=s.filter(h=>!o.includes(h));switch(e.behaviour.overlap){case"drop":{t.stored=[...r,...o.filter(h=>!n.includes(h))];break}case"invert":{t.stored=[...r,...o.filter(h=>!i.removed.includes(h))];break}case"keep":{t.stored=[...o,...s.filter(h=>!o.includes(h))];break}}}trigger(e,t=!0){this._onTapStart(e,t)}resolveSelectables(){this._selectables=A(this._options.selectables,this._options.document)}clearSelection(e=!0,t=!1){const{selected:s,stored:i,changed:n}=this._selection;n.added=[],n.removed.push(...s,...e?i:[]),t||(this._emitEvent("move",null),this._emitEvent("stop",null)),this._latestElement=void 0,this._selection={stored:e?[]:i,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:i,stored:n}=this._selection,o=A(e,this._options.document).filter(r=>!i.includes(r)&&!n.includes(r));return n.push(...o),i.push(...o),s.added.push(...o),s.removed=[],this._latestElement=void 0,t||(this._emitEvent("move",null),this._emitEvent("stop",null)),o}deselect(e,t=!1){const{selected:s,stored:i,changed:n}=this._selection,o=A(e,this._options.document).filter(r=>s.includes(r)||i.includes(r));o.length&&(this._selection.stored=i.filter(r=>!o.includes(r)),this._selection.selected=s.filter(r=>!o.includes(r)),this._selection.changed.added=[],this._selection.changed.removed.push(...o.filter(r=>!n.removed.includes(r))),this._latestElement=void 0,t||(this._emitEvent("move",null),this._emitEvent("stop",null)))}}return u(B,"version","3.2.6"),B}); | ||
//# sourceMappingURL=viselect.umd.js.map |
{ | ||
"name": "@viselect/vanilla", | ||
"version": "3.2.5", | ||
"version": "3.2.6", | ||
"description": "Simple, lightweight and modern library library for making visual DOM Selections.", | ||
@@ -39,3 +39,3 @@ "author": "Simon Reinisch <contact@reinisch.io>", | ||
}, | ||
"gitHead": "558c601e4c98e7bac787063213fa479b86ab4a32" | ||
"gitHead": "d5194526d1f8431ec0f4fd49a4628ec3e5ae91cf" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
176872
829
18