diff --git a/apps/client/src/features/editor/components/table/table-background-color.tsx b/apps/client/src/features/editor/components/table/table-background-color.tsx new file mode 100644 index 00000000..5426edf8 --- /dev/null +++ b/apps/client/src/features/editor/components/table/table-background-color.tsx @@ -0,0 +1,156 @@ +import React, { FC } from "react"; +import { IconCheck, IconPalette } from "@tabler/icons-react"; +import { + ActionIcon, + ColorSwatch, + Popover, + Stack, + Text, + Tooltip, + UnstyledButton, +} from "@mantine/core"; +import { useEditor } from "@tiptap/react"; +import { useTranslation } from "react-i18next"; + +export interface TableColorItem { + name: string; + color: string; +} + +interface TableBackgroundColorProps { + editor: ReturnType; +} + +const TABLE_COLORS: TableColorItem[] = [ + // First row - grays + { name: "Default", color: "" }, + { name: "Light blue", color: "#E6E9FF" }, + { name: "Light cyan", color: "#E0F2FE" }, + { name: "Light teal", color: "#CCFBF1" }, + { name: "Light yellow", color: "#FEF3C7" }, + { name: "Light pink", color: "#FCE7F3" }, + { name: "Light purple", color: "#EDE9FE" }, + + // Second row - light colors + { name: "Gray", color: "#F3F4F6" }, + { name: "Blue", color: "#BFDBFE" }, + { name: "Cyan", color: "#A5F3FC" }, + { name: "Teal", color: "#99F6E4" }, + { name: "Yellow", color: "#FDE68A" }, + { name: "Pink", color: "#FBCFE8" }, + { name: "Purple", color: "#DDD6FE" }, + + // Third row - bold colors + { name: "Dark gray", color: "#9CA3AF" }, + { name: "Bold blue", color: "#60A5FA" }, + { name: "Bold cyan", color: "#22D3EE" }, + { name: "Bold teal", color: "#2DD4BF" }, + { name: "Bold orange", color: "#FB923C" }, + { name: "Bold red", color: "#F87171" }, + { name: "Bold purple", color: "#A78BFA" }, +]; + +export const TableBackgroundColor: FC = ({ + editor, +}) => { + const { t } = useTranslation(); + const [opened, setOpened] = React.useState(false); + + const setTableCellBackground = (color: string) => { + editor + .chain() + .focus() + .updateAttributes("tableCell", { backgroundColor: color || null }) + .updateAttributes("tableHeader", { backgroundColor: color || null }) + .run(); + setOpened(false); + }; + + // Get current cell's background color + const getCurrentColor = () => { + if (editor.isActive("tableCell")) { + const attrs = editor.getAttributes("tableCell"); + return attrs.backgroundColor || ""; + } + if (editor.isActive("tableHeader")) { + const attrs = editor.getAttributes("tableHeader"); + return attrs.backgroundColor || ""; + } + return ""; + }; + + const currentColor = getCurrentColor(); + + return ( + + + + setOpened(!opened)} + > + + + + + + + + + {t("Background color")} + + +
+ {TABLE_COLORS.map((item, index) => ( + setTableCellBackground(item.color)} + style={{ + position: "relative", + width: "24px", + height: "24px", + }} + title={t(item.name)} + > + + {currentColor === item.color && ( + + )} + + + ))} +
+
+
+
+ ); +}; \ No newline at end of file diff --git a/apps/client/src/features/editor/components/table/table-cell-menu.tsx b/apps/client/src/features/editor/components/table/table-cell-menu.tsx index 446936ac..7756eb73 100644 --- a/apps/client/src/features/editor/components/table/table-cell-menu.tsx +++ b/apps/client/src/features/editor/components/table/table-cell-menu.tsx @@ -15,6 +15,7 @@ import { IconTableRow, } from "@tabler/icons-react"; import { useTranslation } from "react-i18next"; +import { TableBackgroundColor } from "./table-background-color"; export const TableCellMenu = React.memo( ({ editor, appendTo }: EditorMenuProps): JSX.Element => { @@ -65,6 +66,8 @@ export const TableCellMenu = React.memo( shouldShow={shouldShow} > + + element.style.backgroundColor || null, + renderHTML: (attributes) => { + if (!attributes.backgroundColor) { + return {}; + } + return { + style: `background-color: ${attributes.backgroundColor}`, + }; + }, + }, + }; + }, }); diff --git a/packages/editor-ext/src/lib/table/header.ts b/packages/editor-ext/src/lib/table/header.ts new file mode 100644 index 00000000..ca507e38 --- /dev/null +++ b/packages/editor-ext/src/lib/table/header.ts @@ -0,0 +1,24 @@ +import { TableHeader as TiptapTableHeader } from "@tiptap/extension-table-header"; + +export const TableHeader = TiptapTableHeader.extend({ + name: "tableHeader", + content: "paragraph+", + + addAttributes() { + return { + ...this.parent?.(), + backgroundColor: { + default: null, + parseHTML: (element) => element.style.backgroundColor || null, + renderHTML: (attributes) => { + if (!attributes.backgroundColor) { + return {}; + } + return { + style: `background-color: ${attributes.backgroundColor}`, + }; + }, + }, + }; + }, +}); \ No newline at end of file diff --git a/packages/editor-ext/src/lib/table/index.ts b/packages/editor-ext/src/lib/table/index.ts index 5661ef84..656c1825 100644 --- a/packages/editor-ext/src/lib/table/index.ts +++ b/packages/editor-ext/src/lib/table/index.ts @@ -1,2 +1,3 @@ export * from "./row"; export * from "./cell"; +export * from "./header";