command.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {
  2. memo,
  3. useMemo,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import cn from 'classnames'
  7. import { useStore } from '../store'
  8. import { useCommand } from './hooks'
  9. import { Link01 } from '@/app/components/base/icons/src/vender/line/general'
  10. import {
  11. Bold01,
  12. Dotpoints01,
  13. Italic01,
  14. Strikethrough01,
  15. } from '@/app/components/base/icons/src/vender/line/editor'
  16. import TooltipPlus from '@/app/components/base/tooltip-plus'
  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 <Bold01 className={cn('w-4 h-4', selectedIsBold && 'text-primary-600')} />
  34. case 'italic':
  35. return <Italic01 className={cn('w-4 h-4', selectedIsItalic && 'text-primary-600')} />
  36. case 'strikethrough':
  37. return <Strikethrough01 className={cn('w-4 h-4', selectedIsStrikeThrough && 'text-primary-600')} />
  38. case 'link':
  39. return <Link01 className={cn('w-4 h-4', selectedIsLink && 'text-primary-600')} />
  40. case 'bullet':
  41. return <Dotpoints01 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. <TooltipPlus popupContent={tip}>
  60. <div
  61. className={cn(
  62. 'flex items-center justify-center w-8 h-8 cursor-pointer rounded-md text-gray-500 hover:text-gray-800 hover:bg-black/5',
  63. type === 'bold' && selectedIsBold && 'bg-primary-50',
  64. type === 'italic' && selectedIsItalic && 'bg-primary-50',
  65. type === 'strikethrough' && selectedIsStrikeThrough && 'bg-primary-50',
  66. type === 'link' && selectedIsLink && 'bg-primary-50',
  67. type === 'bullet' && selectedIsBullet && 'bg-primary-50',
  68. )}
  69. onClick={() => handleCommand(type)}
  70. >
  71. {icon}
  72. </div>
  73. </TooltipPlus>
  74. )
  75. }
  76. export default memo(Command)