import { useActive, useChainedCommands, useCommands } from '@remirror/react'; import { FiBold, FiItalic, FiType, FiUnderline, FiAlignCenter, FiAlignLeft, FiAlignRight, FiCode } from 'react-icons/fi' import { FaListOl, FaListUl, FaUndo, FaRedo } from 'react-icons/fa' import { Menu, MenuItem, MenuButton } from '@szhsin/react-menu'; import '@szhsin/react-menu/dist/index.css'; import '@szhsin/react-menu/dist/transitions/slide.css'; import { BiCodeCurly } from 'react-icons/bi'; interface Props { cmd: Command classes?: Partial<{ button: string, icon: string, active: string, enabled: string disabled: string }> } export default function ToolButton({ cmd: _cmd, classes }: Props) { const buttonClasses = classes?.button ?? `w-36 h-36 flex justify-center items-center`; const activeClasses = classes?.active ?? 'font-bold bg-gray-300 hover:bg-gray-300 text-black' const enabledClasses = classes?.enabled ?? 'hover:bg-gray-200'; const disabledClasses = classes?.disabled ?? 'opacity-40 text-gray-600 pointer-events-none' const iconClasses = classes?.icon ?? '' const commands = useCommands(); const active = useActive(); // const chain = useChainedCommands(); // commands.toggleCo // active.list const runCommand = (cmd: string, attrs?: any) => { if (commands[cmd]) { commands[cmd](attrs); commands.focus(); } } if (_cmd === 'heading') { return } transition> {Array(6).fill(0).map((_, idx) => runCommand('toggleHeading', { level: idx + 1 })} > Heading{idx + 1} )} } if (isCommand(_cmd)) { const { activeCmd, cmd, tip, Icon } = cmdToBtn[_cmd] return ( ) } return <> } const cmdToBtn = { 'bold': { cmd: 'toggleBold', activeCmd: 'bold', tip: "Bold", Icon: FiBold }, 'italic': { cmd: 'toggleItalic', activeCmd: 'italic', tip: "Italic", Icon: FiItalic }, underline: { cmd: 'toggleUnderline', activeCmd: 'underline', tip: "Underline", Icon: FiUnderline }, heading: { cmd: 'toggleHeading', activeCmd: 'heading', Icon: FiType, }, leftAlign: { cmd: 'leftAlign', activeCmd: null, tip: "Left Align", Icon: FiAlignLeft, }, centerAlign: { cmd: 'centerAlign', activeCmd: null, tip: "Center Align", Icon: FiAlignCenter, }, rightAlign: { cmd: 'rightAlign', activeCmd: null, tip: "Right Align", Icon: FiAlignRight, }, bulletList: { cmd: 'toggleBulletList', activeCmd: 'bulletList', tip: "Bullets List", Icon: FaListUl, }, orderedList: { cmd: 'toggleOrderedList', activeCmd: 'orderedList', tip: "Numbered List", Icon: FaListOl, }, undo: { cmd: 'undo', activeCmd: null, tip: "Undo", Icon: FaUndo, }, redo: { cmd: 'redo', activeCmd: null, tip: "Redo", Icon: FaRedo, }, code: { cmd: 'toggleCode', activeCmd: 'code', tip: "Code", Icon: FiCode, }, codeBlock: { cmd: 'toggleCodeBlock', activeCmd: 'codeBlock', tip: "Code Block", Icon: BiCodeCurly, }, } as const type Command = keyof typeof cmdToBtn function isCommand(cmd: string): cmd is Command { return cmd in cmdToBtn; }