section-heading-dropdown.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {
  2. useCodeMirrorStateContext,
  3. useCodeMirrorViewContext,
  4. } from '../codemirror-context'
  5. import {
  6. findCurrentSectionHeadingLevel,
  7. setSectionHeadingLevel,
  8. } from '../../extensions/toolbar/sections'
  9. import { useMemo } from 'react'
  10. import { emitToolbarEvent } from '../../extensions/toolbar/utils/analytics'
  11. import MaterialIcon from '@/shared/components/material-icon'
  12. import { useTranslation } from 'react-i18next'
  13. import { ToolbarButtonMenu } from './button-menu'
  14. import OLListGroupItem from '@/shared/components/ol/ol-list-group-item'
  15. const levels = new Map([
  16. ['text', 'Normal text'],
  17. ['section', 'Section'],
  18. ['subsection', 'Subsection'],
  19. ['subsubsection', 'Subsubsection'],
  20. ['paragraph', 'Paragraph'],
  21. ['subparagraph', 'Subparagraph'],
  22. ])
  23. const levelsEntries = [...levels.entries()]
  24. export const SectionHeadingDropdown = () => {
  25. const state = useCodeMirrorStateContext()
  26. const view = useCodeMirrorViewContext()
  27. const { t } = useTranslation()
  28. const currentLevel = useMemo(
  29. () => findCurrentSectionHeadingLevel(state),
  30. [state]
  31. )
  32. return (
  33. <ToolbarButtonMenu
  34. id="section-heading-menu-button"
  35. label={t('toolbar_section_heading_level')}
  36. className="ol-cm-toolbar-button-wide"
  37. icon={
  38. <>
  39. <MaterialIcon
  40. type="text_fields"
  41. style={{ transform: 'scaleX(-1)' }}
  42. />
  43. <MaterialIcon type="expand_more" />
  44. </>
  45. }
  46. >
  47. {levelsEntries.map(([level, label]) => (
  48. <OLListGroupItem
  49. role="menuitem"
  50. key={level}
  51. active={level === currentLevel?.level}
  52. onClick={() => {
  53. emitToolbarEvent(view, 'section-level-change')
  54. setSectionHeadingLevel(view, level)
  55. view.focus()
  56. }}
  57. className={`ol-cm-section-heading-menu-item section-level-${level}`}
  58. >
  59. {label}
  60. </OLListGroupItem>
  61. ))}
  62. </ToolbarButtonMenu>
  63. )
  64. }