From fc70584c5accc93a07c63527764db23ae5e7aace Mon Sep 17 00:00:00 2001 From: zbeyens Date: Tue, 3 Oct 2023 18:24:59 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../default/plate-ui/table-element.tsx | 94 ++++---- .../resizable/src/components/ResizeHandle.tsx | 3 +- .../TableCellElement/getCellsOffsets.ts | 15 -- .../components/TableCellElement/getClosest.ts | 14 -- .../src/components/TableCellElement/index.ts | 1 - .../useTableCellElementResizable.ts | 2 +- .../useTableCellElementState.ts | 36 ++-- .../TableCellElement/useTableCellsMerge.ts | 202 ------------------ .../TableElement/useTableColSizes.ts | 2 +- packages/table/src/index.ts | 1 + packages/table/src/merge/getCellsOffsets.ts | 13 ++ packages/table/src/merge/getClosest.ts | 16 ++ .../TableCellElement => merge}/getColSpan.ts | 6 +- .../TableCellElement => merge}/getRowSpan.ts | 6 +- packages/table/src/merge/index.ts | 10 + packages/table/src/merge/mergeTableCells.ts | 99 +++++++++ packages/table/src/merge/unmergeTableCells.ts | 64 ++++++ .../table/src/transforms/insertTableRow.ts | 1 + packages/table/src/types.ts | 3 + 19 files changed, 277 insertions(+), 311 deletions(-) delete mode 100644 packages/table/src/components/TableCellElement/getCellsOffsets.ts delete mode 100644 packages/table/src/components/TableCellElement/getClosest.ts delete mode 100644 packages/table/src/components/TableCellElement/useTableCellsMerge.ts create mode 100644 packages/table/src/merge/getCellsOffsets.ts create mode 100644 packages/table/src/merge/getClosest.ts rename packages/table/src/{components/TableCellElement => merge}/getColSpan.ts (54%) rename packages/table/src/{components/TableCellElement => merge}/getRowSpan.ts (54%) create mode 100644 packages/table/src/merge/index.ts create mode 100644 packages/table/src/merge/mergeTableCells.ts create mode 100644 packages/table/src/merge/unmergeTableCells.ts diff --git a/apps/www/src/registry/default/plate-ui/table-element.tsx b/apps/www/src/registry/default/plate-ui/table-element.tsx index 45b7b7d312..5741164e1b 100644 --- a/apps/www/src/registry/default/plate-ui/table-element.tsx +++ b/apps/www/src/registry/default/plate-ui/table-element.tsx @@ -3,6 +3,7 @@ import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu'; import { PopoverAnchor, PopoverContentProps } from '@radix-ui/react-popover'; import { isCollapsed, + isExpanded, PlateElement, PlateElementProps, useEditorState, @@ -10,10 +11,11 @@ import { useRemoveNodeButton, } from '@udecode/plate-common'; import { - isTableRectangular, + getTableGridAbove, + mergeTableCells, TTableElement, + unmergeTableCells, useTableBordersDropdownMenuContentState, - useTableCellsMerge, useTableElement, useTableElementState, } from '@udecode/plate-table'; @@ -114,56 +116,47 @@ const TableFloatingToolbar = React.forwardRef< const { props: buttonProps } = useRemoveNodeButton({ element }); const readOnly = useReadOnly(); + const selected = useSelected(); const editor = useEditorState(); - const isSelected = useSelected(); + const collapsed = !readOnly && selected && isCollapsed(editor.selection); + const expanded = !readOnly && selected && isExpanded(editor.selection); + const open = !readOnly && selected; - const { onMergeCells, onUnmerge, cellEntries, subTable } = - useTableCellsMerge(); - const tableSelection = subTable?.[0]?.[0]; + const cellEntries = getTableGridAbove(editor, { format: 'cell' }); - const collapsedToolbarActive = - isSelected && !readOnly && isCollapsed(editor.selection); - - const hasEntries = !!cellEntries?.length; const canUnmerge = - hasEntries && - cellEntries?.length === 1 && + collapsed && + cellEntries && + cellEntries.length === 1 && ((cellEntries[0][0] as any)?.colSpan > 1 || (cellEntries[0][0] as any)?.rowSpan > 1); - const mergeToolbarActive = - isSelected && - !readOnly && - hasEntries && - cellEntries.length > 1 && - !isCollapsed(editor.selection) && - isTableRectangular(tableSelection); - - const mergeButton = ( + const mergeContent = expanded && ( ); - const collapsedContent = ( + const unmergeButton = canUnmerge && ( + + ); + + const bordersContent = collapsed && ( <> - {canUnmerge && ( - - )}