(function(f,d){typeof exports=="object"&&typeof module<"u"?module.exports=d(require("quill")):typeof define=="function"&&define.amd?define(["quill"],d):(f=typeof globalThis<"u"?globalThis:f||self,f["@umn-latis/quill-better-image-module"]=d(f.Quill))})(this,function(f){"use strict";var zo=Object.defineProperty;var Fo=(f,d,y)=>d in f?zo(f,d,{enumerable:!0,configurable:!0,writable:!0,value:y}):f[d]=y;var l=(f,d,y)=>Fo(f,typeof d!="symbol"?d+"":d,y);var d=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{};function y(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function De(e,t,r){switch(r.length){case 0:return e.call(t);case 1:return e.call(t,r[0]);case 2:return e.call(t,r[0],r[1]);case 3:return e.call(t,r[0],r[1],r[2])}return e.apply(t,r)}var R=De;function Be(e){return e}var K=Be,Ue=R,W=Math.max;function ke(e,t,r){return t=W(t===void 0?e.length-1:t,0),function(){for(var a=arguments,n=-1,i=W(a.length-t,0),o=Array(i);++n0){if(++t>=Xt)return arguments[0]}else t=0;return e.apply(void 0,arguments)}}var Zt=Yt,er=Vt,tr=Zt,rr=tr(er),ar=rr,nr=K,ir=ze,sr=ar;function or(e,t){return sr(ir(e,t,nr),e+"")}var ae=or;function lr(){this.__data__=[],this.size=0}var cr=lr;function hr(e,t){return e===t||e!==e&&t!==t}var j=hr,ur=j;function dr(e,t){for(var r=e.length;r--;)if(ur(e[r][0],t))return r;return-1}var I=dr,pr=I,fr=Array.prototype,vr=fr.splice;function gr(e){var t=this.__data__,r=pr(t,e);if(r<0)return!1;var a=t.length-1;return r==a?t.pop():vr.call(t,r,1),--this.size,!0}var yr=gr,br=I;function _r(e){var t=this.__data__,r=br(t,e);return r<0?void 0:t[r][1]}var mr=_r,$r=I;function xr(e){return $r(this.__data__,e)>-1}var Cr=xr,wr=I;function Tr(e,t){var r=this.__data__,a=wr(r,e);return a<0?(++this.size,r.push([e,t])):r[a][1]=t,this}var Or=Tr,Sr=cr,Ar=yr,jr=mr,Ir=Cr,Mr=Or;function _(e){var t=-1,r=e==null?0:e.length;for(this.clear();++t-1&&e%1==0&&e<=Vn}var ye=Xn,Qn=k,Jn=ye;function Yn(e){return e!=null&&Jn(e.length)&&!Qn(e)}var N=Yn,Zn=N,ei=O;function ti(e){return ei(e)&&Zn(e)}var ri=ti,q={exports:{}};function ai(){return!1}var ni=ai;q.exports,function(e,t){var r=b,a=ni,n=t&&!t.nodeType&&t,i=n&&!0&&e&&!e.nodeType&&e,o=i&&i.exports===n,s=o?r.Buffer:void 0,c=s?s.isBuffer:void 0,u=c||a;e.exports=u}(q,q.exports);var be=q.exports,ii=A,si=ue,oi=O,li="[object Object]",ci=Function.prototype,hi=Object.prototype,_e=ci.toString,ui=hi.hasOwnProperty,di=_e.call(Object);function pi(e){if(!oi(e)||ii(e)!=li)return!1;var t=si(e);if(t===null)return!0;var r=ui.call(t,"constructor")&&t.constructor;return typeof r=="function"&&r instanceof r&&_e.call(r)==di}var fi=pi,vi=A,gi=ye,yi=O,bi="[object Arguments]",_i="[object Array]",mi="[object Boolean]",$i="[object Date]",xi="[object Error]",Ci="[object Function]",wi="[object Map]",Ti="[object Number]",Oi="[object Object]",Si="[object RegExp]",Ai="[object Set]",ji="[object String]",Ii="[object WeakMap]",Mi="[object ArrayBuffer]",Pi="[object DataView]",Ei="[object Float32Array]",Li="[object Float64Array]",qi="[object Int8Array]",Di="[object Int16Array]",Bi="[object Int32Array]",Ui="[object Uint8Array]",ki="[object Uint8ClampedArray]",zi="[object Uint16Array]",Fi="[object Uint32Array]",h={};h[Ei]=h[Li]=h[qi]=h[Di]=h[Bi]=h[Ui]=h[ki]=h[zi]=h[Fi]=!0,h[bi]=h[_i]=h[Mi]=h[mi]=h[Pi]=h[$i]=h[xi]=h[Ci]=h[wi]=h[Ti]=h[Oi]=h[Si]=h[Ai]=h[ji]=h[Ii]=!1;function Gi(e){return yi(e)&&gi(e.length)&&!!h[vi(e)]}var Ni=Gi;function Hi(e){return function(t){return e(t)}}var Ri=Hi,D={exports:{}};D.exports,function(e,t){var r=V,a=t&&!t.nodeType&&t,n=a&&!0&&e&&!e.nodeType&&e,i=n&&n.exports===a,o=i&&r.process,s=function(){try{var c=n&&n.require&&n.require("util").types;return c||o&&o.binding&&o.binding("util")}catch{}}();e.exports=s}(D,D.exports);var Ki=D.exports,Wi=Ni,Vi=Ri,me=Ki,$e=me&&me.isTypedArray,Xi=$e?Vi($e):Wi,xe=Xi;function Qi(e,t){if(!(t==="constructor"&&typeof e[t]=="function")&&t!="__proto__")return e[t]}var Ce=Qi,Ji=G,Yi=j,Zi=Object.prototype,es=Zi.hasOwnProperty;function ts(e,t,r){var a=e[t];(!(es.call(e,t)&&Yi(a,r))||r===void 0&&!(t in e))&&Ji(e,t,r)}var rs=ts,as=rs,ns=G;function is(e,t,r,a){var n=!r;r||(r={});for(var i=-1,o=t.length;++i-1&&e%1==0&&e1?r[n-1]:void 0,o=n>2?r[2]:void 0;for(i=e.length>3&&typeof i=="function"?(n--,i):void 0,o&&vo(r[0],r[1],o)&&(i=n<3?void 0:i,n=1),t=Object(t);++a{});l(this,"onDestroy",()=>{});l(this,"onUpdate",()=>{});this.overlay=t.overlay,this.img=t.img,this.options=t.options,this.requestUpdate=t.onUpdate}}class Io extends B{constructor(){super(...arguments);l(this,"onCreate",()=>{this.display=document.createElement("div"),Object.assign(this.display.style,this.options.displayStyles),this.overlay.appendChild(this.display)});l(this,"onDestroy",()=>{});l(this,"onUpdate",()=>{if(!this.display||!this.img)return;const r=this.getCurrentSize();this.display.innerHTML=r.join(" × ")});l(this,"getCurrentSize",()=>[this.img.width,Math.round(this.img.width/this.img.naturalWidth*this.img.naturalHeight)])}}const Mo=` `,Po=` `,Eo=` `,C=new f.Parchment.StyleAttributor("float","float"),S=new f.Parchment.StyleAttributor("margin","margin"),U=new f.Parchment.StyleAttributor("display","display");class Lo extends B{constructor(){super(...arguments);l(this,"onCreate",()=>{this.toolbar=document.createElement("div"),Object.assign(this.toolbar.style,this.options.toolbarStyles),this.overlay.appendChild(this.toolbar),this._defineAlignments(),this._addToolbarButtons()});l(this,"_defineAlignments",()=>{this.alignments=[{icon:Mo,apply:()=>{U.add(this.img,"inline"),C.add(this.img,"left"),S.add(this.img,"0 1em 1em 0")},isApplied:()=>C.value(this.img)=="left"},{icon:Po,apply:()=>{U.add(this.img,"block"),C.remove(this.img),S.add(this.img,"auto")},isApplied:()=>S.value(this.img)=="auto"},{icon:Eo,apply:()=>{U.add(this.img,"inline"),C.add(this.img,"right"),S.add(this.img,"0 0 1em 1em")},isApplied:()=>C.value(this.img)=="right"}]});l(this,"_addToolbarButtons",()=>{const r=[];this.alignments.forEach((a,n)=>{const i=document.createElement("span");r.push(i),i.innerHTML=a.icon,i.addEventListener("click",()=>{r.forEach(o=>o.style.filter=""),a.isApplied()?(C.remove(this.img),S.remove(this.img),U.remove(this.img)):(this._selectButton(i),a.apply()),this.requestUpdate()}),Object.assign(i.style,this.options.toolbarButtonStyles),n>0&&(i.style.borderLeftWidth="0"),i.children[0]&&Object.assign(i.children[0].style,this.options.toolbarButtonSvgStyles),a.isApplied()&&this._selectButton(i),this.toolbar.appendChild(i)})});l(this,"_selectButton",r=>{r.style.filter="invert(20%)"})}}class qo extends B{constructor(){super(...arguments);l(this,"onCreate",()=>{this.boxes=[],this.addBox("nwse-resize"),this.addBox("nesw-resize"),this.addBox("nwse-resize"),this.addBox("nesw-resize"),this.positionBoxes()});l(this,"onDestroy",()=>{this.setCursor("")});l(this,"positionBoxes",()=>{const r=`${-parseFloat(this.options.handleStyles.width)/2}px`,a=`${-parseFloat(this.options.handleStyles.height)/2}px`;[{left:r,top:a},{right:r,top:a},{right:r,bottom:a},{left:r,bottom:a}].forEach((n,i)=>{Object.assign(this.boxes[i].style,n)})});l(this,"addBox",r=>{const a=document.createElement("div");Object.assign(a.style,this.options.handleStyles),a.style.cursor=r,a.style.width=`${this.options.handleStyles.width}px`,a.style.height=`${this.options.handleStyles.height}px`,a.addEventListener("mousedown",this.handleMousedown,!1),this.overlay.appendChild(a),this.boxes.push(a)});l(this,"handleMousedown",r=>{this.dragBox=r.target,this.dragStartX=r.clientX,this.preDragWidth=this.img.width||this.img.naturalWidth,this.setCursor(this.dragBox.style.cursor),document.addEventListener("mousemove",this.handleDrag,!1),document.addEventListener("mouseup",this.handleMouseup,!1)});l(this,"handleMouseup",()=>{this.setCursor(""),document.removeEventListener("mousemove",this.handleDrag),document.removeEventListener("mouseup",this.handleMouseup)});l(this,"handleDrag",r=>{if(!this.img)return;const a=r.clientX-this.dragStartX;this.dragBox===this.boxes[0]||this.dragBox===this.boxes[3]?this.img.width=Math.round(this.preDragWidth-a):this.img.width=Math.round(this.preDragWidth+a),this.requestUpdate()});l(this,"setCursor",r=>{[document.body,this.img].forEach(a=>{a.style.cursor=r})})}}const Do='',Bo=`.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border-width: 0; } .ql-alt-text { position: absolute; bottom: 0; left: 0; width: 100%; } .ql-alt-text .ql-alt-text__editor { display: none; } .ql-alt-text--is-open .ql-alt-text__editor { display: block; width: 100%; } .ql-alt-text__button { font-size: 0.75rem; display: inline-flex; gap: 0.5rem; padding: 0.5rem; align-items: center; justify-content: center; line-height: 0.75; text-transform: uppercase; background: rgba(0, 0, 0, 0.5); color: #ccc; border: none; margin: 0; } .ql-alt-text__button .icon svg { fill: #ccc; } .ql-alt-text--is-open .ql-alt-text__button { background: rgba(255, 255, 255, 0.75); color: #000; } .ql-alt-text--is-open .ql-alt-text__button .icon svg { fill: #000; } .ql-alt-text__text-area { display: block; width: 100%; padding: 0.5rem; background: rgba(255, 255, 255, 0.75); color: #000; /* give a min-width and height so that the textarea is usable even for small images This may overflow the container, but that's okay */ min-height: 4rem; min-width: 10rem; border: none; margin: 0; } `;class Uo extends B{constructor(){super(...arguments);l(this,"onCreate",()=>{this.altText=document.createElement("div"),this.altText.classList.add("ql-alt-text"),Object.assign(this.altText.style,this.options.altTextStyles),this.altText.innerHTML=`
`,this.altText.querySelector(".ql-alt-text__button").addEventListener("click",()=>{this.altText.classList.toggle("ql-alt-text--is-open"),this.altText.querySelector(".ql-alt-text__label").classList.toggle("sr-only")}),this.altText.querySelector("#alt-text-input").addEventListener("input",n=>{n.stopPropagation(),this.img.alt=n.target.value}),this.overlay.appendChild(this.altText)})}}const ko={DisplaySize:Io,Toolbar:Lo,Resize:qo,AltText:Uo};class Le{constructor(t,r={}){l(this,"initializeModules",()=>{this.removeModules(),this.modules=this.moduleClasses.map(t=>new(ko[t]||t)(this)),this.modules.forEach(t=>{t.onCreate()}),this.onUpdate()});l(this,"onUpdate",()=>{this.repositionElements(),this.modules.forEach(t=>{t.onUpdate()})});l(this,"removeModules",()=>{this.modules.forEach(t=>{t.onDestroy()}),this.modules=[]});l(this,"handleClick",t=>{if(t.target&&t.target.tagName&&t.target.tagName.toUpperCase()==="IMG"){if(this.img===t.target)return;this.img&&this.hide(),this.show(t.target)}else this.img&&this.hide()});l(this,"show",t=>{this.img=t,this.showOverlay(),this.initializeModules()});l(this,"showOverlay",()=>{this.overlay&&this.hideOverlay(),this.quill.setSelection(null),this.setUserSelect("none"),document.addEventListener("keyup",this.checkImage,!0),this.quill.root.addEventListener("input",this.checkImage,!0),this.overlay=document.createElement("div"),Object.assign(this.overlay.style,this.options.overlayStyles),this.quill.root.parentNode.appendChild(this.overlay),this.repositionElements()});l(this,"hideOverlay",()=>{this.overlay&&(this.quill.root.parentNode.removeChild(this.overlay),this.overlay=void 0,document.removeEventListener("keyup",this.checkImage),this.quill.root.removeEventListener("input",this.checkImage),this.setUserSelect(""))});l(this,"repositionElements",()=>{if(!this.overlay||!this.img)return;const t=this.quill.root.parentNode,r=this.img.getBoundingClientRect(),a=t.getBoundingClientRect();Object.assign(this.overlay.style,{left:`${r.left-a.left-1+t.scrollLeft}px`,top:`${r.top-a.top+t.scrollTop}px`,width:`${r.width}px`,height:`${r.height}px`})});l(this,"hide",()=>{this.hideOverlay(),this.removeModules(),this.img=void 0});l(this,"setUserSelect",t=>{["userSelect","mozUserSelect","webkitUserSelect","msUserSelect"].forEach(r=>{this.quill.root.style[r]=t,document.documentElement.style[r]=t})});l(this,"checkImage",t=>{this.img&&(["input","textarea"].includes(t.target.tagName.toLowerCase())||(t.keyCode==46||t.keyCode==8)&&window.Quill.find(this.img).deleteAt(0))});this.quill=t;let a=!1;r.modules&&(a=r.modules.slice()),this.options=Ao({},r,jo),a!==!1&&(this.options.modules=a),document.execCommand("enableObjectResizing",!1,"false"),this.quill.root.addEventListener("click",this.handleClick,!1),t.emitter.listenDOM("click",document.body,n=>{this.quill.root.parentNode.contains(n.target)||this.hide()}),this.quill.root.parentNode.style.position=this.quill.root.parentNode.style.position||"relative",this.moduleClasses=this.options.modules,this.modules=[]}}return window.Quill&&window.Quill.register("modules/betterImage",Le),Le});