libretime/legacy/public/js/wavesurfer/regions.min.js

8 lines
19 KiB
JavaScript

/*!
* wavesurfer.js regions plugin 4.6.0 (2024-02-05)
* https://wavesurfer-js.org
* @license BSD-3-Clause
*/
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("WaveSurfer",[],t):"object"==typeof exports?exports.WaveSurfer=t():(e.WaveSurfer=e.WaveSurfer||{},e.WaveSurfer.regions=t())}(this,(()=>(()=>{"use strict";var e={608:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var n=r(488);function i(e){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i(e)}function s(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function a(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?s(Object(r),!0).forEach((function(t){o(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):s(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function o(e,t,r){return(t=l(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function u(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,l(n.key),n)}}function l(e){var t=function(e,t){if("object"!=i(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=i(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==i(t)?t:String(t)}t.default=function(){function e(t,r){var i=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.params=t,this.wavesurfer=r,this.util=a(a({},r.util),{},{getRegionSnapToGridValue:function(e){return i.getRegionSnapToGridValue(e,t)}}),this.maxRegions=t.maxRegions,this.regionsMinLength=t.regionsMinLength||null,Object.getOwnPropertyNames(this.util.Observer.prototype).forEach((function(e){n.Region.prototype[e]=i.util.Observer.prototype[e]})),this.wavesurfer.Region=n.Region,this._onBackendCreated=function(){i.wrapper=i.wavesurfer.drawer.wrapper,i.params.regions&&i.params.regions.forEach((function(e){e.edgeScrollWidth=i.params.edgeScrollWidth||.05*i.wrapper.clientWidth,i.add(e)}))},this.list={},this._onReady=function(){i.wrapper=i.wavesurfer.drawer.wrapper,i.params.dragSelection&&i.enableDragSelection(i.params),Object.keys(i.list).forEach((function(e){i.list[e].updateRender()}))}}var t,r,i;return t=e,i=[{key:"create",value:function(t){return{name:"regions",deferInit:!(!t||!t.deferInit)&&t.deferInit,params:t,staticProps:{addRegion:function(e){return this.initialisedPluginList.regions||this.initPlugin("regions"),this.regions.add(e)},clearRegions:function(){this.regions&&this.regions.clear()},enableDragSelection:function(e){this.initialisedPluginList.regions||this.initPlugin("regions"),this.regions.enableDragSelection(e)},disableDragSelection:function(){this.regions.disableDragSelection()}},instance:e}}}],(r=[{key:"init",value:function(){this.wavesurfer.isReady?(this._onBackendCreated(),this._onReady()):(this.wavesurfer.once("ready",this._onReady),this.wavesurfer.once("backend-created",this._onBackendCreated))}},{key:"destroy",value:function(){this.wavesurfer.un("ready",this._onReady),this.wavesurfer.un("backend-created",this._onBackendCreated),this.disableDragSelection(),this.clear()}},{key:"wouldExceedMaxRegions",value:function(){return this.maxRegions&&Object.keys(this.list).length>=this.maxRegions}},{key:"add",value:function(e){var t=this;if(this.wouldExceedMaxRegions())return null;!e.minLength&&this.regionsMinLength&&(e=a(a({},e),{},{minLength:this.regionsMinLength}));var r=new this.wavesurfer.Region(e,this.util,this.wavesurfer);return this.list[r.id]=r,r.on("remove",(function(){delete t.list[r.id]})),r}},{key:"clear",value:function(){var e=this;Object.keys(this.list).forEach((function(t){e.list[t].remove()}))}},{key:"enableDragSelection",value:function(e){var t=this;this.disableDragSelection();var r,n,i,s,a,o,u,l=e.slop||2,h=this.wavesurfer.drawer.container,d=!1!==e.scroll&&this.wavesurfer.params.scrollParent,c=e.scrollSpeed||1,f=e.scrollThreshold||10,v=this.wavesurfer.getDuration(),p=0,g=function e(r){if(s&&o){var a=t.wrapper.scrollLeft+c*o;t.wrapper.scrollLeft=a=Math.min(n,Math.max(0,a));var u=t.wavesurfer.drawer.handleEvent(r);s.update({start:Math.min(u*v,i*v),end:Math.max(u*v,i*v)}),a<n&&a>0&&window.requestAnimationFrame((function(){e(r)}))}},m=function(e){e.touches&&e.touches.length>1||(v=t.wavesurfer.getDuration(),a=e.targetTouches?e.targetTouches[0].identifier:null,n=t.wrapper.scrollWidth-t.wrapper.clientWidth,u=t.wrapper.getBoundingClientRect(),r=!0,i=t.wavesurfer.drawer.handleEvent(e,!0),s=null,o=null)};this.wrapper.addEventListener("mousedown",m),this.wrapper.addEventListener("touchstart",m),this.on("disable-drag-selection",(function(){t.wrapper.removeEventListener("touchstart",m),t.wrapper.removeEventListener("mousedown",m)}));var w=function(e){e.touches&&e.touches.length>1||(r=!1,p=0,o=null,s&&(t.util.preventClick(),s.fireEvent("update-end",e),t.wavesurfer.fireEvent("region-update-end",s,e)),s=null)};this.wrapper.addEventListener("mouseleave",w),this.wrapper.addEventListener("mouseup",w),this.wrapper.addEventListener("touchend",w),document.body.addEventListener("mouseup",w),document.body.addEventListener("touchend",w),this.on("disable-drag-selection",(function(){document.body.removeEventListener("mouseup",w),document.body.removeEventListener("touchend",w),t.wrapper.removeEventListener("touchend",w),t.wrapper.removeEventListener("mouseup",w),t.wrapper.removeEventListener("mouseleave",w)}));var b=function(n){if(r&&!(++p<=l)&&!(n.touches&&n.touches.length>1)&&(!n.targetTouches||n.targetTouches[0].identifier==a)&&(s||(s=t.add(e||{})))){var c=t.wavesurfer.drawer.handleEvent(n),m=t.wavesurfer.regions.util.getRegionSnapToGridValue(i*v),w=t.wavesurfer.regions.util.getRegionSnapToGridValue(c*v);if(s.update({start:Math.min(w,m),end:Math.max(w,m)}),d&&h.clientWidth<t.wrapper.scrollWidth){var b=n.clientX-u.left;(o=b<=f?-1:b>=u.right-f?1:null)&&g(n)}}};this.wrapper.addEventListener("mousemove",b),this.wrapper.addEventListener("touchmove",b),this.on("disable-drag-selection",(function(){t.wrapper.removeEventListener("touchmove",b),t.wrapper.removeEventListener("mousemove",b)})),this.wavesurfer.on("region-created",(function(e){t.regionsMinLength&&(e.minLength=t.regionsMinLength)}))}},{key:"disableDragSelection",value:function(){this.fireEvent("disable-drag-selection")}},{key:"getCurrentRegion",value:function(){var e=this,t=this.wavesurfer.getCurrentTime(),r=null;return Object.keys(this.list).forEach((function(n){var i=e.list[n];i.start<=t&&i.end>=t&&(!r||i.end-i.start<r.end-r.start)&&(r=i)})),r}},{key:"getRegionSnapToGridValue",value:function(e,t){if(t.snapToGridInterval){var r=t.snapToGridOffset||0;return Math.round((e-r)/t.snapToGridInterval)*t.snapToGridInterval+r}return e}}])&&u(t.prototype,r),i&&u(t,i),Object.defineProperty(t,"prototype",{writable:!1}),e}();e.exports=t.default},488:(e,t)=>{function r(e){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},r(e)}function n(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,i(n.key),n)}}function i(e){var t=function(e,t){if("object"!=r(e)||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var i=n.call(e,t||"default");if("object"!=r(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==r(t)?t:String(t)}Object.defineProperty(t,"__esModule",{value:!0}),t.Region=void 0;t.Region=function(){function e(t,r,n){var i=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.wavesurfer=n,this.wrapper=n.drawer.wrapper,this.util=n.util,this.style=this.util.style,this.regionsUtil=r,this.id=null==t.id?n.util.getId():t.id,this.start=Number(t.start)||0,this.end=null==t.end?this.start+4/this.wrapper.scrollWidth*this.wavesurfer.getDuration():Number(t.end),this.resize=void 0===t.resize||Boolean(t.resize),this.drag=void 0===t.drag||Boolean(t.drag),this.isResizing=!1,this.isDragging=!1,this.loop=Boolean(t.loop),this.color=t.color||"rgba(0, 0, 0, 0.1)",this.handleStyle=t.handleStyle||{left:{},right:{}},this.handleLeftEl=null,this.handleRightEl=null,this.data=t.data||{},this.attributes=t.attributes||{},this.maxLength=t.maxLength,this.minLength=t.minLength,this._onRedraw=function(){return i.updateRender()},this.scroll=!1!==t.scroll&&n.params.scrollParent,this.scrollSpeed=t.scrollSpeed||1,this.scrollThreshold=t.scrollThreshold||10,this.preventContextMenu=void 0!==t.preventContextMenu&&Boolean(t.preventContextMenu);var s=null==t.channelIdx?-1:parseInt(t.channelIdx);if(this.regionHeight="100%",this.marginTop="0px",-1!==s){var a=null!=this.wavesurfer.backend.buffer?this.wavesurfer.backend.buffer.numberOfChannels:-1;a>=0&&s<a&&(this.regionHeight=Math.floor(1/a*100)+"%",this.marginTop=this.wavesurfer.getHeight()*s+"px")}this.formatTimeCallback=t.formatTimeCallback,this.edgeScrollWidth=t.edgeScrollWidth,this.bindInOut(),this.render(),this.wavesurfer.on("zoom",this._onRedraw),this.wavesurfer.on("redraw",this._onRedraw),this.wavesurfer.fireEvent("region-created",this)}var t,r,i;return t=e,(r=[{key:"update",value:function(e){null!=e.start&&(this.start=Number(e.start)),null!=e.end&&(this.end=Number(e.end)),null!=e.loop&&(this.loop=Boolean(e.loop)),null!=e.color&&(this.color=e.color),null!=e.handleStyle&&(this.handleStyle=e.handleStyle),null!=e.data&&(this.data=e.data),null!=e.resize&&(this.resize=Boolean(e.resize),this.updateHandlesResize(this.resize)),null!=e.drag&&(this.drag=Boolean(e.drag)),null!=e.maxLength&&(this.maxLength=Number(e.maxLength)),null!=e.minLength&&(this.minLength=Number(e.minLength)),null!=e.attributes&&(this.attributes=e.attributes),this.updateRender(),this.fireEvent("update"),this.wavesurfer.fireEvent("region-updated",this)}},{key:"remove",value:function(){this.element&&(this.wrapper.removeChild(this.element),this.element=null,this.fireEvent("remove"),this.wavesurfer.un("zoom",this._onRedraw),this.wavesurfer.un("redraw",this._onRedraw),this.wavesurfer.fireEvent("region-removed",this))}},{key:"play",value:function(e){var t=e||this.start;this.wavesurfer.play(t,this.end),this.fireEvent("play"),this.wavesurfer.fireEvent("region-play",this)}},{key:"playLoop",value:function(e){this.loop=!0,this.play(e)}},{key:"setLoop",value:function(e){this.loop=e}},{key:"render",value:function(){var e=document.createElement("region");for(var t in e.className="wavesurfer-region",e.title=this.formatTime(this.start,this.end),e.setAttribute("data-id",this.id),this.attributes)e.setAttribute("data-region-"+t,this.attributes[t]);if(this.style(e,{position:"absolute",zIndex:2,height:this.regionHeight,top:this.marginTop}),this.resize){this.handleLeftEl=e.appendChild(document.createElement("handle")),this.handleRightEl=e.appendChild(document.createElement("handle")),this.handleLeftEl.className="wavesurfer-handle wavesurfer-handle-start",this.handleRightEl.className="wavesurfer-handle wavesurfer-handle-end";var r={cursor:"col-resize",position:"absolute",top:"0px",width:"2px",height:"100%",backgroundColor:"rgba(0, 0, 0, 1)"},n="none"!==this.handleStyle.left?Object.assign({left:"0px"},r,this.handleStyle.left):null,i="none"!==this.handleStyle.right?Object.assign({right:"0px"},r,this.handleStyle.right):null;n&&this.style(this.handleLeftEl,n),i&&this.style(this.handleRightEl,i)}this.element=this.wrapper.appendChild(e),this.updateRender(),this.bindEvents(e)}},{key:"formatTime",value:function(e,t){return this.formatTimeCallback?this.formatTimeCallback(e,t):(e==t?[e]:[e,t]).map((function(e){return[Math.floor(e%3600/60),("00"+Math.floor(e%60)).slice(-2)].join(":")})).join("-")}},{key:"getWidth",value:function(){return this.wavesurfer.drawer.width/this.wavesurfer.params.pixelRatio}},{key:"updateRender",value:function(){var e=this.wavesurfer.getDuration(),t=this.getWidth(),r=this.start,n=this.end;if(r<0&&(n-=r=0),n>e&&(r=e-((n=e)-r)),null!=this.minLength&&(n=Math.max(r+this.minLength,n)),null!=this.maxLength&&(n=Math.min(r+this.maxLength,n)),null!=this.element){var i=Math.round(r/e*t),s=Math.round(n/e*t)-i;for(var a in this.style(this.element,{left:i+"px",width:s+"px",backgroundColor:this.color,cursor:this.drag?"move":"default"}),this.attributes)this.element.setAttribute("data-region-"+a,this.attributes[a]);this.element.title=this.formatTime(this.start,this.end)}}},{key:"bindInOut",value:function(){var e=this;this.firedIn=!1,this.firedOut=!1;var t=function(t){var r=Math.round(10*e.start)/10,n=Math.round(10*e.end)/10;t=Math.round(10*t)/10,!e.firedOut&&e.firedIn&&(r>t||n<=t)&&(e.firedOut=!0,e.firedIn=!1,e.fireEvent("out"),e.wavesurfer.fireEvent("region-out",e)),!e.firedIn&&r<=t&&n>t&&(e.firedIn=!0,e.firedOut=!1,e.fireEvent("in"),e.wavesurfer.fireEvent("region-in",e))};this.wavesurfer.backend.on("audioprocess",t),this.on("remove",(function(){e.wavesurfer.backend.un("audioprocess",t)})),this.on("out",(function(){if(e.loop){var t=e.wavesurfer.getCurrentTime();t>=e.start&&t<=e.end&&e.wavesurfer.play(e.start)}}))}},{key:"bindEvents",value:function(){var e=this,t=this.preventContextMenu;this.element.addEventListener("mouseenter",(function(t){e.fireEvent("mouseenter",t),e.wavesurfer.fireEvent("region-mouseenter",e,t)})),this.element.addEventListener("mouseleave",(function(t){e.fireEvent("mouseleave",t),e.wavesurfer.fireEvent("region-mouseleave",e,t)})),this.element.addEventListener("click",(function(t){t.preventDefault(),e.fireEvent("click",t),e.wavesurfer.fireEvent("region-click",e,t)})),this.element.addEventListener("dblclick",(function(t){t.stopPropagation(),t.preventDefault(),e.fireEvent("dblclick",t),e.wavesurfer.fireEvent("region-dblclick",e,t)})),this.element.addEventListener("contextmenu",(function(r){t&&r.preventDefault(),e.fireEvent("contextmenu",r),e.wavesurfer.fireEvent("region-contextmenu",e,r)})),(this.drag||this.resize)&&this.bindDragEvents()}},{key:"bindDragEvents",value:function(){var e,t,r,n,i,s,a,o,u,l=this,h=this.wavesurfer.drawer.container,d=this.scrollSpeed,c=(this.scrollThreshold,!1),f=function t(h){var c=l.wavesurfer.getDuration();if(s&&(r||i)){var f=h.clientX,v=0,p=0,g=0,m=l.regionsUtil.getRegionSnapToGridValue(l.wavesurfer.drawer.handleEvent(h)*c);if(r)-1===s?(p=o*l.wavesurfer.params.minPxPerSec,v=f-a.left):(p=u*l.wavesurfer.params.minPxPerSec,v=a.right-f);else{var w=l.minLength;w||(w=0),"start"===i?(m>l.end-w&&(m=l.end-w,g=d*s),m<0&&(m=0)):"end"===i&&(m<l.start+w&&(m=l.start+w,g=d*s),m>c&&(m=c))}if(-1===s){if(0===Math.round(l.wrapper.scrollLeft))return;if(Math.round(l.wrapper.scrollLeft-p+v)<=0)return}else{if(Math.round(l.wrapper.scrollLeft)===n)return;if(Math.round(l.wrapper.scrollLeft+p-v)>=n)return}var b=l.wrapper.scrollLeft-g+d*s;if(-1===s){var y=Math.max(0+p-v,b);l.wrapper.scrollLeft=b=y}else{var E=Math.min(n-p+v,b);l.wrapper.scrollLeft=b=E}var L=m-e;e=m,r?l.onDrag(L):l.onResize(L,i),window.requestAnimationFrame((function(){t(h)}))}},v=function(s){var h=l.wavesurfer.getDuration();s.touches&&s.touches.length>1||(t=s.targetTouches?s.targetTouches[0].identifier:null,(l.drag||l.resize)&&s.stopPropagation(),e=l.regionsUtil.getRegionSnapToGridValue(l.wavesurfer.drawer.handleEvent(s,!0)*h),o=e-l.start,u=l.end-e,n=l.wrapper.scrollWidth-l.wrapper.clientWidth,a=l.wrapper.getBoundingClientRect(),l.isResizing=!1,l.isDragging=!1,"handle"===s.target.tagName.toLowerCase()?(l.isResizing=!0,i=s.target.classList.contains("wavesurfer-handle-start")?"start":"end"):(l.isDragging=!0,r=!0,i=!1))},p=function(e){e.touches&&e.touches.length>1||((r||i)&&(l.isDragging=!1,l.isResizing=!1,r=!1,s=null,i=!1),c&&(c=!1,l.util.preventClick(),l.fireEvent("update-end",e),l.wavesurfer.fireEvent("region-update-end",l,e)))},g=function(n){var d=l.wavesurfer.getDuration();if(!(n.touches&&n.touches.length>1)&&(!n.targetTouches||n.targetTouches[0].identifier==t)&&(r||i)){var v=l.regionsUtil.getRegionSnapToGridValue(l.wavesurfer.drawer.handleEvent(n)*d);if(r){var p=l.wavesurfer.getDuration();v>p-u&&(v=p-u),v-o<0&&(v=o)}if(i){var g=l.minLength;g||(g=0),"start"===i?(v>l.end-g&&(v=l.end-g),v<0&&(v=0)):"end"===i&&(v<l.start+g&&(v=l.start+g),v>d&&(v=d))}var m=v-e;if(e=v,l.drag&&r&&(c=c||!!m,l.onDrag(m)),l.resize&&i&&(c=c||!!m,l.onResize(m,i)),l.scroll&&h.clientWidth<l.wrapper.scrollWidth){if(r){var w=n.clientX;s=w<a.left+l.edgeScrollWidth?-1:w>a.right-l.edgeScrollWidth?1:null}else{var b=n.clientX;s=b<a.left+l.edgeScrollWidth?-1:b>a.right-l.edgeScrollWidth?1:null}s&&f(n)}}};this.element.addEventListener("mousedown",v),this.element.addEventListener("touchstart",v),document.body.addEventListener("mousemove",g),document.body.addEventListener("touchmove",g),document.body.addEventListener("mouseup",p),document.body.addEventListener("touchend",p),this.on("remove",(function(){document.body.removeEventListener("mouseup",p),document.body.removeEventListener("touchend",p),document.body.removeEventListener("mousemove",g),document.body.removeEventListener("touchmove",g)})),this.wavesurfer.on("destroy",(function(){document.body.removeEventListener("mouseup",p),document.body.removeEventListener("touchend",p)}))}},{key:"onDrag",value:function(e){var t=this.wavesurfer.getDuration();this.end+e>t&&(e=t-this.end),this.start+e<0&&(e=-1*this.start),this.update({start:this.start+e,end:this.end+e})}},{key:"onResize",value:function(e,t){var r=this.wavesurfer.getDuration();"start"===t?(e>0&&this.end-(this.start+e)<this.minLength&&(e=this.end-this.minLength-this.start),e<0&&this.start+e<0&&(e=-1*this.start),this.update({start:Math.min(this.start+e,this.end),end:Math.max(this.start+e,this.end)})):(e<0&&this.end+e-this.start<this.minLength&&(e=this.start+this.minLength-this.end),e>0&&this.end+e>r&&(e=r-this.end),this.update({start:Math.min(this.end+e,this.start),end:Math.max(this.end+e,this.start)}))}},{key:"updateHandlesResize",value:function(e){var t=e?"col-resize":"auto";this.handleLeftEl&&this.style(this.handleLeftEl,{cursor:t}),this.handleRightEl&&this.style(this.handleRightEl,{cursor:t})}}])&&n(t.prototype,r),i&&n(t,i),Object.defineProperty(t,"prototype",{writable:!1}),e}()}},t={};var r=function r(n){var i=t[n];if(void 0!==i)return i.exports;var s=t[n]={exports:{}};return e[n](s,s.exports,r),s.exports}(608);return r})()));
//# sourceMappingURL=wavesurfer.regions.min.js.map