UNPKG

@lobehub/editor

Version:

A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.

73 lines (72 loc) 3.34 kB
import { $createTableNodeWithDimensions, $createTableSelection, $findTableNode, $isTableNode, $isTableSelection } from '@lexical/table'; import { $insertNodeToNearestRoot, mergeRegister } from '@lexical/utils'; import { $getNodeByKey, $getPreviousSelection, $getSelection, $isElementNode, $isRangeSelection, $isTextNode, $setSelection, COMMAND_PRIORITY_EDITOR, createCommand } from 'lexical'; export var INSERT_TABLE_COMMAND = createCommand(); export var SELECT_TABLE_COMMAND = createCommand(); export function registerTableCommand(editor) { return mergeRegister(editor.registerCommand(INSERT_TABLE_COMMAND, function (_ref) { var rows = _ref.rows, columns = _ref.columns, includeHeaders = _ref.includeHeaders; var selection = $getSelection() || $getPreviousSelection(); if (!selection || !$isRangeSelection(selection)) { return false; } // Prevent nested tables by checking if we're already inside a table if ($findTableNode(selection.anchor.getNode())) { return false; } var anchorNode = selection.anchor.getNode(); var tableNode = $createTableNodeWithDimensions(Number(rows), Number(columns), includeHeaders); if ($isElementNode(anchorNode) && anchorNode.isEmpty()) { anchorNode.replace(tableNode); } else { $insertNodeToNearestRoot(tableNode); } var firstDescendant = tableNode.getFirstDescendant(); if ($isTextNode(firstDescendant)) { firstDescendant.select(); } return true; }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(SELECT_TABLE_COMMAND, function (_ref2) { var table = _ref2.table, columnIndex = _ref2.columnIndex, rowIndex = _ref2.rowIndex; editor.update(function () { var prevSelection = $getSelection(); var tableNode = $getNodeByKey(table); if (!tableNode || !$isTableNode(tableNode)) { return; } var tableSelection = $isTableSelection(prevSelection) ? prevSelection : $createTableSelection(); if (rowIndex !== undefined) { var firstRow = tableNode.getChildren()[rowIndex]; if (!firstRow) return; var firstCell = firstRow.getFirstChild(); var lastCell = firstRow.getLastChild(); if (!firstCell || !lastCell) return; tableSelection.set(table, firstCell.getKey(), lastCell.getKey()); $setSelection(tableSelection); } else if (columnIndex !== undefined) { var _firstRow = tableNode.getFirstChild(); var lastRow = tableNode.getLastChild(); if (!_firstRow || !lastRow) return; var _firstCell = _firstRow.getChildren()[columnIndex]; var _lastCell = lastRow.getChildren()[columnIndex]; if (!_firstCell || !_lastCell) return; tableSelection.set(table, _firstCell.getKey(), _lastCell.getKey()); $setSelection(tableSelection); } else { var _firstRow2 = tableNode.getFirstChild(); var _lastRow = tableNode.getLastChild(); if (!_firstRow2 || !_lastRow) return; var _firstCell2 = _firstRow2.getFirstChild(); var _lastCell2 = _lastRow.getLastChild(); if (!_firstCell2 || !_lastCell2) return; tableSelection.set(table, _firstCell2.getKey(), _lastCell2.getKey()); $setSelection(tableSelection); } }); return false; }, COMMAND_PRIORITY_EDITOR)); }