UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

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