editor-switch.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { ChangeEvent, FC, memo, useCallback } from 'react'
  2. import OLTooltip from '@/shared/components/ol/ol-tooltip'
  3. import { sendMB } from '../../../infrastructure/event-tracking'
  4. import { isValidTeXFile } from '../../../main/is-valid-tex-file'
  5. import { useTranslation } from 'react-i18next'
  6. import { useEditorOpenDocContext } from '@/features/ide-react/context/editor-open-doc-context'
  7. import { useEditorPropertiesContext } from '@/features/ide-react/context/editor-properties-context'
  8. function EditorSwitch() {
  9. const { t } = useTranslation()
  10. const { showVisual: visual, setShowVisual: setVisual } =
  11. useEditorPropertiesContext()
  12. const { openDocName } = useEditorOpenDocContext()
  13. const richTextAvailable = openDocName ? isValidTeXFile(openDocName) : false
  14. const handleChange = useCallback(
  15. (event: ChangeEvent<HTMLInputElement>) => {
  16. const editorType = event.target.value
  17. switch (editorType) {
  18. case 'cm6':
  19. setVisual(false)
  20. break
  21. case 'rich-text':
  22. setVisual(true)
  23. break
  24. }
  25. sendMB('editor-switch-change', { editorType })
  26. },
  27. [setVisual]
  28. )
  29. return (
  30. <div
  31. className="editor-toggle-switch"
  32. aria-label={t('toolbar_code_visual_editor_switch')}
  33. >
  34. <fieldset className="toggle-switch">
  35. <legend className="visually-hidden">Editor mode.</legend>
  36. <input
  37. type="radio"
  38. name="editor"
  39. value="cm6"
  40. id="editor-switch-cm6"
  41. className="toggle-switch-input"
  42. checked={!richTextAvailable || !visual}
  43. onChange={handleChange}
  44. />
  45. <label htmlFor="editor-switch-cm6" className="toggle-switch-label">
  46. <span>{t('code_editor')}</span>
  47. </label>
  48. <RichTextToggle
  49. checked={richTextAvailable && visual}
  50. disabled={!richTextAvailable}
  51. handleChange={handleChange}
  52. />
  53. </fieldset>
  54. </div>
  55. )
  56. }
  57. const RichTextToggle: FC<{
  58. checked: boolean
  59. disabled: boolean
  60. handleChange: (event: ChangeEvent<HTMLInputElement>) => void
  61. }> = ({ checked, disabled, handleChange }) => {
  62. const { t } = useTranslation()
  63. const toggle = (
  64. <span>
  65. <input
  66. type="radio"
  67. name="editor"
  68. value="rich-text"
  69. id="editor-switch-rich-text"
  70. className="toggle-switch-input"
  71. checked={checked}
  72. onChange={handleChange}
  73. disabled={disabled}
  74. />
  75. <label htmlFor="editor-switch-rich-text" className="toggle-switch-label">
  76. <span>{t('visual_editor')}</span>
  77. </label>
  78. </span>
  79. )
  80. if (disabled) {
  81. return (
  82. <OLTooltip
  83. description={t('visual_editor_is_only_available_for_tex_files')}
  84. id="rich-text-toggle-tooltip"
  85. overlayProps={{ placement: 'bottom' }}
  86. tooltipProps={{ className: 'tooltip-wide' }}
  87. >
  88. {toggle}
  89. </OLTooltip>
  90. )
  91. }
  92. return (
  93. <OLTooltip
  94. id="rich-text-toggle-tooltip"
  95. description={t('toolbar_change_editor_mode')}
  96. overlayProps={{ placement: 'bottom' }}
  97. tooltipProps={{ className: 'tooltip-wide' }}
  98. >
  99. {toggle}
  100. </OLTooltip>
  101. )
  102. }
  103. export default memo(EditorSwitch)