iflrandevu/assets/ext/trumbowyg/plugins/resizimg/trumbowyg.resizimg.min.js

1 line
3.6 KiB
JavaScript
Raw Normal View History

2020-05-12 21:47:37 +03:00
!function(e){"use strict";function i(e){e.stopPropagation(),e.preventDefault()}function t(e){e.$ed.find("canvas.resizable").resizable("destroy").off("mousedown",i).removeClass("resizable"),r.reset(),e.syncCode()}var s={minSize:32,step:4},n=function(){this.resizeCanvas=document.createElement("canvas"),this.resizeCanvas.setAttribute("tabindex","0"),this.resizeCanvas.id="trumbowyg-resizimg-"+ +new Date,this.ctx=null,this.resizeImg=null,this.pressEscape=function(e){e.reset()},this.pressBackspaceOrDelete=function(i){e(i.resizeCanvas).replaceWith(""),i.resizeImg=null};var t,s,n=!1,r=!1,a=function(e){var i=e.getBoundingClientRect();t=i.left,s=i.top},h=function(e,i,t,s,n){return i.translate(.5,.5),i.lineWidth=1,i.drawImage(t,5,5,s-10,n-10),i.beginPath(),i.rect(5,5,s-10,n-10),i.stroke(),i.beginPath(),i.fillStyle="rgb(255, 255, 255)",i.rect(s-10,n-10,9,9),i.fill(),i.stroke(),a(e),i};this.init=function(){var i=this;e(window).on("scroll resize",function(){i.reCalcOffset()})},this.reCalcOffset=function(){a(this.resizeCanvas)},this.canvasId=function(){return this.resizeCanvas.id},this.isActive=function(){return null!==this.resizeImg},this.isFocusedNow=function(){return n},this.blurNow=function(){n=!1},this.reset=function(){null!==this.resizeImg&&(this.resizeImg.width=this.resizeCanvas.clientWidth-10,this.resizeImg.height=this.resizeCanvas.clientHeight-10,this.resizeImg.removeAttribute("style"),e(this.resizeCanvas).replaceWith(e(this.resizeImg)),this.resizeCanvas.removeAttribute("style"),this.resizeImg=null)},this.setup=function(a,o){if(this.resizeImg=a,!this.resizeCanvas.getContext)return!1;n=!0,this.resizeCanvas.width=e(this.resizeImg).width()+10,this.resizeCanvas.height=e(this.resizeImg).height()+10,this.resizeCanvas.style.margin="-5px",this.ctx=this.resizeCanvas.getContext("2d"),e(this.resizeImg).replaceWith(e(this.resizeCanvas)),h(this.resizeCanvas,this.ctx,this.resizeImg,this.resizeCanvas.width,this.resizeCanvas.height),e(this.resizeCanvas).resizable(o).on("mousedown",i);var c=this;return e(this.resizeCanvas).on("mousemove",function(e){var i=Math.round(e.clientX-t),n=Math.round(e.clientY-s),a=r;c.ctx.rect(c.resizeCanvas.width-10,c.resizeCanvas.height-10,9,9),r=c.ctx.isPointInPath(i,n),a!==r&&(this.style.cursor=r?"se-resize":"default")}).on("keydown",function(e){if(c.isActive()){var i=e.keyCode;27===i?c.pressEscape(c):8!==i&&46!==i||c.pressBackspaceOrDelete(c)}}).on("focus",i),this.resizeCanvas.focus(),!0},this.refresh=function(){this.resizeCanvas.getContext&&(this.resizeCanvas.width=this.resizeCanvas.clientWidth,this.resizeCanvas.height=this.resizeCanvas.clientHeight,h(this.resizeCanvas,this.ctx,this.resizeImg,this.resizeCanvas.width,this.resizeCanvas.height))}},r=new n;e.extend(!0,e.trumbowyg,{plugins:{resizimg:{init:function(n){function a(){n.$ed.find("img").off("click").on("click",function(e){r.isActive()&&r.reset(),r.setup(this,n.o.plugins.resizimg.resizable),i(e)})}n.o.plugins.resizimg=e.extend(!0,{},s,n.o.plugins.resizimg||{},{resizable:{resizeWidth:!1,onDragStart:function(e,i){var t=n.o.plugins.resizimg,s=e.pageX-i.offset().left,r=e.pageY-i.offset().top;if(s<i.width()-t.minSize||r<i.height()-t.minSize)return!1},onDrag:function(e,i,t,s){var r=n.o.plugins.resizimg;return s<r.minSize&&(s=r.minSize),s-=s%r.step,i.height(s),!1},onDragEnd:function(){r.refresh(),n.syncCode()}}}),n.$c.on("tbwinit",function(){a(),n.$ed.on("click",function(t){e(t.target).is("img")||t.target.id===r.canvasId()||(i(t),r.reset(),n.$c.trigger("tbwchange"))}),n.$ed.on("scroll",function(){r.reCalcOffset()})}),n.$c.on("tbwfocus tbwchange",a),n.$c.on("tbwresize",function(){r.reCalcOffset()}),n.$c.on("tbwblur",function(){r.isFocusedNow()?r.blurNow():t(n)})},destroy:function(e){t(e)}}}})}(jQuery);