UNPKG

medium-proeditor

Version:

A powerful & customizable Medium-style rich text editor

1 lines 75 kB
!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).Media={})}(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 n(t,r){const i={...t};if(e(t)&&e(r))for(const s of Object.keys(r))e(r[s])&&e(t[s])?i[s]=n(t[s],r[s]):i[s]=r[s];return i}class r{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 r(t)}configure(t={}){return new r({...this.config,addOptions:()=>n(this.options,t)})}}function i(t,e,n){for(let r=0;;r++){if(r==t.childCount||r==e.childCount)return t.childCount==e.childCount?null:n;let s=t.child(r),o=e.child(r);if(s!=o){if(!s.sameMarkup(o))return n;if(s.isText&&s.text!=o.text){for(let t=0;s.text[t]==o.text[t];t++)n++;return n}if(s.content.size||o.content.size){let t=i(s.content,o.content,n+1);if(null!=t)return t}n+=s.nodeSize}else n+=s.nodeSize}}function s(t,e,n,r){for(let i=t.childCount,o=e.childCount;;){if(0==i||0==o)return i==o?null:{a:n,b:r};let a=t.child(--i),l=e.child(--o),h=a.nodeSize;if(a!=l){if(!a.sameMarkup(l))return{a:n,b:r};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++,n--,r--;return{a:n,b:r}}if(a.content.size||l.content.size){let t=s(a.content,l.content,n-1,r-1);if(t)return t}n-=h,r-=h}else n-=h,r-=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,n,r=0,i){for(let s=0,o=0;o<e;s++){let a=this.content[s],l=o+a.nodeSize;if(l>t&&!1!==n(a,r+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),n,r+i)}o=l}}descendants(t){this.nodesBetween(0,this.size,t)}textBetween(t,e,n,r){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?r?"function"==typeof r?r(o):r:o.type.spec.leafText?o.type.spec.leafText(o):"":"";o.isBlock&&(o.isLeaf&&l||o.isTextblock)&&n&&(s?s=!1:i+=n),i+=l}),0),i}append(t){if(!t.size)return this;if(!this.size)return t;let e=this.lastChild,n=t.firstChild,r=this.content.slice(),i=0;for(e.isText&&e.sameMarkup(n)&&(r[r.length-1]=e.withText(e.text+n.text),i=1);i<t.content.length;i++)r.push(t.content[i]);return new o(r,this.size+t.size)}cut(t,e=this.size){if(0==t&&e==this.size)return this;let n=[],r=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))),n.push(o),r+=o.nodeSize),s=a}return new o(n,r)}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 n=this.content[t];if(n==e)return this;let r=this.content.slice(),i=this.size+e.nodeSize-n.nodeSize;return r[t]=e,new o(r,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,n=0;e<this.content.length;e++){let r=this.content[e];t(r,n,e),n+=r.nodeSize}}findDiffStart(t,e=0){return i(this,t,e)}findDiffEnd(t,e=this.size,n=t.size){return s(this,t,e,n)}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 n=0,r=0;;n++){let i=r+this.child(n).nodeSize;if(i>=t)return i==t||e>0?l(n+1,i):l(n,r);r=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,n=0;for(let r=0;r<t.length;r++){let i=t[r];n+=i.nodeSize,r&&i.isText&&t[r-1].sameMarkup(i)?(e||(e=t.slice(0,r)),e[e.length-1]=i.withText(e[e.length-1].text+i.text)):e&&e.push(i)}return new o(e||t,n)}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 n=Array.isArray(t);if(Array.isArray(e)!=n)return!1;if(n){if(t.length!=e.length)return!1;for(let n=0;n<t.length;n++)if(!h(t[n],e[n]))return!1}else{for(let n in t)if(!(n in e)||!h(t[n],e[n]))return!1;for(let n in e)if(!(n in t))return!1}return!0}class c{constructor(t,e){this.type=t,this.attrs=e}addToSet(t){let e,n=!1;for(let r=0;r<t.length;r++){let i=t[r];if(this.eq(i))return t;if(this.type.excludes(i.type))e||(e=t.slice(0,r));else{if(i.type.excludes(this.type))return t;!n&&i.type.rank>this.type.rank&&(e||(e=t.slice(0,r)),e.push(this),n=!0),e&&e.push(i)}}return e||(e=t.slice()),n||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 n=t.marks[e.type];if(!n)throw new RangeError(`There is no mark type ${e.type} in this schema`);let r=n.create(e.attrs);return n.checkAttrs(r.attrs),r}static sameSet(t,e){if(t==e)return!0;if(t.length!=e.length)return!1;for(let n=0;n<t.length;n++)if(!t[n].eq(e[n]))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 d extends Error{}class u{constructor(t,e,n){this.content=t,this.openStart=e,this.openEnd=n}get size(){return this.content.size-this.openStart-this.openEnd}insertAt(t,e){let n=f(this.content,t+this.openStart,e);return n&&new u(n,this.openStart,this.openEnd)}removeBetween(t,e){return new u(p(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 u.empty;let n=e.openStart||0,r=e.openEnd||0;if("number"!=typeof n||"number"!=typeof r)throw new RangeError("Invalid input for Slice.fromJSON");return new u(o.fromJSON(t,e.content),n,r)}static maxOpen(t,e=!0){let n=0,r=0;for(let r=t.firstChild;r&&!r.isLeaf&&(e||!r.type.spec.isolating);r=r.firstChild)n++;for(let n=t.lastChild;n&&!n.isLeaf&&(e||!n.type.spec.isolating);n=n.lastChild)r++;return new u(t,n,r)}}function p(t,e,n){let{index:r,offset:i}=t.findIndex(e),s=t.maybeChild(r),{index:o,offset:a}=t.findIndex(n);if(i==e||s.isText){if(a!=n&&!t.child(o).isText)throw new RangeError("Removing non-flat range");return t.cut(0,e).append(t.cut(n))}if(r!=o)throw new RangeError("Removing non-flat range");return t.replaceChild(r,s.copy(p(s.content,e-i-1,n-i-1)))}function f(t,e,n,r){let{index:i,offset:s}=t.findIndex(e),o=t.maybeChild(i);if(s==e||o.isText)return r&&!r.canReplace(i,i,n)?null:t.cut(0,e).append(n).append(t.cut(e));let a=f(o.content,e-s-1,n);return a&&t.replaceChild(i,o.copy(a))}u.empty=new u(o.empty,0,0);class m{constructor(t,e,n){this.hasDefault=Object.prototype.hasOwnProperty.call(n,"default"),this.default=n.default,this.validate="string"==typeof n.validate?function(t,e,n){let r=n.split("|");return n=>{let i=null===n?"null":typeof n;if(r.indexOf(i)<0)throw new RangeError(`Expected value of type ${r} for attribute ${e} on type ${t}, got ${i}`)}}(t,e,n.validate):n.validate}get isRequired(){return!this.hasDefault}}class g{constructor(t,e,n,r){this.name=t,this.rank=e,this.schema=n,this.spec=r,this.attrs=function(t,e){let n=Object.create(null);if(e)for(let r in e)n[r]=new m(t,r,e[r]);return n}(t,r.attrs),this.excluded=null;let i=function(t){let e=Object.create(null);for(let n in t){let r=t[n];if(!r.hasDefault)return null;e[n]=r.default}return e}(this.attrs);this.instance=i?new c(this,i):null}create(t=null){return!t&&this.instance?this.instance:new c(this,function(t,e){let n=Object.create(null);for(let r in t){let i=e&&e[r];if(void 0===i){let e=t[r];if(!e.hasDefault)throw new RangeError("No value supplied for attribute "+r);i=e.default}n[r]=i}return n}(this.attrs,t))}static compile(t,e){let n=Object.create(null),r=0;return t.forEach(((t,i)=>n[t]=new g(t,r++,e,i))),n}removeFromSet(t){for(var e=0;e<t.length;e++)t[e].type==this&&(t=t.slice(0,e).concat(t.slice(e+1)),e--);return t}isInSet(t){for(let e=0;e<t.length;e++)if(t[e].type==this)return t[e]}checkAttrs(t){!function(t,e,n){for(let r in e)if(!(r in t))throw new RangeError(`Unsupported attribute ${r} for ${n} of type ${r}`);for(let n in t){let r=t[n];r.validate&&r.validate(e[n])}}(this.attrs,t,"mark",this.name)}excludes(t){return this.excluded.indexOf(t)>-1}}const y=Math.pow(2,16);function w(t){return 65535&t}class k{constructor(t,e,n){this.pos=t,this.delInfo=e,this.recover=n}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 S{constructor(t,e=!1){if(this.ranges=t,this.inverted=e,!t.length&&S.empty)return S.empty}recover(t){let e=0,n=w(t);if(!this.inverted)for(let t=0;t<n;t++)e+=this.ranges[3*t+2]-this.ranges[3*t+1];return this.ranges[3*n]+e+function(t){return(t-(65535&t))/y}(t)}mapResult(t,e=1){return this._map(t,e,!1)}map(t,e=1){return this._map(t,e,!0)}_map(t,e,n){let r=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?r: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+r+((l?t==a?-1:t==c?1:e:e)<0?0:h);if(n)return i;let s=t==(e<0?a:c)?null:o/3+(t-a)*y,d=t==a?2:t==c?1:4;return(e<0?t!=a:t!=c)&&(d|=8),new k(i,d,s)}r+=h-l}return n?t+r:new k(t+r,0,null)}touches(t,e){let n=0,r=w(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?n:0);if(o>t)break;let a=this.ranges[e+i];if(t<=o+a&&e==3*r)return!0;n+=this.ranges[e+s]-a}return!1}forEach(t){let e=this.inverted?2:1,n=this.inverted?1:2;for(let r=0,i=0;r<this.ranges.length;r+=3){let s=this.ranges[r],o=s-(this.inverted?i:0),a=s+(this.inverted?0:i),l=this.ranges[r+e],h=this.ranges[r+n];t(o,o+l,a,a+h),i+=h-l}}invert(){return new S(this.ranges,!this.inverted)}toString(){return(this.inverted?"-":"")+JSON.stringify(this.ranges)}static offset(t){return 0==t?S.empty:new S(t<0?[0,-t,0]:[0,0,t])}}S.empty=new S([]);class v{constructor(t,e,n=0,r=(t?t.length:0)){this.mirror=e,this.from=n,this.to=r,this._maps=t||[],this.ownData=!(t||e)}get maps(){return this._maps}slice(t=0,e=this.maps.length){return new v(this._maps,this.mirror,t,e)}appendMap(t,e){this.ownData||(this._maps=this._maps.slice(),this.mirror=this.mirror&&this.mirror.slice(),this.ownData=!0),this.to=this._maps.push(t),null!=e&&this.setMirror(this._maps.length-1,e)}appendMapping(t){for(let e=0,n=this._maps.length;e<t._maps.length;e++){let r=t.getMirror(e);this.appendMap(t._maps[e],null!=r&&r<e?n+r:void 0)}}getMirror(t){if(this.mirror)for(let e=0;e<this.mirror.length;e++)if(this.mirror[e]==t)return this.mirror[e+(e%2?-1:1)]}setMirror(t,e){this.mirror||(this.mirror=[]),this.mirror.push(t,e)}appendMappingInverted(t){for(let e=t.maps.length-1,n=this._maps.length+t._maps.length;e>=0;e--){let r=t.getMirror(e);this.appendMap(t._maps[e].invert(),null!=r&&r>e?n-r-1:void 0)}}invert(){let t=new v;return t.appendMappingInverted(this),t}map(t,e=1){if(this.mirror)return this._map(t,e,!0);for(let n=this.from;n<this.to;n++)t=this._maps[n].map(t,e);return t}mapResult(t,e=1){return this._map(t,e,!1)}_map(t,e,n){let r=0;for(let n=this.from;n<this.to;n++){let i=this._maps[n].mapResult(t,e);if(null!=i.recover){let e=this.getMirror(n);if(null!=e&&e>n&&e<this.to){n=e,t=this._maps[e].recover(i.recover);continue}}r|=i.delInfo,t=i.pos}return n?t:new k(t,r,null)}}const b=Object.create(null);class E{getMap(){return S.empty}merge(t){return null}static fromJSON(t,e){if(!e||!e.stepType)throw new RangeError("Invalid input for Step.fromJSON");let n=b[e.stepType];if(!n)throw new RangeError(`No step type ${e.stepType} defined`);return n.fromJSON(t,e)}static jsonID(t,e){if(t in b)throw new RangeError("Duplicate use of step JSON ID "+t);return b[t]=e,e.prototype.jsonID=t,e}}class C{constructor(t,e){this.doc=t,this.failed=e}static ok(t){return new C(t,null)}static fail(t){return new C(null,t)}static fromReplace(t,e,n,r){try{return C.ok(t.replace(e,n,r))}catch(t){if(t instanceof d)return C.fail(t.message);throw t}}}function x(t,e,n){let r=[];for(let i=0;i<t.childCount;i++){let s=t.child(i);s.content.size&&(s=s.copy(x(s.content,e,s))),s.isInline&&(s=e(s,n,i)),r.push(s)}return o.fromArray(r)}class O extends E{constructor(t,e,n){super(),this.from=t,this.to=e,this.mark=n}apply(t){let e=t.slice(this.from,this.to),n=t.resolve(this.from),r=n.node(n.sharedDepth(this.to)),i=new u(x(e.content,((t,e)=>t.isAtom&&e.type.allowsMarkType(this.mark.type)?t.mark(this.mark.addToSet(t.marks)):t),r),e.openStart,e.openEnd);return C.fromReplace(t,this.from,this.to,i)}invert(){return new R(this.from,this.to,this.mark)}map(t){let e=t.mapResult(this.from,1),n=t.mapResult(this.to,-1);return e.deleted&&n.deleted||e.pos>=n.pos?null:new O(e.pos,n.pos,this.mark)}merge(t){return t instanceof O&&t.mark.eq(this.mark)&&this.from<=t.to&&this.to>=t.from?new O(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 O(e.from,e.to,t.markFromJSON(e.mark))}}E.jsonID("addMark",O);class R extends E{constructor(t,e,n){super(),this.from=t,this.to=e,this.mark=n}apply(t){let e=t.slice(this.from,this.to),n=new u(x(e.content,(t=>t.mark(this.mark.removeFromSet(t.marks))),t),e.openStart,e.openEnd);return C.fromReplace(t,this.from,this.to,n)}invert(){return new O(this.from,this.to,this.mark)}map(t){let e=t.mapResult(this.from,1),n=t.mapResult(this.to,-1);return e.deleted&&n.deleted||e.pos>=n.pos?null:new R(e.pos,n.pos,this.mark)}merge(t){return t instanceof R&&t.mark.eq(this.mark)&&this.from<=t.to&&this.to>=t.from?new R(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 R(e.from,e.to,t.markFromJSON(e.mark))}}E.jsonID("removeMark",R);class A extends E{constructor(t,e){super(),this.pos=t,this.mark=e}apply(t){let e=t.nodeAt(this.pos);if(!e)return C.fail("No node at mark step's position");let n=e.type.create(e.attrs,null,this.mark.addToSet(e.marks));return C.fromReplace(t,this.pos,this.pos+1,new u(o.from(n),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 n=0;n<e.marks.length;n++)if(!e.marks[n].isInSet(t))return new A(this.pos,e.marks[n]);return new A(this.pos,this.mark)}}return new N(this.pos,this.mark)}map(t){let e=t.mapResult(this.pos,1);return e.deletedAfter?null:new A(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 A(e.pos,t.markFromJSON(e.mark))}}E.jsonID("addNodeMark",A);class N extends E{constructor(t,e){super(),this.pos=t,this.mark=e}apply(t){let e=t.nodeAt(this.pos);if(!e)return C.fail("No node at mark step's position");let n=e.type.create(e.attrs,null,this.mark.removeFromSet(e.marks));return C.fromReplace(t,this.pos,this.pos+1,new u(o.from(n),0,e.isLeaf?0:1))}invert(t){let e=t.nodeAt(this.pos);return e&&this.mark.isInSet(e.marks)?new A(this.pos,this.mark):this}map(t){let e=t.mapResult(this.pos,1);return e.deletedAfter?null:new N(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 N(e.pos,t.markFromJSON(e.mark))}}E.jsonID("removeNodeMark",N);class T extends E{constructor(t,e,n,r=!1){super(),this.from=t,this.to=e,this.slice=n,this.structure=r}apply(t){return this.structure&&I(t,this.from,this.to)?C.fail("Structure replace would overwrite content"):C.fromReplace(t,this.from,this.to,this.slice)}getMap(){return new S([this.from,this.to-this.from,this.slice.size])}invert(t){return new T(this.from,this.from+this.slice.size,t.slice(this.from,this.to))}map(t){let e=t.mapResult(this.from,1),n=t.mapResult(this.to,-1);return e.deletedAcross&&n.deletedAcross?null:new T(e.pos,Math.max(e.pos,n.pos),this.slice)}merge(t){if(!(t instanceof T)||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?u.empty:new u(t.slice.content.append(this.slice.content),t.slice.openStart,this.slice.openEnd);return new T(t.from,this.to,e,this.structure)}}{let e=this.slice.size+t.slice.size==0?u.empty:new u(this.slice.content.append(t.slice.content),this.slice.openStart,t.slice.openEnd);return new T(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 T(e.from,e.to,u.fromJSON(t,e.slice),!!e.structure)}}E.jsonID("replace",T);class M extends E{constructor(t,e,n,r,i,s,o=!1){super(),this.from=t,this.to=e,this.gapFrom=n,this.gapTo=r,this.slice=i,this.insert=s,this.structure=o}apply(t){if(this.structure&&(I(t,this.from,this.gapFrom)||I(t,this.gapTo,this.to)))return C.fail("Structure gap-replace would overwrite content");let e=t.slice(this.gapFrom,this.gapTo);if(e.openStart||e.openEnd)return C.fail("Gap is not a flat range");let n=this.slice.insertAt(this.insert,e.content);return n?C.fromReplace(t,this.from,this.to,n):C.fail("Content does not fit in gap")}getMap(){return new S([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 M(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),n=t.mapResult(this.to,-1),r=this.from==this.gapFrom?e.pos:t.map(this.gapFrom,-1),i=this.to==this.gapTo?n.pos:t.map(this.gapTo,1);return e.deletedAcross&&n.deletedAcross||r<e.pos||i>n.pos?null:new M(e.pos,n.pos,r,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 M(e.from,e.to,e.gapFrom,e.gapTo,u.fromJSON(t,e.slice),e.insert,!!e.structure)}}function I(t,e,n){let r=t.resolve(e),i=n-e,s=r.depth;for(;i>0&&s>0&&r.indexAfter(s)==r.node(s).childCount;)s--,i--;if(i>0){let t=r.node(s).maybeChild(r.indexAfter(s));for(;i>0;){if(!t||t.isLeaf)return!0;t=t.firstChild,i--}}return!1}function z(t,e,n,r=n.contentMatch,i=!0){let s=t.doc.nodeAt(e),a=[],l=e+1;for(let e=0;e<s.childCount;e++){let h=s.child(e),c=l+h.nodeSize,d=r.matchType(h.type);if(d){r=d;for(let e=0;e<h.marks.length;e++)n.allowsMarkType(h.marks[e].type)||t.step(new R(l,c,h.marks[e]));if(i&&h.isText&&"pre"!=n.whitespace){let t,e,r=/\r?\n|\r/g;for(;t=r.exec(h.text);)e||(e=new u(o.from(n.schema.text(" ",n.allowedMarks(h.marks))),0,0)),a.push(new T(l+t.index,l+t.index+t[0].length,e))}}else a.push(new T(l,c,u.empty));l=c}if(!r.validEnd){let e=r.fillBefore(o.empty,!0);t.replace(l,l,new u(e,0,0))}for(let e=a.length-1;e>=0;e--)t.step(a[e])}function L(t,e,n,r){e.forEach(((i,s)=>{if(i.isText){let o,a=/\r?\n|\r/g;for(;o=a.exec(i.text);){let i=t.mapping.slice(r).map(n+1+s+o.index);t.replaceWith(i,i+1,e.type.schema.linebreakReplacement.create())}}}))}function D(t,e,n,r){e.forEach(((i,s)=>{if(i.type==i.type.schema.linebreakReplacement){let i=t.mapping.slice(r).map(n+1+s);t.replaceWith(i,i+1,e.type.schema.text("\n"))}}))}function F(t,e,n){return!n.openStart&&!n.openEnd&&t.start()==e.start()&&t.parent.canReplace(t.index(),e.index(),n.content)}E.jsonID("replaceAround",M);class j{constructor(t,e,n){this.$from=t,this.$to=e,this.unplaced=n,this.frontier=[],this.placed=o.empty;for(let e=0;e<=t.depth;e++){let n=t.node(e);this.frontier.push({type:n.type,match:n.contentMatchAt(t.indexAfter(e))})}for(let e=t.depth;e>0;e--)this.placed=o.from(t.node(e).copy(this.placed))}get depth(){return this.frontier.length-1}fit(){for(;this.unplaced.size;){let t=this.findFittable();t?this.placeNodes(t):this.openMore()||this.dropNode()}let t=this.mustMoveInline(),e=this.placed.size-this.depth-this.$from.depth,n=this.$from,r=this.close(t<0?this.$to:n.doc.resolve(t));if(!r)return null;let i=this.placed,s=n.depth,o=r.depth;for(;s&&o&&1==i.childCount;)i=i.firstChild.content,s--,o--;let a=new u(i,s,o);return t>-1?new M(n.pos,t,this.$to.pos,this.$to.end(),a,e):a.size||n.pos!=this.$to.pos?new T(n.pos,r.pos,a):null}findFittable(){let t=this.unplaced.openStart;for(let e=this.unplaced.content,n=0,r=this.unplaced.openEnd;n<t;n++){let i=e.firstChild;if(e.childCount>1&&(r=0),i.type.spec.isolating&&r<=n){t=n;break}e=i.content}for(let e=1;e<=2;e++)for(let n=1==e?t:this.unplaced.openStart;n>=0;n--){let t,r=null;n?(r=B(this.unplaced.content,n-1).firstChild,t=r.content):t=this.unplaced.content;let i=t.firstChild;for(let t=this.depth;t>=0;t--){let s,{type:a,match:l}=this.frontier[t],h=null;if(1==e&&(i?l.matchType(i.type)||(h=l.fillBefore(o.from(i),!1)):r&&a.compatibleContent(r.type)))return{sliceDepth:n,frontierDepth:t,parent:r,inject:h};if(2==e&&i&&(s=l.findWrapping(i.type)))return{sliceDepth:n,frontierDepth:t,parent:r,wrap:s};if(r&&l.matchType(r.type))break}}}openMore(){let{content:t,openStart:e,openEnd:n}=this.unplaced,r=B(t,e);return!(!r.childCount||r.firstChild.isLeaf)&&(this.unplaced=new u(t,e+1,Math.max(n,r.size+e>=t.size-n?e+1:0)),!0)}dropNode(){let{content:t,openStart:e,openEnd:n}=this.unplaced,r=B(t,e);if(r.childCount<=1&&e>0){let i=t.size-e<=e+r.size;this.unplaced=new u($(t,e-1,1),e-1,i?e-1:n)}else this.unplaced=new u($(t,e,1),e,n)}placeNodes({sliceDepth:t,frontierDepth:e,parent:n,inject:r,wrap:i}){for(;this.depth>e;)this.closeFrontierNode();if(i)for(let t=0;t<i.length;t++)this.openFrontierNode(i[t]);let s=this.unplaced,a=n?n.content:s.content,l=s.openStart-t,h=0,c=[],{match:d,type:p}=this.frontier[e];if(r){for(let t=0;t<r.childCount;t++)c.push(r.child(t));d=d.matchFragment(r)}let f=a.size+t-(s.content.size-s.openEnd);for(;h<a.childCount;){let t=a.child(h),e=d.matchType(t.type);if(!e)break;h++,(h>1||0==l||t.content.size)&&(d=e,c.push(J(t.mark(p.allowedMarks(t.marks)),1==h?l:0,h==a.childCount?f:-1)))}let m=h==a.childCount;m||(f=-1),this.placed=H(this.placed,e,o.from(c)),this.frontier[e].match=d,m&&f<0&&n&&n.type==this.frontier[this.depth].type&&this.frontier.length>1&&this.closeFrontierNode();for(let t=0,e=a;t<f;t++){let t=e.lastChild;this.frontier.push({type:t.type,match:t.contentMatchAt(t.childCount)}),e=t.content}this.unplaced=m?0==t?u.empty:new u($(s.content,t-1,1),t-1,f<0?s.openEnd:t-1):new u($(s.content,t,h),s.openStart,s.openEnd)}mustMoveInline(){if(!this.$to.parent.isTextblock)return-1;let t,e=this.frontier[this.depth];if(!e.type.isTextblock||!P(this.$to,this.$to.depth,e.type,e.match,!1)||this.$to.depth==this.depth&&(t=this.findCloseLevel(this.$to))&&t.depth==this.depth)return-1;let{depth:n}=this.$to,r=this.$to.after(n);for(;n>1&&r==this.$to.end(--n);)++r;return r}findCloseLevel(t){t:for(let e=Math.min(this.depth,t.depth);e>=0;e--){let{match:n,type:r}=this.frontier[e],i=e<t.depth&&t.end(e+1)==t.pos+(t.depth-(e+1)),s=P(t,e,r,n,i);if(s){for(let n=e-1;n>=0;n--){let{match:e,type:r}=this.frontier[n],i=P(t,n,r,e,!0);if(!i||i.childCount)continue t}return{depth:e,fit:s,move:i?t.doc.resolve(t.after(e+1)):t}}}}close(t){let e=this.findCloseLevel(t);if(!e)return null;for(;this.depth>e.depth;)this.closeFrontierNode();e.fit.childCount&&(this.placed=H(this.placed,e.depth,e.fit)),t=e.move;for(let n=e.depth+1;n<=t.depth;n++){let e=t.node(n),r=e.type.contentMatch.fillBefore(e.content,!0,t.index(n));this.openFrontierNode(e.type,e.attrs,r)}return t}openFrontierNode(t,e=null,n){let r=this.frontier[this.depth];r.match=r.match.matchType(t),this.placed=H(this.placed,this.depth,o.from(t.create(e,n))),this.frontier.push({type:t,match:t.contentMatch})}closeFrontierNode(){let t=this.frontier.pop().match.fillBefore(o.empty,!0);t.childCount&&(this.placed=H(this.placed,this.frontier.length,t))}}function $(t,e,n){return 0==e?t.cutByIndex(n,t.childCount):t.replaceChild(0,t.firstChild.copy($(t.firstChild.content,e-1,n)))}function H(t,e,n){return 0==e?t.append(n):t.replaceChild(t.childCount-1,t.lastChild.copy(H(t.lastChild.content,e-1,n)))}function B(t,e){for(let n=0;n<e;n++)t=t.firstChild.content;return t}function J(t,e,n){if(e<=0)return t;let r=t.content;return e>1&&(r=r.replaceChild(0,J(r.firstChild,e-1,1==r.childCount?n-1:0))),e>0&&(r=t.type.contentMatch.fillBefore(r).append(r),n<=0&&(r=r.append(t.type.contentMatch.matchFragment(r).fillBefore(o.empty,!0)))),t.copy(r)}function P(t,e,n,r,i){let s=t.node(e),o=i?t.indexAfter(e):t.index(e);if(o==s.childCount&&!n.compatibleContent(s.type))return null;let a=r.fillBefore(s.content,!0,o);return a&&!function(t,e,n){for(let r=n;r<e.childCount;r++)if(!t.allowsMarks(e.child(r).marks))return!0;return!1}(n,s.content,o)?a:null}function W(t,e,n,r,i){if(e<n){let i=t.firstChild;t=t.replaceChild(0,i.copy(W(i.content,e+1,n,r,i)))}if(e>r){let e=i.contentMatchAt(0),n=e.fillBefore(t).append(t);t=n.append(e.matchFragment(n).fillBefore(o.empty,!0))}return t}function U(t,e){let n=[];for(let r=Math.min(t.depth,e.depth);r>=0;r--){let i=t.start(r);if(i<t.pos-(t.depth-r)||e.end(r)>e.pos+(e.depth-r)||t.node(r).type.spec.isolating||e.node(r).type.spec.isolating)break;(i==e.start(r)||r==t.depth&&r==e.depth&&t.parent.inlineContent&&e.parent.inlineContent&&r&&e.start(r-1)==i-1)&&n.push(r)}return n}class _ extends E{constructor(t,e,n){super(),this.pos=t,this.attr=e,this.value=n}apply(t){let e=t.nodeAt(this.pos);if(!e)return C.fail("No node at attribute step's position");let n=Object.create(null);for(let t in e.attrs)n[t]=e.attrs[t];n[this.attr]=this.value;let r=e.type.create(n,null,e.marks);return C.fromReplace(t,this.pos,this.pos+1,new u(o.from(r),0,e.isLeaf?0:1))}getMap(){return S.empty}invert(t){return new _(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 _(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 _(e.pos,e.attr,e.value)}}E.jsonID("attr",_);class q extends E{constructor(t,e){super(),this.attr=t,this.value=e}apply(t){let e=Object.create(null);for(let n in t.attrs)e[n]=t.attrs[n];e[this.attr]=this.value;let n=t.type.create(e,t.content,t.marks);return C.ok(n)}getMap(){return S.empty}invert(t){return new q(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 q(e.attr,e.value)}}E.jsonID("docAttr",q);let K=class extends Error{};K=function t(e){let n=Error.call(this,e);return n.__proto__=t.prototype,n},(K.prototype=Object.create(Error.prototype)).constructor=K,K.prototype.name="TransformError";class G{constructor(t){this.doc=t,this.steps=[],this.docs=[],this.mapping=new v}get before(){return this.docs.length?this.docs[0]:this.doc}step(t){let e=this.maybeStep(t);if(e.failed)throw new K(e.failed);return this}maybeStep(t){let e=t.apply(this.doc);return e.failed||this.addStep(t,e.doc),e}get docChanged(){return this.steps.length>0}addStep(t,e){this.docs.push(this.doc),this.steps.push(t),this.mapping.appendMap(t.getMap()),this.doc=e}replace(t,e=t,n=u.empty){let r=function(t,e,n=e,r=u.empty){if(e==n&&!r.size)return null;let i=t.resolve(e),s=t.resolve(n);return F(i,s,r)?new T(e,n,r):new j(i,s,r).fit()}(this.doc,t,e,n);return r&&this.step(r),this}replaceWith(t,e,n){return this.replace(t,e,new u(o.from(n),0,0))}delete(t,e){return this.replace(t,e,u.empty)}insert(t,e){return this.replaceWith(t,t,e)}replaceRange(t,e,n){return function(t,e,n,r){if(!r.size)return t.deleteRange(e,n);let i=t.doc.resolve(e),s=t.doc.resolve(n);if(F(i,s,r))return t.step(new T(e,n,r));let o=U(i,t.doc.resolve(n));0==o[o.length-1]&&o.pop();let a=-(i.depth+1);o.unshift(a);for(let t=i.depth,e=i.pos-1;t>0;t--,e--){let n=i.node(t).type.spec;if(n.defining||n.definingAsContext||n.isolating)break;o.indexOf(t)>-1?a=t:i.before(t)==e&&o.splice(1,0,-t)}let l=o.indexOf(a),h=[],c=r.openStart;for(let t=r.content,e=0;;e++){let n=t.firstChild;if(h.push(n),e==r.openStart)break;t=n.content}for(let t=c-1;t>=0;t--){let e=h[t],n=(d=e.type).spec.defining||d.spec.definingForContent;if(n&&!e.sameMarkup(i.node(Math.abs(a)-1)))c=t;else if(n||!e.type.isTextblock)break}var d;for(let e=r.openStart;e>=0;e--){let a=(e+c+1)%(r.openStart+1),d=h[a];if(d)for(let e=0;e<o.length;e++){let h=o[(e+l)%o.length],c=!0;h<0&&(c=!1,h=-h);let p=i.node(h-1),f=i.index(h-1);if(p.canReplaceWith(f,f,d.type,d.marks))return t.replace(i.before(h),c?s.after(h):n,new u(W(r.content,0,r.openStart,a),a,r.openEnd))}}let p=t.steps.length;for(let a=o.length-1;a>=0&&(t.replace(e,n,r),!(t.steps.length>p));a--){let t=o[a];t<0||(e=i.before(t),n=s.after(t))}}(this,t,e,n),this}replaceRangeWith(t,e,n){return function(t,e,n,r){if(!r.isInline&&e==n&&t.doc.resolve(e).parent.content.size){let i=function(t,e,n){let r=t.resolve(e);if(r.parent.canReplaceWith(r.index(),r.index(),n))return e;if(0==r.parentOffset)for(let t=r.depth-1;t>=0;t--){let e=r.index(t);if(r.node(t).canReplaceWith(e,e,n))return r.before(t+1);if(e>0)return null}if(r.parentOffset==r.parent.content.size)for(let t=r.depth-1;t>=0;t--){let e=r.indexAfter(t);if(r.node(t).canReplaceWith(e,e,n))return r.after(t+1);if(e<r.node(t).childCount)return null}return null}(t.doc,e,r.type);null!=i&&(e=n=i)}t.replaceRange(e,n,new u(o.from(r),0,0))}(this,t,e,n),this}deleteRange(t,e){return function(t,e,n){let r=t.doc.resolve(e),i=t.doc.resolve(n),s=U(r,i);for(let e=0;e<s.length;e++){let n=s[e],o=e==s.length-1;if(o&&0==n||r.node(n).type.contentMatch.validEnd)return t.delete(r.start(n),i.end(n));if(n>0&&(o||r.node(n-1).canReplace(r.index(n-1),i.indexAfter(n-1))))return t.delete(r.before(n),i.after(n))}for(let s=1;s<=r.depth&&s<=i.depth;s++)if(e-r.start(s)==r.depth-s&&n>r.end(s)&&i.end(s)-n!=i.depth-s&&r.start(s-1)==i.start(s-1)&&r.node(s-1).canReplace(r.index(s-1),i.index(s-1)))return t.delete(r.before(s),n);t.delete(e,n)}(this,t,e),this}lift(t,e){return function(t,e,n){let{$from:r,$to:i,depth:s}=e,a=r.before(s+1),l=i.after(s+1),h=a,c=l,d=o.empty,p=0;for(let t=s,e=!1;t>n;t--)e||r.index(t)>0?(e=!0,d=o.from(r.node(t).copy(d)),p++):h--;let f=o.empty,m=0;for(let t=s,e=!1;t>n;t--)e||i.after(t+1)<i.end(t)?(e=!0,f=o.from(i.node(t).copy(f)),m++):c++;t.step(new M(h,c,a,l,new u(d.append(f),p,m),d.size-p,!0))}(this,t,e),this}join(t,e=1){return function(t,e,n){let r=null,{linebreakReplacement:i}=t.doc.type.schema,s=t.doc.resolve(e-n),o=s.node().type;if(i&&o.inlineContent){let t="pre"==o.whitespace,e=!!o.contentMatch.matchType(i);t&&!e?r=!1:!t&&e&&(r=!0)}let a=t.steps.length;if(!1===r){let r=t.doc.resolve(e+n);D(t,r.node(),r.before(),a)}o.inlineContent&&z(t,e+n-1,o,s.node().contentMatchAt(s.index()),null==r);let l=t.mapping.slice(a),h=l.map(e-n);if(t.step(new T(h,l.map(e+n,-1),u.empty,!0)),!0===r){let e=t.doc.resolve(h);L(t,e.node(),e.before(),t.steps.length)}}(this,t,e),this}wrap(t,e){return function(t,e,n){let r=o.empty;for(let t=n.length-1;t>=0;t--){if(r.size){let e=n[t].type.contentMatch.matchFragment(r);if(!e||!e.validEnd)throw new RangeError("Wrapper type given to Transform.wrap does not form valid content of its parent wrapper")}r=o.from(n[t].type.create(n[t].attrs,r))}let i=e.start,s=e.end;t.step(new M(i,s,i,s,new u(r,0,0),n.length,!0))}(this,t,e),this}setBlockType(t,e=t,n,r=null){return function(t,e,n,r,i){if(!r.isTextblock)throw new RangeError("Type given to setBlockType should be a textblock");let s=t.steps.length;t.doc.nodesBetween(e,n,((e,n)=>{let a="function"==typeof i?i(e):i;if(e.isTextblock&&!e.hasMarkup(r,a)&&function(t,e,n){let r=t.resolve(e),i=r.index();return r.parent.canReplaceWith(i,i+1,n)}(t.doc,t.mapping.slice(s).map(n),r)){let i=null;if(r.schema.linebreakReplacement){let t="pre"==r.whitespace,e=!!r.contentMatch.matchType(r.schema.linebreakReplacement);t&&!e?i=!1:!t&&e&&(i=!0)}!1===i&&D(t,e,n,s),z(t,t.mapping.slice(s).map(n,1),r,void 0,null===i);let l=t.mapping.slice(s),h=l.map(n,1),c=l.map(n+e.nodeSize,1);return t.step(new M(h,c,h+1,c-1,new u(o.from(r.create(a,null,e.marks)),0,0),1,!0)),!0===i&&L(t,e,n,s),!1}}))}(this,t,e,n,r),this}setNodeMarkup(t,e,n=null,r){return function(t,e,n,r,i){let s=t.doc.nodeAt(e);if(!s)throw new RangeError("No node at given position");n||(n=s.type);let a=n.create(r,null,i||s.marks);if(s.isLeaf)return t.replaceWith(e,e+s.nodeSize,a);if(!n.validContent(s.content))throw new RangeError("Invalid content for node type "+n.name);t.step(new M(e,e+s.nodeSize,e+1,e+s.nodeSize-1,new u(o.from(a),0,0),1,!0))}(this,t,e,n,r),this}setNodeAttribute(t,e,n){return this.step(new _(t,e,n)),this}setDocAttribute(t,e){return this.step(new q(t,e)),this}addNodeMark(t,e){return this.step(new A(t,e)),this}removeNodeMark(t,e){if(!(e instanceof c)){let n=this.doc.nodeAt(t);if(!n)throw new RangeError("No node at position "+t);if(!(e=e.isInSet(n.marks)))return this}return this.step(new N(t,e)),this}split(t,e=1,n){return function(t,e,n=1,r){let i=t.doc.resolve(e),s=o.empty,a=o.empty;for(let t=i.depth,e=i.depth-n,l=n-1;t>e;t--,l--){s=o.from(i.node(t).copy(s));let e=r&&r[l];a=o.from(e?e.type.create(e.attrs,a):i.node(t).copy(a))}t.step(new T(e,e,new u(s.append(a),n,n),!0))}(this,t,e,n),this}addMark(t,e,n){return function(t,e,n,r){let i,s,o=[],a=[];t.doc.nodesBetween(e,n,((t,l,h)=>{if(!t.isInline)return;let c=t.marks;if(!r.isInSet(c)&&h.type.allowsMarkType(r.type)){let h=Math.max(l,e),d=Math.min(l+t.nodeSize,n),u=r.addToSet(c);for(let t=0;t<c.length;t++)c[t].isInSet(u)||(i&&i.to==h&&i.mark.eq(c[t])?i.to=d:o.push(i=new R(h,d,c[t])));s&&s.to==h?s.to=d:a.push(s=new O(h,d,r))}})),o.forEach((e=>t.step(e))),a.forEach((e=>t.step(e)))}(this,t,e,n),this}removeMark(t,e,n){return function(t,e,n,r){let i=[],s=0;t.doc.nodesBetween(e,n,((t,o)=>{if(!t.isInline)return;s++;let a=null;if(r instanceof g){let e,n=t.marks;for(;e=r.isInSet(n);)(a||(a=[])).push(e),n=e.removeFromSet(n)}else r?r.isInSet(t.marks)&&(a=[r]):a=t.marks;if(a&&a.length){let r=Math.min(o+t.nodeSize,n);for(let t=0;t<a.length;t++){let n,l=a[t];for(let t=0;t<i.length;t++){let e=i[t];e.step==s-1&&l.eq(i[t].style)&&(n=e)}n?(n.to=r,n.step=s):i.push({style:l,from:Math.max(o,e),to:r,step:s})}}})),i.forEach((e=>t.step(new R(e.from,e.to,e.style))))}(this,t,e,n),this}clearIncompatible(t,e,n){return z(this,t,e,n),this}}const V=Object.create(null);class Q{constructor(t,e,n){this.$anchor=t,this.$head=e,this.ranges=n||[new Y(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=u.empty){let n=e.content.lastChild,r=null;for(let t=0;t<e.openEnd;t++)r=n,n=n.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?u.empty:e),0==o&&at(t,i,(n?n.isInline:r&&r.isTextblock)?-1:1)}}replaceWith(t,e){let n=t.steps.length,r=this.ranges;for(let i=0;i<r.length;i++){let{$from:s,$to:o}=r[i],a=t.mapping.slice(n),l=a.map(s.pos),h=a.map(o.pos);i?t.deleteRange(l,h):(t.replaceRangeWith(l,h,e),at(t,n,e.isInline?-1:1))}}static findFrom(t,e,n=!1){let r=t.parent.inlineContent?new tt(t):ot(t.node(0),t.parent,t.pos,t.index(),e,n);if(r)return r;for(let r=t.depth-1;r>=0;r--){let i=e<0?ot(t.node(0),t.node(r),t.before(r+1),t.index(r),e,n):ot(t.node(0),t.node(r),t.after(r+1),t.index(r)+1,e,n);if(i)return i}return null}static near(t,e=1){return this.findFrom(t,e)||this.findFrom(t,-e)||new it(t.node(0))}static atStart(t){return ot(t,t,0,0,1)||new it(t)}static atEnd(t){return ot(t,t,t.content.size,t.childCount,-1)||new it(t)}static fromJSON(t,e){if(!e||!e.type)throw new RangeError("Invalid input for Selection.fromJSON");let n=V[e.type];if(!n)throw new RangeError(`No selection type ${e.type} defined`);return n.fromJSON(t,e)}static jsonID(t,e){if(t in V)throw new RangeError("Duplicate use of selection JSON ID "+t);return V[t]=e,e.prototype.jsonID=t,e}getBookmark(){return tt.between(this.$anchor,this.$head).getBookmark()}}Q.prototype.visible=!0;class Y{constructor(t,e){this.$from=t,this.$to=e}}let X=!1;function Z(t){X||t.parent.inlineContent||(X=!0,console.warn("TextSelection endpoint not pointing into a node with inline content ("+t.parent.type.name+")"))}class tt extends Q{constructor(t,e=t){Z(t),Z(e),super(t,e)}get $cursor(){return this.$anchor.pos==this.$head.pos?this.$head:null}map(t,e){let n=t.resolve(e.map(this.head));if(!n.parent.inlineContent)return Q.near(n);let r=t.resolve(e.map(this.anchor));return new tt(r.parent.inlineContent?r:n,n)}replace(t,e=u.empty){if(super.replace(t,e),e==u.empty){let e=this.$from.marksAcross(this.$to);e&&t.ensureMarks(e)}}eq(t){return t instanceof tt&&t.anchor==this.anchor&&t.head==this.head}getBookmark(){return new et(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 tt(t.resolve(e.anchor),t.resolve(e.head))}static create(t,e,n=e){let r=t.resolve(e);return new this(r,n==e?r:t.resolve(n))}static between(t,e,n){let r=t.pos-e.pos;if(n&&!r||(n=r>=0?1:-1),!e.parent.inlineContent){let t=Q.findFrom(e,n,!0)||Q.findFrom(e,-n,!0);if(!t)return Q.near(e,n);e=t.$head}return t.parent.inlineContent||(0==r||(t=(Q.findFrom(t,-n,!0)||Q.findFrom(t,n,!0)).$anchor).pos<e.pos!=r<0)&&(t=e),new tt(t,e)}}Q.jsonID("text",tt);class et{constructor(t,e){this.anchor=t,this.head=e}map(t){return new et(t.map(this.anchor),t.map(this.head))}resolve(t){return tt.between(t.resolve(this.anchor),t.resolve(this.head))}}class nt extends Q{constructor(t){let e=t.nodeAfter,n=t.node(0).resolve(t.pos+e.nodeSize);super(t,n),this.node=e}map(t,e){let{deleted:n,pos:r}=e.mapResult(this.anchor),i=t.resolve(r);return n?Q.near(i):new nt(i)}content(){return new u(o.from(this.node),0,0)}eq(t){return t instanceof nt&&t.anchor==this.anchor}toJSON(){return{type:"node",anchor:this.anchor}}getBookmark(){return new rt(this.anchor)}static fromJSON(t,e){if("number"!=typeof e.anchor)throw new RangeError("Invalid input for NodeSelection.fromJSON");return new nt(t.resolve(e.anchor))}static create(t,e){return new nt(t.resolve(e))}static isSelectable(t){return!t.isText&&!1!==t.type.spec.selectable}}nt.prototype.visible=!1,Q.jsonID("node",nt);class rt{constructor(t){this.anchor=t}map(t){let{deleted:e,pos:n}=t.mapResult(this.anchor);return e?new et(n,n):new rt(n)}resolve(t){let e=t.resolve(this.anchor),n=e.nodeAfter;return n&&nt.isSelectable(n)?new nt(e):Q.near(e)}}class it extends Q{constructor(t){super(t.resolve(0),t.resolve(t.content.size))}replace(t,e=u.empty){if(e==u.empty){t.delete(0,t.doc.content.size);let e=Q.atStart(t.doc);e.eq(t.selection)||t.setSelection(e)}else super.replace(t,e)}toJSON(){return{type:"all"}}static fromJSON(t){return new it(t)}map(t){return new it(t)}eq(t){return t instanceof it}getBookmark(){return st}}Q.jsonID("all",it);const st={map(){return this},resolve:t=>new it(t)};function ot(t,e,n,r,i,s=!1){if(e.inlineContent)return tt.create(t,n);for(let o=r-(i>0?0:1);i>0?o<e.childCount:o>=0;o+=i){let r=e.child(o);if(r.isAtom){if(!s&&nt.isSelectable(r))return nt.create(t,n-(i<0?r.nodeSize:0))}else{let e=ot(t,r,n+i,i<0?r.childCount:0,i,s);if(e)return e}n+=r.nodeSize*i}return null}function at(t,e,n){let r=t.steps.length-1;if(r<e)return;let i,s=t.steps[r];(s instanceof T||s instanceof M)&&(t.mapping.maps[r].forEach(((t,e,n,r)=>{null==i&&(i=r)})),t.setSelection(Q.near(t.doc.resolve(i),n)))}function lt(t,e){return e&&t?t.bind(e):t}class ht{constructor(t,e,n){this.name=t,this.init=lt(e.init,n),this.apply=lt(e.apply,n)}}function ct(t,e,n){for(let r in t){let i=t[r];i instanceof Function?i=i.bind(e):"handleDOMEvents"==r&&(i=ct(i,e,{})),n[r]=i}return n}new ht("doc",{init:t=>t.doc||t.schema.topNodeType.createAndFill(),apply:t=>t.doc}),new ht("selection",{init:(t,e)=>t.selection||Q.atStart(e.doc),apply:t=>t.selection}),new ht("storedMarks",{init:t=>t.storedMarks||null,apply:(t,e,n,r)=>r.selection.$cursor?t.storedMarks:null}),new ht("scrollToSelection",{init:()=>0,apply:(t,e)=>t.scrolledIntoView?e+1:e});class dt{constructor(t){this.spec=t,this.props={},t.props&&ct(t.props,this,this.props),this.key=t.key?t.key.key:pt("plugin")}getState(t){return t[this.key]}}const ut=Object.create(null);function pt(t){return t in ut?t+"$"+ ++ut[t]:(ut[t]=0,t+"$")}class ft{constructor(t="key"){this.key=pt(t)}get(t){return t.config.pluginsByKey[this.key]}getState(t){return t[this.key]}}function mt(t,e,n){const r=[];return t.nodesBetween(e.from,e.to,((t,e)=>{n(t)&&r.push({node:t,pos:e})})),r}const gt={"inset-center":700,"outset-center":1192,"fill-width":null,grid:null};function yt(t,e){const n=gt[t&&void 0!==gt[t]?t:"inset-center"];return null===n?null:void 0!==e?Math.min(n,e):n}function wt(t){const e=t.firstChild,n="grid"===e.attrs.layout?e.type.create({...e.attrs,layout:"inset-center"},e.content,e.marks):e;return t.type.create({...t.attrs,width:null},n,t.marks)}function kt(t,e,n,r){const i=1032-10*(Math.min(3,r)-1);return i*(t/e)/n/i*100}function St(t,e){const{selection:n}=t;if(n instanceof nt){const{node:t}=n;return["image","iframe"].includes(t.type.name)&&t.attrs.layout===e}return!1}const vt=t=>(e,n)=>{const{selection:r}=e;if(r instanceof nt){const i=r.node;if(["image","iframe"].includes(i.type.name)){const s=e.tr.setNodeMarkup(r.from,void 0,{...i.attrs,layout:t});return n&&n(s),!0}}return!1};function bt(t,e){const{selection:n}=t;if(!(n instanceof nt))return!1;const r=n.node;if(!["image","iframe"].includes(r.type.name))return!1;if("iframe"===r.type.name)return!0;const{width:i}=r.attrs;return"inset-center"===e||!!["outset-center","fill-width"].includes(e)&&i>=1192}class Et{editorView;dropCursor=null;isFirefox=/Firefox/.test(navigator.userAgent);dragFigure=null;dragPlaceholder;boundHandlers={};constructor(t){this.editorView=t,this.dragPlaceholder=document.createElement("div"),this.dragPlaceholder.className="drag-placeholder",this.dropCursor=document.createElement("div"),this.dropCursor.className="drop-cursor",this.editorView.dom.parentElement.appendChild(this.dragPlaceholder),this.editorView.dom.parentElement.appendChild(this.dropCursor),this.boundHandlers={dragstart:t=>this.handleDragStart(t),dragend:t=>this.handleDragEnd(t),dragover:t=>this.handleDragOver(t),drop:t=>this.handleDrop(t)};for(const[t,e]of Object.entries(this.boundHandlers))this.editorView.dom.addEventListener(t,e)}handleDragStart(t){const e=t.target;if(!e||"IMG"!==e.tagName)return void t.preventDefault();if(this.dragFigure=e.closest("figure"),!this.dragPlaceholder)return;this.dragPlaceholder.innerHTML="",this.dragPlaceholder.appendChild(e.cloneNode(!0));const n=this.isFirefox?this.dragPlaceholder:this.dragPlaceholder.firstElementChild;t.dataTransfer?.setDragImage(n,0,0)}handleDragEnd(t){this.dragFigure&&(this.dragFigure=null),this.hideDropCursor()}getClosestChild(t){const{clientX:e,clientY:n,target:r}=t;if(!(r instanceof HTMLElement))return null;const i=r.closest(".proEditor .images-grid > figure, .proEditor > *"),s=i?.classList.contains("images-grid"),o=i?.closest(".images-grid"),a="figure"===i?.tagName.toLocaleLowerCase()&&!i.querySelector("iframe")&&!o;if(r===this.editorView.dom||!i||s){const t=this.findClosestChild(i||this.editorView.dom,e,n);return t?{element:t,isFigureCollapsed:a,grid:o}:null}return{element:i,isFigureCollapsed:a,grid:o}}findClosestChild(t,e,n){if(!t||!t.children.length)return null;let r=null,i=Number.POSITIVE_INFINITY;for(const s of t.children){const t=s.getBoundingClientRect(),o=Math.max(t.left,Math.min(e,t.right)),a=Math.max(t.top,Math.min(n,t.bottom)),l=Math.sqrt((o-e)**2+(a-n)**2);l<i&&(i=l,r=s)}return r}resolveDropTarget(t){const e=this.getClosestChild(t);if(!e||e.element===this.dragFigure&&e.isFigureCollapsed)return;const n=this.dragFigure?.closest(".images-grid")===e.grid;e.grid&&e.grid.children.length>=3&&!n&&(e.element=e.grid,e.grid=null);const r=e.element.getBoundingClientRect(),i=e.isFigureCollapsed||e.grid?t.clientX-r.left<r.width/2:t.clientY-r.top<r.height/2;return{...e,insertBefore:i}}handleDragOver(t){const e=this.resolveDropTarget(t);e?(this.updateDropCursor(e),t.preventDefault()):this.hideDropCursor()}updateDropCursor({element:t,insertBefore:e,isFigureCollapsed:n,grid:r}){if(!this.dropCursor)return;const{scrollX:i,scrollY:s}=window;this.dropCursor.classList.add("active");const o=("figure"===t.tagName.toLowerCase()&&!t.querySelector("iframe")?t.querySelector("img"):t).getBoundingClientRect(),a=t.previousElementSibling,l=t.nextElementSibling;if(this.dropCursor.style.transition="none",n||r){const t=r?`${i+(e?o.left-7:o.right+3)}px`:`${i+(e?o.left-4:o.right)}px`;Object.assign(this.dropCursor.style,{left:t,width:"4px",height:`${o.height}px`,top:`${s+o.top}px`,transition:"left 75ms ease-out"})}else{let t;if(e&&a){t=(a.getBoundingClientRect().bottom+o.top)/2-1}else if(!e&&l){const e=l.getBoundingClientRect();t=(o.bottom+e.top)/2}else t=e?o.top:o.bottom;Object.assign(this.dropCursor.style,{width:`${o.width}px`,height:"4px",left:`${i+o.left}px`,top:s+t-2+"px",transition:"top 75ms ease-out"})}}hideDropCursor(){this.dropCursor&&this.dropCursor.classList.remove("active")}handleDrop(t){t.preventDefault();const{dataTransfer:e}=t,{state:n,dispatch:r}=this.editorView,{tr:i,schema:s}=n,o=this.resolveDropTarget(t);if(o){if(this.dragFigure){const t=this.editorView.posAtDOM(this.dragFigure,0)-1,e=n.doc.nodeAt(t),a=this.editorView.posAtDOM(o.element,0)-1,l=n.doc.nodeAt(a);if(!l||!e)return;if(o.isFigureCollapsed&&!o.grid){const n=o.insertBefore?[e,l]:[l,e],r=s.nodes.imagesGrid.create(null,n);i.delete(t,t+e.nodeSize);const h=i.mapping.map(a);i.delete(h,h+l.nodeSize),i.insert(i.mapping.map(a),r)}else{i.delete(t,t+e.nodeSize);const n=o.insertBefore?a:a+l.nodeSize,r=o.grid||"grid"!==e.firstChild?.attrs.layout?e:wt(e);i.insert(i.mapping.map(n),r)}r(i)}else e&&e.files.length>0&&Array.from(e.files).filter((t=>t.type.startsWith("image/")));this.dragFigure=null,this.hideDropCursor()}}destroy(){for(const[t,e]of Object.entries(this.boundHandlers))this.editorView.dom.removeEventListener(t,e);this.dropCursor?.remove(),this.dropCursor=null,this.dragPlaceholder?.remove(),this.dragPlaceholder=null,this.dragFigure=null}}function Ct(t){const e=function(t,e=JSON.stringify){const n=new Set;return t.filter((t=>{const r=e(t);return!n.has(r)&&(n.add(r),!0)}))}(t);return 1===e.length?e:e.filter(((t,n)=>!e.filter(((t,e)=>e!==n)).some((e=>t.oldRange.from>=e.oldRange.from&&t.oldRange.to<=e.oldRange.to&&t.newRange.from>=e.newRange.from&&t.newRange.to<=e.newRange.to))))}function xt(){return new dt({key:new ft("dragHandler"),view:t=>new Et(t),appendTransaction:(t,{doc:e},