editor-switch.tsx 3.3 KB

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