mirror of
https://github.com/zadam/trilium.git
synced 2024-11-10 17:13:45 +08:00
1 line
18 KiB
JavaScript
1 line
18 KiB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).WZoom=e()}(this,(function(){"use strict";function t(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(t);e&&(o=o.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,o)}return n}function e(e){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?t(Object(o),!0).forEach((function(t){r(e,t,o[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):t(Object(o)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))}))}return e}function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,g(o.key),o)}}function i(t,e,n){return e&&o(t.prototype,e),n&&o(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function r(t,e,n){return(e=g(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function s(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&a(t,e)}function c(t){return c=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},c(t)}function a(t,e){return a=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},a(t,e)}function l(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function u(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}();return function(){var n,o=c(t);if(e){var i=c(this).constructor;n=Reflect.construct(o,arguments,i)}else n=o.apply(this,arguments);return function(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return l(t)}(this,n)}}function h(){return h="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,n){var o=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=c(t)););return t}(t,e);if(o){var i=Object.getOwnPropertyDescriptor(o,e);return i.get?i.get.call(arguments.length<3?t:n):i.value}},h.apply(this,arguments)}function f(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=n){var o,i,r,s,c=[],a=!0,l=!1;try{if(r=(n=n.call(t)).next,0===e){if(Object(n)!==n)return;a=!1}else for(;!(a=(o=r.call(n)).done)&&(c.push(o.value),c.length!==e);a=!0);}catch(t){l=!0,i=t}finally{try{if(!a&&null!=n.return&&(s=n.return(),Object(s)!==s))return}finally{if(l)throw i}}return c}}(t,e)||p(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function p(t,e){if(t){if("string"==typeof t)return d(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?d(t,e):void 0}}function d(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=new Array(e);n<e;n++)o[n]=t[n];return o}function m(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=p(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var o=0,i=function(){};return{s:i,n:function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var r,s=!0,c=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){c=!0,r=t},f:function(){try{s||null==n.return||n.return()}finally{if(c)throw r}}}}function g(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var o=n.call(t,e||"default");if("object"!=typeof o)return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}function v(t){var e=t.getBoundingClientRect(),n=document,o=n.body,i=n.documentElement,r=b(),s=y(),c=i.clientTop||o.clientTop||0,a=i.clientLeft||o.clientLeft||0;return{top:e.top+r-c,left:e.left+s-a}}function y(){var t=void 0!==window.pageXOffset,e="CSS1Compat"===(document.compatMode||"");return t?window.pageXOffset:e?document.documentElement.scrollLeft:document.body.scrollLeft}function b(){var t=void 0!==window.pageYOffset,e="CSS1Compat"===(document.compatMode||"");return t?window.pageYOffset:e?document.documentElement.scrollTop:document.body.scrollTop}function _(t,e,n){var o=arguments.length>3&&void 0!==arguments[3]&&arguments[3];t.addEventListener(e,n,o)}function w(t,e,n){var o=arguments.length>3&&void 0!==arguments[3]&&arguments[3];t.removeEventListener(e,n,o)}function H(){return"ontouchstart"in window||navigator.MaxTouchPoints>0||navigator.msMaxTouchPoints>0}function O(t){return"wheel"===t.type||"pointerup"===t.type||"pointerdown"===t.type||"pointermove"===t.type||"mousedown"===t.type||"mousemove"===t.type||"mouseup"===t.type?t.clientX:t.changedTouches[0].clientX}function S(t){return"wheel"===t.type||"pointerup"===t.type||"pointerdown"===t.type||"pointermove"===t.type||"mousedown"===t.type||"mousemove"===t.type||"mouseup"===t.type?t.clientY:t.changedTouches[0].clientY}function T(t,e,n,o,i,r){var s=i/2-(t+e-n)+o;return s*r-s+o}function P(t,e,n,o,i){switch(t){case"left":o/2-i<e/2&&(i=(o-e)/2);break;case"right":o/2+i<e/2&&(i=(o-e)/2*-1);break;default:if((o-e)/2+n<Math.abs(i))i=((o-e)/2+n)*(i<0?-1:1)}return i}var x={type:"image",width:null,height:null,dragScrollable:!0,dragScrollableOptions:{},minScale:null,maxScale:1,speed:1.1,zoomOnClick:!0,zoomOnDblClick:!1,smoothTime:.25,alignContent:"center",disableWheelZoom:!1,reverseWheelDirection:!1},j=function(){function t(){n(this,t),this.subscribes={}}return i(t,[{key:"on",value:function(t,e){return t in this.subscribes||(this.subscribes[t]=[]),this.subscribes[t].push(e),this}},{key:"destroy",value:function(){for(var t in this)this.hasOwnProperty(t)&&(this[t]=null)}},{key:"_run",value:function(t,e){if(this.subscribes[t]){var n,o=m(this.subscribes[t]);try{for(o.s();!(n=o.n()).done;){(0,n.value)(e)}}catch(t){o.e(t)}finally{o.f()}}}}]),t}(),k="grab",W="move",M="drop",D=function(t){s(r,t);var o=u(r);function r(t){var e;return n(this,r),(e=o.call(this)).target=t,e.moveTimer=null,e.coordinates=null,e.coordinatesShift=null,e.isTouch=H(),e.events=e.isTouch?{grab:"touchstart",move:"touchmove",drop:"touchend"}:{grab:"mousedown",move:"mousemove",drop:"mouseup"},e.events.options=!!e.isTouch&&{passive:!1},e._dropHandler=e._dropHandler.bind(l(e)),e._grabHandler=e._grabHandler.bind(l(e)),e._moveHandler=e._moveHandler.bind(l(e)),_(e.target,e.events.grab,e._grabHandler,e.events.options),e}return i(r,[{key:"destroy",value:function(){w(this.target,this.events.grab,this._grabHandler,this.events.options),h(c(r.prototype),"destroy",this).call(this)}},{key:"_grabHandler",value:function(t){(this.isTouch&&1===t.touches.length||1===t.buttons)&&(this.coordinates={x:O(t),y:S(t)},this.coordinatesShift={x:0,y:0},_(document,this.events.drop,this._dropHandler,this.events.options),_(document,this.events.move,this._moveHandler,this.events.options),this._run(k,t))}},{key:"_dropHandler",value:function(t){w(document,this.events.drop,this._dropHandler),w(document,this.events.move,this._moveHandler),this._run(M,t)}},{key:"_moveHandler",value:function(t){if(this.isTouch&&t.touches.length>1)return!1;var n=this.coordinatesShift,o=this.coordinates;n.x=O(t)-o.x,n.y=S(t)-o.y,o.x=O(t),o.y=S(t),clearTimeout(this.moveTimer),this.moveTimer=setTimeout((function(){n.x=0,n.y=0}),50),t.data=e(e({},t.data||{}),{},{x:n.x,y:n.y}),this._run(W,t)}}]),r}(j),$="click",E="dblclick",z="wheel",C=function(t){s(o,t);var e=u(o);function o(t){var i;return n(this,o),(i=e.call(this)).target=t,i.coordsOnDown=null,i.pressingTimeout=null,i.firstClick=!0,i.isTouch=H(),i.events=i.isTouch?{down:"touchstart",up:"touchend"}:{down:"mousedown",up:"mouseup"},i.events.options=!!i.isTouch&&{passive:!0},i._downHandler=i._downHandler.bind(l(i)),i._upHandler=i._upHandler.bind(l(i)),i._wheelHandler=i._wheelHandler.bind(l(i)),_(i.target,i.events.down,i._downHandler,i.events.options),_(i.target,i.events.up,i._upHandler,i.events.options),_(i.target,z,i._wheelHandler),i}return i(o,[{key:"destroy",value:function(){w(this.target,this.events.down,this._downHandler,this.events.options),w(this.target,this.events.up,this._upHandler,this.events.options),w(this.target,z,this._wheelHandler,this.events.options),h(c(o.prototype),"destroy",this).call(this)}},{key:"_downHandler",value:function(t){this.coordsOnDown=null,(this.isTouch&&1===t.touches.length||1===t.buttons)&&(this.coordsOnDown={x:O(t),y:S(t)}),clearTimeout(this.pressingTimeout)}},{key:"_upHandler",value:function(t){var e=this,n=this.subscribes[E]?200:0;this.firstClick?(this.pressingTimeout=setTimeout((function(){e.coordsOnDown&&e.coordsOnDown.x===O(t)&&e.coordsOnDown.y===S(t)&&e._run($,t),e.firstClick=!0}),n),this.firstClick=!1):this.pressingTimeout=setTimeout((function(){e._run(E,t),e.firstClick=!0}),n/2)}},{key:"_wheelHandler",value:function(t){this._run(z,t)}}]),o}(j),L="pinchtozoom",Y=function(t){s(r,t);var o=u(r);function r(t){var e;return n(this,r),(e=o.call(this)).target=t,e.fingersHypot=null,e.zoomPinchWasDetected=!1,e._touchMoveHandler=e._touchMoveHandler.bind(l(e)),e._touchEndHandler=e._touchEndHandler.bind(l(e)),_(e.target,"touchmove",e._touchMoveHandler),_(e.target,"touchend",e._touchEndHandler),e}return i(r,[{key:"destroy",value:function(){w(this.target,"touchmove",this._touchMoveHandler),w(this.target,"touchend",this._touchEndHandler),h(c(r.prototype),"destroy",this).call(this)}},{key:"_touchMoveHandler",value:function(t){if(2===t.targetTouches.length){var n=t.targetTouches[0].clientX,o=t.targetTouches[0].clientY,i=t.targetTouches[1].clientX,r=t.targetTouches[1].clientY,s=Math.round(Math.sqrt(Math.pow(Math.abs(n-i),2)+Math.pow(Math.abs(o-r),2))),c=0;if(s>this.fingersHypot+5&&(c=-1),s<this.fingersHypot-5&&(c=1),0!==c){if(null!==this.fingersHypot||1===c){var a=Math.min(n,i)+Math.abs(n-i)/2,l=Math.min(o,r)+Math.abs(o-r)/2;t.data=e(e({},t.data||{}),{},{clientX:a,clientY:l,direction:c}),this._run(L,t)}this.fingersHypot=s,this.zoomPinchWasDetected=!0}}}},{key:"_touchEndHandler",value:function(){this.zoomPinchWasDetected&&(this.fingersHypot=null,this.zoomPinchWasDetected=!1)}}]),r}(j);function X(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(this._init=this._init.bind(this),this._prepare=this._prepare.bind(this),this._computeScale=this._computeScale.bind(this),this._computePosition=this._computePosition.bind(this),this._transform=this._transform.bind(this),this.content={},"string"==typeof t)this.content.$element=document.querySelector(t);else{if(!(t instanceof HTMLElement))throw"WZoom: `selectorOrHTMLElement` must be selector or HTMLElement, and not ".concat({}.toString.call(t));this.content.$element=t}this.content.$element&&(this.viewport={},this.viewport.$element=this.content.$element.parentElement,this.options=function(t,e){var n=Object.assign({},e,t);n.dragScrollableOptions=Object.assign({},n.dragScrollableOptions),n.smoothTime=Number(n.smoothTime)||x.smoothTime,H()&&(n.smoothTime=0);return n}(e,x),this.isTouch=H(),this.direction=1,this.observers=[],"image"===this.options.type?this.content.$element.complete?this._init():_(this.content.$element,"load",this._init,{once:!0}):this._init())}return X.prototype={constructor:X,_init:function(){var t=this,e=this.viewport,n=this.content,o=this.options,i=this.observers;if(this._prepare(),this._destroyObservers(),!0===o.dragScrollable){var r=new D(n.$element);i.push(r),"function"==typeof o.dragScrollableOptions.onGrab&&r.on(k,(function(e){e.preventDefault(),o.dragScrollableOptions.onGrab(e,t)})),"function"==typeof o.dragScrollableOptions.onDrop&&r.on(M,(function(e){e.preventDefault(),o.dragScrollableOptions.onDrop(e,t)})),r.on(W,(function(i){i.preventDefault();var r=i.data,s=r.x,c=r.y,a=n.currentLeft+s,l=n.currentTop+c,u=(n.currentWidth-e.originalWidth)/2+n.correctX,h=(n.currentHeight-e.originalHeight)/2+n.correctY;Math.abs(a)<=u&&(n.currentLeft=a),Math.abs(l)<=h&&(n.currentTop=l),t._transform(),"function"==typeof o.dragScrollableOptions.onMove&&o.dragScrollableOptions.onMove(i,t)}))}var s=new C(n.$element);if(i.push(s),!o.disableWheelZoom)if(this.isTouch){var c=new Y(n.$element);i.push(c),c.on(L,(function(e){var n=e.data,o=n.clientX,i=n.clientY,r=n.direction,s=t._computeScale(r);t._computePosition(s,o,i),t._transform()}))}else s.on(z,(function(e){e.preventDefault();var n=o.reverseWheelDirection?-e.deltaY:e.deltaY,i=t._computeScale(n);t._computePosition(i,O(e),S(e)),t._transform()}));if(o.zoomOnClick||o.zoomOnDblClick){var a=o.zoomOnDblClick?E:$;s.on(a,(function(e){var o=1===t.direction?n.maxScale:n.minScale;t._computePosition(o,O(e),S(e)),t._transform(),t.direction*=-1}))}},_prepare:function(){var t=this.viewport,e=this.content,n=this.options,o=v(t.$element),i=o.left,r=o.top;t.originalWidth=t.$element.offsetWidth,t.originalHeight=t.$element.offsetHeight,t.originalLeft=i,t.originalTop=r,"image"===n.type?(e.originalWidth=n.width||e.$element.naturalWidth,e.originalHeight=n.height||e.$element.naturalHeight):(e.originalWidth=n.width||e.$element.offsetWidth,e.originalHeight=n.height||e.$element.offsetHeight),e.maxScale=n.maxScale,e.minScale=n.minScale||Math.min(t.originalWidth/e.originalWidth,t.originalHeight/e.originalHeight,e.maxScale),e.currentScale=e.minScale,e.currentWidth=e.originalWidth*e.currentScale,e.currentHeight=e.originalHeight*e.currentScale;var s=function(t,e,n){var o=0,i=0;switch(n){case"top":i=(e.currentHeight-t.originalHeight)/2;break;case"right":o=(e.currentWidth-t.originalWidth)/2*-1;break;case"bottom":i=(e.currentHeight-t.originalHeight)/2*-1;break;case"left":o=(e.currentWidth-t.originalWidth)/2}return[o,i]}(t,e,n.alignContent),c=f(s,2);e.alignPointX=c[0],e.alignPointY=c[1],e.currentLeft=e.alignPointX,e.currentTop=e.alignPointY;var a=function(t,e,n){var o=Math.max(0,(t.originalWidth-e.currentWidth)/2),i=Math.max(0,(t.originalHeight-e.currentHeight)/2);switch(n){case"top":i=0;break;case"right":o=0;break;case"bottom":i*=2;break;case"left":o*=2}return[o,i]}(t,e,n.alignContent),l=f(a,2);e.correctX=l[0],e.correctY=l[1],"function"==typeof n.prepare&&n.prepare(this),this._transform()},_computeScale:function(t){this.direction=t<0?1:-1;var e=this.content,n=e.minScale,o=e.maxScale,i=e.currentScale*Math.pow(this.options.speed,this.direction);return i<=n?(this.direction=1,n):i>=o?(this.direction=-1,o):i},_computePosition:function(t,e,n){var o=this.viewport,i=this.content,r=this.options,s=this.direction,c=i.originalWidth*t,a=i.originalHeight*t,l=y(),u=b(),h=T(e,l,o.originalLeft,i.currentLeft,o.originalWidth,c/i.currentWidth),f=T(n,u,o.originalTop,i.currentTop,o.originalHeight,a/i.currentHeight);-1===s&&(h=P(r.alignContent,o.originalWidth,i.correctX,c,h),f=P(r.alignContent,o.originalHeight,i.correctY,a,f)),t===i.minScale&&(h=i.alignPointX,f=i.alignPointY),i.currentWidth=c,i.currentHeight=a,i.currentLeft=h,i.currentTop=f,i.currentScale=t},_transform:function(){var t,e;t=this.content.$element,(e=this.options.smoothTime)?t.style.transition="transform ".concat(e,"s"):t.style.removeProperty("transition"),function(t,e,n,o){t.style.transform="translate(".concat(e,"px, ").concat(n,"px) scale(").concat(o,")")}(this.content.$element,this.content.currentLeft,this.content.currentTop,this.content.currentScale),"function"==typeof this.options.rescale&&this.options.rescale(this)},_zoom:function(t){var e,n,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};void 0!==o.x&&void 0!==o.y||(e=this.viewport,o={x:(n=v(e.$element)).left+e.originalWidth/2-y(),y:n.top+e.originalHeight/2-b()}),this._computePosition(t,o.x,o.y),this._transform()},_destroyObservers:function(){var t,e=m(this.observers);try{for(e.s();!(t=e.n()).done;){t.value.destroy()}}catch(t){e.e(t)}finally{e.f()}},prepare:function(){this._prepare()},transform:function(t,e,n){var o=this.content;o.currentWidth=o.originalWidth*n,o.currentHeight=o.originalHeight*n,o.currentLeft=e,o.currentTop=t,o.currentScale=n,this._transform()},zoomUp:function(){this._zoom(this._computeScale(-1))},zoomDown:function(){this._zoom(this._computeScale(1))},maxZoomUp:function(){this._zoom(this.content.maxScale)},maxZoomDown:function(){this._zoom(this.content.minScale)},zoomUpToPoint:function(t){this._zoom(this._computeScale(-1),t)},zoomDownToPoint:function(t){this._zoom(this._computeScale(1),t)},maxZoomUpToPoint:function(t){this._zoom(this.content.maxScale,t)},destroy:function(){for(var t in this.content.$element.style.removeProperty("transition"),this.content.$element.style.removeProperty("transform"),"image"===this.options.type&&w(this.content.$element,"load",this._init),this._destroyObservers(),this)this.hasOwnProperty(t)&&(this[t]=null)}},X.create=function(t){return new X(t,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{})},X}));
|