command.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {
  2. memo,
  3. useMemo,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. RiBold,
  8. RiItalic,
  9. RiLink,
  10. RiListUnordered,
  11. RiStrikethrough,
  12. } from '@remixicon/react'
  13. import { useStore } from '../store'
  14. import { useCommand } from './hooks'
  15. import cn from '@/utils/classnames'
  16. import Tooltip from '@/app/components/base/tooltip'
  17. type CommandProps = {
  18. type: 'bold' | 'italic' | 'strikethrough' | 'link' | 'bullet'
  19. }
  20. const Command = ({
  21. type,
  22. }: CommandProps) => {
  23. const { t } = useTranslation()
  24. const selectedIsBold = useStore(s => s.selectedIsBold)
  25. const selectedIsItalic = useStore(s => s.selectedIsItalic)
  26. const selectedIsStrikeThrough = useStore(s => s.selectedIsStrikeThrough)
  27. const selectedIsLink = useStore(s => s.selectedIsLink)
  28. const selectedIsBullet = useStore(s => s.selectedIsBullet)
  29. const { handleCommand } = useCommand()
  30. const icon = useMemo(() => {
  31. switch (type) {
  32. case 'bold':
  33. return <RiBold className={cn('w-4 h-4', selectedIsBold && 'text-primary-600')} />
  34. case 'italic':
  35. return <RiItalic className={cn('w-4 h-4', selectedIsItalic && 'text-primary-600')} />
  36. case 'strikethrough':
  37. return <RiStrikethrough className={cn('w-4 h-4', selectedIsStrikeThrough && 'text-primary-600')} />
  38. case 'link':
  39. return <RiLink className={cn('w-4 h-4', selectedIsLink && 'text-primary-600')} />
  40. case 'bullet':
  41. return <RiListUnordered className={cn('w-4 h-4', selectedIsBullet && 'text-primary-600')} />
  42. }
  43. }, [type, selectedIsBold, selectedIsItalic, selectedIsStrikeThrough, selectedIsLink, selectedIsBullet])
  44. const tip = useMemo(() => {
  45. switch (type) {
  46. case 'bold':
  47. return t('workflow.nodes.note.editor.bold')
  48. case 'italic':
  49. return t('workflow.nodes.note.editor.italic')
  50. case 'strikethrough':
  51. return t('workflow.nodes.note.editor.strikethrough')
  52. case 'link':
  53. return t('workflow.nodes.note.editor.link')
  54. case 'bullet':
  55. return t('workflow.nodes.note.editor.bulletList')
  56. }
  57. }, [type, t])
  58. return (
  59. <Tooltip
  60. popupContent={tip}
  61. >
  62. <div
  63. className={cn(
  64. 'flex items-center justify-center w-8 h-8 cursor-pointer rounded-md text-gray-500 hover:text-gray-800 hover:bg-black/5',
  65. type === 'bold' && selectedIsBold && 'bg-primary-50',
  66. type === 'italic' && selectedIsItalic && 'bg-primary-50',
  67. type === 'strikethrough' && selectedIsStrikeThrough && 'bg-primary-50',
  68. type === 'link' && selectedIsLink && 'bg-primary-50',
  69. type === 'bullet' && selectedIsBullet && 'bg-primary-50',
  70. )}
  71. onClick={() => handleCommand(type)}
  72. >
  73. {icon}
  74. </div>
  75. </Tooltip>
  76. )
  77. }
  78. export default memo(Command)