UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

10 lines (8 loc) 3.09 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{useLexicalComposerContext as e}from"@lexical/react/LexicalComposerContext";import{TableNode as t,TableCellNode as r,TableRowNode as o,INSERT_TABLE_COMMAND as n,$createTableNodeWithDimensions as l,$computeTableMapSkipCellCheck as s,$createTableCellNode as i,$isTableNode as a,$getNodeTriplet as c,$computeTableMap as d,$isTableRowNode as f,$isTableCellNode as u,applyTableHandlers as g}from"@lexical/table";import{mergeRegister as p,$insertNodeToNearestRoot as m,$insertFirst as h}from"@lexical/utils";import{$isTextNode as C,COMMAND_PRIORITY_EDITOR as x,$createParagraphNode as v,$getNodeByKey as w}from"lexical";import{useEffect as S}from"react";function L(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var N=L((function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));function b({hasCellMerge:L=!0,hasCellBackgroundColor:b=!0,hasTabHandler:M=!0}){const[y]=e();return S((()=>(y.hasNodes([t,r,o])||N(10),p(y.registerCommand(n,(({columns:e,rows:t,includeHeaders:r})=>{const o=l(Number(t),Number(e),r);m(o);const n=o.getFirstDescendant();return C(n)&&n.select(),!0}),x),y.registerNodeTransform(t,(e=>{const[t]=s(e,null,null),r=t.reduce(((e,t)=>Math.max(e,t.length)),0),o=e.getChildren();for(let e=0;e<t.length;++e){const n=o[e];if(!n)continue;const l=t[e].reduce(((e,t)=>t?1+e:e),0);if(l!==r)for(let e=l;e<r;++e){const e=i(0);e.append(v()),n.append(e)}}}))))),[y]),S((()=>{const e=new Map,r=(t,r,o)=>{const n=o,l=g(t,n,y,M);e.set(r,[l,n])},o=y.registerMutationListener(t,(t=>{for(const[o,n]of t)if("created"===n||"updated"===n){const t=e.get(o),n=y.getElementByKey(o);t&&n===t[1]||(t&&(t[0].removeListeners(),e.delete(o)),null!==n&&y.getEditorState().read((()=>{const e=w(o);a(e)&&r(e,o,n)})))}else if("destroyed"===n){const t=e.get(o);void 0!==t&&(t[0].removeListeners(),e.delete(o))}}),{skipInitialization:!1});return()=>{o();for(const[,[t]]of e)t.removeListeners()}}),[y,M]),S((()=>{if(!L)return y.registerNodeTransform(r,(e=>{if(e.getColSpan()>1||e.getRowSpan()>1){const[,,t]=c(e),[r]=d(t,e,e),o=r.length,n=r[0].length;let l=t.getFirstChild();f(l)||N(175);const s=[];for(let e=0;e<o;e++){0!==e&&(l=l.getNextSibling(),f(l)||N(175));let t=null;for(let o=0;o<n;o++){const n=r[e][o],a=n.cell;if(n.startRow===e&&n.startColumn===o)t=a,s.push(a);else if(a.getColSpan()>1||a.getRowSpan()>1){u(a)||N(176);const e=i(a.__headerState);null!==t?t.insertAfter(e):h(l,e)}}}for(const e of s)e.setColSpan(1),e.setRowSpan(1)}}))}),[y,L]),S((()=>{if(!b)return y.registerNodeTransform(r,(e=>{null!==e.getBackgroundColor()&&e.setBackgroundColor(null)}))}),[y,b,L]),null}export{b as TablePlugin};