'use client';
import React from 'react';
import type * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu';
import type { TTableElement } from '@udecode/plate-table';
import { PopoverAnchor } from '@radix-ui/react-popover';
import { cn, withRef } from '@udecode/cn';
import { isSelectionExpanded } from '@udecode/plate-common';
import {
useEditorRef,
useEditorSelector,
useElement,
useRemoveNodeButton,
withHOC,
} from '@udecode/plate-common/react';
import {
TableProvider,
mergeTableCells,
unmergeTableCells,
useTableBordersDropdownMenuContentState,
useTableElement,
useTableElementState,
useTableMergeState,
} from '@udecode/plate-table/react';
import { type LucideProps, Combine, Trash2Icon, Ungroup } from 'lucide-react';
import { useReadOnly, useSelected } from 'slate-react';
import { Button } from './button';
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuPortal,
DropdownMenuTrigger,
} from './dropdown-menu';
import { PlateElement } from './plate-element';
import { Popover, PopoverContent, popoverVariants } from './popover';
export const TableBordersDropdownMenuContent = withRef<
typeof DropdownMenuPrimitive.Content
>((props, ref) => {
const {
getOnSelectTableBorder,
hasBottomBorder,
hasLeftBorder,
hasNoBorders,
hasOuterBorders,
hasRightBorder,
hasTopBorder,
} = useTableBordersDropdownMenuContentState();
return (
Bottom Border
Top Border
Left Border
Right Border
No Border
Outside Borders
);
});
export const TableFloatingToolbar = withRef(
({ children, ...props }, ref) => {
const element = useElement();
const { props: buttonProps } = useRemoveNodeButton({ element });
const selectionCollapsed = useEditorSelector(
(editor) => !isSelectionExpanded(editor),
[]
);
const readOnly = useReadOnly();
const selected = useSelected();
const editor = useEditorRef();
const collapsed = !readOnly && selected && selectionCollapsed;
const open = !readOnly && selected;
const { canMerge, canUnmerge } = useTableMergeState();
const mergeContent = canMerge && (
);
const unmergeButton = canUnmerge && (
);
const bordersContent = collapsed && (
<>
>
);
return (
{children}
{(canMerge || canUnmerge || collapsed) && (
e.preventDefault()}
{...props}
>
{unmergeButton}
{mergeContent}
{bordersContent}
)}
);
}
);
export const TableElement = withHOC(
TableProvider,
withRef(({ children, className, ...props }, ref) => {
const { colSizes, isSelectingCell, marginLeft, minColumnWidth } =
useTableElementState();
const { colGroupProps, props: tableProps } = useTableElement();
return (
{colSizes.map((width, index) => (
))}
{children}
);
})
);
const BorderAll = (props: LucideProps) => (
);
const BorderBottom = (props: LucideProps) => (
);
const BorderLeft = (props: LucideProps) => (
);
const BorderNone = (props: LucideProps) => (
);
const BorderRight = (props: LucideProps) => (
);
const BorderTop = (props: LucideProps) => (
);