"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const d=require("../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.js"),k=require("../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.js"),r=require("../core/quill.js"),K=require("../core/logger.js"),T=require("../core/module.js"),D=require("../../../../lodash-es@4.17.21/node_modules/lodash-es/isEqual.js"),A=require("../../../../lodash-es@4.17.21/node_modules/lodash-es/cloneDeep.js"),F=K.default("quill:keyboard"),b=/Mac/i.test(navigator.platform)?"metaKey":"ctrlKey";class x extends T.default{static match(e,l){return["altKey","ctrlKey","metaKey","shiftKey"].some(s=>!!l[s]!==e[s]&&l[s]!==null)?!1:l.key===e.key||l.key===e.which}constructor(e,l){super(e,l),this.bindings={},Object.keys(this.options.bindings).forEach(s=>{this.options.bindings[s]&&this.addBinding(this.options.bindings[s])}),this.addBinding({key:"Enter",shiftKey:null},this.handleEnter),this.addBinding({key:"Enter",metaKey:null,ctrlKey:null,altKey:null},()=>{}),/Firefox/i.test(navigator.userAgent)?(this.addBinding({key:"Backspace"},{collapsed:!0},this.handleBackspace),this.addBinding({key:"Delete"},{collapsed:!0},this.handleDelete)):(this.addBinding({key:"Backspace"},{collapsed:!0,prefix:/^.?$/},this.handleBackspace),this.addBinding({key:"Delete"},{collapsed:!0,suffix:/^.?$/},this.handleDelete)),this.addBinding({key:"Backspace"},{collapsed:!1},this.handleDeleteRange),this.addBinding({key:"Delete"},{collapsed:!1},this.handleDeleteRange),this.addBinding({key:"Backspace",altKey:null,ctrlKey:null,metaKey:null,shiftKey:null},{collapsed:!0,offset:0},this.handleBackspace),this.listen()}addBinding(e){let l=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},s=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{};const i=R(e);if(i==null){F.warn("Attempted to add invalid keyboard binding",i);return}typeof l=="function"&&(l={handler:l}),typeof s=="function"&&(s={handler:s}),(Array.isArray(i.key)?i.key:[i.key]).forEach(n=>{const o={...i,key:n,...l,...s};this.bindings[o.key]=this.bindings[o.key]||[],this.bindings[o.key].push(o)})}listen(){this.quill.root.addEventListener("keydown",e=>{if(e.defaultPrevented||e.isComposing||e.keyCode===229&&(e.key==="Enter"||e.key==="Backspace"))return;const i=(this.bindings[e.key]||[]).concat(this.bindings[e.which]||[]).filter(a=>x.match(e,a));if(i.length===0)return;const u=r.default.find(e.target,!0);if(u&&u.scroll!==this.quill.scroll)return;const n=this.quill.getSelection();if(n==null||!this.quill.hasFocus())return;const[o,f]=this.quill.getLine(n.index),[c,m]=this.quill.getLeaf(n.index),[y,w]=n.length===0?[c,m]:this.quill.getLeaf(n.index+n.length),U=c instanceof k.TextBlot?c.value().slice(0,m):"",B=y instanceof k.TextBlot?y.value().slice(w):"",h={collapsed:n.length===0,empty:n.length===0&&o.length()<=1,format:this.quill.getFormat(n),line:o,offset:f,prefix:U,suffix:B,event:e};i.some(a=>{if(a.collapsed!=null&&a.collapsed!==h.collapsed||a.empty!=null&&a.empty!==h.empty||a.offset!=null&&a.offset!==h.offset)return!1;if(Array.isArray(a.format)){if(a.format.every(p=>h.format[p]==null))return!1}else if(typeof a.format=="object"&&!Object.keys(a.format).every(p=>a.format[p]===!0?h.format[p]!=null:a.format[p]===!1?h.format[p]==null:D.default(a.format[p],h.format[p])))return!1;return a.prefix!=null&&!a.prefix.test(h.prefix)||a.suffix!=null&&!a.suffix.test(h.suffix)?!1:a.handler.call(this,n,h,a)!==!0})&&e.preventDefault()})}handleBackspace(e,l){const s=/[\uD800-\uDBFF][\uDC00-\uDFFF]$/.test(l.prefix)?2:1;if(e.index===0||this.quill.getLength()<=1)return;let i={};const[u]=this.quill.getLine(e.index);let n=new d.default().retain(e.index-s).delete(s);if(l.offset===0){const[o]=this.quill.getLine(e.index-1);if(o&&!(o.statics.blotName==="block"&&o.length()<=1)){const c=u.formats(),m=this.quill.getFormat(e.index-1,1);if(i=d.DeltaExports.AttributeMap.diff(c,m)||{},Object.keys(i).length>0){const y=new d.default().retain(e.index+u.length()-2).retain(1,i);n=n.compose(y)}}}this.quill.updateContents(n,r.default.sources.USER),this.quill.focus()}handleDelete(e,l){const s=/^[\uD800-\uDBFF][\uDC00-\uDFFF]/.test(l.suffix)?2:1;if(e.index>=this.quill.getLength()-s)return;let i={};const[u]=this.quill.getLine(e.index);let n=new d.default().retain(e.index).delete(s);if(l.offset>=u.length()-1){const[o]=this.quill.getLine(e.index+1);if(o){const f=u.formats(),c=this.quill.getFormat(e.index,1);i=d.DeltaExports.AttributeMap.diff(f,c)||{},Object.keys(i).length>0&&(n=n.retain(o.length()-1).retain(1,i))}}this.quill.updateContents(n,r.default.sources.USER),this.quill.focus()}handleDeleteRange(e){L({range:e,quill:this.quill}),this.quill.focus()}handleEnter(e,l){const s=Object.keys(l.format).reduce((u,n)=>(this.quill.scroll.query(n,k.Scope.BLOCK)&&!Array.isArray(l.format[n])&&(u[n]=l.format[n]),u),{}),i=new d.default().retain(e.index).delete(e.length).insert(`
`,s);this.quill.updateContents(i,r.default.sources.USER),this.quill.setSelection(e.index+1,r.default.sources.SILENT),this.quill.focus()}}const C={bindings:{bold:S("bold"),italic:S("italic"),underline:S("underline"),indent:{key:"Tab",format:["blockquote","indent","list"],handler(t,e){return e.collapsed&&e.offset!==0?!0:(this.quill.format("indent","+1",r.default.sources.USER),!1)}},outdent:{key:"Tab",shiftKey:!0,format:["blockquote","indent","list"],handler(t,e){return e.collapsed&&e.offset!==0?!0:(this.quill.format("indent","-1",r.default.sources.USER),!1)}},"outdent backspace":{key:"Backspace",collapsed:!0,shiftKey:null,metaKey:null,ctrlKey:null,altKey:null,format:["indent","list"],offset:0,handler(t,e){e.format.indent!=null?this.quill.format("indent","-1",r.default.sources.USER):e.format.list!=null&&this.quill.format("list",!1,r.default.sources.USER)}},"indent code-block":E(!0),"outdent code-block":E(!1),"remove tab":{key:"Tab",shiftKey:!0,collapsed:!0,prefix:/\t$/,handler(t){this.quill.deleteText(t.index-1,1,r.default.sources.USER)}},tab:{key:"Tab",handler(t,e){if(e.format.table)return!0;this.quill.history.cutoff();const l=new d.default().retain(t.index).delete(t.length).insert("	");return this.quill.updateContents(l,r.default.sources.USER),this.quill.history.cutoff(),this.quill.setSelection(t.index+1,r.default.sources.SILENT),!1}},"blockquote empty enter":{key:"Enter",collapsed:!0,format:["blockquote"],empty:!0,handler(){this.quill.format("blockquote",!1,r.default.sources.USER)}},"list empty enter":{key:"Enter",collapsed:!0,format:["list"],empty:!0,handler(t,e){const l={list:!1};e.format.indent&&(l.indent=!1),this.quill.formatLine(t.index,t.length,l,r.default.sources.USER)}},"checklist enter":{key:"Enter",collapsed:!0,format:{list:"checked"},handler(t){const[e,l]=this.quill.getLine(t.index),s={...e.formats(),list:"checked"},i=new d.default().retain(t.index).insert(`
`,s).retain(e.length()-l-1).retain(1,{list:"unchecked"});this.quill.updateContents(i,r.default.sources.USER),this.quill.setSelection(t.index+1,r.default.sources.SILENT),this.quill.scrollSelectionIntoView()}},"header enter":{key:"Enter",collapsed:!0,format:["header"],suffix:/^$/,handler(t,e){const[l,s]=this.quill.getLine(t.index),i=new d.default().retain(t.index).insert(`
`,e.format).retain(l.length()-s-1).retain(1,{header:null});this.quill.updateContents(i,r.default.sources.USER),this.quill.setSelection(t.index+1,r.default.sources.SILENT),this.quill.scrollSelectionIntoView()}},"table backspace":{key:"Backspace",format:["table"],collapsed:!0,offset:0,handler(){}},"table delete":{key:"Delete",format:["table"],collapsed:!0,suffix:/^$/,handler(){}},"table enter":{key:"Enter",shiftKey:null,format:["table"],handler(t){const e=this.quill.getModule("table");if(e){const[l,s,i,u]=e.getTable(t),n=v(l,s,i,u);if(n==null)return;let o=l.offset();if(n<0){const f=new d.default().retain(o).insert(`
`);this.quill.updateContents(f,r.default.sources.USER),this.quill.setSelection(t.index+1,t.length,r.default.sources.SILENT)}else if(n>0){o+=l.length();const f=new d.default().retain(o).insert(`
`);this.quill.updateContents(f,r.default.sources.USER),this.quill.setSelection(o,r.default.sources.USER)}}}},"table tab":{key:"Tab",shiftKey:null,format:["table"],handler(t,e){const{event:l,line:s}=e,i=s.offset(this.quill.scroll);l.shiftKey?this.quill.setSelection(i-1,r.default.sources.USER):this.quill.setSelection(i+s.length(),r.default.sources.USER)}},"list autofill":{key:" ",shiftKey:null,collapsed:!0,format:{"code-block":!1,blockquote:!1,table:!1},prefix:/^\s*?(\d+\.|-|\*|\[ ?\]|\[x\])$/,handler(t,e){if(this.quill.scroll.query("list")==null)return!0;const{length:l}=e.prefix,[s,i]=this.quill.getLine(t.index);if(i>l)return!0;let u;switch(e.prefix.trim()){case"[]":case"[ ]":u="unchecked";break;case"[x]":u="checked";break;case"-":case"*":u="bullet";break;default:u="ordered"}this.quill.insertText(t.index," ",r.default.sources.USER),this.quill.history.cutoff();const n=new d.default().retain(t.index-i).delete(l+1).retain(s.length()-2-i).retain(1,{list:u});return this.quill.updateContents(n,r.default.sources.USER),this.quill.history.cutoff(),this.quill.setSelection(t.index-l,r.default.sources.SILENT),!1}},"code exit":{key:"Enter",collapsed:!0,format:["code-block"],prefix:/^$/,suffix:/^\s*$/,handler(t){const[e,l]=this.quill.getLine(t.index);let s=2,i=e;for(;i!=null&&i.length()<=1&&i.formats()["code-block"];)if(i=i.prev,s-=1,s<=0){const u=new d.default().retain(t.index+e.length()-l-2).retain(1,{"code-block":null}).delete(1);return this.quill.updateContents(u,r.default.sources.USER),this.quill.setSelection(t.index-1,r.default.sources.SILENT),!1}return!0}},"embed left":q("ArrowLeft",!1),"embed left shift":q("ArrowLeft",!0),"embed right":q("ArrowRight",!1),"embed right shift":q("ArrowRight",!0),"table down":g(!1),"table up":g(!0)}};x.DEFAULTS=C;function E(t){return{key:"Tab",shiftKey:!t,format:{"code-block":!0},handler(e,l){let{event:s}=l;const i=this.quill.scroll.query("code-block"),{TAB:u}=i;if(e.length===0&&!s.shiftKey){this.quill.insertText(e.index,u,r.default.sources.USER),this.quill.setSelection(e.index+u.length,r.default.sources.SILENT);return}const n=e.length===0?this.quill.getLines(e.index,1):this.quill.getLines(e);let{index:o,length:f}=e;n.forEach((c,m)=>{t?(c.insertAt(0,u),m===0?o+=u.length:f+=u.length):c.domNode.textContent.startsWith(u)&&(c.deleteAt(0,u.length),m===0?o-=u.length:f-=u.length)}),this.quill.update(r.default.sources.USER),this.quill.setSelection(o,f,r.default.sources.SILENT)}}}function q(t,e){return{key:t,shiftKey:e,altKey:null,[t==="ArrowLeft"?"prefix":"suffix"]:/^$/,handler(s){let{index:i}=s;t==="ArrowRight"&&(i+=s.length+1);const[u]=this.quill.getLeaf(i);return u instanceof k.EmbedBlot?(t==="ArrowLeft"?e?this.quill.setSelection(s.index-1,s.length+1,r.default.sources.USER):this.quill.setSelection(s.index-1,r.default.sources.USER):e?this.quill.setSelection(s.index,s.length+1,r.default.sources.USER):this.quill.setSelection(s.index+s.length+1,r.default.sources.USER),!1):!0}}}function S(t){return{key:t[0],shortKey:!0,handler(e,l){this.quill.format(t,!l.format[t],r.default.sources.USER)}}}function g(t){return{key:t?"ArrowUp":"ArrowDown",collapsed:!0,format:["table"],handler(e,l){const s=t?"prev":"next",i=l.line,u=i.parent[s];if(u!=null){if(u.statics.blotName==="table-row"){let n=u.children.head,o=i;for(;o.prev!=null;)o=o.prev,n=n.next;const f=n.offset(this.quill.scroll)+Math.min(l.offset,n.length()-1);this.quill.setSelection(f,0,r.default.sources.USER)}}else{const n=i.table()[s];n!=null&&(t?this.quill.setSelection(n.offset(this.quill.scroll)+n.length()-1,0,r.default.sources.USER):this.quill.setSelection(n.offset(this.quill.scroll),0,r.default.sources.USER))}return!1}}}function R(t){if(typeof t=="string"||typeof t=="number")t={key:t};else if(typeof t=="object")t=A.default(t);else return null;return t.shortKey&&(t[b]=t.shortKey,delete t.shortKey),t}function L(t){let{quill:e,range:l}=t;const s=e.getLines(l);let i={};if(s.length>1){const u=s[0].formats(),n=s[s.length-1].formats();i=d.DeltaExports.AttributeMap.diff(n,u)||{}}e.deleteText(l,r.default.sources.USER),Object.keys(i).length>0&&e.formatLine(l.index,1,i,r.default.sources.USER),e.setSelection(l.index,r.default.sources.SILENT)}function v(t,e,l,s){return e.prev==null&&e.next==null?l.prev==null&&l.next==null?s===0?-1:1:l.prev==null?-1:1:e.prev==null?-1:e.next==null?1:null}exports.SHORTKEY=b;exports.default=x;exports.deleteRange=L;exports.normalize=R;
