mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
1 line
6.0 KiB
JavaScript
1 line
6.0 KiB
JavaScript
(()=>(e,t)=>{function s(e,t,s){if(t in e){Object.defineProperty(e,t,{value:s,enumerable:true,configurable:true,writable:true})}else{e[t]=s}return e}const r={horizontal:{name:"horizontal",getMoveTransform:(e,t)=>`translateX(${e}px)`,setOtherTransform:_.throttle(((e,t,s,r)=>{if(!t.classList.contains(n.ReorderingClassName)){return}const a=e.find((e=>e.element===t));if(!a){return}const o=a.rect;const l=e.filter((e=>e.rect.left<o.left)).sort(((e,t)=>t.rect.left-e.rect.left));const c=e.filter((e=>e.rect.left>o.left)).sort(((e,t)=>e.rect.left-t.rect.left));if(s>=0){const[e]=c;l.forEach((e=>{e.element.style.transform="";e.element.classList.remove(n.ReorderIncreaseClassName)}));c.forEach((t=>{if(o.left+s+o.width>=t.rect.left+t.rect.width/2){t.element.classList.add(n.ReorderDecreaseClassName);t.element.style.transform=`translateX(-${e.rect.left-o.left}px)`}else{t.element.classList.remove(n.ReorderDecreaseClassName);t.element.style.transform=""}}))}else{const[e]=l;c.forEach((e=>{e.element.style.transform="";e.element.classList.remove(n.ReorderDecreaseClassName)}));l.forEach((t=>{if(o.left+s<=t.rect.left+t.rect.width/2){t.element.classList.add(n.ReorderIncreaseClassName);t.element.style.transform=`translateX(${o.left+o.width-e.rect.left-e.rect.width}px)`}else{t.element.classList.remove(n.ReorderIncreaseClassName);t.element.style.transform=""}}))}}),50)},vertical:{name:"vertical",getMoveTransform:(e,t)=>`translateY(${t}px)`,setOtherTransform:_.throttle(((e,t,s,r)=>{if(!t.classList.contains(n.ReorderingClassName)){return}const a=e.find((e=>e.element===t));if(!a){return}const o=a.rect;const l=e.filter((e=>e.rect.top<o.top)).sort(((e,t)=>t.rect.top-e.rect.top));const c=e.filter((e=>e.rect.top>o.top)).sort(((e,t)=>e.rect.top-t.rect.top));if(r>=0){const[e]=c;l.forEach((e=>{e.element.style.transform="";e.element.classList.remove(n.ReorderIncreaseClassName)}));c.forEach((t=>{if(o.top+r+o.height>=t.rect.top+t.rect.height/2){t.element.classList.add(n.ReorderDecreaseClassName);t.element.style.transform=`translateY(-${e.rect.top-o.top}px)`}else{t.element.classList.remove(n.ReorderDecreaseClassName);t.element.style.transform=""}}))}else{const[e]=l;c.forEach((e=>{e.element.style.transform="";e.element.classList.remove(n.ReorderDecreaseClassName)}));l.forEach((t=>{if(o.top+r<=t.rect.top+t.rect.height/2){t.element.classList.add(n.ReorderIncreaseClassName);t.element.style.transform=`translateY(${o.top+o.height-e.rect.top-e.rect.height}px)`}else{t.element.classList.remove(n.ReorderIncreaseClassName);t.element.style.transform=""}}))}}),50)}};class n extends EventTarget{constructor(e){super();this.container=e;s(this,"orientation",r.horizontal);s(this,"enabled",false);s(this,"children",void 0);s(this,"snapshots",new Map);s(this,"attachedEvents",[]);this.children=[...this.container.children];if(this.children.every((e=>e.style.order===""))){this.children.forEach(((e,t)=>{e.style.order=(t+1).toString()}))}}addEventListener(e,t,s){super.addEventListener(e,t,s)}removeEventListener(e,t,s){super.addEventListener(e,t,s)}getOrderMap(){return new Map(this.children.map((e=>[e,parseInt(e.style.order)])))}toggle(){if(this.enabled){this.disable()}else{this.enable()}}generateSnapshots(){this.children.forEach((e=>{this.snapshots.set(e,{element:e,rect:e.getBoundingClientRect()})}))}enable(){this.children.forEach((e=>{let t=0;let s=0;let r=false;e.classList.add(n.ReorderEnabledClassName);const a=()=>{const a=(a,o)=>{e.classList.add(n.ReorderingClassName);e.style.transition="none";e.style.userSelect="none";this.generateSnapshots();this.children.filter((t=>t!==e)).forEach((e=>e.style.transition="transform .2s ease-out"));t=a;s=o;r=true};const o=e=>{a(e.screenX,e.screenY)};e.addEventListener("mousedown",o);this.attachedEvents.push((()=>e.removeEventListener("mousedown",o)));const l=e=>{if(e.touches.length!==1){return}const[t]=e.touches;a(t.screenX,t.screenY)};e.addEventListener("touchstart",l);this.attachedEvents.push((()=>e.removeEventListener("touchstart",l)))};const o=()=>{const n=(r,n)=>{const a=r-t;const o=n-s;e.style.transform=this.orientation.getMoveTransform(a,o);this.orientation.setOtherTransform([...this.snapshots.values()],e,a,o)};const a=e=>{if(!r){return}n(e.screenX,e.screenY);if(e.cancelable){e.preventDefault()}};document.addEventListener("mousemove",a);this.attachedEvents.push((()=>document.removeEventListener("mousemove",a)));const o=e=>{if(!r){return}if(e.touches.length!==1){return}const[t]=e.touches;n(t.screenX,t.screenY);if(e.cancelable){e.preventDefault()}};e.addEventListener("touchmove",o,{passive:false});this.attachedEvents.push((()=>e.removeEventListener("touchmove",o)))};const l=()=>{const t=()=>{if(!r){return}e.classList.remove(n.ReorderingClassName);r=false;let t=0;const s=this.children.filter((t=>t!==e));const a=[];s.forEach((e=>{e.style.transform="";e.style.transition="";if(e.classList.contains(n.ReorderIncreaseClassName)){t--;e.style.order=(parseInt(e.style.order)+1).toString();e.classList.remove(n.ReorderIncreaseClassName);a.push(this.snapshots.get(e))}else if(e.classList.contains(n.ReorderDecreaseClassName)){t++;e.style.order=(parseInt(e.style.order)-1).toString();e.classList.remove(n.ReorderDecreaseClassName);a.push(this.snapshots.get(e))}}));e.style.userSelect="";e.style.order=(parseInt(e.style.order)+t).toString();e.style.transform="";e.style.transition="";this.dispatchEvent(new CustomEvent("reorder",{detail:this.children.map((e=>({element:e,order:parseInt(e.style.order)})))}))};document.addEventListener("mouseup",t);this.attachedEvents.push((()=>document.removeEventListener("mouseup",t)));e.addEventListener("touchend",t);this.attachedEvents.push((()=>e.removeEventListener("touchend",t)))};a();o();l()}));this.enabled=true}disable(){this.attachedEvents.forEach((e=>e()));this.children.forEach((e=>e.classList.remove(n.ReorderEnabledClassName)));this.enabled=false}}s(n,"ReorderDecreaseClassName","reorder-decrease");s(n,"ReorderIncreaseClassName","reorder-increase");s(n,"ReorderEnabledClassName","reorder-enabled");s(n,"ReorderingClassName","reordering");return{export:{ReorderOrientations:r,Reorder:n}}})(); |