"use client"; import { Button, ButtonGroup, Grid, IconButton, ToggleButton, ToggleButtonGroup, } from "@mui/material"; import "./MailField.css"; import { useEditor, EditorContent, Editor } from "@tiptap/react"; import FormatBoldIcon from "@mui/icons-material/FormatBold"; import React, { useCallback } from "react"; import { FormatItalic, FormatUnderlined } from "@mui/icons-material"; import { MuiColorInput, MuiColorInputColors, MuiColorInputValue, } from "mui-color-input"; import BorderColorIcon from "@mui/icons-material/BorderColor"; import ExpandMoreIcon from "@mui/icons-material/ExpandMore"; import FormatColorTextIcon from "@mui/icons-material/FormatColorText"; import FormatAlignLeftIcon from "@mui/icons-material/FormatAlignLeft"; import FormatAlignJustifyIcon from "@mui/icons-material/FormatAlignJustify"; import FormatAlignRightIcon from "@mui/icons-material/FormatAlignRight"; import { useTranslation } from "react-i18next"; import GridOnIcon from "@mui/icons-material/GridOn"; interface Props { editor: Editor | null; } const colorInputSx = { width: 150, height: 25, ".MuiInputBase-colorPrimary": { margin: -1, borderRadius: "0px 5px 5px 0px", borderColor: "rgba(0, 0, 0, 0)", backgroundColor: "rgba(0, 0, 0, 0)", }, ".Mui-focused": { borderColor: "rgba(0, 0, 0, 0)", }, ".MuiColorInput-Button": { marginBottom: 2, borderColor: "rgba(0, 0, 0, 0)", backgroundColor: "rgba(0, 0, 0, 0)", }, ".MuiInputBase-input": { marginBottom: 1.5, }, }; const fontFamily = [ { label: "Arial", value: "Arial", }, { label: "Times New Roman", value: "Times New Roman", }, { label: "Courier New", value: "Courier New", }, { label: "Georgia", value: "Georgia", }, {}, ]; const MailToolbar: React.FC = ({ editor }) => { const { t } = useTranslation(); if (editor == null) { return null; } const [fontStyle, setFontStyle] = React.useState(["alignLeft"]); const [colorHighlightValue, setColorHighlightValue] = React.useState("red"); const [colorTextValue, setColorTextValue] = React.useState("black"); const colorHighlightValueInputRef = React.useRef(null); const colorTextValueInputRef = React.useRef(null); const handleFontStyle = useCallback( (event: React.MouseEvent, newFontStyles: string[]) => { setFontStyle((prev) => { const id = event.currentTarget?.id; const include = prev.includes(id); if (include) { return prev.filter((ele) => ele !== id); } else { prev = prev.filter((ele) => !ele.includes("align")); prev.push(id); return prev; } }); }, [], ); const handleColorHighlightValue = useCallback( (value: string, colors: MuiColorInputColors) => { // console.log(colors) setColorHighlightValue(() => value); // editor.chain().focus().toggleHighlight({ color: value }).run() }, [], ); const handleColorHighlightValueClick = useCallback( (event: React.MouseEvent) => { editor .chain() .focus() .toggleHighlight({ color: colorHighlightValue.toString() }) .run(); }, [colorHighlightValue], ); const handleColorHighlightValueClose = useCallback( (event: {}, reason: "backdropClick" | "escapeKeyDown") => { // console.log(event) editor .chain() .focus() .toggleHighlight({ color: colorHighlightValue.toString() }) .run(); }, [colorHighlightValue], ); const handleColorHighlightValueBlur = useCallback( (event: React.FocusEvent) => { editor .chain() .focus() .toggleHighlight({ color: colorHighlightValue.toString() }) .run(); }, [colorHighlightValue], ); const handleColorTextValue = useCallback( (value: string, colors: MuiColorInputColors) => { // console.log(colors) setColorTextValue(() => value); // if (editor.isActive("textStyle")) { // editor.chain().focus().unsetColor().run() // } else { // editor.chain().focus().setColor(value).run() // } }, [], ); const handleColorTextValueClick = useCallback( (event: React.MouseEvent) => { if (editor.isActive("textStyle", { color: colorTextValue.toString() })) { editor.chain().focus().unsetColor().run(); } else { editor.chain().focus().setColor(colorTextValue.toString()).run(); } }, [colorTextValue], ); const handleColorTextValueClose = useCallback( (event: {}, reason: "backdropClick" | "escapeKeyDown") => { if (editor.isActive("textStyle", { color: colorTextValue.toString() })) { editor.chain().focus().unsetColor().run(); } else { editor.chain().focus().setColor(colorTextValue.toString()).run(); } }, [colorTextValue], ); const handleColorTextValueBlur = useCallback( (event: React.FocusEvent) => { if (editor.isActive("textStyle", { color: colorTextValue.toString() })) { editor.chain().focus().unsetColor().run(); } else { editor.chain().focus().setColor(colorTextValue.toString()).run(); } }, [colorTextValue], ); const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === "Enter") { if (colorHighlightValueInputRef.current !== null) { colorHighlightValueInputRef.current.blur(); } if (colorTextValueInputRef.current !== null) { colorTextValueInputRef.current.blur(); } } }; const handleTextAlign = useCallback( (event: React.MouseEvent, value: any) => { console.log(value); switch (value) { case "alignLeft": if (editor.isActive({ textAlign: "left" })) { editor.chain().focus().unsetTextAlign().run(); } else { editor.chain().focus().setTextAlign("left").run(); } break; case "alignCenter": if (editor.isActive({ textAlign: "center" })) { editor.chain().focus().unsetTextAlign().run(); } else { editor.chain().focus().setTextAlign("center").run(); } break; case "alignRight": if (editor.isActive({ textAlign: "right" })) { editor.chain().focus().unsetTextAlign().run(); } else { editor.chain().focus().setTextAlign("right").run(); } break; default: break; } }, [], ); const handleInsertTable = useCallback(() => { editor .chain() .focus() .insertTable({ rows: 3, cols: 3, withHeaderRow: true }) .run(); }, []); React.useEffect(() => { editor.on("selectionUpdate", ({ editor }) => { const currentFormatList: string[] = []; if (editor.isActive("bold")) { currentFormatList.push("bold"); } if (editor.isActive("italic")) { currentFormatList.push("italic"); } if (editor.isActive("underline")) { currentFormatList.push("underline"); } if ( editor.isActive("highlight", { color: colorHighlightValue.toString() }) ) { currentFormatList.push("highlight"); } if (editor.isActive("textStyle", { color: colorTextValue.toString() })) { currentFormatList.push("textStyle"); } if (editor.isActive({ textAlign: "left" })) { currentFormatList.push("alignLeft"); } if (editor.isActive({ textAlign: "center" })) { currentFormatList.push("alignCenter"); } if (editor.isActive({ textAlign: "right" })) { currentFormatList.push("alignRight"); } console.log(currentFormatList); setFontStyle(() => currentFormatList); }); }, [editor]); return ( editor.chain().focus().toggleBold().run()} > editor.chain().focus().toggleItalic().run()} > editor.chain().focus().toggleUnderline().run()} > {/* console.log("Expand more")}> */} editor .chain() .focus() .insertTable({ rows: 3, cols: 3, withHeaderRow: true }) .run() } > {t("Insert Table")} editor.chain().focus().deleteTable().run()} > {t("Delete table")} editor.chain().focus().addColumnBefore().run()} > {t("Add column before")} editor.chain().focus().addColumnAfter().run()} > {t("Add column after")} editor.chain().focus().deleteColumn().run()} > {t("Delete column")} editor.chain().focus().addRowBefore().run()} > {t("Add row before")} editor.chain().focus().addRowAfter().run()} > {t("Add row after")} editor.chain().focus().deleteRow().run()} > {t("Delete row")} editor.chain().focus().mergeCells().run()} > {t("Merge cells")} editor.chain().focus().splitCell().run()} > {t("Split cell")} editor.chain().focus().toggleHeaderColumn().run()} > {t("Toggle header column")} editor.chain().focus().toggleHeaderRow().run()} > {t("Toggle header row")} editor.chain().focus().toggleHeaderCell().run()} > {t("Toggle header cell")} editor.chain().focus().mergeOrSplit().run()} > {t("Merge or split")} editor.chain().focus().setCellAttribute("colspan", 2).run() } > {t("Set cell attribute")} editor.chain().focus().fixTables().run()} > {t("Fix tables")} editor.chain().focus().goToNextCell().run()} > {t("Go to next cell")} editor.chain().focus().goToPreviousCell().run()} > {t("Go to previous cell")} ); }; export default MailToolbar;