import React, { FC } from "react"; import { IconAlignCenter, IconAlignLeft, IconAlignRight, IconCheck, } from "@tabler/icons-react"; import { ActionIcon, Button, Popover, ScrollArea, Tooltip, } from "@mantine/core"; import type { Editor } from "@tiptap/react"; import { useEditorState } from "@tiptap/react"; import { useTranslation } from "react-i18next"; interface TableTextAlignmentProps { editor: Editor | null; } interface AlignmentItem { name: string; icon: React.ElementType; command: () => void; isActive: () => boolean; value: string; } export const TableTextAlignment: FC = ({ editor }) => { const { t } = useTranslation(); const [opened, setOpened] = React.useState(false); const editorState = useEditorState({ editor, selector: (ctx) => { if (!ctx.editor) { return null; } return { isAlignLeft: ctx.editor.isActive({ textAlign: "left" }), isAlignCenter: ctx.editor.isActive({ textAlign: "center" }), isAlignRight: ctx.editor.isActive({ textAlign: "right" }), }; }, }); if (!editor || !editorState) { return null; } const items: AlignmentItem[] = [ { name: "Align left", value: "left", isActive: () => editorState?.isAlignLeft, command: () => editor.chain().focus().setTextAlign("left").run(), icon: IconAlignLeft, }, { name: "Align center", value: "center", isActive: () => editorState?.isAlignCenter, command: () => editor.chain().focus().setTextAlign("center").run(), icon: IconAlignCenter, }, { name: "Align right", value: "right", isActive: () => editorState?.isAlignRight, command: () => editor.chain().focus().setTextAlign("right").run(), icon: IconAlignRight, }, ]; const activeItem = items.find((item) => item.isActive()) || items[0]; return ( setOpened(!opened)} > {items.map((item, index) => ( ))} ); };