UNPKG

@lexical/table

Version:

This package provides the Table feature for Lexical.

9 lines (8 loc) • 68.4 kB
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import{ElementNode as e,$getDocument as t,addClassNamesToElement as n,isHTMLElement as o,$isInlineElementOrDecoratorNode as l,$isTextNode as r,$isLineBreakNode as s,$createParagraphNode as i,$applyNodeReplacement as c,createCommand as a,$createTextNode as u,$getSelection as h,$isRangeSelection as d,$findMatchingParent as f,$isParagraphNode as g,$createPoint as m,$getNodeByKey as p,$isElementNode as C,$normalizeSelection__EXPERIMENTAL as _,isCurrentlyReadOnlyMode as S,TEXT_TYPE_TO_FORMAT as b,$getEditor as w,$setSelection as y,SELECTION_CHANGE_COMMAND as N,removeClassNamesFromElement as x,getDOMSelection as v,$createRangeSelection as T,$isRootNode as R,INSERT_PARAGRAPH_COMMAND as F,registerEventListener as O,KEY_ARROW_DOWN_COMMAND as A,KEY_ARROW_UP_COMMAND as K,KEY_ARROW_LEFT_COMMAND as k,KEY_ARROW_RIGHT_COMMAND as M,COMMAND_PRIORITY_HIGH as E,KEY_ESCAPE_COMMAND as $,DELETE_WORD_COMMAND as W,DELETE_LINE_COMMAND as z,DELETE_CHARACTER_COMMAND as H,KEY_BACKSPACE_COMMAND as B,KEY_DELETE_COMMAND as L,CUT_COMMAND as P,PASTE_COMMAND as D,getComposedEventTarget as I,isDOMNode as U,getActiveElement as X,COPY_COMMAND as Y,FORMAT_TEXT_COMMAND as J,FORMAT_ELEMENT_COMMAND as q,CONTROLLED_TEXT_INSERTION_COMMAND as j,KEY_TAB_COMMAND as V,FOCUS_COMMAND as G,$getNearestNodeFromDOMNode as Q,$isRootOrShadowRoot as Z,getDOMSelectionRange as ee,getDOMSelectionPoints as te,$getPreviousSelection as ne,$caretFromPoint as oe,$isExtendableTextPointCaret as le,$extendCaretToRange as re,$isSiblingCaret as se,$getSiblingCaret as ie,$setPointFromCaret as ce,$normalizeCaret as ae,$createRangeSelectionFromDom as ue,$isChildCaret as he,$getChildCaret as de,$getAdjacentChildCaret as fe,IS_FIREFOX as ge,$getNodeByKeyOrThrow as me,isDOMDocumentNode as pe,isDOMShadowRoot as Ce,setDOMStyleFromCSS as _e,setDOMUnmanaged as Se,mergeRegister as be,COMMAND_PRIORITY_EDITOR as we,SELECTION_INSERT_CLIPBOARD_NODES_COMMAND as ye,SELECT_ALL_COMMAND as Ne,COMMAND_PRIORITY_LOW as xe,CLICK_COMMAND as ve,$getRoot as Te,isHTMLTableRowElement as Re,IS_BOLD as Fe,IS_ITALIC as Oe,IS_UNDERLINE as Ae,IS_STRIKETHROUGH as Ke,defineExtension as ke,$fullReconcile as Me,configExtension as Ee,safeCast as $e}from"lexical";import{signal as We,effect as ze,namedSignals as He}from"@lexical/extension";import{defineImportRule as Be,sel as Le,ImportTextFormat as Pe,ImportTextStyle as De,contextValue as Ie,$propagateTextAlignToBlockChildren as Ue,CoreImportExtension as Xe,DOMImportExtension as Ye}from"@lexical/html";import{$descendantsMatching as Je,objectKlassEquals as qe,$insertFirst as je,$insertNodeToNearestRoot as Ve,$dfs as Ge,$unwrapAndFilterDescendants as Qe}from"@lexical/utils";import{copyToClipboard as Ze,$getClipboardDataFromSelection as et}from"@lexical/clipboard";const tt=/^(\d+(?:\.\d+)?)px$/,nt={BOTH:3,COLUMN:2,NO_STATUS:0,ROW:1};class ot extends e{__colSpan;__rowSpan;__headerState;__width;__backgroundColor;__verticalAlign;static getType(){return"tablecell"}static clone(e){return new ot(e.__headerState,e.__colSpan,e.__width,e.__key)}afterCloneFrom(e){super.afterCloneFrom(e),this.__rowSpan=e.__rowSpan,this.__backgroundColor=e.__backgroundColor,this.__verticalAlign=e.__verticalAlign,this.__colSpan=e.__colSpan,this.__headerState=e.__headerState,this.__width=e.__width}static importDOM(){return{td:e=>({conversion:rt,priority:0}),th:e=>({conversion:rt,priority:0})}}static importJSON(e){return st().updateFromJSON(e)}updateFromJSON(e){return super.updateFromJSON(e).setHeaderStyles(e.headerState).setColSpan(e.colSpan||1).setRowSpan(e.rowSpan||1).setWidth(e.width||void 0).setBackgroundColor(e.backgroundColor||null).setVerticalAlign(e.verticalAlign||void 0)}constructor(e=nt.NO_STATUS,t=1,n,o){super(o),this.__colSpan=t,this.__rowSpan=1,this.__headerState=e,this.__width=n,this.__backgroundColor=null,this.__verticalAlign=void 0}createDOM(e){const o=t().createElement(this.getTag());return this.__width&&(o.style.width=`${this.__width}px`),this.__colSpan>1&&(o.colSpan=this.__colSpan),this.__rowSpan>1&&(o.rowSpan=this.__rowSpan),null!==this.__backgroundColor&&(o.style.backgroundColor=this.__backgroundColor),lt(this.__verticalAlign)&&(o.style.verticalAlign=this.__verticalAlign),n(o,e.theme.tableCell,this.hasHeader()&&e.theme.tableCellHeader),o}exportDOM(e){const t=super.exportDOM(e);if(o(t.element)){const e=t.element;e.setAttribute("data-temporary-table-cell-lexical-key",this.getKey()),e.style.border="1px solid black",this.__colSpan>1&&(e.colSpan=this.__colSpan),this.__rowSpan>1&&(e.rowSpan=this.__rowSpan),e.style.width=`${this.getWidth()||75}px`,e.style.verticalAlign=this.getVerticalAlign()||"top",e.style.textAlign="start",null===this.__backgroundColor&&this.hasHeader()&&(e.style.backgroundColor="#f2f3f5")}return t}exportJSON(){return{...super.exportJSON(),...lt(this.__verticalAlign)&&{verticalAlign:this.__verticalAlign},backgroundColor:this.getBackgroundColor(),colSpan:this.__colSpan,headerState:this.__headerState,rowSpan:this.__rowSpan,width:this.getWidth()}}getColSpan(){return this.getLatest().__colSpan}setColSpan(e){const t=this.getWritable();return t.__colSpan=e,t}getRowSpan(){return this.getLatest().__rowSpan}setRowSpan(e){const t=this.getWritable();return t.__rowSpan=e,t}getTag(){return this.hasHeader()?"th":"td"}setHeaderStyles(e,t=nt.BOTH){const n=this.getWritable();return n.__headerState=e&t|n.__headerState&~t,n}getHeaderStyles(){return this.getLatest().__headerState}setWidth(e){const t=this.getWritable();return t.__width=e,t}getWidth(){return this.getLatest().__width}getBackgroundColor(){return this.getLatest().__backgroundColor}setBackgroundColor(e){const t=this.getWritable();return t.__backgroundColor=e,t}getVerticalAlign(){return this.getLatest().__verticalAlign}setVerticalAlign(e){const t=this.getWritable();return t.__verticalAlign=e||void 0,t}toggleHeaderStyle(e){const t=this.getWritable();return(t.__headerState&e)===e?t.__headerState-=e:t.__headerState+=e,t}hasHeaderState(e){return(this.getHeaderStyles()&e)===e}hasHeader(){return this.getLatest().__headerState!==nt.NO_STATUS}updateDOM(e){return e.__headerState!==this.__headerState||e.__width!==this.__width||e.__colSpan!==this.__colSpan||e.__rowSpan!==this.__rowSpan||e.__backgroundColor!==this.__backgroundColor||e.__verticalAlign!==this.__verticalAlign}isShadowRoot(){return!0}collapseAtStart(){return!0}canBeEmpty(){return!1}canIndent(){return!1}}function lt(e){return"middle"===e||"bottom"===e}function rt(e){const t=e,n=e.nodeName.toLowerCase();let c;tt.test(t.style.width)&&(c=parseFloat(t.style.width));let a=nt.NO_STATUS;if("th"===n){const e=t.getAttribute("scope");if("col"===e)a=nt.COLUMN;else if("row"===e)a=nt.ROW;else{const e=t.parentElement,n=o(e)&&"tr"===e.nodeName.toLowerCase()&&o(e.parentElement)&&("thead"===e.parentElement.nodeName.toLowerCase()||0===e.rowIndex),l=0===t.cellIndex;n&&(a|=nt.ROW),l&&(a|=nt.COLUMN),a===nt.NO_STATUS&&(a=nt.ROW)}}const u=st(a,t.colSpan,c);u.__rowSpan=t.rowSpan;const h=t.style.backgroundColor;""!==h&&(u.__backgroundColor=h);const d=t.style.verticalAlign;lt(d)&&(u.__verticalAlign=d);const f=t.style,g=(f&&f.textDecoration||"").split(" "),m="700"===f.fontWeight||"bold"===f.fontWeight,p=g.includes("line-through"),C="italic"===f.fontStyle,_=g.includes("underline"),S=f.color;return{after:e=>{const t=[];let n=null;const o=()=>{if(n){const e=n.getFirstChild();s(e)&&1===n.getChildrenSize()&&e.remove()}};for(const c of e)if(l(c)||r(c)||s(c)){if(r(c)&&(m&&c.toggleFormat("bold"),p&&c.toggleFormat("strikethrough"),C&&c.toggleFormat("italic"),_&&c.toggleFormat("underline"),S)){const e=c.getStyle();e.includes("color:")||c.setStyle(e+`color: ${S};`)}n?n.append(c):(n=i().append(c),t.push(n))}else t.push(c),o(),n=null;return o(),0===t.length&&t.push(i()),t},node:u}}function st(e=nt.NO_STATUS,t=1,n){return c(new ot(e,t,n))}function it(e){return e instanceof ot}const ct=/* @__PURE__ */a("INSERT_TABLE_COMMAND");function at(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}class ut extends e{__height;static getType(){return"tablerow"}static clone(e){return new ut(e.__height,e.__key)}afterCloneFrom(e){super.afterCloneFrom(e),this.__height=e.__height}static importDOM(){return{tr:e=>({conversion:ht,priority:0})}}static importJSON(e){return dt().updateFromJSON(e)}updateFromJSON(e){return super.updateFromJSON(e).setHeight(e.height)}constructor(e,t){super(t),this.__height=e}exportJSON(){const e=this.getHeight();return{...super.exportJSON(),...void 0===e?void 0:{height:e}}}createDOM(e){const o=t().createElement("tr");return this.__height&&(o.style.height=`${this.__height}px`),n(o,e.theme.tableRow),o}extractWithChild(e,t,n){return"html"===n}isShadowRoot(){return!0}setHeight(e){const t=this.getWritable();return t.__height=e,t}getHeight(){return this.getLatest().__height}updateDOM(e){return e.__height!==this.__height}canBeEmpty(){return!1}canIndent(){return!1}}function ht(e){const t=e;let n;return tt.test(t.style.height)&&(n=parseFloat(t.style.height)),{after:e=>Je(e,it),node:dt(n)}}function dt(e){return c(new ut(e))}function ft(e){return e instanceof ut}function gt(e,t,n=!0){const o=no();for(let l=0;l<e;l++){const e=dt();for(let o=0;o<t;o++){let t=nt.NO_STATUS;"object"==typeof n?(0===l&&n.rows&&(t|=nt.ROW),0===o&&n.columns&&(t|=nt.COLUMN)):n&&(0===l&&(t|=nt.ROW),0===o&&(t|=nt.COLUMN));const r=st(t),s=i();s.append(u()),r.append(s),e.append(r)}o.append(e)}return o}function mt(e){const t=f(e,e=>it(e));return it(t)?t:null}function pt(e){const t=f(e,e=>ft(e));if(ft(t))return t;throw new Error("Expected table cell to be inside of table row.")}function Ct(e){const t=f(e,e=>oo(e));if(oo(t))return t;throw new Error("Expected table cell to be inside of table.")}function _t(e){const t=pt(e);return Ct(t).getChildren().findIndex(e=>e.is(t))}function St(e){return pt(e).getChildren().findIndex(t=>t.is(e))}function bt(e,t){const n=Ct(e),{x:o,y:l}=n.getCordsFromCellNode(e,t);return{above:n.getCellNodeFromCords(o,l-1,t),below:n.getCellNodeFromCords(o,l+1,t),left:n.getCellNodeFromCords(o-1,l,t),right:n.getCellNodeFromCords(o+1,l,t)}}function wt(e,t){const n=e.getChildren();if(t>=n.length||t<0)throw new Error("Expected table cell to be inside of table row.");return n[t].remove(),e}function yt(e,t,n=!0,o,l){const r=e.getChildren();if(t>=r.length||t<0)throw new Error("Table row target index out of range");const s=r[t];if(!ft(s))throw new Error("Row before insertion index does not exist.");for(let e=0;e<o;e++){const e=s.getChildren(),t=e.length,o=dt();for(let n=0;n<t;n++){const t=e[n];it(t)||at(12);const{above:r,below:s}=bt(t,l);let c=nt.NO_STATUS;const a=r&&r.getWidth()||s&&s.getWidth()||void 0;(r&&r.hasHeaderState(nt.COLUMN)||s&&s.hasHeaderState(nt.COLUMN))&&(c|=nt.COLUMN);const u=st(c,1,a);u.append(i()),o.append(u)}n?s.insertAfter(o):s.insertBefore(o)}return e}const Nt=(e,t)=>e===nt.BOTH||e===t?t:nt.NO_STATUS;function xt(e=!0){const t=h();d(t)||tn(t)||at(188);const n=t.anchor.getNode(),o=t.focus.getNode(),[l]=Ut(n),[r,,s]=Ut(o),[,i,c]=Dt(s,r,l),{startRow:a}=c,{startRow:u}=i;return e?Tt(a+l.__rowSpan>u+r.__rowSpan?l:r,!0):Tt(u<a?r:l,!1)}const vt=xt;function Tt(e,t=!0){const[,,n]=Ut(e),[o,l]=Dt(n,e,e),r=o[0].length,{startRow:s}=l;let c=null;if(t){const t=s+e.__rowSpan-1,l=o[t],a=dt();for(let e=0;e<r;e++){const{cell:n,startRow:o}=l[e];if(o+n.__rowSpan-1<=t){const t=l[e].cell.__headerState,n=Nt(t,nt.COLUMN);a.append(st(n).append(i()))}else n.setRowSpan(n.__rowSpan+1)}const u=n.getChildAtIndex(t);ft(u)||at(256),u.insertAfter(a),c=a}else{const e=s,t=o[e],l=dt();for(let n=0;n<r;n++){const{cell:o,startRow:r}=t[n];if(r===e){const e=t[n].cell.__headerState,o=Nt(e,nt.COLUMN);l.append(st(o).append(i()))}else o.setRowSpan(o.__rowSpan+1)}const a=n.getChildAtIndex(e);ft(a)||at(257),a.insertBefore(l),c=l}return c}function Rt(e,t,n=!0,o,l){const r=e.getChildren(),s=[];for(let e=0;e<r.length;e++){const n=r[e];if(ft(n))for(let e=0;e<o;e++){const e=n.getChildren();if(t>=e.length||t<0)throw new Error("Table column target index out of range");const o=e[t];it(o)||at(12);const{left:r,right:c}=bt(o,l);let a=nt.NO_STATUS;(r&&r.hasHeaderState(nt.ROW)||c&&c.hasHeaderState(nt.ROW))&&(a|=nt.ROW);const u=st(a);u.append(i()),s.push({newTableCell:u,targetCell:o})}}return s.forEach(({newTableCell:e,targetCell:t})=>{n?t.insertAfter(e):t.insertBefore(e)}),e}function Ft(e=!0){const t=h();d(t)||tn(t)||at(188);const n=t.anchor.getNode(),o=t.focus.getNode(),[l]=Ut(n),[r,,s]=Ut(o),[,i,c]=Dt(s,r,l),{startColumn:a}=c,{startColumn:u}=i;return e?At(a+l.__colSpan>u+r.__colSpan?l:r,!0):At(u<a?r:l,!1)}const Ot=Ft;function At(e,t=!0,n=!0){const[,,o]=Ut(e),[l,r]=Dt(o,e,e),s=l.length,{startColumn:c}=r,a=t?c+e.__colSpan-1:c-1,u=o.getFirstChild();ft(u)||at(120);let h=null;function d(e=nt.NO_STATUS){const t=st(e).append(i());return null===h&&(h=t),t}let f=u;e:for(let e=0;e<s;e++){if(0!==e){const e=f.getNextSibling();ft(e)||at(121),f=e}const t=l[e],n=t[a<0?0:a].cell.__headerState,o=Nt(n,nt.ROW);if(a<0){zt(f,d(o));continue}const{cell:r,startColumn:s,startRow:i}=t[a];if(s+r.__colSpan-1<=a){let n=r,l=i,s=a;for(;l!==e&&n.__rowSpan>1;){if(s-=r.__colSpan,!(s>=0)){f.append(d(o));continue e}{const{cell:e,startRow:o}=t[s];n=e,l=o}}n.insertAfter(d(o))}else r.setColSpan(r.__colSpan+1)}null!==h&&n&&Wt(h);const g=o.getColWidths();if(g){const e=[...g],t=a<0?0:a,n=e[t];e.splice(t,0,n),o.setColWidths(e)}return h}function Kt(e,t){const n=e.getChildren();for(let e=0;e<n.length;e++){const o=n[e];if(ft(o)){const e=o.getChildren();if(t>=e.length||t<0)throw new Error("Table column target index out of range");e[t].remove()}}return e}function kt(){const e=h();d(e)||tn(e)||at(188);const[t,n]=e.isBackward()?[e.focus.getNode(),e.anchor.getNode()]:[e.anchor.getNode(),e.focus.getNode()],[o,,l]=Ut(t),[r]=Ut(n),[s,i,c]=Dt(l,o,r),{startRow:a}=i,{startRow:u}=c,f=u+r.__rowSpan-1;if(s.length===f-a+1)return void l.remove();const g=s[0].length,m=s[f+1],p=l.getChildAtIndex(f+1);for(let e=f;e>=a;e--){for(let t=g-1;t>=0;t--){const{cell:n,startRow:o,startColumn:l}=s[e][t];if(l===t){if(o<a||o+n.__rowSpan-1>f){const e=Math.max(o,a),t=Math.min(n.__rowSpan+o-1,f),l=e<=t?t-e+1:0;n.setRowSpan(n.__rowSpan-l)}if(o>=a&&o+n.__rowSpan-1>f&&e===f){ft(p)||at(387);let o=null;for(let n=0;n<t;n++){const t=m[n],l=t.cell;t.startRow===e+1&&(o=l),l.__colSpan>1&&(n+=l.__colSpan-1)}null===o?zt(p,n):o.insertAfter(n)}}}const t=l.getChildAtIndex(e);ft(t)||at(206,String(e)),t.remove()}if(void 0!==m){const{cell:e}=m[0];Wt(e)}else{const e=s[a-1],{cell:t}=e[0];Wt(t)}}const Mt=kt;function Et(){const e=h();d(e)||tn(e)||at(188);const t=e.anchor.getNode(),n=e.focus.getNode(),[o,,l]=Ut(t),[r]=Ut(n),[s,i,c]=Dt(l,o,r),{startColumn:a}=i,{startRow:u,startColumn:f}=c,g=Math.min(a,f),m=Math.max(a+o.__colSpan-1,f+r.__colSpan-1),p=m-g+1;if(s[0].length===m-g+1)return l.selectPrevious(),void l.remove();const C=s.length;for(let e=0;e<C;e++)for(let t=g;t<=m;t++){const{cell:n,startColumn:o}=s[e][t];if(o<g){if(t===g){const e=g-o;n.setColSpan(n.__colSpan-Math.min(p,n.__colSpan-e))}}else if(o+n.__colSpan-1>m){if(t===m){const e=m-o+1;n.setColSpan(n.__colSpan-e)}}else n.remove()}const _=s[u],S=a>f?_[a+o.__colSpan]:_[f+r.__colSpan];if(void 0!==S){const{cell:e}=S;Wt(e)}else{const e=f<a?_[f-1]:_[a-1],{cell:t}=e;Wt(t)}const b=l.getColWidths();if(b){const e=[...b];e.splice(g,p),l.setColWidths(e)}}const $t=Et;function Wt(e){const t=e.getFirstDescendant();null==t?e.selectStart():t.getParentOrThrow().selectStart()}function zt(e,t){const n=e.getFirstChild();null!==n?n.insertBefore(t):e.append(t)}function Ht(e){if(0===e.length)return null;const t=Ct(e[0]),[n]=It(t,null,null);let o=1/0,l=-1/0,r=1/0,s=-1/0;const c=new Set;for(const t of n)for(const n of t){if(!n||!n.cell)continue;const t=n.cell.getKey();if(!c.has(t)&&e.some(e=>e.is(n.cell))){c.add(t);const e=n.startRow,i=n.startColumn,a=n.cell.__rowSpan||1,u=n.cell.__colSpan||1;o=Math.min(o,e),l=Math.max(l,e+a-1),r=Math.min(r,i),s=Math.max(s,i+u-1)}}if(o===1/0||r===1/0)return null;const a=l-o+1,u=s-r+1,h=n[o][r];if(!h.cell)return null;const d=h.cell;d.setColSpan(u),d.setRowSpan(a);const f=new Set([d.getKey()]);for(let e=o;e<=l;e++)for(let t=r;t<=s;t++){const o=n[e][t];if(!o.cell)continue;const l=o.cell,r=l.getKey();if(!f.has(r)){f.add(r);Bt(l)||d.append(...l.getChildren()),l.remove()}}return 0===d.getChildrenSize()&&d.append(i()),d}function Bt(e){if(1!==e.getChildrenSize())return!1;const t=e.getFirstChildOrThrow();return!(!g(t)||!t.isEmpty())}function Lt(){const e=h();d(e)||tn(e)||at(188);const t=e.anchor.getNode(),n=f(t,it);return it(n)||at(148),Pt(n)}function Pt(e){const[t,n,o]=Ut(e),l=t.__colSpan,r=t.__rowSpan;if(1===l&&1===r)return;const[s,c]=Dt(o,t,t),{startColumn:a,startRow:u}=c,h=t.__headerState&nt.COLUMN,d=Array.from({length:l},(e,t)=>{let n=h;for(let e=0;0!==n&&e<s.length;e++)n&=s[e][t+a].cell.__headerState;return n}),f=t.__headerState&nt.ROW,g=Array.from({length:r},(e,t)=>{let n=f;for(let e=0;0!==n&&e<s[0].length;e++)n&=s[t+u][e].cell.__headerState;return n});if(l>1){for(let e=1;e<l;e++)t.insertAfter(st(d[e]|g[0]).append(i()));t.setColSpan(1)}if(r>1){let e;for(let t=1;t<r;t++){const o=u+t,r=s[o];e=(e||n).getNextSibling(),ft(e)||at(125);let c=null;for(let e=0;e<a;e++){const t=r[e],n=t.cell;t.startRow===o&&(c=n),n.__colSpan>1&&(e+=n.__colSpan-1)}if(null===c)for(let n=l-1;n>=0;n--)zt(e,st(d[n]|g[t]).append(i()));else for(let e=l-1;e>=0;e--)c.insertAfter(st(d[e]|g[t]).append(i()))}t.setRowSpan(1)}}function Dt(e,t,n){const[o,l,r]=It(e,t,n);return null===l&&at(207),null===r&&at(208),[o,l,r]}function It(e,t,n){const o=[];let l=null,r=null;function s(e){let t=o[e];return void 0===t&&(o[e]=t=[]),t}const i=e.getChildren();for(let e=0;e<i.length;e++){const o=i[e];ft(o)||at(209);const c=s(e);for(let a=o.getFirstChild(),u=0;null!=a;a=a.getNextSibling()){for(it(a)||at(147);void 0!==c[u];)u++;const o={cell:a,startColumn:u,startRow:e},{__rowSpan:h,__colSpan:d}=a;for(let t=0;t<h&&!(e+t>=i.length);t++){const n=s(e+t);for(let e=0;e<d;e++)n[u+e]=o}null!==t&&null===l&&t.is(a)&&(l=o),null!==n&&null===r&&n.is(a)&&(r=o)}}return[o,l,r]}function Ut(e){let t;if(e instanceof ot)t=e;else if("__type"in e){const n=f(e,it);it(n)||at(148),t=n}else{const n=f(e.getNode(),it);it(n)||at(148),t=n}const n=t.getParent();ft(n)||at(149);const o=n.getParent();return oo(o)||at(210),[t,n,o]}function Xt(e,t,n){let o,l=Math.min(t.startColumn,n.startColumn),r=Math.min(t.startRow,n.startRow),s=Math.max(t.startColumn+t.cell.__colSpan-1,n.startColumn+n.cell.__colSpan-1),i=Math.max(t.startRow+t.cell.__rowSpan-1,n.startRow+n.cell.__rowSpan-1);do{o=!1;for(let t=0;t<e.length;t++)for(let n=0;n<e[0].length;n++){const c=e[t][n];if(!c)continue;const a=c.startColumn+c.cell.__colSpan-1,u=c.startRow+c.cell.__rowSpan-1,h=c.startColumn<=s&&a>=l,d=c.startRow<=i&&u>=r;if(h&&d){const e=Math.min(l,c.startColumn),t=Math.max(s,a),n=Math.min(r,c.startRow),h=Math.max(i,u);e===l&&t===s&&n===r&&h===i||(l=e,s=t,r=n,i=h,o=!0)}}}while(o);return{maxColumn:s,maxRow:i,minColumn:l,minRow:r}}function Yt(e){const t=e.getChildren();let n=null;for(const e of t){if(!ft(e))return!1;if(null===n&&(n=e.getChildrenSize()),e.getChildrenSize()!==n)return!1;const t=e.getChildren();for(const e of t)if(!it(e)||1!==e.getRowSpan()||1!==e.getColSpan())return!1}return(n||0)>0}function Jt(e,t,n){if(t===n)return;const o=e.getColumnCount();if(t<0||t>=o||n<0||n>=o)return;if(!Yt(e))return;e.getChildren().filter(ft).forEach(e=>{const o=e.getChildren(),[l]=o.splice(t,1);o.splice(n,0,l),e.splice(0,o.length,o)});const l=e.getColWidths();if(l&&l.length===o){const o=[...l],[r]=o.splice(t,1);o.splice(n,0,r),e.setColWidths(o)}}function qt(e,t,n){if(t===n)return;const o=e.getChildren().filter(ft),l=o.length;if(t<0||t>=l||n<0||n>=l)return;if(!Yt(e))return;const r=o[t],s=o[n];n>t?s.insertAfter(r):s.insertBefore(r)}function jt(e){const[t,,n]=Ut(e),o=n.getChildren().filter(ft),l=o.length,r=o[0].getChildren().length,s=new Array(l);for(let e=0;e<l;e++)s[e]=new Array(r);for(let e=0;e<l;e++){const n=o[e].getChildren().filter(it);let l=0;for(let o=0;o<n.length;o++){for(;s[e][l];)l++;const r=n[o],i=r.__rowSpan||1,c=r.__colSpan||1;for(let t=0;t<i;t++)for(let n=0;n<c;n++)s[e+t][l+n]=r;if(t===r)return{colSpan:c,columnIndex:l,rowIndex:e,rowSpan:i};l+=c}}return null}function Vt(e,t){const n=t.getStartEndPoints(),o=tn(t);if(null===n)return!1;const[l,s]=n,[c,a,u]=Ut(l),h=f(s.getNode(),e=>it(e));if(!(it(c)&&it(h)&&ft(a)&&oo(u)))return!1;const[d,g,m]=Dt(u,c,h),[p]=It(e,null,null),C=d.length,_=C>0?d[0].length:0;let S=g.startRow,b=g.startColumn,w=p.length,y=w>0?p[0].length:0;if(o){const e=Xt(d,g,m),t=e.maxRow-e.minRow+1,n=e.maxColumn-e.minColumn+1;S=e.minRow,b=e.minColumn,w=Math.min(w,t),y=Math.min(y,n)}let N=!1;const x=Math.min(C,S+w)-1,v=Math.min(_,b+y)-1,T=new Set;for(let e=S;e<=x;e++)for(let t=b;t<=v;t++){const n=d[e][t];T.has(n.cell.getKey())||(1===n.cell.__rowSpan&&1===n.cell.__colSpan||(Pt(n.cell),T.add(n.cell.getKey()),N=!0))}let[R]=It(u.getWritable(),null,null);const F=w-C+S;for(let e=0;e<F;e++){Tt(R[C-1][0].cell)}const O=y-_+b;for(let e=0;e<O;e++){At(R[0][_-1].cell,!0,!1)}[R]=It(u.getWritable(),null,null);for(let e=S;e<S+w;e++)for(let t=b;t<b+y;t++){const n=e-S,o=t-b,l=p[n][o];if(l.startRow!==n||l.startColumn!==o)continue;const s=l.cell;if(1!==s.__rowSpan||1!==s.__colSpan){const n=[],o=Math.min(e+s.__rowSpan,S+w)-1,l=Math.min(t+s.__colSpan,b+y)-1;for(let r=e;r<=o;r++)for(let e=t;e<=l;e++){const t=R[r][e];n.push(t.cell)}Ht(n),N=!0}const{cell:c}=R[e][t],a=s.getBackgroundColor();null!=a&&c.setBackgroundColor(a);const u=c.getChildren();s.getChildren().forEach(e=>{if(r(e)){i().append(e),c.append(e)}else c.append(e)}),u.forEach(e=>e.remove())}if(o&&N){const[e]=It(u.getWritable(),null,null);e[g.startRow][g.startColumn].cell.selectEnd()}return!0}function Gt(e,t,n){const[o]=It(e,null,null);t>=0&&t<o.length||at(396,String(t));const l=o[t],r=n?nt.ROW:nt.NO_STATUS,s=new Set;for(let e=0;e<l.length;e++){const t=l[e];null!=t&&(s.has(t.cell)||(s.add(t.cell),t.cell.setHeaderStyles(r,nt.ROW)))}}function Qt(e,t,n){const[o]=It(e,null,null);o.length>0&&t>=0&&t<o[0].length||at(397,String(t));const l=n?nt.COLUMN:nt.NO_STATUS,r=new Set;for(let e=0;e<o.length;e++){const n=o[e][t];null!=n&&(r.has(n.cell)||(r.add(n.cell),n.cell.setHeaderStyles(l,nt.COLUMN)))}}function Zt(e){const[[t,n,o,l],[r,s,i,c]]=["anchor","focus"].map(t=>{const n=e[t].getNode(),o=f(n,it);it(o)||at(238,t,n.getKey(),n.getType());const l=o.getParent();ft(l)||at(239,t);const r=l.getParent();return oo(r)||at(240,t),[n,o,l,r]});return l.is(c)||at(241),{anchorCell:n,anchorNode:t,anchorRow:o,anchorTable:l,focusCell:s,focusNode:r,focusRow:i,focusTable:c}}class en{tableKey;anchor;focus;_cachedNodes;dirty;constructor(e,t,n){this.anchor=t,this.focus=n,t._selection=this,n._selection=this,this._cachedNodes=null,this.dirty=!1,this.tableKey=e}getStartEndPoints(){return[this.anchor,this.focus]}isValid(){if("root"===this.tableKey||"root"===this.anchor.key||"element"!==this.anchor.type||"root"===this.focus.key||"element"!==this.focus.type)return!1;const e=p(this.tableKey),t=p(this.anchor.key),n=p(this.focus.key);return null!==e&&null!==t&&null!==n}isBackward(){return this.focus.isBefore(this.anchor)}getCachedNodes(){return this._cachedNodes}setCachedNodes(e){this._cachedNodes=e}is(e){return tn(e)&&this.tableKey===e.tableKey&&this.anchor.is(e.anchor)&&this.focus.is(e.focus)}set(e,t,n){this.dirty=this.dirty||e!==this.tableKey||t!==this.anchor.key||n!==this.focus.key,this.tableKey=e,this.anchor.key=t,this.focus.key=n,this._cachedNodes=null}clone(){return new en(this.tableKey,m(this.anchor.key,this.anchor.offset,this.anchor.type),m(this.focus.key,this.focus.offset,this.focus.type))}isCollapsed(){return!1}extract(){return this.getNodes()}insertRawText(e){if(""===e)return;const t=(e.endsWith("\n")?e.slice(0,-1):e).split("\n").map(e=>e.split("\t")),n=no();for(const e of t){const t=dt();for(const n of e){const e=st(nt.NO_STATUS),o=i();n&&o.append(u(n)),e.append(o),t.append(e)}n.append(t)}const{anchorCell:o}=Zt(this);Vt(n,o.select(0,o.getChildrenSize()))}insertText(){}hasFormat(e){let t=0;this.getNodes().filter(it).forEach(e=>{const n=e.getFirstChild();g(n)&&(t|=n.getTextFormat())});const n=b[e];return 0!==(t&n)}insertNodes(e){const t=this.focus.getNode();C(t)||at(151);_(t.select(0,t.getChildrenSize())).insertNodes(e)}getShape(){const{anchorCell:e,focusCell:t}=Zt(this),n=jt(e);null===n&&at(153);const o=jt(t);null===o&&at(155);const l=Math.min(n.columnIndex,o.columnIndex),r=Math.max(n.columnIndex+n.colSpan-1,o.columnIndex+o.colSpan-1),s=Math.min(n.rowIndex,o.rowIndex),i=Math.max(n.rowIndex+n.rowSpan-1,o.rowIndex+o.rowSpan-1);return{fromX:Math.min(l,r),fromY:Math.min(s,i),toX:Math.max(l,r),toY:Math.max(s,i)}}getNodes(){if(!this.isValid())return[];const e=this._cachedNodes;if(null!==e)return e;const{anchorTable:t,anchorCell:n,focusCell:o}=Zt(this),l=o.getParents()[1];if(l!==t){if(t.isParentOf(o)){const e=l.getParent();null==e&&at(159),this.set(this.tableKey,o.getKey(),e.getKey())}else{const e=t.getParent();null==e&&at(158),this.set(this.tableKey,e.getKey(),o.getKey())}return this.getNodes()}const[r,s,i]=Dt(t,n,o),{minColumn:c,maxColumn:a,minRow:u,maxRow:h}=Xt(r,s,i),d=new Map([[t.getKey(),t]]);let f=null;for(let e=u;e<=h;e++)for(let t=c;t<=a;t++){const{cell:n}=r[e][t],o=n.getParent();ft(o)||at(160),o!==f&&(d.set(o.getKey(),o),f=o),d.has(n.getKey())||ln(n,e=>{d.set(e.getKey(),e)})}const g=Array.from(d.values());return S()||(this._cachedNodes=g),g}getTextContent(){const e=this.getNodes().filter(e=>it(e));let t="";for(let n=0;n<e.length;n++){const o=e[n],l=o.__parent,r=(e[n+1]||{}).__parent;t+=o.getTextContent()+(r!==l?"\n":"\t")}return t}}function tn(e){return e instanceof en}function nn(){const e=m("root",0,"element"),t=m("root",0,"element");return new en("root",e,t)}function on(e,t,n){e.getKey(),t.getKey(),n.getKey();const o=h(),l=tn(o)?o.clone():nn();return l.set(e.getKey(),t.getKey(),n.getKey()),l}function ln(e,t){const n=[[e]];for(let e=n.at(-1);void 0!==e&&n.length>0;e=n.at(-1)){const o=e.pop();void 0===o?n.pop():!1!==t(o)&&C(o)&&n.push(o.getChildren())}}function rn(e,t=w()){const n=p(e);oo(n)||at(231,e);const o=gn(n,t.getElementByKey(e));return null===o&&at(232,e),{tableElement:o,tableNode:n}}class sn{observers;nextFocus;shouldCheckSelectionForTable;constructor(){this.observers=new Map,this.nextFocus=null,this.shouldCheckSelectionForTable=null}setNextFocus(e){this.nextFocus=e}getAndClearNextFocus(){const{nextFocus:e}=this;return null!==e&&(this.nextFocus=null),e}setShouldCheckSelectionForTable(e){this.shouldCheckSelectionForTable=e}getAndClearShouldCheckSelectionForTable(){const{shouldCheckSelectionForTable:e}=this;return e?(this.shouldCheckSelectionForTable=null,e):null}removeObserver(e){const t=this.observers.get(e);return void 0!==t&&(t[0].removeListeners(),this.observers.delete(e),!0)}removeAllObservers(){for(const e of Array.from(this.observers.keys()))this.removeObserver(e)}$getTableNodesAndObservers(){const e=[];for(const[t,[n]]of Array.from(this.observers.entries())){const o=p(t);oo(o)?e.push([o,n]):this.removeObserver(t)}return e}}class cn{focusX;focusY;listenersToRemove;table;isHighlightingCells;anchorX;anchorY;tableNodeKey;anchorCell;focusCell;anchorCellNodeKey;focusCellNodeKey;editor;tableSelection;hasHijackedSelectionStyles;isSelecting;pointerType;abortController;listenerOptions;constructor(e,t){this.isHighlightingCells=!1,this.anchorX=-1,this.anchorY=-1,this.focusX=-1,this.focusY=-1,this.listenersToRemove=new Set,this.tableNodeKey=t,this.editor=e,this.table={columns:0,domRows:[],rows:0},this.tableSelection=null,this.anchorCellNodeKey=null,this.focusCellNodeKey=null,this.anchorCell=null,this.focusCell=null,this.hasHijackedSelectionStyles=!1,this.isSelecting=!1,this.pointerType=null,this.abortController=new AbortController,this.listenerOptions={signal:this.abortController.signal},this.trackTable()}getTable(){return this.table}removeListeners(){this.abortController.abort("removeListeners"),Array.from(this.listenersToRemove).forEach(e=>e()),this.listenersToRemove.clear()}$lookup(){return rn(this.tableNodeKey,this.editor)}trackTable(){const e=new MutationObserver(e=>{this.editor.read("latest",()=>{let t=!1;for(let n=0;n<e.length;n++){const o=e[n].target.nodeName;if("TABLE"===o||"TBODY"===o||"THEAD"===o||"TR"===o){t=!0;break}}if(!t)return;const{tableNode:n,tableElement:o}=this.$lookup();this.table=Rn(n,o)})});this.editor.read("latest",()=>{const{tableNode:t,tableElement:n}=this.$lookup();this.table=Rn(t,n),e.observe(n,{attributes:!0,childList:!0,subtree:!0})})}$clearHighlight(e=!0){const t=this.editor;this.isHighlightingCells=!1,this.anchorX=-1,this.anchorY=-1,this.focusX=-1,this.focusY=-1,this.tableSelection=null,this.anchorCellNodeKey=null,this.focusCellNodeKey=null,this.anchorCell=null,this.focusCell=null,this.hasHijackedSelectionStyles=!1,this.$enableHighlightStyle();const{tableNode:n,tableElement:o}=this.$lookup();Fn(t,Rn(n,o),null),e&&null!==h()&&(y(null),t.dispatchCommand(N,void 0))}$enableHighlightStyle(){const e=this.editor,{tableElement:t}=this.$lookup();x(t,e._config.theme.tableSelection),t.classList.remove("disable-selection"),this.hasHijackedSelectionStyles=!1}$disableHighlightStyle(){const{tableElement:e}=this.$lookup();n(e,this.editor._config.theme.tableSelection),this.hasHijackedSelectionStyles=!0}$updateTableTableSelection(e){if(null!==e){e.tableKey!==this.tableNodeKey&&at(233,e.tableKey,this.tableNodeKey);const t=this.editor;this.tableSelection=e,this.isHighlightingCells=!0,this.$disableHighlightStyle(),this.updateDOMSelection(),Fn(t,this.table,this.tableSelection)}else this.$clearHighlight()}updateDOMSelection(){if(null!==this.anchorCell&&null!==this.focusCell){const e=v(this.editor._window);e&&e.rangeCount>0&&e.removeAllRanges()}}$setFocusCellForSelection(e,t=!1){const n=this.editor,{tableNode:o}=this.$lookup(),l=e.x,r=e.y;if(this.focusCell=e,!this.isHighlightingCells){(t||this.anchorX!==l||this.anchorY!==r||null!=this.tableSelection&&null!=this.anchorCellNodeKey)&&(this.isHighlightingCells=!0,this.$disableHighlightStyle())}if(-1!==this.focusX&&-1!==this.focusY&&l===this.focusX&&r===this.focusY)return!1;if(this.focusX=l,this.focusY=r,this.isHighlightingCells){const s=Jn(o,e.elem);if(null!=this.tableSelection&&null!=this.anchorCellNodeKey){let e=s;if(null===e&&t&&(e=o.getCellNodeFromCords(l,r,this.table)),null!==e){const t=this.$getAnchorTableCellOrThrow();return this.focusCellNodeKey=e.getKey(),this.tableSelection=on(o,t,e),y(this.tableSelection),n.dispatchCommand(N,void 0),Fn(n,this.table,this.tableSelection),!0}}}return!1}$getAnchorTableCell(){const e=this.anchorCellNodeKey?p(this.anchorCellNodeKey):null;return it(e)?e:null}$getAnchorTableCellOrThrow(){const e=this.$getAnchorTableCell();return null===e&&at(234),e}$getFocusTableCell(){const e=this.focusCellNodeKey?p(this.focusCellNodeKey):null;return it(e)?e:null}$getFocusTableCellOrThrow(){const e=this.$getFocusTableCell();return null===e&&at(235),e}$setAnchorCellForSelection(e){this.isHighlightingCells=!1,this.anchorCell=e,this.anchorX=e.x,this.anchorY=e.y,this.focusX=-1,this.focusY=-1,this.focusCell=null,this.focusCellNodeKey=null;const{tableNode:t}=this.$lookup(),n=Jn(t,e.elem);if(null!==n){const e=n.getKey();null!=this.tableSelection?(this.tableSelection=this.tableSelection.clone(),this.tableSelection.set(t.getKey(),e,e)):this.tableSelection=on(t,n,n),this.anchorCellNodeKey=e}}$formatCells(e){const t=h();tn(t)||at(236);const n=T(),o=n.anchor,l=n.focus,r=t.getNodes().filter(it);r.length>0||at(237);const s=r[0].getFirstChild(),i=g(s)?s.getFormatFlags(e,null):null;r.forEach(t=>{o.set(t.getKey(),0,"element"),l.set(t.getKey(),t.getChildrenSize(),"element"),n.formatText(e,i)}),y(t),this.editor.dispatchCommand(N,void 0)}$clearText(){const{editor:e}=this,t=p(this.tableNodeKey);if(!oo(t))throw new Error("Expected TableNode.");const n=h();tn(n)||at(253);const o=n.getNodes().filter(it),l=t.getFirstChild(),r=t.getLastChild();if(o.length>0&&null!==l&&null!==r&&ft(l)&&ft(r)&&o[0]===l.getFirstChild()&&o[o.length-1]===r.getLastChild()){t.selectPrevious();const n=t.getParent();return t.remove(),void(R(n)&&n.isEmpty()&&e.dispatchCommand(F,void 0))}o.forEach(e=>{if(C(e)){const t=i(),n=u();t.append(n),e.append(t),e.getChildren().forEach(e=>{e!==t&&e.remove()})}}),Fn(e,this.table,null),y(null),e.dispatchCommand(N,void 0)}}const an="__lexicalTableSelection";function un(e){const t=me(e);return oo(t)||at(386,e),t}const hn=40;function dn(e,t,n){const o=e=>Math.max(1,Math.ceil(Math.min(hn,e)/hn*18));return e<=t+hn?-o(t+hn-e):e>=n-hn?o(e-(n-hn)):0}function fn(e){return o(e)&&"TABLE"===e.nodeName}function gn(e,t){if(!t)return t;const n=fn(t)?t:t.querySelector("table");return fn(n)||at(341,e.constructor.name,e.getType(),e.getKey(),t.nodeName),n}function mn(e){return e._window}function pn(e,t){for(let n=t,o=null;null!==n;n=n.getParent()){if(e.is(n))return o;it(n)&&(o=n)}return null}const Cn=[[A,"down"],[K,"up"],[k,"backward"],[M,"forward"]],_n=[W,z,H],Sn=[B,L];function bn(e,t){return e.registerRootListener(n=>{if(null===n)return;const o=e._window;if(null===o)return;return O(o,"pointerdown",o=>{const l=I(o);if(0!==o.button||!U(l)||!n.contains(l))return;const r=function(e){const t=vn(e);if(null===t)return null;let n=t.elem;for(;null!=n;){if("TABLE"===n.nodeName&&an in n&&n[an])return{cellElement:t,tableElement:n,tableObserver:n[an]};n=n.parentNode}return null}(l);e.update(()=>{if(tn(h())){for(const[e]of t.observers.values())e.$clearHighlight(!1);y(null),e.dispatchCommand(N,void 0)}if(!r)return;const{tableObserver:n,tableElement:l,cellElement:s}=r;!function(e,t,n,o,l,r){const s=e._window;if(!s)return;const i=n=>{if(l.isSelecting)return;l.isSelecting=!0,null!==n&&null===l.anchorCell&&e.update(()=>{l.$setAnchorCellForSelection(n)});let i=t.clientX,c=t.clientY,a=null;const u=()=>{l.isSelecting=!1,null!==a&&(s.cancelAnimationFrame(a),a=null),s.removeEventListener("pointerup",S),s.removeEventListener("pointermove",b)},h=(e,t)=>{const n=o.getRootNode();if(!pe(n)&&!Ce(n))return null;for(const l of n.elementsFromPoint(e,t)){const e=Tn(o,l);if(e)return e}return null},d=(t,n)=>{null===l.anchorCell&&e.update(()=>{l.$setAnchorCellForSelection(t)}),null!==l.focusCell&&t.elem===l.focusCell.elem||(r.setNextFocus({focusCell:t,override:n,tableKey:l.tableNodeKey}),e.dispatchCommand(N,void 0))},f=e=>{for(let t=o.parentElement;t;t=t.parentElement){if("x"===e?t.scrollWidth>t.clientWidth:t.scrollHeight>t.clientHeight){const n=s.getComputedStyle(t),o="x"===e?n.overflowX:n.overflowY;if("auto"===o||"scroll"===o)return t}}return null},g=(e,t,n)=>{let o,l;if(null===e)o=0,l="x"===n?s.innerWidth:s.innerHeight;else{const t=e.getBoundingClientRect();o="x"===n?t.left:t.top,l="x"===n?t.right:t.bottom}const r=dn(t,o,l);if(0===r)return!1;if(null===e){const e="x"===n?s.scrollX:s.scrollY;return s.scrollBy("x"===n?r:0,"x"===n?0:r),("x"===n?s.scrollX:s.scrollY)!==e}if("x"===n){const t=e.scrollLeft;return e.scrollLeft+=r,e.scrollLeft!==t}const i=e.scrollTop;return e.scrollTop+=r,e.scrollTop!==i},m=(e,t)=>{let n=i,o=c;if(null===e)n=Math.min(Math.max(n,1),s.innerWidth-1);else{const t=e.getBoundingClientRect();n=Math.min(Math.max(n,t.left+1),t.right-1)}if(null===t)o=Math.min(Math.max(o,1),s.innerHeight-1);else{const e=t.getBoundingClientRect();o=Math.min(Math.max(o,e.top+1),e.bottom-1)}return[n,o]},p=()=>{const e=f("x");if(null!==e){const t=e.getBoundingClientRect();if(0!==dn(i,t.left,t.right))return!0}const t=f("y"),n=null===t?0:t.getBoundingClientRect().top,o=null===t?s.innerHeight:t.getBoundingClientRect().bottom;return 0!==dn(c,n,o)},C=()=>{if(a=null,!l.isSelecting)return;const e=f("x"),t=f("y"),n=null!==e&&g(e,i,"x"),o=g(t,c,"y");if(n||o){const[n,o]=m(e,t),l=h(n,o);l&&d(l,!1),a=s.requestAnimationFrame(C)}},_=()=>{null===a&&"touch"!==l.pointerType&&p()&&(a=s.requestAnimationFrame(C))},S=()=>{u()},b=e=>{if(!(e=>!(1&~e.buttons))(e)&&l.isSelecting)return void u();const t=I(e);if(!U(t))return;i=e.clientX,c=e.clientY;let n=null;const r=!(ge||o.contains(t));n=r?Tn(o,t):h(e.clientX,e.clientY),n&&d(n,r),_()};s.addEventListener("pointerup",S,l.listenerOptions),s.addEventListener("pointermove",b,l.listenerOptions)};l.pointerType=t.pointerType;const c=un(l.tableNodeKey),a=ne();if(ge&&t.shiftKey&&$n(a,c)&&(d(a)||tn(a))){const e=a.anchor.getNode(),o=pn(c,a.anchor.getNode());if(o)l.$setAnchorCellForSelection(Yn(l,o)),l.$setFocusCellForSelection(n),In(t);else{(c.isBefore(e)?c.selectStart():c.selectEnd()).anchor.set(a.anchor.key,a.anchor.offset,a.anchor.type)}}else"touch"!==t.pointerType&&l.$setAnchorCellForSelection(n);i(n)}(e,o,s,l,n,t)})})})}function wn(e,t,n,o,l){const r=n.getRootElement(),s=mn(n);null!==r&&null!==s||at(246);const i=new cn(n,e.getKey()),c=gn(e,t);!function(e,t){null!==xn(e)&&at(205);e[an]=t}(c,i),i.listenersToRemove.add(()=>function(e,t){xn(e)===t&&delete e[an]}(c,i));i.listenersToRemove.add(O(c,"mousedown",e=>{const t=I(e);if(e.detail>=3&&U(t)){null!==vn(t)&&e.preventDefault()}},i.listenerOptions));for(const[t,o]of Cn)i.listenersToRemove.add(n.registerCommand(t,t=>Dn(n,t,o,e,i,l),E));i.listenersToRemove.add(n.registerCommand($,t=>{const n=h();if(tn(n)){const o=pn(e,n.focus.getNode());if(null!==o)return In(t),o.selectEnd(),!0}return!1},E));const a=t=>()=>{const n=h();if(!$n(n,e))return!1;if(tn(n))return i.$clearText(),!0;if(d(n)){if(!it(pn(e,n.anchor.getNode())))return!1;const o=n.anchor.getNode(),l=n.focus.getNode(),r=e.isParentOf(o),s=e.isParentOf(l);if(r&&!s||s&&!r)return i.$clearText(),!0;const c=f(n.anchor.getNode(),e=>C(e)),a=c&&f(c,e=>C(e)&&it(e.getParent()));if(!C(a)||!C(c))return!1;if(t===z&&null===a.getPreviousSibling())return!0}return!1};for(const e of _n)i.listenersToRemove.add(n.registerCommand(e,a(e),E));const g=t=>{const n=h();if(!tn(n)&&!d(n))return!1;const o=e.isParentOf(n.anchor.getNode());if(o!==e.isParentOf(n.focus.getNode())){const t=o?"anchor":"focus",l=o?"focus":"anchor",{key:r,offset:s,type:i}=n[l];return e[n[t].isBefore(n[l])?"selectPrevious":"selectNext"]()[l].set(r,s,i),!1}return!!$n(n,e)&&(!!tn(n)&&(t&&(t.preventDefault(),t.stopPropagation()),i.$clearText(),!0))};for(const e of Sn)i.listenersToRemove.add(n.registerCommand(e,g,E));i.listenersToRemove.add(n.registerCommand(P,e=>{const t=h();if(t){if(!tn(t)&&!d(t))return!1;Ze(n,qe(e,ClipboardEvent)?e:null,et(t));const o=g(e);return d(t)?(t.removeText(),!0):o}return!1},E));const m=r.ownerDocument;return i.listenersToRemove.add(O(m,"paste",t=>{if(t.defaultPrevented)return;n.read("latest",()=>{const t=h();return r.contains(m.activeElement)&&tn(t)&&$n(t,e)})&&(t.preventDefault(),n.dispatchCommand(D,t))})),i.listenersToRemove.add(O(m,"copy",t=>{if(t.defaultPrevented)return;const o=I(t);if(o===r||U(o)&&r.contains(o))return;n.read("latest",()=>{const t=h();return r.contains(X(r))&&tn(t)&&$n(t,e)})&&(t.preventDefault(),n.dispatchCommand(Y,t))})),i.listenersToRemove.add(n.registerCommand(J,t=>{const n=h();if(!$n(n,e))return!1;if(tn(n))return i.$formatCells(t),!0;if(d(n)){const e=f(n.anchor.getNode(),e=>it(e));if(!it(e))return!1}return!1},E)),i.listenersToRemove.add(n.registerCommand(q,t=>{const n=h();if(!tn(n)||!$n(n,e))return!1;const o=n.anchor.getNode(),l=n.focus.getNode();if(!it(o)||!it(l))return!1;if(function(e,t){if(tn(e)){const n=e.anchor.getNode(),o=e.focus.getNode();if(t&&n&&o){const[e]=Dt(t,n,o);return n.getKey()===e[0][0].cell.getKey()&&o.getKey()===e[e.length-1].at(-1).cell.getKey()}}return!1}(n,e))return e.setFormat(t),!0;const[r,s,i]=Dt(e,o,l),c=Math.max(s.startRow+s.cell.__rowSpan-1,i.startRow+i.cell.__rowSpan-1),a=Math.max(s.startColumn+s.cell.__colSpan-1,i.startColumn+i.cell.__colSpan-1),u=Math.min(s.startRow,i.startRow),d=Math.min(s.startColumn,i.startColumn),f=new Set;for(let e=u;e<=c;e++)for(let n=d;n<=a;n++){const o=r[e][n].cell;if(f.has(o))continue;f.add(o),o.setFormat(t);const l=o.getChildren();for(let e=0;e<l.length;e++){const n=l[e];C(n)&&!n.isInline()&&n.setFormat(t)}}return!0},E)),i.listenersToRemove.add(n.registerCommand(j,t=>{const o=h();if(!$n(o,e))return!1;if(tn(o))return i.$clearHighlight(),!1;if(d(o)){const l=f(o.anchor.getNode(),e=>it(e));if(!it(l))return!1;if("string"==typeof t){const l=Xn(n,o,e);if(l)return Un(l,e,[u(t)]),!0}}return!1},E)),o&&i.listenersToRemove.add(n.registerCommand(V,t=>{const n=h();if(!d(n)||!n.isCollapsed()||!$n(n,e))return!1;const o=Bn(n.anchor.getNode());return!(null===o||!e.is(Ln(o)))&&(In(t),function(e,t){const n="next"===t?"getNextSibling":"getPreviousSibling",o="next"===t?"getFirstChild":"getLastChild",l=e[n]();if(C(l))return l.selectEnd();const r=f(e,ft);null===r&&at(247);for(let e=r[n]();ft(e);e=e[n]()){const t=e[o]();if(C(t))return t.selectEnd()}const s=f(r,oo);null===s&&at(248);"next"===t?s.selectNext():s.selectPrevious()}(o,t.shiftKey?"previous":"next"),!0)},E)),i.listenersToRemove.add(n.registerCommand(G,t=>e.isSelected(),E)),i.listenersToRemove.add(n.registerCommand(F,()=>{const t=h();if(!d(t)||!t.isCollapsed()||!$n(t,e))return!1;const o=Xn(n,t,e);return!!o&&(Un(o,e),!0)},E)),i}function yn(e,t){const n=h(),o=ne(),l=e.getAndClearNextFocus();if(null!==l){const{tableKey:t,focusCell:o}=l,r=e.observers.get(t);r||at(335,t);const[s]=r;if(tn(n)&&n.tableKey===s.tableNodeKey)return(o.x!==s.focusX||o.y!==s.focusY)&&(s.$setFocusCellForSelection(o),!0);if(null!==s.anchorCell&&null!==s.anchorCellNodeKey&&o.elem!==s.anchorCell.elem&&null!==s.tableSelection)return s.$setFocusCellForSelection(o,!0),!0}const r=e.getAndClearShouldCheckSelectionForTable();if(r&&d(o)&&d(n)&&n.isCollapsed()){const e=p(r);if(oo(e)){const t=n.anchor.getNode(),o=e.getFirstChild(),l=Bn(t);if(null!==l&&ft(o)){const t=o.getFirstChild();if(it(t)&&e.is(f(l,n=>n.is(e)||n.is(t))))return t.selectStart(),!0}}}tn(n)&&function(e,t){const n=mn(e),o=ne();if(!t.is(o))return;const l=un(t.tableKey),r=v(n),s=r&&te(r,e.getRootElement());if(r&&s&&s.anchorNode&&s.focusNode){const n=Q(s.focusNode),o=n&&!l.isParentOf(n),i=Q(s.anchorNode),c=i&&l.isParentOf(i);if(o&&c&&r.rangeCount>0){const n=ue(r,e);n&&(n.anchor.set(l.getKey(),t.isBackward()?l.getChildrenSize():0,"element"),r.removeAllRanges(),y(n))}}}(t,n),d(n)&&function(e,t){const n=ne(),{anchor:o,focus:l}=e,r=o.getNode(),s=l.getNode(),i=Bn(r),c=Bn(s),a=i?Ln(i):null,u=c?Ln(c):null,h=e.isBackward(),f=i&&c&&a&&u&&a.is(u),g=u&&(!a||a.isParentOf(u)),m=a&&(!u||u.isParentOf(a));if(g){const t=e.clone(),[n]=Dt(u,c,c),o=n[0][0].cell,l=n[n.length-1].at(-1).cell;t.focus.set(h?o.getKey():l.getKey(),h?0:l.getChildrenSize(),"element"),y(t)}else if(m){const t=e.clone(),[n]=Dt(a,i,i),o=n[0][0].cell,l=n[n.length-1].at(-1).cell;t.anchor.set(h?l.getKey():o.getKey(),h?l.getChildrenSize():0,"element"),y(t)}else if(f){const o=t.observers.get(a.getKey());o||at(335,a.getKey());const[l]=o;if(i.is(c)||(l.$setAnchorCellForSelection(Yn(l,i)),l.$setFocusCellForSelection(Yn(l,c),!0)),"touch"===l.pointerType&&l.isSelecting&&e.isCollapsed()&&d(n)&&n.isCollapsed()){const e=Bn(n.anchor.getNode());e&&!e.is(c)&&(l.$setAnchorCellForSelection(Yn(l,e)),l.$setFocusCellForSelection(Yn(l,c),!0),l.pointerType=null)}}}(n,e);for(const[n,o]of e.$getTableNodesAndObservers())Nn(t,n,o);return!1}function Nn(e,t,n){const o=h(),l=ne();o&&!o.is(l)&&(tn(o)||tn(l))&&n.tableSelection&&!n.tableSelection.is(l)&&(tn(o)&&o.tableKey===n.tableNodeKey?n.$updateTableTableSelection(o):!tn(o)&&tn(l)&&l.tableKey===n.tableNodeKey&&n.$updateTableTableSelection(null)),n.hasHijackedSelectionStyles&&!t.isSelected()?function(e,t){t.$enableHighlightStyle(),On(t.table,t=>{const n=t.elem;t.highlighted=!1,Hn(e,t),n.getAttribute("style")||n.removeAttribute("style")})}(e,n):!n.hasHijackedSelectionStyles&&t.isSelected()&&function(e,t){t.$disableHighlightStyle(),On(t.table,t=>{t.highlighted=!0,zn(e,t)})}(e,n)}function xn(e){return e[an]||null}function vn(e){let t=e;for(;null!=t;){const e=t.nodeName;if("TD"===e||"TH"===e){const e=t._cell;return void 0===e?null:e}t=t.parentNode}return null}function Tn(e,t){if(!e.contains(t))return null;let n=null;for(let o=t;null!=o;o=o.parentNode){if(o===e)return n;const t=o.nodeName;"TD"!==t&&"TH"!==t||(n=o._cell||null)}return null}function Rn(e,t){const n=[],o={columns:0,domRows:n,rows:0};let l=gn(e,t).querySelector("tr"),r=0,s=0;for(n.length=0;null!=l;){const e=l.nodeName;if("TD"===e||"TH"===e){const e={elem:l,hasBackgroundColor:""!==l.style.backgroundColor,highlighted:!1,x:r,y:s};l._cell=e;let t=n[s];void 0===t&&(t=n[s]=[]),t[r]=e}else{const e=l.firstChild;if(null!=e){l=e;continue}}const t=l.nextSibling;if(null!=t){r++,l=t;continue}const o=l.parentNode;if(null!=o){const e=o.nextSibling;if(null==e)break;s++,r=0,l=e}}return o.columns=r+1,o.rows=s+1,o}function Fn(e,t,n){const o=new Set(n?n.getNodes():[]);On(t,(t,n)=>{const l=t.elem;o.has(n)?(t.highlighted=!0,zn(e,t)):(t.highlighted=!1,Hn(e,t),l.getAttribute("style")||l.removeAttribute("style"))})}function On(e,t){const{domRows:n}=e;for(let e=0;e<n.length;e++){const o=n[e];if(o)for(let n=0;n<o.length;n++){const l=o[n];if(!l)continue;const r=Q(l.elem);null!==r&&t(l,r,{x:n,y:e})}}}const An=(e,t,n,o,l)=>{const r="forward"===l;switch(l){case"backward":case"forward":return n!==(r?e.table.columns-1:0)?Wn(t.getCellNodeFromCordsOrThrow(n+(r?1:-1),o,e.table),r):o!==(r?e.table.rows-1:0)?Wn(t.getCellNodeFromCordsOrThrow(r?0:e.table.columns-1,o+(r?1:-1),e.table),r):r?t.selectNext():t.selectPrevious(),!0;case"up":return 0!==o?Wn(t.getCellNodeFromCordsOrThrow(n,o-1,e.table),!1):t.selectPrevious(),!0;case"down":return o!==e.table.rows-1?Wn(t.getCellNodeFromCordsOrThrow(n,o+1,e.table),!0):t.selectNext(),!0;default:return!1}};function Kn(e,t){let n,o;if(t.startColumn===e.minColumn)n="minColumn";else{if(t.startColumn+t.cell.__colSpan-1!==e.maxColumn)return null;n="maxColumn"}if(t.startRow===e.minRow)o="minRow";else{if(t.startRow+t.cell.__rowSpan-1!==e.maxRow)return null;o="maxRow"}return[n,o]}function kn([e,t]){return["minColumn"===e?"maxColumn":"minColumn","minRow"===t?"maxRow":"minRow"]}function Mn(e,t,[n,o]){const l=t[o],r=e[l];void 0===r&&at(250,o,String(l));const s=t[n],i=r[s];return void 0===i&&at(250,n,String(s)),i}function En(e,t,n,o,l){const r=Xt(t,n,o),s=function(e,t){const{minColumn:n,maxColumn:o,minRow:l,maxRow:r}=t;let s=1,i=1,c=1,a=1;const u=e[l],h=e[r];for(let e=n;e<=o;e++)s=Math.max(s,u[e].cell.__rowSpan),a=Math.max(a,h[e].cell.__rowSpan);for(let t=l;t<=r;t++)i=Math.max(i,e[t][n].cell.__colSpan),c=Math.max(c,e[t][o].cell.__colSpan);return{bottomSpan:a,leftSpan:i,rightSpan:c,topSpan:s}}(t,r),{topSpan:i,leftSpan:c,bottomSpan:a,rightSpan:u}=s,h=function(e,t){const n=Kn(e,t);return null===n&&at(249,t.cell.getKey()),n}(r,n),[d,f]=kn(h);let g=r[d],m=r[f];"forward"===l?g+="maxColumn"===d?1:c:"backward"===l?g-="minColumn"===d?1:u:"down"===l?m+="maxRow"===f?1:i:"up"===l&&(m-="minRow"===f?1:a);const p=t[m];if(void 0===p)return!1;const C=p[g];if(void 0===C)return!1;const[_,S]=function(e,t,n){const o=Xt(e,t,n),l=Kn(o,t);if(l)return[Mn(e,o,l),Mn(e,o,kn(l))];const r=Kn(o,n);if(r)return[Mn(e,o,kn(r)),Mn(e,o,r)];const s=["minColumn","minRow"];return[Mn(e,o,s),Mn(e,o,kn(s))]}(t,n,C),b=Yn(e,_.cell),w=Yn(e,S.cell);return e.$setAnchorCellForSelection(b),e.$setFocusCellForSelection(w,!0),!0}function $n(e,t){if(d(e)||tn(e)){const n=t.isParentOf(e.anchor.getNode()),o=t.isParentOf(e.focus.getNode());return n&&o}return!1}function Wn(e,t){t?e.selectStart():e.selectEnd()}function zn(e,t){const o=t.elem,l=e._config.theme;it(Q(o))||at(131),n(o,l.tableCellSelected)}function Hn(e,t){const n=t.elem;it(Q(n))||at(131);const o=e._config.theme;x(n,o.tableCellSelected)}function Bn(e){const t=f(e,it);return it(t)?t:null}function Ln(e){const t=f(e,oo);return oo(t)?t:null}function Pn(e,t,n,o,l,r,s){const i=oe(n.focus,l?"previous":"next");if(le(i))return!1;let c=i;for(const e of re(i).iterNodeCarets("shadowRoot")){if(!se(e)||!C(e.origin))return!1;c=e}const a=c.getParentAtCaret();if(!it(a))return!1;const u=a,h=function(e){for(const t of re(e).iterNodeCarets("root")){const{origin:n}=t;if(it(n)){if(he(t))return de(n,e.direction)}else if(!ft(n))break}return null}(ie(u,c.direction)),d=f(u,oo);if(!d||!d.is(r))return!1;const g=e.getElementByKey(u.getKey()),m=vn(g);if(!g||!m)return!1;const p=eo(e,d);if(s.table=p,h)if("extend"===o){const t=vn(e.getElementByKey(h.origin.getKey()));if(!t)return!1;s.$setAnchorCellForSelection(m),s.$setFocusCellForSelection(t,!0)}else{const e=ae(h);ce(n.anchor,e),ce(n.focus,e)}else if("extend"===o)s.$setAnchorCellForSelection(m),s.$setFocusCellForSelection(m,!0);else{const e=function(e){const t=fe(e);return he(t)?ae(t):e}(ie(d,i.direction));ce(n.anchor,e),ce(n.focus,e)}return In(t),!0}function Dn(e,t,n,o,l,s){if(("up"===n||"down"===n)&&function(e){const t=e.getRootElement();if(!t)return!1;return t.hasAttribute("aria-controls")&&"typeahead-menu"===t.getAttribute("aria-controls")}(e))return!1;const i=h();if(!$n(i,o)){if(d(i)){if("backward"===n){if(i.focus.offset>0)return!1;const e=function(e){for(let t=e,n=e;null!==n;t=n,n=n.getParent())if(C(n)){if(n!==t&&n.getFirstChild()!==t)return null;if(!n.isInline())return n}return null}(i.focus.getNode());if(!e)return!1;const n=e.getPreviousSibling();return!!oo(n)&&(In(t),t.shiftKey?i.focus.set(n.getParentOrThrow().getKey(),n.getIndexWithinParent(),"element"):n.selectEnd(),!0)}if(t.shiftKey&&("up"===n||"down"===n)){const e=i.focus.getNode();if(!i.isCollapsed()&&("up"===n&&!i.isBackward()||"down"===n&&i.isBackward())){let l=f(e,e=>oo(e));if(it(l)&&(l=f(l,oo)),l!==o)return!1;if(!l)return!1;const s="down"===n?l.getNextSibling():l.getPreviousSibling();if(!s)return!1;let c=0;"up"===n&&C(s)&&(c=s.getChildrenSize());let a=s;if("up"===n&&C(s)){const e=s.getLastChild();a=e||s,c=r(a)?a.getTextContentSize():0}const u=i.clone();return u.focus.set(a.getKey(),c,r(a)?"text":"element"),y(u),In(t),!0}if(Z(e)){const e="up"===n?i.getNodes()[i