"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const o=require("../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.js"),l=require("../core/quill.js"),c=require("../core/module.js"),u=require("../formats/table.js");class d extends c.default{static register(){l.default.register(u.TableCell),l.default.register(u.TableRow),l.default.register(u.TableBody),l.default.register(u.TableContainer)}constructor(){super(...arguments),this.listenBalanceCells()}balanceTables(){this.quill.scroll.descendants(u.TableContainer).forEach(t=>{t.balanceCells()})}deleteColumn(){const[t,,e]=this.getTable();e!=null&&(t.deleteColumn(e.cellOffset()),this.quill.update(l.default.sources.USER))}deleteRow(){const[,t]=this.getTable();t!=null&&(t.remove(),this.quill.update(l.default.sources.USER))}deleteTable(){const[t]=this.getTable();if(t==null)return;const e=t.offset();t.remove(),this.quill.update(l.default.sources.USER),this.quill.setSelection(e,l.default.sources.SILENT)}getTable(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:this.quill.getSelection();if(t==null)return[null,null,null,-1];const[e,s]=this.quill.getLine(t.index);if(e==null||e.statics.blotName!==u.TableCell.blotName)return[null,null,null,-1];const n=e.parent;return[n.parent.parent,n,e,s]}insertColumn(t){const e=this.quill.getSelection();if(!e)return;const[s,n,i]=this.getTable(e);if(i==null)return;const r=i.cellOffset();s.insertColumn(r+t),this.quill.update(l.default.sources.USER);let a=n.rowOffset();t===0&&(a+=1),this.quill.setSelection(e.index+a,e.length,l.default.sources.SILENT)}insertColumnLeft(){this.insertColumn(0)}insertColumnRight(){this.insertColumn(1)}insertRow(t){const e=this.quill.getSelection();if(!e)return;const[s,n,i]=this.getTable(e);if(i==null)return;const r=n.rowOffset();s.insertRow(r+t),this.quill.update(l.default.sources.USER),t>0?this.quill.setSelection(e,l.default.sources.SILENT):this.quill.setSelection(e.index+n.children.length,e.length,l.default.sources.SILENT)}insertRowAbove(){this.insertRow(0)}insertRowBelow(){this.insertRow(1)}insertTable(t,e){const s=this.quill.getSelection();if(s==null)return;const n=new Array(t).fill(0).reduce(i=>{const r=new Array(e).fill(`
`).join("");return i.insert(r,{table:u.tableId()})},new o.default().retain(s.index));this.quill.updateContents(n,l.default.sources.USER),this.quill.setSelection(s.index,l.default.sources.SILENT),this.balanceTables()}listenBalanceCells(){this.quill.on(l.default.events.SCROLL_OPTIMIZE,t=>{t.some(e=>["TD","TR","TBODY","TABLE"].includes(e.target.tagName)?(this.quill.once(l.default.events.TEXT_CHANGE,(s,n,i)=>{i===l.default.sources.USER&&this.balanceTables()}),!0):!1)})}}exports.default=d;
