UNPKG

@lexical/table

Version:

This package provides the Table feature for Lexical.

9 lines (8 loc) • 72.1 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. * */ "use strict";var e=require("lexical"),t=require("@lexical/extension"),n=require("@lexical/html"),o=require("@lexical/utils"),r=require("@lexical/clipboard");const l=/^(\d+(?:\.\d+)?)px$/,s={BOTH:3,COLUMN:2,NO_STATUS:0,ROW:1};class i extends e.ElementNode{__colSpan;__rowSpan;__headerState;__width;__backgroundColor;__verticalAlign;static getType(){return"tablecell"}static clone(e){return new i(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:c,priority:0}),th:e=>({conversion:c,priority:0})}}static importJSON(e){return u().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=s.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(t){const n=e.$getDocument().createElement(this.getTag());return this.__width&&(n.style.width=`${this.__width}px`),this.__colSpan>1&&(n.colSpan=this.__colSpan),this.__rowSpan>1&&(n.rowSpan=this.__rowSpan),null!==this.__backgroundColor&&(n.style.backgroundColor=this.__backgroundColor),a(this.__verticalAlign)&&(n.style.verticalAlign=this.__verticalAlign),e.addClassNamesToElement(n,t.theme.tableCell,this.hasHeader()&&t.theme.tableCellHeader),n}exportDOM(t){const n=super.exportDOM(t);if(e.isHTMLElement(n.element)){const e=n.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 n}exportJSON(){return{...super.exportJSON(),...a(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=s.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!==s.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 a(e){return"middle"===e||"bottom"===e}function c(t){const n=t,o=t.nodeName.toLowerCase();let r;l.test(n.style.width)&&(r=parseFloat(n.style.width));let i=s.NO_STATUS;if("th"===o){const t=n.getAttribute("scope");if("col"===t)i=s.COLUMN;else if("row"===t)i=s.ROW;else{const t=n.parentElement,o=e.isHTMLElement(t)&&"tr"===t.nodeName.toLowerCase()&&e.isHTMLElement(t.parentElement)&&("thead"===t.parentElement.nodeName.toLowerCase()||0===t.rowIndex),r=0===n.cellIndex;o&&(i|=s.ROW),r&&(i|=s.COLUMN),i===s.NO_STATUS&&(i=s.ROW)}}const c=u(i,n.colSpan,r);c.__rowSpan=n.rowSpan;const d=n.style.backgroundColor;""!==d&&(c.__backgroundColor=d);const h=n.style.verticalAlign;a(h)&&(c.__verticalAlign=h);const g=n.style,f=(g&&g.textDecoration||"").split(" "),m="700"===g.fontWeight||"bold"===g.fontWeight,p=f.includes("line-through"),C="italic"===g.fontStyle,_=f.includes("underline"),S=g.color;return{after:t=>{const n=[];let o=null;const r=()=>{if(o){const t=o.getFirstChild();e.$isLineBreakNode(t)&&1===o.getChildrenSize()&&t.remove()}};for(const l of t)if(e.$isInlineElementOrDecoratorNode(l)||e.$isTextNode(l)||e.$isLineBreakNode(l)){if(e.$isTextNode(l)&&(m&&l.toggleFormat("bold"),p&&l.toggleFormat("strikethrough"),C&&l.toggleFormat("italic"),_&&l.toggleFormat("underline"),S)){const e=l.getStyle();e.includes("color:")||l.setStyle(e+`color: ${S};`)}o?o.append(l):(o=e.$createParagraphNode().append(l),n.push(o))}else n.push(l),r(),o=null;return r(),0===n.length&&n.push(e.$createParagraphNode()),n},node:c}}function u(t=s.NO_STATUS,n=1,o){return e.$applyNodeReplacement(new i(t,n,o))}function d(e){return e instanceof i}const h=/* @__PURE__ */e.createCommand("INSERT_TABLE_COMMAND");function g(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 f extends e.ElementNode{__height;static getType(){return"tablerow"}static clone(e){return new f(e.__height,e.__key)}afterCloneFrom(e){super.afterCloneFrom(e),this.__height=e.__height}static importDOM(){return{tr:e=>({conversion:m,priority:0})}}static importJSON(e){return p().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(t){const n=e.$getDocument().createElement("tr");return this.__height&&(n.style.height=`${this.__height}px`),e.addClassNamesToElement(n,t.theme.tableRow),n}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 m(e){const t=e;let n;return l.test(t.style.height)&&(n=parseFloat(t.style.height)),{after:e=>o.$descendantsMatching(e,d),node:p(n)}}function p(t){return e.$applyNodeReplacement(new f(t))}function C(e){return e instanceof f}function _(t,n,o=!0){const r=qe();for(let l=0;l<t;l++){const t=p();for(let r=0;r<n;r++){let n=s.NO_STATUS;"object"==typeof o?(0===l&&o.rows&&(n|=s.ROW),0===r&&o.columns&&(n|=s.COLUMN)):o&&(0===l&&(n|=s.ROW),0===r&&(n|=s.COLUMN));const i=u(n),a=e.$createParagraphNode();a.append(e.$createTextNode()),i.append(a),t.append(i)}r.append(t)}return r}function S(t){const n=e.$findMatchingParent(t,e=>C(e));if(C(n))return n;throw new Error("Expected table cell to be inside of table row.")}function N(t){const n=e.$findMatchingParent(t,e=>Ge(e));if(Ge(n))return n;throw new Error("Expected table cell to be inside of table.")}function b(e,t){const n=N(e),{x:o,y:r}=n.getCordsFromCellNode(e,t);return{above:n.getCellNodeFromCords(o,r-1,t),below:n.getCellNodeFromCords(o,r+1,t),left:n.getCellNodeFromCords(o-1,r,t),right:n.getCellNodeFromCords(o+1,r,t)}}const w=(e,t)=>e===s.BOTH||e===t?t:s.NO_STATUS;function T(t=!0){const n=e.$getSelection();e.$isRangeSelection(n)||q(n)||g(188);const o=n.anchor.getNode(),r=n.focus.getNode(),[l]=L(o),[s,,i]=L(r),[,a,c]=H(i,s,l),{startRow:u}=c,{startRow:d}=a;return t?y(u+l.__rowSpan>d+s.__rowSpan?l:s,!0):y(d<u?s:l,!1)}const $=T;function y(t,n=!0){const[,,o]=L(t),[r,l]=H(o,t,t),i=r[0].length,{startRow:a}=l;let c=null;if(n){const n=a+t.__rowSpan-1,l=r[n],d=p();for(let t=0;t<i;t++){const{cell:o,startRow:r}=l[t];if(r+o.__rowSpan-1<=n){const n=l[t].cell.__headerState,o=w(n,s.COLUMN);d.append(u(o).append(e.$createParagraphNode()))}else o.setRowSpan(o.__rowSpan+1)}const h=o.getChildAtIndex(n);C(h)||g(256),h.insertAfter(d),c=d}else{const t=a,n=r[t],l=p();for(let o=0;o<i;o++){const{cell:r,startRow:i}=n[o];if(i===t){const t=n[o].cell.__headerState,r=w(t,s.COLUMN);l.append(u(r).append(e.$createParagraphNode()))}else r.setRowSpan(r.__rowSpan+1)}const d=o.getChildAtIndex(t);C(d)||g(257),d.insertBefore(l),c=l}return c}function R(t=!0){const n=e.$getSelection();e.$isRangeSelection(n)||q(n)||g(188);const o=n.anchor.getNode(),r=n.focus.getNode(),[l]=L(o),[s,,i]=L(r),[,a,c]=H(i,s,l),{startColumn:u}=c,{startColumn:d}=a;return t?M(u+l.__colSpan>d+s.__colSpan?l:s,!0):M(d<u?s:l,!1)}const x=R;function M(t,n=!0,o=!0){const[,,r]=L(t),[l,i]=H(r,t,t),a=l.length,{startColumn:c}=i,d=n?c+t.__colSpan-1:c-1,h=r.getFirstChild();C(h)||g(120);let f=null;function m(t=s.NO_STATUS){const n=u(t).append(e.$createParagraphNode());return null===f&&(f=n),n}let p=h;e:for(let e=0;e<a;e++){if(0!==e){const e=p.getNextSibling();C(e)||g(121),p=e}const t=l[e],n=t[d<0?0:d].cell.__headerState,o=w(n,s.ROW);if(d<0){P(p,m(o));continue}const{cell:r,startColumn:i,startRow:a}=t[d];if(i+r.__colSpan-1<=d){let n=r,l=a,s=d;for(;l!==e&&n.__rowSpan>1;){if(s-=r.__colSpan,!(s>=0)){p.append(m(o));continue e}{const{cell:e,startRow:o}=t[s];n=e,l=o}}n.insertAfter(m(o))}else r.setColSpan(r.__colSpan+1)}null!==f&&o&&F(f);const _=r.getColWidths();if(_){const e=[..._],t=d<0?0:d,n=e[t];e.splice(t,0,n),r.setColWidths(e)}return f}function O(){const t=e.$getSelection();e.$isRangeSelection(t)||q(t)||g(188);const[n,o]=t.isBackward()?[t.focus.getNode(),t.anchor.getNode()]:[t.anchor.getNode(),t.focus.getNode()],[r,,l]=L(n),[s]=L(o),[i,a,c]=H(l,r,s),{startRow:u}=a,{startRow:d}=c,h=d+s.__rowSpan-1;if(i.length===h-u+1)return void l.remove();const f=i[0].length,m=i[h+1],p=l.getChildAtIndex(h+1);for(let e=h;e>=u;e--){for(let t=f-1;t>=0;t--){const{cell:n,startRow:o,startColumn:r}=i[e][t];if(r===t){if(o<u||o+n.__rowSpan-1>h){const e=Math.max(o,u),t=Math.min(n.__rowSpan+o-1,h),r=e<=t?t-e+1:0;n.setRowSpan(n.__rowSpan-r)}if(o>=u&&o+n.__rowSpan-1>h&&e===h){C(p)||g(387);let o=null;for(let n=0;n<t;n++){const t=m[n],r=t.cell;t.startRow===e+1&&(o=r),r.__colSpan>1&&(n+=r.__colSpan-1)}null===o?P(p,n):o.insertAfter(n)}}}const t=l.getChildAtIndex(e);C(t)||g(206,String(e)),t.remove()}if(void 0!==m){const{cell:e}=m[0];F(e)}else{const e=i[u-1],{cell:t}=e[0];F(t)}}const E=O;function A(){const t=e.$getSelection();e.$isRangeSelection(t)||q(t)||g(188);const n=t.anchor.getNode(),o=t.focus.getNode(),[r,,l]=L(n),[s]=L(o),[i,a,c]=H(l,r,s),{startColumn:u}=a,{startRow:d,startColumn:h}=c,f=Math.min(u,h),m=Math.max(u+r.__colSpan-1,h+s.__colSpan-1),p=m-f+1;if(i[0].length===m-f+1)return l.selectPrevious(),void l.remove();const C=i.length;for(let e=0;e<C;e++)for(let t=f;t<=m;t++){const{cell:n,startColumn:o}=i[e][t];if(o<f){if(t===f){const e=f-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 _=i[d],S=u>h?_[u+r.__colSpan]:_[h+s.__colSpan];if(void 0!==S){const{cell:e}=S;F(e)}else{const e=h<u?_[h-1]:_[u-1],{cell:t}=e;F(t)}const N=l.getColWidths();if(N){const e=[...N];e.splice(f,p),l.setColWidths(e)}}const v=A;function F(e){const t=e.getFirstDescendant();null==t?e.selectStart():t.getParentOrThrow().selectStart()}function P(e,t){const n=e.getFirstChild();null!==n?n.insertBefore(t):e.append(t)}function D(t){if(0===t.length)return null;const n=N(t[0]),[o]=k(n,null,null);let r=1/0,l=-1/0,s=1/0,i=-1/0;const a=new Set;for(const e of o)for(const n of e){if(!n||!n.cell)continue;const e=n.cell.getKey();if(!a.has(e)&&t.some(e=>e.is(n.cell))){a.add(e);const t=n.startRow,o=n.startColumn,c=n.cell.__rowSpan||1,u=n.cell.__colSpan||1;r=Math.min(r,t),l=Math.max(l,t+c-1),s=Math.min(s,o),i=Math.max(i,o+u-1)}}if(r===1/0||s===1/0)return null;const c=l-r+1,u=i-s+1,d=o[r][s];if(!d.cell)return null;const h=d.cell;h.setColSpan(u),h.setRowSpan(c);const g=new Set([h.getKey()]);for(let e=r;e<=l;e++)for(let t=s;t<=i;t++){const n=o[e][t];if(!n.cell)continue;const r=n.cell,l=r.getKey();if(!g.has(l)){g.add(l);K(r)||h.append(...r.getChildren()),r.remove()}}return 0===h.getChildrenSize()&&h.append(e.$createParagraphNode()),h}function K(t){if(1!==t.getChildrenSize())return!1;const n=t.getFirstChildOrThrow();return!(!e.$isParagraphNode(n)||!n.isEmpty())}function I(t){const[n,o,r]=L(t),l=n.__colSpan,i=n.__rowSpan;if(1===l&&1===i)return;const[a,c]=H(r,n,n),{startColumn:d,startRow:h}=c,f=n.__headerState&s.COLUMN,m=Array.from({length:l},(e,t)=>{let n=f;for(let e=0;0!==n&&e<a.length;e++)n&=a[e][t+d].cell.__headerState;return n}),p=n.__headerState&s.ROW,_=Array.from({length:i},(e,t)=>{let n=p;for(let e=0;0!==n&&e<a[0].length;e++)n&=a[t+h][e].cell.__headerState;return n});if(l>1){for(let t=1;t<l;t++)n.insertAfter(u(m[t]|_[0]).append(e.$createParagraphNode()));n.setColSpan(1)}if(i>1){let t;for(let n=1;n<i;n++){const r=h+n,s=a[r];t=(t||o).getNextSibling(),C(t)||g(125);let i=null;for(let e=0;e<d;e++){const t=s[e],n=t.cell;t.startRow===r&&(i=n),n.__colSpan>1&&(e+=n.__colSpan-1)}if(null===i)for(let o=l-1;o>=0;o--)P(t,u(m[o]|_[n]).append(e.$createParagraphNode()));else for(let t=l-1;t>=0;t--)i.insertAfter(u(m[t]|_[n]).append(e.$createParagraphNode()))}n.setRowSpan(1)}}function H(e,t,n){const[o,r,l]=k(e,t,n);return null===r&&g(207),null===l&&g(208),[o,r,l]}function k(e,t,n){const o=[];let r=null,l=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];C(o)||g(209);const a=s(e);for(let c=o.getFirstChild(),u=0;null!=c;c=c.getNextSibling()){for(d(c)||g(147);void 0!==a[u];)u++;const o={cell:c,startColumn:u,startRow:e},{__rowSpan:h,__colSpan:f}=c;for(let t=0;t<h&&!(e+t>=i.length);t++){const n=s(e+t);for(let e=0;e<f;e++)n[u+e]=o}null!==t&&null===r&&t.is(c)&&(r=o),null!==n&&null===l&&n.is(c)&&(l=o)}}return[o,r,l]}function L(t){let n;if(t instanceof i)n=t;else if("__type"in t){const o=e.$findMatchingParent(t,d);d(o)||g(148),n=o}else{const o=e.$findMatchingParent(t.getNode(),d);d(o)||g(148),n=o}const o=n.getParent();C(o)||g(149);const r=o.getParent();return Ge(r)||g(210),[n,o,r]}function W(e,t,n){let o,r=Math.min(t.startColumn,n.startColumn),l=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 a=e[t][n];if(!a)continue;const c=a.startColumn+a.cell.__colSpan-1,u=a.startRow+a.cell.__rowSpan-1,d=a.startColumn<=s&&c>=r,h=a.startRow<=i&&u>=l;if(d&&h){const e=Math.min(r,a.startColumn),t=Math.max(s,c),n=Math.min(l,a.startRow),d=Math.max(i,u);e===r&&t===s&&n===l&&d===i||(r=e,s=t,l=n,i=d,o=!0)}}}while(o);return{maxColumn:s,maxRow:i,minColumn:r,minRow:l}}function B(e){const t=e.getChildren();let n=null;for(const e of t){if(!C(e))return!1;if(null===n&&(n=e.getChildrenSize()),e.getChildrenSize()!==n)return!1;const t=e.getChildren();for(const e of t)if(!d(e)||1!==e.getRowSpan()||1!==e.getColSpan())return!1}return(n||0)>0}function z(e){const[t,,n]=L(e),o=n.getChildren().filter(C),r=o.length,l=o[0].getChildren().length,s=new Array(r);for(let e=0;e<r;e++)s[e]=new Array(l);for(let e=0;e<r;e++){const n=o[e].getChildren().filter(d);let r=0;for(let o=0;o<n.length;o++){for(;s[e][r];)r++;const l=n[o],i=l.__rowSpan||1,a=l.__colSpan||1;for(let t=0;t<i;t++)for(let n=0;n<a;n++)s[e+t][r+n]=l;if(t===l)return{colSpan:a,columnIndex:r,rowIndex:e,rowSpan:i};r+=a}}return null}function Y(t,n){const o=n.getStartEndPoints(),r=q(n);if(null===o)return!1;const[l,s]=o,[i,a,c]=L(l),u=e.$findMatchingParent(s.getNode(),e=>d(e));if(!(d(i)&&d(u)&&C(a)&&Ge(c)))return!1;const[h,g,f]=H(c,i,u),[m]=k(t,null,null),p=h.length,_=p>0?h[0].length:0;let S=g.startRow,N=g.startColumn,b=m.length,w=b>0?m[0].length:0;if(r){const e=W(h,g,f),t=e.maxRow-e.minRow+1,n=e.maxColumn-e.minColumn+1;S=e.minRow,N=e.minColumn,b=Math.min(b,t),w=Math.min(w,n)}let T=!1;const $=Math.min(p,S+b)-1,R=Math.min(_,N+w)-1,x=new Set;for(let e=S;e<=$;e++)for(let t=N;t<=R;t++){const n=h[e][t];x.has(n.cell.getKey())||(1===n.cell.__rowSpan&&1===n.cell.__colSpan||(I(n.cell),x.add(n.cell.getKey()),T=!0))}let[O]=k(c.getWritable(),null,null);const E=b-p+S;for(let e=0;e<E;e++){y(O[p-1][0].cell)}const A=w-_+N;for(let e=0;e<A;e++){M(O[0][_-1].cell,!0,!1)}[O]=k(c.getWritable(),null,null);for(let t=S;t<S+b;t++)for(let n=N;n<N+w;n++){const o=t-S,r=n-N,l=m[o][r];if(l.startRow!==o||l.startColumn!==r)continue;const s=l.cell;if(1!==s.__rowSpan||1!==s.__colSpan){const e=[],o=Math.min(t+s.__rowSpan,S+b)-1,r=Math.min(n+s.__colSpan,N+w)-1;for(let l=t;l<=o;l++)for(let t=n;t<=r;t++){const n=O[l][t];e.push(n.cell)}D(e),T=!0}const{cell:i}=O[t][n],a=s.getBackgroundColor();null!=a&&i.setBackgroundColor(a);const c=i.getChildren();s.getChildren().forEach(t=>{if(e.$isTextNode(t)){e.$createParagraphNode().append(t),i.append(t)}else i.append(t)}),c.forEach(e=>e.remove())}if(r&&T){const[e]=k(c.getWritable(),null,null);e[g.startRow][g.startColumn].cell.selectEnd()}return!0}function U(t){const[[n,o,r,l],[s,i,a,c]]=["anchor","focus"].map(n=>{const o=t[n].getNode(),r=e.$findMatchingParent(o,d);d(r)||g(238,n,o.getKey(),o.getType());const l=r.getParent();C(l)||g(239,n);const s=l.getParent();return Ge(s)||g(240,n),[o,r,l,s]});return l.is(c)||g(241),{anchorCell:o,anchorNode:n,anchorRow:r,anchorTable:l,focusCell:i,focusNode:s,focusRow:a,focusTable:c}}class X{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 t=e.$getNodeByKey(this.tableKey),n=e.$getNodeByKey(this.anchor.key),o=e.$getNodeByKey(this.focus.key);return null!==t&&null!==n&&null!==o}isBackward(){return this.focus.isBefore(this.anchor)}getCachedNodes(){return this._cachedNodes}setCachedNodes(e){this._cachedNodes=e}is(e){return q(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 X(this.tableKey,e.$createPoint(this.anchor.key,this.anchor.offset,this.anchor.type),e.$createPoint(this.focus.key,this.focus.offset,this.focus.type))}isCollapsed(){return!1}extract(){return this.getNodes()}insertRawText(t){if(""===t)return;const n=(t.endsWith("\n")?t.slice(0,-1):t).split("\n").map(e=>e.split("\t")),o=qe();for(const t of n){const n=p();for(const o of t){const t=u(s.NO_STATUS),r=e.$createParagraphNode();o&&r.append(e.$createTextNode(o)),t.append(r),n.append(t)}o.append(n)}const{anchorCell:r}=U(this);Y(o,r.select(0,r.getChildrenSize()))}insertText(){}hasFormat(t){let n=0;this.getNodes().filter(d).forEach(t=>{const o=t.getFirstChild();e.$isParagraphNode(o)&&(n|=o.getTextFormat())});const o=e.TEXT_TYPE_TO_FORMAT[t];return 0!==(n&o)}insertNodes(t){const n=this.focus.getNode();e.$isElementNode(n)||g(151);e.$normalizeSelection__EXPERIMENTAL(n.select(0,n.getChildrenSize())).insertNodes(t)}getShape(){const{anchorCell:e,focusCell:t}=U(this),n=z(e);null===n&&g(153);const o=z(t);null===o&&g(155);const r=Math.min(n.columnIndex,o.columnIndex),l=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(r,l),fromY:Math.min(s,i),toX:Math.max(r,l),toY:Math.max(s,i)}}getNodes(){if(!this.isValid())return[];const t=this._cachedNodes;if(null!==t)return t;const{anchorTable:n,anchorCell:o,focusCell:r}=U(this),l=r.getParents()[1];if(l!==n){if(n.isParentOf(r)){const e=l.getParent();null==e&&g(159),this.set(this.tableKey,r.getKey(),e.getKey())}else{const e=n.getParent();null==e&&g(158),this.set(this.tableKey,e.getKey(),r.getKey())}return this.getNodes()}const[s,i,a]=H(n,o,r),{minColumn:c,maxColumn:u,minRow:d,maxRow:h}=W(s,i,a),f=new Map([[n.getKey(),n]]);let m=null;for(let e=d;e<=h;e++)for(let t=c;t<=u;t++){const{cell:n}=s[e][t],o=n.getParent();C(o)||g(160),o!==m&&(f.set(o.getKey(),o),m=o),f.has(n.getKey())||j(n,e=>{f.set(e.getKey(),e)})}const p=Array.from(f.values());return e.isCurrentlyReadOnlyMode()||(this._cachedNodes=p),p}getTextContent(){const e=this.getNodes().filter(e=>d(e));let t="";for(let n=0;n<e.length;n++){const o=e[n],r=o.__parent,l=(e[n+1]||{}).__parent;t+=o.getTextContent()+(l!==r?"\n":"\t")}return t}}function q(e){return e instanceof X}function G(){const t=e.$createPoint("root",0,"element"),n=e.$createPoint("root",0,"element");return new X("root",t,n)}function J(t,n,o){t.getKey(),n.getKey(),o.getKey();const r=e.$getSelection(),l=q(r)?r.clone():G();return l.set(t.getKey(),n.getKey(),o.getKey()),l}function j(t,n){const o=[[t]];for(let t=o.at(-1);void 0!==t&&o.length>0;t=o.at(-1)){const r=t.pop();void 0===r?o.pop():!1!==n(r)&&e.$isElementNode(r)&&o.push(r.getChildren())}}function V(t,n=e.$getEditor()){const o=e.$getNodeByKey(t);Ge(o)||g(231,t);const r=le(o,n.getElementByKey(t));return null===r&&g(232,t),{tableElement:r,tableNode:o}}class Q{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 t=[];for(const[n,[o]]of Array.from(this.observers.entries())){const r=e.$getNodeByKey(n);Ge(r)?t.push([r,o]):this.removeObserver(n)}return t}}class Z{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 V(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=_e(n,o)})});this.editor.read("latest",()=>{const{tableNode:t,tableElement:n}=this.$lookup();this.table=_e(t,n),e.observe(n,{attributes:!0,childList:!0,subtree:!0})})}$clearHighlight(t=!0){const n=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:o,tableElement:r}=this.$lookup();Se(n,_e(o,r),null),t&&null!==e.$getSelection()&&(e.$setSelection(null),n.dispatchCommand(e.SELECTION_CHANGE_COMMAND,void 0))}$enableHighlightStyle(){const t=this.editor,{tableElement:n}=this.$lookup();e.removeClassNamesFromElement(n,t._config.theme.tableSelection),n.classList.remove("disable-selection"),this.hasHijackedSelectionStyles=!1}$disableHighlightStyle(){const{tableElement:t}=this.$lookup();e.addClassNamesToElement(t,this.editor._config.theme.tableSelection),this.hasHijackedSelectionStyles=!0}$updateTableTableSelection(e){if(null!==e){e.tableKey!==this.tableNodeKey&&g(233,e.tableKey,this.tableNodeKey);const t=this.editor;this.tableSelection=e,this.isHighlightingCells=!0,this.$disableHighlightStyle(),this.updateDOMSelection(),Se(t,this.table,this.tableSelection)}else this.$clearHighlight()}updateDOMSelection(){if(null!==this.anchorCell&&null!==this.focusCell){const t=e.getDOMSelection(this.editor._window);t&&t.rangeCount>0&&t.removeAllRanges()}}$setFocusCellForSelection(t,n=!1){const o=this.editor,{tableNode:r}=this.$lookup(),l=t.x,s=t.y;if(this.focusCell=t,!this.isHighlightingCells){(n||this.anchorX!==l||this.anchorY!==s||null!=this.tableSelection&&null!=this.anchorCellNodeKey)&&(this.isHighlightingCells=!0,this.$disableHighlightStyle())}if(-1!==this.focusX&&-1!==this.focusY&&l===this.focusX&&s===this.focusY)return!1;if(this.focusX=l,this.focusY=s,this.isHighlightingCells){const i=He(r,t.elem);if(null!=this.tableSelection&&null!=this.anchorCellNodeKey){let t=i;if(null===t&&n&&(t=r.getCellNodeFromCords(l,s,this.table)),null!==t){const n=this.$getAnchorTableCellOrThrow();return this.focusCellNodeKey=t.getKey(),this.tableSelection=J(r,n,t),e.$setSelection(this.tableSelection),o.dispatchCommand(e.SELECTION_CHANGE_COMMAND,void 0),Se(o,this.table,this.tableSelection),!0}}}return!1}$getAnchorTableCell(){const t=this.anchorCellNodeKey?e.$getNodeByKey(this.anchorCellNodeKey):null;return d(t)?t:null}$getAnchorTableCellOrThrow(){const e=this.$getAnchorTableCell();return null===e&&g(234),e}$getFocusTableCell(){const t=this.focusCellNodeKey?e.$getNodeByKey(this.focusCellNodeKey):null;return d(t)?t:null}$getFocusTableCellOrThrow(){const e=this.$getFocusTableCell();return null===e&&g(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=He(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=J(t,n,n),this.anchorCellNodeKey=e}}$formatCells(t){const n=e.$getSelection();q(n)||g(236);const o=e.$createRangeSelection(),r=o.anchor,l=o.focus,s=n.getNodes().filter(d);s.length>0||g(237);const i=s[0].getFirstChild(),a=e.$isParagraphNode(i)?i.getFormatFlags(t,null):null;s.forEach(e=>{r.set(e.getKey(),0,"element"),l.set(e.getKey(),e.getChildrenSize(),"element"),o.formatText(t,a)}),e.$setSelection(n),this.editor.dispatchCommand(e.SELECTION_CHANGE_COMMAND,void 0)}$clearText(){const{editor:t}=this,n=e.$getNodeByKey(this.tableNodeKey);if(!Ge(n))throw new Error("Expected TableNode.");const o=e.$getSelection();q(o)||g(253);const r=o.getNodes().filter(d),l=n.getFirstChild(),s=n.getLastChild();if(r.length>0&&null!==l&&null!==s&&C(l)&&C(s)&&r[0]===l.getFirstChild()&&r[r.length-1]===s.getLastChild()){n.selectPrevious();const o=n.getParent();return n.remove(),void(e.$isRootNode(o)&&o.isEmpty()&&t.dispatchCommand(e.INSERT_PARAGRAPH_COMMAND,void 0))}r.forEach(t=>{if(e.$isElementNode(t)){const n=e.$createParagraphNode(),o=e.$createTextNode();n.append(o),t.append(n),t.getChildren().forEach(e=>{e!==n&&e.remove()})}}),Se(t,this.table,null),e.$setSelection(null),t.dispatchCommand(e.SELECTION_CHANGE_COMMAND,void 0)}}const ee="__lexicalTableSelection";function te(t){const n=e.$getNodeByKeyOrThrow(t);return Ge(n)||g(386,t),n}const ne=40;function oe(e,t,n){const o=e=>Math.max(1,Math.ceil(Math.min(ne,e)/ne*18));return e<=t+ne?-o(t+ne-e):e>=n-ne?o(e-(n-ne)):0}function re(t){return e.isHTMLElement(t)&&"TABLE"===t.nodeName}function le(e,t){if(!t)return t;const n=re(t)?t:t.querySelector("table");return re(n)||g(341,e.constructor.name,e.getType(),e.getKey(),t.nodeName),n}function se(e){return e._window}function ie(e,t){for(let n=t,o=null;null!==n;n=n.getParent()){if(e.is(n))return o;d(n)&&(o=n)}return null}const ae=[[e.KEY_ARROW_DOWN_COMMAND,"down"],[e.KEY_ARROW_UP_COMMAND,"up"],[e.KEY_ARROW_LEFT_COMMAND,"backward"],[e.KEY_ARROW_RIGHT_COMMAND,"forward"]],ce=[e.DELETE_WORD_COMMAND,e.DELETE_LINE_COMMAND,e.DELETE_CHARACTER_COMMAND],ue=[e.KEY_BACKSPACE_COMMAND,e.KEY_DELETE_COMMAND];function de(t,n){return t.registerRootListener(o=>{if(null===o)return;const r=t._window;if(null===r)return;return e.registerEventListener(r,"pointerdown",r=>{const l=e.getComposedEventTarget(r);if(0!==r.button||!e.isDOMNode(l)||!o.contains(l))return;const s=function(e){const t=pe(e);if(null===t)return null;let n=t.elem;for(;null!=n;){if("TABLE"===n.nodeName&&ee in n&&n[ee])return{cellElement:t,tableElement:n,tableObserver:n[ee]};n=n.parentNode}return null}(l);t.update(()=>{if(q(e.$getSelection())){for(const[e]of n.observers.values())e.$clearHighlight(!1);e.$setSelection(null),t.dispatchCommand(e.SELECTION_CHANGE_COMMAND,void 0)}if(!s)return;const{tableObserver:o,tableElement:l,cellElement:i}=s;!function(t,n,o,r,l,s){const i=t._window;if(!i)return;const a=o=>{if(l.isSelecting)return;l.isSelecting=!0,null!==o&&null===l.anchorCell&&t.update(()=>{l.$setAnchorCellForSelection(o)});let a=n.clientX,c=n.clientY,u=null;const d=()=>{l.isSelecting=!1,null!==u&&(i.cancelAnimationFrame(u),u=null),i.removeEventListener("pointerup",N),i.removeEventListener("pointermove",b)},h=(t,n)=>{const o=r.getRootNode();if(!e.isDOMDocumentNode(o)&&!e.isDOMShadowRoot(o))return null;for(const e of o.elementsFromPoint(t,n)){const t=Ce(r,e);if(t)return t}return null},g=(n,o)=>{null===l.anchorCell&&t.update(()=>{l.$setAnchorCellForSelection(n)}),null!==l.focusCell&&n.elem===l.focusCell.elem||(s.setNextFocus({focusCell:n,override:o,tableKey:l.tableNodeKey}),t.dispatchCommand(e.SELECTION_CHANGE_COMMAND,void 0))},f=e=>{for(let t=r.parentElement;t;t=t.parentElement){if("x"===e?t.scrollWidth>t.clientWidth:t.scrollHeight>t.clientHeight){const n=i.getComputedStyle(t),o="x"===e?n.overflowX:n.overflowY;if("auto"===o||"scroll"===o)return t}}return null},m=(e,t,n)=>{let o,r;if(null===e)o=0,r="x"===n?i.innerWidth:i.innerHeight;else{const t=e.getBoundingClientRect();o="x"===n?t.left:t.top,r="x"===n?t.right:t.bottom}const l=oe(t,o,r);if(0===l)return!1;if(null===e){const e="x"===n?i.scrollX:i.scrollY;return i.scrollBy("x"===n?l:0,"x"===n?0:l),("x"===n?i.scrollX:i.scrollY)!==e}if("x"===n){const t=e.scrollLeft;return e.scrollLeft+=l,e.scrollLeft!==t}const s=e.scrollTop;return e.scrollTop+=l,e.scrollTop!==s},p=(e,t)=>{let n=a,o=c;if(null===e)n=Math.min(Math.max(n,1),i.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),i.innerHeight-1);else{const e=t.getBoundingClientRect();o=Math.min(Math.max(o,e.top+1),e.bottom-1)}return[n,o]},C=()=>{const e=f("x");if(null!==e){const t=e.getBoundingClientRect();if(0!==oe(a,t.left,t.right))return!0}const t=f("y"),n=null===t?0:t.getBoundingClientRect().top,o=null===t?i.innerHeight:t.getBoundingClientRect().bottom;return 0!==oe(c,n,o)},_=()=>{if(u=null,!l.isSelecting)return;const e=f("x"),t=f("y"),n=null!==e&&m(e,a,"x"),o=m(t,c,"y");if(n||o){const[n,o]=p(e,t),r=h(n,o);r&&g(r,!1),u=i.requestAnimationFrame(_)}},S=()=>{null===u&&"touch"!==l.pointerType&&C()&&(u=i.requestAnimationFrame(_))},N=()=>{d()},b=t=>{if(!(e=>!(1&~e.buttons))(t)&&l.isSelecting)return void d();const n=e.getComposedEventTarget(t);if(!e.isDOMNode(n))return;a=t.clientX,c=t.clientY;let o=null;const s=!(e.IS_FIREFOX||r.contains(n));o=s?Ce(r,n):h(t.clientX,t.clientY),o&&g(o,s),S()};i.addEventListener("pointerup",N,l.listenerOptions),i.addEventListener("pointermove",b,l.listenerOptions)};l.pointerType=n.pointerType;const c=te(l.tableNodeKey),u=e.$getPreviousSelection();if(e.IS_FIREFOX&&n.shiftKey&&Re(u,c)&&(e.$isRangeSelection(u)||q(u))){const e=u.anchor.getNode(),t=ie(c,u.anchor.getNode());if(t)l.$setAnchorCellForSelection(Ie(l,t)),l.$setFocusCellForSelection(o),Pe(n);else{(c.isBefore(e)?c.selectStart():c.selectEnd()).anchor.set(u.anchor.key,u.anchor.offset,u.anchor.type)}}else"touch"!==n.pointerType&&l.$setAnchorCellForSelection(o);a(o)}(t,r,i,l,o,n)})})})}function he(t,n,l,s,i){const a=l.getRootElement(),c=se(l);null!==a&&null!==c||g(246);const u=new Z(l,t.getKey()),h=le(t,n);!function(e,t){null!==me(e)&&g(205);e[ee]=t}(h,u),u.listenersToRemove.add(()=>function(e,t){me(e)===t&&delete e[ee]}(h,u));u.listenersToRemove.add(e.registerEventListener(h,"mousedown",t=>{const n=e.getComposedEventTarget(t);if(t.detail>=3&&e.isDOMNode(n)){null!==pe(n)&&t.preventDefault()}},u.listenerOptions));for(const[n,o]of ae)u.listenersToRemove.add(l.registerCommand(n,e=>Fe(l,e,o,t,u,i),e.COMMAND_PRIORITY_HIGH));u.listenersToRemove.add(l.registerCommand(e.KEY_ESCAPE_COMMAND,n=>{const o=e.$getSelection();if(q(o)){const e=ie(t,o.focus.getNode());if(null!==e)return Pe(n),e.selectEnd(),!0}return!1},e.COMMAND_PRIORITY_HIGH));const f=n=>()=>{const o=e.$getSelection();if(!Re(o,t))return!1;if(q(o))return u.$clearText(),!0;if(e.$isRangeSelection(o)){if(!d(ie(t,o.anchor.getNode())))return!1;const r=o.anchor.getNode(),l=o.focus.getNode(),s=t.isParentOf(r),i=t.isParentOf(l);if(s&&!i||i&&!s)return u.$clearText(),!0;const a=e.$findMatchingParent(o.anchor.getNode(),t=>e.$isElementNode(t)),c=a&&e.$findMatchingParent(a,t=>e.$isElementNode(t)&&d(t.getParent()));if(!e.$isElementNode(c)||!e.$isElementNode(a))return!1;if(n===e.DELETE_LINE_COMMAND&&null===c.getPreviousSibling())return!0}return!1};for(const t of ce)u.listenersToRemove.add(l.registerCommand(t,f(t),e.COMMAND_PRIORITY_HIGH));const m=n=>{const o=e.$getSelection();if(!q(o)&&!e.$isRangeSelection(o))return!1;const r=t.isParentOf(o.anchor.getNode());if(r!==t.isParentOf(o.focus.getNode())){const e=r?"anchor":"focus",n=r?"focus":"anchor",{key:l,offset:s,type:i}=o[n];return t[o[e].isBefore(o[n])?"selectPrevious":"selectNext"]()[n].set(l,s,i),!1}return!!Re(o,t)&&(!!q(o)&&(n&&(n.preventDefault(),n.stopPropagation()),u.$clearText(),!0))};for(const t of ue)u.listenersToRemove.add(l.registerCommand(t,m,e.COMMAND_PRIORITY_HIGH));u.listenersToRemove.add(l.registerCommand(e.CUT_COMMAND,t=>{const n=e.$getSelection();if(n){if(!q(n)&&!e.$isRangeSelection(n))return!1;r.copyToClipboard(l,o.objectKlassEquals(t,ClipboardEvent)?t:null,r.$getClipboardDataFromSelection(n));const s=m(t);return e.$isRangeSelection(n)?(n.removeText(),!0):s}return!1},e.COMMAND_PRIORITY_HIGH));const p=a.ownerDocument;return u.listenersToRemove.add(e.registerEventListener(p,"paste",n=>{if(n.defaultPrevented)return;l.read("latest",()=>{const n=e.$getSelection();return a.contains(p.activeElement)&&q(n)&&Re(n,t)})&&(n.preventDefault(),l.dispatchCommand(e.PASTE_COMMAND,n))})),u.listenersToRemove.add(e.registerEventListener(p,"copy",n=>{if(n.defaultPrevented)return;const o=e.getComposedEventTarget(n);if(o===a||e.isDOMNode(o)&&a.contains(o))return;l.read("latest",()=>{const n=e.$getSelection();return a.contains(e.getActiveElement(a))&&q(n)&&Re(n,t)})&&(n.preventDefault(),l.dispatchCommand(e.COPY_COMMAND,n))})),u.listenersToRemove.add(l.registerCommand(e.FORMAT_TEXT_COMMAND,n=>{const o=e.$getSelection();if(!Re(o,t))return!1;if(q(o))return u.$formatCells(n),!0;if(e.$isRangeSelection(o)){const t=e.$findMatchingParent(o.anchor.getNode(),e=>d(e));if(!d(t))return!1}return!1},e.COMMAND_PRIORITY_HIGH)),u.listenersToRemove.add(l.registerCommand(e.FORMAT_ELEMENT_COMMAND,n=>{const o=e.$getSelection();if(!q(o)||!Re(o,t))return!1;const r=o.anchor.getNode(),l=o.focus.getNode();if(!d(r)||!d(l))return!1;if(function(e,t){if(q(e)){const n=e.anchor.getNode(),o=e.focus.getNode();if(t&&n&&o){const[e]=H(t,n,o);return n.getKey()===e[0][0].cell.getKey()&&o.getKey()===e[e.length-1].at(-1).cell.getKey()}}return!1}(o,t))return t.setFormat(n),!0;const[s,i,a]=H(t,r,l),c=Math.max(i.startRow+i.cell.__rowSpan-1,a.startRow+a.cell.__rowSpan-1),u=Math.max(i.startColumn+i.cell.__colSpan-1,a.startColumn+a.cell.__colSpan-1),h=Math.min(i.startRow,a.startRow),g=Math.min(i.startColumn,a.startColumn),f=new Set;for(let t=h;t<=c;t++)for(let o=g;o<=u;o++){const r=s[t][o].cell;if(f.has(r))continue;f.add(r),r.setFormat(n);const l=r.getChildren();for(let t=0;t<l.length;t++){const o=l[t];e.$isElementNode(o)&&!o.isInline()&&o.setFormat(n)}}return!0},e.COMMAND_PRIORITY_HIGH)),u.listenersToRemove.add(l.registerCommand(e.CONTROLLED_TEXT_INSERTION_COMMAND,n=>{const o=e.$getSelection();if(!Re(o,t))return!1;if(q(o))return u.$clearHighlight(),!1;if(e.$isRangeSelection(o)){const r=e.$findMatchingParent(o.anchor.getNode(),e=>d(e));if(!d(r))return!1;if("string"==typeof n){const r=Ke(l,o,t);if(r)return De(r,t,[e.$createTextNode(n)]),!0}}return!1},e.COMMAND_PRIORITY_HIGH)),s&&u.listenersToRemove.add(l.registerCommand(e.KEY_TAB_COMMAND,n=>{const o=e.$getSelection();if(!e.$isRangeSelection(o)||!o.isCollapsed()||!Re(o,t))return!1;const r=Ee(o.anchor.getNode());return!(null===r||!t.is(Ae(r)))&&(Pe(n),function(t,n){const o="next"===n?"getNextSibling":"getPreviousSibling",r="next"===n?"getFirstChild":"getLastChild",l=t[o]();if(e.$isElementNode(l))return l.selectEnd();const s=e.$findMatchingParent(t,C);null===s&&g(247);for(let t=s[o]();C(t);t=t[o]()){const n=t[r]();if(e.$isElementNode(n))return n.selectEnd()}const i=e.$findMatchingParent(s,Ge);null===i&&g(248);"next"===n?i.selectNext():i.selectPrevious()}(r,n.shiftKey?"previous":"next"),!0)},e.COMMAND_PRIORITY_HIGH)),u.listenersToRemove.add(l.registerCommand(e.FOCUS_COMMAND,e=>t.isSelected(),e.COMMAND_PRIORITY_HIGH)),u.listenersToRemove.add(l.registerCommand(e.INSERT_PARAGRAPH_COMMAND,()=>{const n=e.$getSelection();if(!e.$isRangeSelection(n)||!n.isCollapsed()||!Re(n,t))return!1;const o=Ke(l,n,t);return!!o&&(De(o,t),!0)},e.COMMAND_PRIORITY_HIGH)),u}function ge(t,n){const o=e.$getSelection(),r=e.$getPreviousSelection(),l=t.getAndClearNextFocus();if(null!==l){const{tableKey:e,focusCell:n}=l,r=t.observers.get(e);r||g(335,e);const[s]=r;if(q(o)&&o.tableKey===s.tableNodeKey)return(n.x!==s.focusX||n.y!==s.focusY)&&(s.$setFocusCellForSelection(n),!0);if(null!==s.anchorCell&&null!==s.anchorCellNodeKey&&n.elem!==s.anchorCell.elem&&null!==s.tableSelection)return s.$setFocusCellForSelection(n,!0),!0}const s=t.getAndClearShouldCheckSelectionForTable();if(s&&e.$isRangeSelection(r)&&e.$isRangeSelection(o)&&o.isCollapsed()){const t=e.$getNodeByKey(s);if(Ge(t)){const n=o.anchor.getNode(),r=t.getFirstChild(),l=Ee(n);if(null!==l&&C(r)){const n=r.getFirstChild();if(d(n)&&t.is(e.$findMatchingParent(l,e=>e.is(t)||e.is(n))))return n.selectStart(),!0}}}q(o)&&function(t,n){const o=se(t),r=e.$getPreviousSelection();if(!n.is(r))return;const l=te(n.tableKey),s=e.getDOMSelection(o),i=s&&e.getDOMSelectionPoints(s,t.getRootElement());if(s&&i&&i.anchorNode&&i.focusNode){const o=e.$getNearestNodeFromDOMNode(i.focusNode),r=o&&!l.isParentOf(o),a=e.$getNearestNodeFromDOMNode(i.anchorNode),c=a&&l.isParentOf(a);if(r&&c&&s.rangeCount>0){const o=e.$createRangeSelectionFromDom(s,t);o&&(o.anchor.set(l.getKey(),n.isBackward()?l.getChildrenSize():0,"element"),s.removeAllRanges(),e.$setSelection(o))}}}(n,o),e.$isRangeSelection(o)&&function(t,n){const o=e.$getPreviousSelection(),{anchor:r,focus:l}=t,s=r.getNode(),i=l.getNode(),a=Ee(s),c=Ee(i),u=a?Ae(a):null,d=c?Ae(c):null,h=t.isBackward(),f=a&&c&&u&&d&&u.is(d),m=d&&(!u||u.isParentOf(d)),p=u&&(!d||d.isParentOf(u));if(m){const n=t.clone(),[o]=H(d,c,c),r=o[0][0].cell,l=o[o.length-1].at(-1).cell;n.focus.set(h?r.getKey():l.getKey(),h?0:l.getChildrenSize(),"element"),e.$setSelection(n)}else if(p){const n=t.clone(),[o]=H(u,a,a),r=o[0][0].cell,l=o[o.length-1].at(-1).cell;n.anchor.set(h?l.getKey():r.getKey(),h?l.getChildrenSize():0,"element"),e.$setSelection(n)}else if(f){const r=n.observers.get(u.getKey());r||g(335,u.getKey());const[l]=r;if(a.is(c)||(l.$setAnchorCellForSelection(Ie(l,a)),l.$setFocusCellForSelection(Ie(l,c),!0)),"touch"===l.pointerType&&l.isSelecting&&t.isCollapsed()&&e.$isRangeSelection(o)&&o.isCollapsed()){const e=Ee(o.anchor.getNode());e&&!e.is(c)&&(l.$setAnchorCellForSelection(Ie(l,e)),l.$setFocusCellForSelection(Ie(l,c),!0),l.pointerType=null)}}}(o,t);for(const[e,o]of t.$getTableNodesAndObservers())fe(n,e,o);return!1}function fe(t,n,o){const r=e.$getSelection(),l=e.$getPreviousSelection();r&&!r.is(l)&&(q(r)||q(l))&&o.tableSelection&&!o.tableSelection.is(l)&&(q(r)&&r.tableKey===o.tableNodeKey?o.$updateTableTableSelection(r):!q(r)&&q(l)&&l.tableKey===o.tableNodeKey&&o.$updateTableTableSelection(null)),o.hasHijackedSelectionStyles&&!n.isSelected()?function(e,t){t.$enableHighlightStyle(),Ne(t.table,t=>{const n=t.elem;t.highlighted=!1,Oe(e,t),n.getAttribute("style")||n.removeAttribute("style")})}(t,o):!o.hasHijackedSelectionStyles&&n.isSelected()&&function(e,t){t.$disableHighlightStyle(),Ne(t.table,t=>{t.highlighted=!0,Me(e,t)})}(t,o)}function me(e){return e[ee]||null}function pe(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 Ce(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 _e(e,t){const n=[],o={columns:0,domRows:n,rows:0};let r=le(e,t).querySelector("tr"),l=0,s=0;for(n.length=0;null!=r;){const e=r.nodeName;if("TD"===e||"TH"===e){const e={elem:r,hasBackgroundColor:""!==r.style.backgroundColor,highlighted:!1,x:l,y:s};r._cell=e;let t=n[s];void 0===t&&(t=n[s]=[]),t[l]=e}else{const e=r.firstChild;if(null!=e){r=e;continue}}const t=r.nextSibling;if(null!=t){l++,r=t;continue}const o=r.parentNode;if(null!=o){const e=o.nextSibling;if(null==e)break;s++,l=0,r=e}}return o.columns=l+1,o.rows=s+1,o}function Se(e,t,n){const o=new Set(n?n.getNodes():[]);Ne(t,(t,n)=>{const r=t.elem;o.has(n)?(t.highlighted=!0,Me(e,t)):(t.highlighted=!1,Oe(e,t),r.getAttribute("style")||r.removeAttribute("style"))})}function Ne(t,n){const{domRows:o}=t;for(let t=0;t<o.length;t++){const r=o[t];if(r)for(let o=0;o<r.length;o++){const l=r[o];if(!l)continue;const s=e.$getNearestNodeFromDOMNode(l.elem);null!==s&&n(l,s,{x:o,y:t})}}}const be=(e,t,n,o,r)=>{const l="forward"===r;switch(r){case"backward":case"forward":return n!==(l?e.table.columns-1:0)?xe(t.getCellNodeFromCordsOrThrow(n+(l?1:-1),o,e.table),l):o!==(l?e.table.rows-1:0)?xe(t.getCellNodeFromCordsOrThrow(l?0:e.table.columns-1,o+(l?1:-1),e.table),l):l?t.selectNext():t.selectPrevious(),!0;case"up":return 0!==o?xe(t.getCellNodeFromCordsOrThrow(n,o-1,e.table),!1):t.selectPrevious(),!0;case"down":return o!==e.table.rows-1?xe(t.getCellNodeFromCordsOrThrow(n,o+1,e.table),!0):t.selectNext(),!0;default:return!1}};function we(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 Te([e,t]){return["minColumn"===e?"maxColumn":"minColumn","minRow"===t?"maxRow":"minRow"]}function $e(e,t,[n,o]){const r=t[o],l=e[r];void 0===l&&g(250,o,String(r));const s=t[n],i=l[s];return void 0===i&&g(250,n,String(s)),i}function ye(e,t,n,o,r){const l=W(t,n,o),s=function(e,t){const{minColumn:n,maxColumn:o,minRow:r,maxRow:l}=t;let s=1,i=1,a=1,c=1;const u=e[r],d=e[l];for(let e=n;e<=o;e++)s=Math.max(s,u[e].cell.__rowSpan),c=Math.max(c,d[e].cell.__rowSpan);for(let t=r;t<=l;t++)i=Math.max(i,e[t][n].cell.__colSpan),a=Math.max(a,e[t][o].cell.__colSpan);return{bottomSpan:c,leftSpan:i,rightSpan:a,topSpan:s}}(t,l),{topSpan:i,leftSpan:a,bottomSpan:c,rightSpan:u}=s,d=function(e,t){const n=we(e,t);return null===n&&g(249,t.cell.getKey()),n}(l,n),[h,f]=Te(d);let m=l[h],p=l[f];"forward"===r?m+="maxColumn"===h?1:a:"backward"===r?m-="minColumn"===h?1:u:"down"===r?p+="maxRow"===f?1:i:"up"===r&&(p-="minRow"===f?1:c);const C=t[p];if(void 0===C)return!1;const _=C[m];if(void 0===_)return!1;const[S,N]=function(e,t,n){const o=W(e,t,n),r=we(o,t);if(r)return[$e(e,o,r),$e(e,o,Te(r))];const l=we(o,n);if(l)return[$e(e,o,Te(l)),$e(e,o,l)];const s=["minColumn","minRow"];return[$e(e,o,s),$e(e,o,Te(s))]}(t,n,_),b=Ie(e,S.cell),w=Ie(e,N.cell);return e.$setAnchorCellForSelection(b),e.$setFocusCellForSelection(w,!0),!0}function Re(t,n){if(e.$isRangeSelection(t)||q(t)){const e=n.isParentOf(t.anchor.getNode()),o=n.isParentOf(t.focus.getNode());return e&&o}return!1}function xe(e,t){t?e.selectStart():e.selectEnd()}function Me(t,n){const o=n.elem,r=t._config.theme;d(e.$getNearestNodeFromDOMNode(o))||g(131),e.addClassNamesToElement(o,r.tableCellSelected)}function Oe(t,n){const o=n.elem;d(e.$getNearestNodeFromDOMNode(o))||g(131);const r=t._config.theme;e.removeClassNamesFromElement(o,r.tableCellSelected)}function Ee(t){const n=e.$findMatchingParent(t,d);return d(n)?n:null}function Ae(t){const n=e.$findMatchingParent(t,Ge);return Ge(n)?n:null}function ve(t,n,o,r,l,s,i){const a=e.$caretFromPoint(o.focus,l?"previous":"next");if(e.$isExtendableTextPointCaret(a))return!1;let c=a;for(const t of e.$extendCaretToRange(a).iterNodeCarets("shadowRoot")){if(!e.$isSiblingCaret(t)||!e.$isElementNode(t.origin))return!1;c=t}const u=c.getParentAtCaret();if(!d(u))return!1;const h=u,g=function(t){for(const n of e.$extendCaretToRange(t).iterNodeCarets("root")){const{origin:o}=n;if(d(o)){if(e.$isChildCaret(n))return e.$getChildCaret(o,t.direction)}else if(!C(o))break}return null}(e.$getSiblingCaret(h,c.direction)),f=e.$findMatchingParent(h,Ge);if(!f||!f.is(s))return!1;const m=t.getElementByKey(h.getKey()),p=pe(m);if(!m||!p)return!1;const _=Ue(t,f);if(i.table=_,g)if("extend"===r){const e=pe(t.getElementByKey(g.origin.getKey()));if(!e)return!1;i.$setAnchorCellForSelection(p),i.$setFocusCellForSelection(e,!0)}else{const t=e.$normalizeCaret(g);e.$setPointFromCaret(o.anchor,t),e.$setPointFromCaret(o.focus,t)}else if("extend"===r)i.$setAnchorCellForSelection(p),i.$setFocusCellForSelection(p,!0);else{const t=function(t){const n=e.$getAdjacentChildCaret(t);return e.$isChildCaret(n)?e.$normalizeCaret(n):t}(e.$getSiblingCaret(f,a.direction));e.$setPointFromCaret(o.anchor,t),e.$setPointFromCaret(o.focus,t)}return Pe(n),!0}function Fe(t,n,o,r,l,s){if(("up"===o||"down"===o)&&function(e){const t=e.getRootElement();if(!t)return!1;return t.hasAttribute("aria-controls")&&"typeahead-menu"===t.getAttribute("aria-controls")}(t))return!1;const i=e.$getSelection();if(!Re(i,r)){if(e.$isRangeSelection(i)){if("backward"===o){if(i.focus.offset>0)return!1;const t=function(t){for(let n=t,o=t;null!==o;n=o,o=o.getParent())if(e.$isElementNode(o)){if(o!==n&&o.getFirstChild()!==n)return null;if(!o.isInline())return o}return null}(i.focus.getNode());if(!t)return!1;const o=t.getPreviousSibling();return!!Ge(o)&&(Pe(n),n.shiftKey?i.focus.set(o.getParentOrThrow().getKey(),o.getIndexWithinParent(),"element"):o.selectEnd(),!0)}if(n.shiftKey&&("up"===o||"down"===o)){const t=i.focus.getNode();if(!i.isCollapsed()&&("up"===o&&!i.isBackward()||"down"===o&&i.isBackward())){let l=e.$findMatchingParent(t,e=>Ge(e));if(d(l)&&(l=e.$findMatchingParent(l,Ge)),l!==r)return!1;if(!l)return!1;const s="down"===o?l.getNextSibling():l.getPreviousSibling();if(!s)return!1;let a=0;"up"===o&&e.$isElementNode(s)&&(a=s.getChildrenSize());let c=s;if("up"===o&&e.$isElementNode(s)){const t=s.getLastChild();c=t||s,a=e.$isTextNode(c)?c.getTextContentSize():0}const u=i.clone();return u.focus.set(c.getKey(),a,e.$isTextNode(c)?"text":"element"),e.$setSelection(u),Pe(n),!0}if(e.$isRootOrShadowRoot(t)){const e="up"===o?i.getNodes()[i.getNodes().length-1]:i.getNodes()[0];if(e){if(null!==ie(r,e)){const e=r.getFirstDescendant(),t=r.getLastDescendant();if(!e||!t)return!1;const[n]=L(e),[o]=L(t),s=r.getCordsFromCellNode(n,l.table),i=r.getCordsFromCellNode(o,l.table),a=r.getDOMCellFromCordsOrThrow(s.x,s.y,l.table),c=r.getDOMCellFromCordsOrThrow(i.x,i.y,l.table);return l.$setAnchorCellForSelection(a),l.$setFocusCellForSelection(c,!0),!0}}return!1}{let r=e.$findMatchingParent(t,t=>e.$isElementNode(t)&&!t.isInline());if(d(r)&&(r=e.$findMatchingParent(r,Ge)),!r)return!1;const s="down"===o?r.getNextSibling():r.getPreviousSibling();if(Ge(s)&&l.tableNodeKey===s.getKey()){const t=s.getFirstDescendant(),r=s.getLastDescendant();if(!t||!r)return!1;const[l]=L(t),[a]=L(r),c=i.clone();return c.focus.set(("up"===o?l:a).getKey(),"up"===o?0:a.getChildrenSize(),"element"),Pe(n),e.$setSelection(c),!0}}}}return"down"===o&&Be(t)&&s.setShouldCheckSelectionForTable(r.getKey()),!1}if(e.$isRangeSelection(i)){if("backward"===o||"forward"===o){return ve(t,n,i,n.shiftKey?"extend":"move","backward"===o,r,l)}if(i.isCollapsed()){const{anchor:a,focus:c}=i,u=e.$findMatchingParent(a.getNode(),d),h=e.$findMatchingParent(c.getNode(),d);if(!d(u)||!u.is(h))return!1;const g=Ae(u);if(g!==r&&null!=g){const e=le(g,t.getElementByKey(g.getKey()));if(null!=e)return l.table=_e(g,e),Fe(t,n,o,g,l,s)}const f=t.getElementByKey(u.__key),m=t.getElementByKey(a.key);if(null==m||null==f)retu