medium-proeditor
Version:
A powerful & customizable Medium-style rich text editor
2 lines (1 loc) • 33 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).InlineTooltip={})}(this,(function(t){"use strict";function e(t){return"Object"===function(t){return Object.prototype.toString.call(t).slice(8,-1)}(t)&&(t.constructor===Object&&Object.getPrototypeOf(t)===Object.prototype)}function r(t,n){const i={...t};if(e(t)&&e(n))for(const s of Object.keys(n))e(n[s])&&e(t[s])?i[s]=r(t[s],n[s]):i[s]=n[s];return i}class n{config;options;storage;constructor(t){this.config={...t},t.addOptions&&(this.options=t.addOptions.call({name:t.name})),t.addStorage&&(this.storage=t.addStorage.call({name:t.name,options:this.options}))}static create(t){return new n(t)}configure(t={}){return new n({...this.config,addOptions:()=>r(this.options,t)})}}function i(t,e,r){for(let n=0;;n++){if(n==t.childCount||n==e.childCount)return t.childCount==e.childCount?null:r;let s=t.child(n),o=e.child(n);if(s!=o){if(!s.sameMarkup(o))return r;if(s.isText&&s.text!=o.text){for(let t=0;s.text[t]==o.text[t];t++)r++;return r}if(s.content.size||o.content.size){let t=i(s.content,o.content,r+1);if(null!=t)return t}r+=s.nodeSize}else r+=s.nodeSize}}function s(t,e,r,n){for(let i=t.childCount,o=e.childCount;;){if(0==i||0==o)return i==o?null:{a:r,b:n};let a=t.child(--i),l=e.child(--o),h=a.nodeSize;if(a!=l){if(!a.sameMarkup(l))return{a:r,b:n};if(a.isText&&a.text!=l.text){let t=0,e=Math.min(a.text.length,l.text.length);for(;t<e&&a.text[a.text.length-t-1]==l.text[l.text.length-t-1];)t++,r--,n--;return{a:r,b:n}}if(a.content.size||l.content.size){let t=s(a.content,l.content,r-1,n-1);if(t)return t}r-=h,n-=h}else r-=h,n-=h}}class o{constructor(t,e){if(this.content=t,this.size=e||0,null==e)for(let e=0;e<t.length;e++)this.size+=t[e].nodeSize}nodesBetween(t,e,r,n=0,i){for(let s=0,o=0;o<e;s++){let a=this.content[s],l=o+a.nodeSize;if(l>t&&!1!==r(a,n+o,i||null,s)&&a.content.size){let i=o+1;a.nodesBetween(Math.max(0,t-i),Math.min(a.content.size,e-i),r,n+i)}o=l}}descendants(t){this.nodesBetween(0,this.size,t)}textBetween(t,e,r,n){let i="",s=!0;return this.nodesBetween(t,e,((o,a)=>{let l=o.isText?o.text.slice(Math.max(t,a)-a,e-a):o.isLeaf?n?"function"==typeof n?n(o):n:o.type.spec.leafText?o.type.spec.leafText(o):"":"";o.isBlock&&(o.isLeaf&&l||o.isTextblock)&&r&&(s?s=!1:i+=r),i+=l}),0),i}append(t){if(!t.size)return this;if(!this.size)return t;let e=this.lastChild,r=t.firstChild,n=this.content.slice(),i=0;for(e.isText&&e.sameMarkup(r)&&(n[n.length-1]=e.withText(e.text+r.text),i=1);i<t.content.length;i++)n.push(t.content[i]);return new o(n,this.size+t.size)}cut(t,e=this.size){if(0==t&&e==this.size)return this;let r=[],n=0;if(e>t)for(let i=0,s=0;s<e;i++){let o=this.content[i],a=s+o.nodeSize;a>t&&((s<t||a>e)&&(o=o.isText?o.cut(Math.max(0,t-s),Math.min(o.text.length,e-s)):o.cut(Math.max(0,t-s-1),Math.min(o.content.size,e-s-1))),r.push(o),n+=o.nodeSize),s=a}return new o(r,n)}cutByIndex(t,e){return t==e?o.empty:0==t&&e==this.content.length?this:new o(this.content.slice(t,e))}replaceChild(t,e){let r=this.content[t];if(r==e)return this;let n=this.content.slice(),i=this.size+e.nodeSize-r.nodeSize;return n[t]=e,new o(n,i)}addToStart(t){return new o([t].concat(this.content),this.size+t.nodeSize)}addToEnd(t){return new o(this.content.concat(t),this.size+t.nodeSize)}eq(t){if(this.content.length!=t.content.length)return!1;for(let e=0;e<this.content.length;e++)if(!this.content[e].eq(t.content[e]))return!1;return!0}get firstChild(){return this.content.length?this.content[0]:null}get lastChild(){return this.content.length?this.content[this.content.length-1]:null}get childCount(){return this.content.length}child(t){let e=this.content[t];if(!e)throw new RangeError("Index "+t+" out of range for "+this);return e}maybeChild(t){return this.content[t]||null}forEach(t){for(let e=0,r=0;e<this.content.length;e++){let n=this.content[e];t(n,r,e),r+=n.nodeSize}}findDiffStart(t,e=0){return i(this,t,e)}findDiffEnd(t,e=this.size,r=t.size){return s(this,t,e,r)}findIndex(t,e=-1){if(0==t)return l(0,t);if(t==this.size)return l(this.content.length,t);if(t>this.size||t<0)throw new RangeError(`Position ${t} outside of fragment (${this})`);for(let r=0,n=0;;r++){let i=n+this.child(r).nodeSize;if(i>=t)return i==t||e>0?l(r+1,i):l(r,n);n=i}}toString(){return"<"+this.toStringInner()+">"}toStringInner(){return this.content.join(", ")}toJSON(){return this.content.length?this.content.map((t=>t.toJSON())):null}static fromJSON(t,e){if(!e)return o.empty;if(!Array.isArray(e))throw new RangeError("Invalid input for Fragment.fromJSON");return new o(e.map(t.nodeFromJSON))}static fromArray(t){if(!t.length)return o.empty;let e,r=0;for(let n=0;n<t.length;n++){let i=t[n];r+=i.nodeSize,n&&i.isText&&t[n-1].sameMarkup(i)?(e||(e=t.slice(0,n)),e[e.length-1]=i.withText(e[e.length-1].text+i.text)):e&&e.push(i)}return new o(e||t,r)}static from(t){if(!t)return o.empty;if(t instanceof o)return t;if(Array.isArray(t))return this.fromArray(t);if(t.attrs)return new o([t],t.nodeSize);throw new RangeError("Can not convert "+t+" to a Fragment"+(t.nodesBetween?" (looks like multiple versions of prosemirror-model were loaded)":""))}}o.empty=new o([],0);const a={index:0,offset:0};function l(t,e){return a.index=t,a.offset=e,a}function h(t,e){if(t===e)return!0;if(!t||"object"!=typeof t||!e||"object"!=typeof e)return!1;let r=Array.isArray(t);if(Array.isArray(e)!=r)return!1;if(r){if(t.length!=e.length)return!1;for(let r=0;r<t.length;r++)if(!h(t[r],e[r]))return!1}else{for(let r in t)if(!(r in e)||!h(t[r],e[r]))return!1;for(let r in e)if(!(r in t))return!1}return!0}class c{constructor(t,e){this.type=t,this.attrs=e}addToSet(t){let e,r=!1;for(let n=0;n<t.length;n++){let i=t[n];if(this.eq(i))return t;if(this.type.excludes(i.type))e||(e=t.slice(0,n));else{if(i.type.excludes(this.type))return t;!r&&i.type.rank>this.type.rank&&(e||(e=t.slice(0,n)),e.push(this),r=!0),e&&e.push(i)}}return e||(e=t.slice()),r||e.push(this),e}removeFromSet(t){for(let e=0;e<t.length;e++)if(this.eq(t[e]))return t.slice(0,e).concat(t.slice(e+1));return t}isInSet(t){for(let e=0;e<t.length;e++)if(this.eq(t[e]))return!0;return!1}eq(t){return this==t||this.type==t.type&&h(this.attrs,t.attrs)}toJSON(){let t={type:this.type.name};for(let e in this.attrs){t.attrs=this.attrs;break}return t}static fromJSON(t,e){if(!e)throw new RangeError("Invalid input for Mark.fromJSON");let r=t.marks[e.type];if(!r)throw new RangeError(`There is no mark type ${e.type} in this schema`);let n=r.create(e.attrs);return r.checkAttrs(n.attrs),n}static sameSet(t,e){if(t==e)return!0;if(t.length!=e.length)return!1;for(let r=0;r<t.length;r++)if(!t[r].eq(e[r]))return!1;return!0}static setFrom(t){if(!t||Array.isArray(t)&&0==t.length)return c.none;if(t instanceof c)return[t];let e=t.slice();return e.sort(((t,e)=>t.type.rank-e.type.rank)),e}}c.none=[];class u extends Error{}class p{constructor(t,e,r){this.content=t,this.openStart=e,this.openEnd=r}get size(){return this.content.size-this.openStart-this.openEnd}insertAt(t,e){let r=f(this.content,t+this.openStart,e);return r&&new p(r,this.openStart,this.openEnd)}removeBetween(t,e){return new p(d(this.content,t+this.openStart,e+this.openStart),this.openStart,this.openEnd)}eq(t){return this.content.eq(t.content)&&this.openStart==t.openStart&&this.openEnd==t.openEnd}toString(){return this.content+"("+this.openStart+","+this.openEnd+")"}toJSON(){if(!this.content.size)return null;let t={content:this.content.toJSON()};return this.openStart>0&&(t.openStart=this.openStart),this.openEnd>0&&(t.openEnd=this.openEnd),t}static fromJSON(t,e){if(!e)return p.empty;let r=e.openStart||0,n=e.openEnd||0;if("number"!=typeof r||"number"!=typeof n)throw new RangeError("Invalid input for Slice.fromJSON");return new p(o.fromJSON(t,e.content),r,n)}static maxOpen(t,e=!0){let r=0,n=0;for(let n=t.firstChild;n&&!n.isLeaf&&(e||!n.type.spec.isolating);n=n.firstChild)r++;for(let r=t.lastChild;r&&!r.isLeaf&&(e||!r.type.spec.isolating);r=r.lastChild)n++;return new p(t,r,n)}}function d(t,e,r){let{index:n,offset:i}=t.findIndex(e),s=t.maybeChild(n),{index:o,offset:a}=t.findIndex(r);if(i==e||s.isText){if(a!=r&&!t.child(o).isText)throw new RangeError("Removing non-flat range");return t.cut(0,e).append(t.cut(r))}if(n!=o)throw new RangeError("Removing non-flat range");return t.replaceChild(n,s.copy(d(s.content,e-i-1,r-i-1)))}function f(t,e,r,n){let{index:i,offset:s}=t.findIndex(e),o=t.maybeChild(i);if(s==e||o.isText)return n&&!n.canReplace(i,i,r)?null:t.cut(0,e).append(r).append(t.cut(e));let a=f(o.content,e-s-1,r);return a&&t.replaceChild(i,o.copy(a))}p.empty=new p(o.empty,0,0);const m=Math.pow(2,16);function g(t){return 65535&t}class w{constructor(t,e,r){this.pos=t,this.delInfo=e,this.recover=r}get deleted(){return(8&this.delInfo)>0}get deletedBefore(){return(5&this.delInfo)>0}get deletedAfter(){return(6&this.delInfo)>0}get deletedAcross(){return(4&this.delInfo)>0}}class y{constructor(t,e=!1){if(this.ranges=t,this.inverted=e,!t.length&&y.empty)return y.empty}recover(t){let e=0,r=g(t);if(!this.inverted)for(let t=0;t<r;t++)e+=this.ranges[3*t+2]-this.ranges[3*t+1];return this.ranges[3*r]+e+function(t){return(t-(65535&t))/m}(t)}mapResult(t,e=1){return this._map(t,e,!1)}map(t,e=1){return this._map(t,e,!0)}_map(t,e,r){let n=0,i=this.inverted?2:1,s=this.inverted?1:2;for(let o=0;o<this.ranges.length;o+=3){let a=this.ranges[o]-(this.inverted?n:0);if(a>t)break;let l=this.ranges[o+i],h=this.ranges[o+s],c=a+l;if(t<=c){let i=a+n+((l?t==a?-1:t==c?1:e:e)<0?0:h);if(r)return i;let s=t==(e<0?a:c)?null:o/3+(t-a)*m,u=t==a?2:t==c?1:4;return(e<0?t!=a:t!=c)&&(u|=8),new w(i,u,s)}n+=h-l}return r?t+n:new w(t+n,0,null)}touches(t,e){let r=0,n=g(e),i=this.inverted?2:1,s=this.inverted?1:2;for(let e=0;e<this.ranges.length;e+=3){let o=this.ranges[e]-(this.inverted?r:0);if(o>t)break;let a=this.ranges[e+i];if(t<=o+a&&e==3*n)return!0;r+=this.ranges[e+s]-a}return!1}forEach(t){let e=this.inverted?2:1,r=this.inverted?1:2;for(let n=0,i=0;n<this.ranges.length;n+=3){let s=this.ranges[n],o=s-(this.inverted?i:0),a=s+(this.inverted?0:i),l=this.ranges[n+e],h=this.ranges[n+r];t(o,o+l,a,a+h),i+=h-l}}invert(){return new y(this.ranges,!this.inverted)}toString(){return(this.inverted?"-":"")+JSON.stringify(this.ranges)}static offset(t){return 0==t?y.empty:new y(t<0?[0,-t,0]:[0,0,t])}}y.empty=new y([]);const S=Object.create(null);class k{getMap(){return y.empty}merge(t){return null}static fromJSON(t,e){if(!e||!e.stepType)throw new RangeError("Invalid input for Step.fromJSON");let r=S[e.stepType];if(!r)throw new RangeError(`No step type ${e.stepType} defined`);return r.fromJSON(t,e)}static jsonID(t,e){if(t in S)throw new RangeError("Duplicate use of step JSON ID "+t);return S[t]=e,e.prototype.jsonID=t,e}}class v{constructor(t,e){this.doc=t,this.failed=e}static ok(t){return new v(t,null)}static fail(t){return new v(null,t)}static fromReplace(t,e,r,n){try{return v.ok(t.replace(e,r,n))}catch(t){if(t instanceof u)return v.fail(t.message);throw t}}}function O(t,e,r){let n=[];for(let i=0;i<t.childCount;i++){let s=t.child(i);s.content.size&&(s=s.copy(O(s.content,e,s))),s.isInline&&(s=e(s,r,i)),n.push(s)}return o.fromArray(n)}class x extends k{constructor(t,e,r){super(),this.from=t,this.to=e,this.mark=r}apply(t){let e=t.slice(this.from,this.to),r=t.resolve(this.from),n=r.node(r.sharedDepth(this.to)),i=new p(O(e.content,((t,e)=>t.isAtom&&e.type.allowsMarkType(this.mark.type)?t.mark(this.mark.addToSet(t.marks)):t),n),e.openStart,e.openEnd);return v.fromReplace(t,this.from,this.to,i)}invert(){return new b(this.from,this.to,this.mark)}map(t){let e=t.mapResult(this.from,1),r=t.mapResult(this.to,-1);return e.deleted&&r.deleted||e.pos>=r.pos?null:new x(e.pos,r.pos,this.mark)}merge(t){return t instanceof x&&t.mark.eq(this.mark)&&this.from<=t.to&&this.to>=t.from?new x(Math.min(this.from,t.from),Math.max(this.to,t.to),this.mark):null}toJSON(){return{stepType:"addMark",mark:this.mark.toJSON(),from:this.from,to:this.to}}static fromJSON(t,e){if("number"!=typeof e.from||"number"!=typeof e.to)throw new RangeError("Invalid input for AddMarkStep.fromJSON");return new x(e.from,e.to,t.markFromJSON(e.mark))}}k.jsonID("addMark",x);class b extends k{constructor(t,e,r){super(),this.from=t,this.to=e,this.mark=r}apply(t){let e=t.slice(this.from,this.to),r=new p(O(e.content,(t=>t.mark(this.mark.removeFromSet(t.marks))),t),e.openStart,e.openEnd);return v.fromReplace(t,this.from,this.to,r)}invert(){return new x(this.from,this.to,this.mark)}map(t){let e=t.mapResult(this.from,1),r=t.mapResult(this.to,-1);return e.deleted&&r.deleted||e.pos>=r.pos?null:new b(e.pos,r.pos,this.mark)}merge(t){return t instanceof b&&t.mark.eq(this.mark)&&this.from<=t.to&&this.to>=t.from?new b(Math.min(this.from,t.from),Math.max(this.to,t.to),this.mark):null}toJSON(){return{stepType:"removeMark",mark:this.mark.toJSON(),from:this.from,to:this.to}}static fromJSON(t,e){if("number"!=typeof e.from||"number"!=typeof e.to)throw new RangeError("Invalid input for RemoveMarkStep.fromJSON");return new b(e.from,e.to,t.markFromJSON(e.mark))}}k.jsonID("removeMark",b);class N extends k{constructor(t,e){super(),this.pos=t,this.mark=e}apply(t){let e=t.nodeAt(this.pos);if(!e)return v.fail("No node at mark step's position");let r=e.type.create(e.attrs,null,this.mark.addToSet(e.marks));return v.fromReplace(t,this.pos,this.pos+1,new p(o.from(r),0,e.isLeaf?0:1))}invert(t){let e=t.nodeAt(this.pos);if(e){let t=this.mark.addToSet(e.marks);if(t.length==e.marks.length){for(let r=0;r<e.marks.length;r++)if(!e.marks[r].isInSet(t))return new N(this.pos,e.marks[r]);return new N(this.pos,this.mark)}}return new I(this.pos,this.mark)}map(t){let e=t.mapResult(this.pos,1);return e.deletedAfter?null:new N(e.pos,this.mark)}toJSON(){return{stepType:"addNodeMark",pos:this.pos,mark:this.mark.toJSON()}}static fromJSON(t,e){if("number"!=typeof e.pos)throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");return new N(e.pos,t.markFromJSON(e.mark))}}k.jsonID("addNodeMark",N);class I extends k{constructor(t,e){super(),this.pos=t,this.mark=e}apply(t){let e=t.nodeAt(this.pos);if(!e)return v.fail("No node at mark step's position");let r=e.type.create(e.attrs,null,this.mark.removeFromSet(e.marks));return v.fromReplace(t,this.pos,this.pos+1,new p(o.from(r),0,e.isLeaf?0:1))}invert(t){let e=t.nodeAt(this.pos);return e&&this.mark.isInSet(e.marks)?new N(this.pos,this.mark):this}map(t){let e=t.mapResult(this.pos,1);return e.deletedAfter?null:new I(e.pos,this.mark)}toJSON(){return{stepType:"removeNodeMark",pos:this.pos,mark:this.mark.toJSON()}}static fromJSON(t,e){if("number"!=typeof e.pos)throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");return new I(e.pos,t.markFromJSON(e.mark))}}k.jsonID("removeNodeMark",I);class M extends k{constructor(t,e,r,n=!1){super(),this.from=t,this.to=e,this.slice=r,this.structure=n}apply(t){return this.structure&&z(t,this.from,this.to)?v.fail("Structure replace would overwrite content"):v.fromReplace(t,this.from,this.to,this.slice)}getMap(){return new y([this.from,this.to-this.from,this.slice.size])}invert(t){return new M(this.from,this.from+this.slice.size,t.slice(this.from,this.to))}map(t){let e=t.mapResult(this.from,1),r=t.mapResult(this.to,-1);return e.deletedAcross&&r.deletedAcross?null:new M(e.pos,Math.max(e.pos,r.pos),this.slice)}merge(t){if(!(t instanceof M)||t.structure||this.structure)return null;if(this.from+this.slice.size!=t.from||this.slice.openEnd||t.slice.openStart){if(t.to!=this.from||this.slice.openStart||t.slice.openEnd)return null;{let e=this.slice.size+t.slice.size==0?p.empty:new p(t.slice.content.append(this.slice.content),t.slice.openStart,this.slice.openEnd);return new M(t.from,this.to,e,this.structure)}}{let e=this.slice.size+t.slice.size==0?p.empty:new p(this.slice.content.append(t.slice.content),this.slice.openStart,t.slice.openEnd);return new M(this.from,this.to+(t.to-t.from),e,this.structure)}}toJSON(){let t={stepType:"replace",from:this.from,to:this.to};return this.slice.size&&(t.slice=this.slice.toJSON()),this.structure&&(t.structure=!0),t}static fromJSON(t,e){if("number"!=typeof e.from||"number"!=typeof e.to)throw new RangeError("Invalid input for ReplaceStep.fromJSON");return new M(e.from,e.to,p.fromJSON(t,e.slice),!!e.structure)}}k.jsonID("replace",M);class E extends k{constructor(t,e,r,n,i,s,o=!1){super(),this.from=t,this.to=e,this.gapFrom=r,this.gapTo=n,this.slice=i,this.insert=s,this.structure=o}apply(t){if(this.structure&&(z(t,this.from,this.gapFrom)||z(t,this.gapTo,this.to)))return v.fail("Structure gap-replace would overwrite content");let e=t.slice(this.gapFrom,this.gapTo);if(e.openStart||e.openEnd)return v.fail("Gap is not a flat range");let r=this.slice.insertAt(this.insert,e.content);return r?v.fromReplace(t,this.from,this.to,r):v.fail("Content does not fit in gap")}getMap(){return new y([this.from,this.gapFrom-this.from,this.insert,this.gapTo,this.to-this.gapTo,this.slice.size-this.insert])}invert(t){let e=this.gapTo-this.gapFrom;return new E(this.from,this.from+this.slice.size+e,this.from+this.insert,this.from+this.insert+e,t.slice(this.from,this.to).removeBetween(this.gapFrom-this.from,this.gapTo-this.from),this.gapFrom-this.from,this.structure)}map(t){let e=t.mapResult(this.from,1),r=t.mapResult(this.to,-1),n=this.from==this.gapFrom?e.pos:t.map(this.gapFrom,-1),i=this.to==this.gapTo?r.pos:t.map(this.gapTo,1);return e.deletedAcross&&r.deletedAcross||n<e.pos||i>r.pos?null:new E(e.pos,r.pos,n,i,this.slice,this.insert,this.structure)}toJSON(){let t={stepType:"replaceAround",from:this.from,to:this.to,gapFrom:this.gapFrom,gapTo:this.gapTo,insert:this.insert};return this.slice.size&&(t.slice=this.slice.toJSON()),this.structure&&(t.structure=!0),t}static fromJSON(t,e){if("number"!=typeof e.from||"number"!=typeof e.to||"number"!=typeof e.gapFrom||"number"!=typeof e.gapTo||"number"!=typeof e.insert)throw new RangeError("Invalid input for ReplaceAroundStep.fromJSON");return new E(e.from,e.to,e.gapFrom,e.gapTo,p.fromJSON(t,e.slice),e.insert,!!e.structure)}}function z(t,e,r){let n=t.resolve(e),i=r-e,s=n.depth;for(;i>0&&s>0&&n.indexAfter(s)==n.node(s).childCount;)s--,i--;if(i>0){let t=n.node(s).maybeChild(n.indexAfter(s));for(;i>0;){if(!t||t.isLeaf)return!0;t=t.firstChild,i--}}return!1}k.jsonID("replaceAround",E);class T extends k{constructor(t,e,r){super(),this.pos=t,this.attr=e,this.value=r}apply(t){let e=t.nodeAt(this.pos);if(!e)return v.fail("No node at attribute step's position");let r=Object.create(null);for(let t in e.attrs)r[t]=e.attrs[t];r[this.attr]=this.value;let n=e.type.create(r,null,e.marks);return v.fromReplace(t,this.pos,this.pos+1,new p(o.from(n),0,e.isLeaf?0:1))}getMap(){return y.empty}invert(t){return new T(this.pos,this.attr,t.nodeAt(this.pos).attrs[this.attr])}map(t){let e=t.mapResult(this.pos,1);return e.deletedAfter?null:new T(e.pos,this.attr,this.value)}toJSON(){return{stepType:"attr",pos:this.pos,attr:this.attr,value:this.value}}static fromJSON(t,e){if("number"!=typeof e.pos||"string"!=typeof e.attr)throw new RangeError("Invalid input for AttrStep.fromJSON");return new T(e.pos,e.attr,e.value)}}k.jsonID("attr",T);class J extends k{constructor(t,e){super(),this.attr=t,this.value=e}apply(t){let e=Object.create(null);for(let r in t.attrs)e[r]=t.attrs[r];e[this.attr]=this.value;let r=t.type.create(e,t.content,t.marks);return v.ok(r)}getMap(){return y.empty}invert(t){return new J(this.attr,t.attrs[this.attr])}map(t){return this}toJSON(){return{stepType:"docAttr",attr:this.attr,value:this.value}}static fromJSON(t,e){if("string"!=typeof e.attr)throw new RangeError("Invalid input for DocAttrStep.fromJSON");return new J(e.attr,e.value)}}k.jsonID("docAttr",J);let R=class extends Error{};R=function t(e){let r=Error.call(this,e);return r.__proto__=t.prototype,r},(R.prototype=Object.create(Error.prototype)).constructor=R,R.prototype.name="TransformError";const A=Object.create(null);class C{constructor(t,e,r){this.$anchor=t,this.$head=e,this.ranges=r||[new $(t.min(e),t.max(e))]}get anchor(){return this.$anchor.pos}get head(){return this.$head.pos}get from(){return this.$from.pos}get to(){return this.$to.pos}get $from(){return this.ranges[0].$from}get $to(){return this.ranges[0].$to}get empty(){let t=this.ranges;for(let e=0;e<t.length;e++)if(t[e].$from.pos!=t[e].$to.pos)return!1;return!0}content(){return this.$from.doc.slice(this.from,this.to,!0)}replace(t,e=p.empty){let r=e.content.lastChild,n=null;for(let t=0;t<e.openEnd;t++)n=r,r=r.lastChild;let i=t.steps.length,s=this.ranges;for(let o=0;o<s.length;o++){let{$from:a,$to:l}=s[o],h=t.mapping.slice(i);t.replaceRange(h.map(a.pos),h.map(l.pos),o?p.empty:e),0==o&&_(t,i,(r?r.isInline:n&&n.isTextblock)?-1:1)}}replaceWith(t,e){let r=t.steps.length,n=this.ranges;for(let i=0;i<n.length;i++){let{$from:s,$to:o}=n[i],a=t.mapping.slice(r),l=a.map(s.pos),h=a.map(o.pos);i?t.deleteRange(l,h):(t.replaceRangeWith(l,h,e),_(t,r,e.isInline?-1:1))}}static findFrom(t,e,r=!1){let n=t.parent.inlineContent?new L(t):P(t.node(0),t.parent,t.pos,t.index(),e,r);if(n)return n;for(let n=t.depth-1;n>=0;n--){let i=e<0?P(t.node(0),t.node(n),t.before(n+1),t.index(n),e,r):P(t.node(0),t.node(n),t.after(n+1),t.index(n)+1,e,r);if(i)return i}return null}static near(t,e=1){return this.findFrom(t,e)||this.findFrom(t,-e)||new q(t.node(0))}static atStart(t){return P(t,t,0,0,1)||new q(t)}static atEnd(t){return P(t,t,t.content.size,t.childCount,-1)||new q(t)}static fromJSON(t,e){if(!e||!e.type)throw new RangeError("Invalid input for Selection.fromJSON");let r=A[e.type];if(!r)throw new RangeError(`No selection type ${e.type} defined`);return r.fromJSON(t,e)}static jsonID(t,e){if(t in A)throw new RangeError("Duplicate use of selection JSON ID "+t);return A[t]=e,e.prototype.jsonID=t,e}getBookmark(){return L.between(this.$anchor,this.$head).getBookmark()}}C.prototype.visible=!0;class ${constructor(t,e){this.$from=t,this.$to=e}}let F=!1;function j(t){F||t.parent.inlineContent||(F=!0,console.warn("TextSelection endpoint not pointing into a node with inline content ("+t.parent.type.name+")"))}class L extends C{constructor(t,e=t){j(t),j(e),super(t,e)}get $cursor(){return this.$anchor.pos==this.$head.pos?this.$head:null}map(t,e){let r=t.resolve(e.map(this.head));if(!r.parent.inlineContent)return C.near(r);let n=t.resolve(e.map(this.anchor));return new L(n.parent.inlineContent?n:r,r)}replace(t,e=p.empty){if(super.replace(t,e),e==p.empty){let e=this.$from.marksAcross(this.$to);e&&t.ensureMarks(e)}}eq(t){return t instanceof L&&t.anchor==this.anchor&&t.head==this.head}getBookmark(){return new D(this.anchor,this.head)}toJSON(){return{type:"text",anchor:this.anchor,head:this.head}}static fromJSON(t,e){if("number"!=typeof e.anchor||"number"!=typeof e.head)throw new RangeError("Invalid input for TextSelection.fromJSON");return new L(t.resolve(e.anchor),t.resolve(e.head))}static create(t,e,r=e){let n=t.resolve(e);return new this(n,r==e?n:t.resolve(r))}static between(t,e,r){let n=t.pos-e.pos;if(r&&!n||(r=n>=0?1:-1),!e.parent.inlineContent){let t=C.findFrom(e,r,!0)||C.findFrom(e,-r,!0);if(!t)return C.near(e,r);e=t.$head}return t.parent.inlineContent||(0==n||(t=(C.findFrom(t,-r,!0)||C.findFrom(t,r,!0)).$anchor).pos<e.pos!=n<0)&&(t=e),new L(t,e)}}C.jsonID("text",L);class D{constructor(t,e){this.anchor=t,this.head=e}map(t){return new D(t.map(this.anchor),t.map(this.head))}resolve(t){return L.between(t.resolve(this.anchor),t.resolve(this.head))}}class B extends C{constructor(t){let e=t.nodeAfter,r=t.node(0).resolve(t.pos+e.nodeSize);super(t,r),this.node=e}map(t,e){let{deleted:r,pos:n}=e.mapResult(this.anchor),i=t.resolve(n);return r?C.near(i):new B(i)}content(){return new p(o.from(this.node),0,0)}eq(t){return t instanceof B&&t.anchor==this.anchor}toJSON(){return{type:"node",anchor:this.anchor}}getBookmark(){return new V(this.anchor)}static fromJSON(t,e){if("number"!=typeof e.anchor)throw new RangeError("Invalid input for NodeSelection.fromJSON");return new B(t.resolve(e.anchor))}static create(t,e){return new B(t.resolve(e))}static isSelectable(t){return!t.isText&&!1!==t.type.spec.selectable}}B.prototype.visible=!1,C.jsonID("node",B);class V{constructor(t){this.anchor=t}map(t){let{deleted:e,pos:r}=t.mapResult(this.anchor);return e?new D(r,r):new V(r)}resolve(t){let e=t.resolve(this.anchor),r=e.nodeAfter;return r&&B.isSelectable(r)?new B(e):C.near(e)}}class q extends C{constructor(t){super(t.resolve(0),t.resolve(t.content.size))}replace(t,e=p.empty){if(e==p.empty){t.delete(0,t.doc.content.size);let e=C.atStart(t.doc);e.eq(t.selection)||t.setSelection(e)}else super.replace(t,e)}toJSON(){return{type:"all"}}static fromJSON(t){return new q(t)}map(t){return new q(t)}eq(t){return t instanceof q}getBookmark(){return H}}C.jsonID("all",q);const H={map(){return this},resolve:t=>new q(t)};function P(t,e,r,n,i,s=!1){if(e.inlineContent)return L.create(t,r);for(let o=n-(i>0?0:1);i>0?o<e.childCount:o>=0;o+=i){let n=e.child(o);if(n.isAtom){if(!s&&B.isSelectable(n))return B.create(t,r-(i<0?n.nodeSize:0))}else{let e=P(t,n,r+i,i<0?n.childCount:0,i,s);if(e)return e}r+=n.nodeSize*i}return null}function _(t,e,r){let n=t.steps.length-1;if(n<e)return;let i,s=t.steps[n];(s instanceof M||s instanceof E)&&(t.mapping.maps[n].forEach(((t,e,r,n)=>{null==i&&(i=n)})),t.setSelection(C.near(t.doc.resolve(i),r)))}function U(t,e){return e&&t?t.bind(e):t}class W{constructor(t,e,r){this.name=t,this.init=U(e.init,r),this.apply=U(e.apply,r)}}function K(t,e,r){for(let n in t){let i=t[n];i instanceof Function?i=i.bind(e):"handleDOMEvents"==n&&(i=K(i,e,{})),r[n]=i}return r}new W("doc",{init:t=>t.doc||t.schema.topNodeType.createAndFill(),apply:t=>t.doc}),new W("selection",{init:(t,e)=>t.selection||C.atStart(e.doc),apply:t=>t.selection}),new W("storedMarks",{init:t=>t.storedMarks||null,apply:(t,e,r,n)=>n.selection.$cursor?t.storedMarks:null}),new W("scrollToSelection",{init:()=>0,apply:(t,e)=>t.scrolledIntoView?e+1:e});class G{constructor(t){this.spec=t,this.props={},t.props&&K(t.props,this,this.props),this.key=t.key?t.key.key:Y("plugin")}getState(t){return t[this.key]}}const X=Object.create(null);function Y(t){return t in X?t+"$"+ ++X[t]:(X[t]=0,t+"$")}class Q{constructor(t="key"){this.key=Y(t)}get(t){return t.config.pluginsByKey[this.key]}getState(t){return t[this.key]}}function Z(t,e){const r=[];return t.descendants(((t,n)=>{e(t)&&r.push({node:t,pos:n})})),r}function tt(t,e,r,n){const i=1032-10*(Math.min(3,n)-1);return i*(t/e)/r/i*100}const et=()=>Math.random().toString(36).slice(2,11);function rt(t,e){const r=t-e;return 4===r?2:Math.min(3,r)}async function nt({images:t,editor:e,view:r}){const{state:n,dispatch:i}=r,{tr:s,schema:o}=n,a=[];if(1===t.length){const{url:e,width:r,height:l,file:h}=t[0],c=et(),u=o.nodes.figure.create(null,[o.nodes.image.create({name:c,src:e,width:r,height:l}),o.nodes.figcaption.create()]);i&&i(s.replaceSelectionWith(u).setSelection(B.near(s.doc.resolve(n.selection.anchor))).scrollIntoView().setMeta("preventFiguresChecks",!0)),a.push({file:h,id:c})}else{const e=[];let r=0;const l=t.length;let h=n.selection.anchor;for(;r<l;){const n=rt(l,r),i=t.slice(r,r+n),s=i.reduce(((t,e)=>t+e.width/e.height),0),c=o.nodes.imagesGrid.create(null,i.map((t=>{const e=tt(t.width,t.height,s,n),r=et();return a.push({file:t.file,id:r}),o.nodes.figure.create({width:e.toFixed(3)},[o.nodes.image.create({name:r,layout:"grid",src:t.url,width:t.width,height:t.height}),o.nodes.figcaption.create()])})));e.push(c),r+=n,h+=c.content.size-1}i&&i(s.insert(n.selection.anchor,e).setMeta("preventFiguresChecks",!0).scrollIntoView())}e.storage?.media?.uploadImage&&await Promise.all(a.map((t=>async function({file:t,id:n}){try{const i=await e.storage.media.uploadImage({file:t});if(!i)throw new Error("Failed to upload the image");const{state:s,dispatch:o}=r,{tr:a}=s,l=Z(a.doc,(t=>t.attrs.name===n))[0];l&&(a.setNodeMarkup(l.pos,void 0,{...l.node.attrs,src:i.url,imageId:i.imageId}).setMeta("addToHistory",!1),o(a))}catch(t){console.error("Image upload failed:",t);const{state:e,dispatch:i}=r,{tr:s}=e,o=Z(s.doc,(t=>t.attrs.name===n))[0];o&&(s.delete(o.pos,o.pos+o.node.nodeSize).setMeta("addToHistory",!1),i(s))}}(t))))}class it{editor;editorView;menu;keepScaled=!1;updateLock=!1;fileUploadInput;boundHandlers={};menuitems;validNodes=["paragraph","h3","h4"];constructor(t,e,r){this.editor=t,this.editorView=e,this.menuitems=Object.fromEntries(r.map((t=>[t.name,t])));const n=document.createElement("div");n.className="inlineTooltip",this.menu=n;const i=document.createElement("div");i.className="inlineTooltip-menu",n.appendChild(i);for(const t of r)"inlineMenu"===t.name?n.prepend(this.createMenuItem(t)):i.appendChild(this.createMenuItem(t));this.editorView.dom.parentNode?.appendChild(this.menu);const s=document.createElement("input");s.type="file",s.accept="image/*",s.multiple=!0,s.style.display="none",this.fileUploadInput=s,this.editorView.dom.parentNode?.appendChild(this.fileUploadInput),s.addEventListener("change",(e=>{const r=e.target;r.files&&r.files.length>0&&function({editor:t,files:e,view:r}){const n=[];let i=0;const s=Array.from(e).sort(((t,e)=>t.name.localeCompare(e.name))),o=()=>{++i===e.length&&nt({images:n,editor:t,view:r})};for(const e of s){if(e.size>(t.storage.media?.maxImageSizeBytes??Number.POSITIVE_INFINITY)){t.storage.media?.onMaxFileSizeError?.(e),o();continue}const r=URL.createObjectURL(e),i=new Image;i.onload=()=>{n.push({url:r,width:i.naturalWidth,height:i.naturalHeight,file:e}),o()},i.onerror=()=>{t.storage?.media?.onFileMountError?.(e),o()},i.src=r}}({editor:t,files:r.files,view:this.editorView})})),this.boundHandlers={mouseup:this.handleKeyup.bind(this),keydown:t=>{"Escape"===t.key&&this.toggleOpen(!1)},resize:this.handleResize.bind(this)},document.addEventListener("mouseup",this.boundHandlers.mouseup),document.addEventListener("keydown",this.boundHandlers.keydown),window.addEventListener("resize",this.boundHandlers.resize),this.update()}handleKeyup(t){const e=t.target;if(this.menu.contains(e)){const t=e.closest("button.inlineMenu-button");if(!t)return;const{action:r}=t.dataset;if(!r)return;const{state:n,dispatch:i}=this.editorView;if("inlineMenu"===r){return void(this.toggleOpen()?(this.keepScaled=!0,i(n.tr.setMeta("removePlaceholder",{pos:n.selection.from-1}))):(this.editorView.dom.focus(),i(n.tr.setMeta("addPlaceholder",{pos:n.selection.from-1}))))}"image"===r?this.fileUploadInput.click():(["video","embed"].includes(r)&&(this.updateLock=!0,this.hideMenu()),this.menuitems[r]?.execute?.(n,i)),this.editorView.focus(),this.toggleOpen(!1)}else this.menu.classList.remove("is-scaled")}createMenuItem(t){const e=document.createElement("button");return e.setAttribute("class","inlineMenu-button"),e.setAttribute("tabindex","-1"),e.setAttribute("title",t.title),e.setAttribute("aria-label",t.title),e.setAttribute("data-action",t.name),t.dataActionValue&&e.setAttribute("data-action-value",t.dataActionValue),e.innerHTML=t.icon,e}handleResize(){this.keepScaled=!0,this.update()}showMenu(t){this.menu.classList.add("is-active"),this.keepScaled||this.menu.classList.remove("is-scaled");const e=this.editorView.coordsAtPos(t.selection.from),r=getComputedStyle(this.editorView.dom).direction;this.menu.style.left="",this.menu.style.right="","rtl"===r?this.menu.style.right=`${window.innerWidth-e.right+window.scrollX}px`:this.menu.style.left=`${e.left+window.scrollX}px`;const n=`${e.top+(e.bottom-e.top)/2-20+window.scrollY}px`;this.menu.style.top=n}hideMenu(){this.menu.classList.remove("is-active","is-scaled")}toggleOpen(t){const e="boolean"==typeof t?t:!this.menu.classList.contains("is-scaled");return this.menu.classList.toggle("is-scaled",e),e}update(){if(this.updateLock)return void(this.updateLock=!1);const{state:t}=this.editorView,{selection:e}=t,{empty:r,$anchor:n}=e,i=n.depth>0?n.node(1):null,s=i?.type?.name;r&&""===n.parent.textContent&&s&&this.validNodes.includes(s)?(this.showMenu(t),this.keepScaled&&(this.keepScaled=!1)):this.hideMenu()}destroy(){document.removeEventListener("mouseup",this.boundHandlers.mouseup),document.removeEventListener("keydown",this.boundHandlers.keydown),window.removeEventListener("resize",this.boundHandlers.resize),this.menu?.remove(),this.fileUploadInput?.remove()}}function st(t,e){return new G({key:new Q("inlineTooltip"),view:r=>new it(t,r,e)})}const ot=n.create({name:"inlineTooltip",priority:-100,addInlineTooltipMenuItems(){return[{name:"inlineMenu",title:this.editor.options.messages.addContent,icon:this.editor.options.icons.addMediaPlus}]},addPlugins(){const t=this.features.flatMap((t=>t.config?.addInlineTooltipMenuItems?t.config.addInlineTooltipMenuItems.call({name:t.config.name,editor:this.editor,options:t.options,storage:t.storage,schema:this.schema,view:this.view,commands:this.commands,features:this.features}):[])).filter((t=>t&&!1!==this.options?.[t.name])).map((t=>({...t,...this.options?.[t.name]||{}}))).sort(((t,e)=>(e.priority||100)-(t.priority||100)));return[st(this.editor,t)]}});t.InlineTooltip=ot,Object.defineProperty(t,"__esModule",{value:!0})}));