@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
10 lines (8 loc) • 3.06 kB
JavaScript
/**
* 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 a,$isTableNode as i,$getNodeTriplet as c,$computeTableMap as f,$isTableRowNode as d,$isTableCellNode as u,applyTableHandlers as g}from"@lexical/table";import{mergeRegister as m,$insertNodeToNearestRoot as p,$insertFirst as h}from"@lexical/utils";import{$isTextNode as C,COMMAND_PRIORITY_EDITOR as x,$createParagraphNode as S,$nodesOfType as w,$getNodeByKey as v}from"lexical";import{useEffect as N}from"react";function b(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var y=b((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 L({hasCellMerge:b=!0,hasCellBackgroundColor:L=!0,hasTabHandler:M=!0}){const[R]=e();return N((()=>(R.hasNodes([t,r,o])||y(10),m(R.registerCommand(n,(({columns:e,rows:t,includeHeaders:r})=>{const o=l(Number(t),Number(e),r);p(o);const n=o.getFirstDescendant();return C(n)&&n.select(),!0}),x),R.registerNodeTransform(t,(e=>{const[t]=s(e,null,null),r=t.reduce(((e,t)=>Math.max(e,t.length)),0);for(let e=0;e<t.length;++e){const o=t[e].length;if(o===r)continue;const n=t[e][o-1].cell;for(let e=o;e<r;++e){const e=a(0);e.append(S()),null!==n?n.insertAfter(e):h(n,e)}}}))))),[R]),N((()=>{const e=new Map,r=t=>{const r=t.getKey(),o=R.getElementByKey(r);if(o&&!e.has(r)){const n=g(t,o,R,M);e.set(r,n)}};R.getEditorState().read((()=>{const e=w(t);for(const t of e)i(t)&&r(t)}));const o=R.registerMutationListener(t,(t=>{for(const[o,n]of t)if("created"===n)R.getEditorState().read((()=>{const e=v(o);i(e)&&r(e)}));else if("destroyed"===n){const t=e.get(o);void 0!==t&&(t.removeListeners(),e.delete(o))}}));return()=>{o();for(const[,t]of e)t.removeListeners()}}),[R,M]),N((()=>{if(!b)return R.registerNodeTransform(r,(e=>{if(e.getColSpan()>1||e.getRowSpan()>1){const[,,t]=c(e),[r]=f(t,e,e),o=r.length,n=r[0].length;let l=t.getFirstChild();d(l)||y(175);const s=[];for(let e=0;e<o;e++){0!==e&&(l=l.getNextSibling(),d(l)||y(175));let t=null;for(let o=0;o<n;o++){const n=r[e][o],i=n.cell;if(n.startRow===e&&n.startColumn===o)t=i,s.push(i);else if(i.getColSpan()>1||i.getRowSpan()>1){u(i)||y(176);const e=a(i.__headerState);null!==t?t.insertAfter(e):h(l,e)}}}for(const e of s)e.setColSpan(1),e.setRowSpan(1)}}))}),[R,b]),N((()=>{if(!L)return R.registerNodeTransform(r,(e=>{null!==e.getBackgroundColor()&&e.setBackgroundColor(null)}))}),[R,L,b]),null}export{L as TablePlugin};