"use strict";var g=Object.defineProperty;var p=(n,i,e)=>i in n?g(n,i,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[i]=e;var u=(n,i,e)=>(p(n,typeof i!="symbol"?i+"":i,e),e);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const y=require("../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.js"),m=require("../core/module.js"),o=require("../core/quill.js");class c extends m.default{constructor(e,s){super(e,s);u(this,"lastRecorded",0);u(this,"ignoreChange",!1);u(this,"stack",{undo:[],redo:[]});u(this,"currentRange",null);this.quill.on(o.default.events.EDITOR_CHANGE,(t,r,l,a)=>{t===o.default.events.SELECTION_CHANGE?r&&a!==o.default.sources.SILENT&&(this.currentRange=r):t===o.default.events.TEXT_CHANGE&&(this.ignoreChange||(!this.options.userOnly||a===o.default.sources.USER?this.record(r,l):this.transform(r)),this.currentRange=d(this.currentRange,r))}),this.quill.keyboard.addBinding({key:"z",shortKey:!0},this.undo.bind(this)),this.quill.keyboard.addBinding({key:["z","Z"],shortKey:!0,shiftKey:!0},this.redo.bind(this)),/Win/i.test(navigator.platform)&&this.quill.keyboard.addBinding({key:"y",shortKey:!0},this.redo.bind(this)),this.quill.root.addEventListener("beforeinput",t=>{t.inputType==="historyUndo"?(this.undo(),t.preventDefault()):t.inputType==="historyRedo"&&(this.redo(),t.preventDefault())})}change(e,s){if(this.stack[e].length===0)return;const t=this.stack[e].pop();if(!t)return;const r=this.quill.getContents(),l=t.delta.invert(r);this.stack[s].push({delta:l,range:d(t.range,l)}),this.lastRecorded=0,this.ignoreChange=!0,this.quill.updateContents(t.delta,o.default.sources.USER),this.ignoreChange=!1,this.restoreSelection(t)}clear(){this.stack={undo:[],redo:[]}}cutoff(){this.lastRecorded=0}record(e,s){if(e.ops.length===0)return;this.stack.redo=[];let t=e.invert(s),r=this.currentRange;const l=Date.now();if(this.lastRecorded+this.options.delay>l&&this.stack.undo.length>0){const a=this.stack.undo.pop();a&&(t=t.compose(a.delta),r=a.range)}else this.lastRecorded=l;t.length()!==0&&(this.stack.undo.push({delta:t,range:r}),this.stack.undo.length>this.options.maxStack&&this.stack.undo.shift())}redo(){this.change("redo","undo")}transform(e){h(this.stack.undo,e),h(this.stack.redo,e)}undo(){this.change("undo","redo")}restoreSelection(e){if(e.range)this.quill.setSelection(e.range,o.default.sources.USER);else{const s=f(this.quill.scroll,e.delta);this.quill.setSelection(s,o.default.sources.USER)}}}u(c,"DEFAULTS",{delay:1e3,maxStack:100,userOnly:!1});function h(n,i){let e=i;for(let s=n.length-1;s>=0;s-=1){const t=n[s];n[s]={delta:e.transform(t.delta,!0),range:t.range&&d(t.range,e)},e=t.delta.transform(e),n[s].delta.length()===0&&n.splice(s,1)}}function R(n,i){const e=i.ops[i.ops.length-1];return e==null?!1:e.insert!=null?typeof e.insert=="string"&&e.insert.endsWith(`
`):e.attributes!=null?Object.keys(e.attributes).some(s=>n.query(s,y.Scope.BLOCK)!=null):!1}function f(n,i){const e=i.reduce((t,r)=>t+(r.delete||0),0);let s=i.length()-e;return R(n,i)&&(s-=1),s}function d(n,i){if(!n)return n;const e=i.transformPosition(n.index),s=i.transformPosition(n.index+n.length);return{index:e,length:s-e}}exports.default=c;exports.getLastChangeIndex=f;
