editor-switch.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { ChangeEvent, FC, memo, useCallback } from 'react'
  2. import useScopeValue from '@/shared/hooks/use-scope-value'
  3. import Tooltip from '@/shared/components/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 className="editor-toggle-switch">
  42. <fieldset className="toggle-switch">
  43. <legend className="sr-only">Editor mode.</legend>
  44. <input
  45. type="radio"
  46. name="editor"
  47. value="cm6"
  48. id="editor-switch-cm6"
  49. className="toggle-switch-input"
  50. checked={!richTextAvailable || !visual}
  51. onChange={handleChange}
  52. />
  53. <EditorSwitchBeginnerTooltip>
  54. <label htmlFor="editor-switch-cm6" className="toggle-switch-label">
  55. <span>{t('code_editor')}</span>
  56. </label>
  57. </EditorSwitchBeginnerTooltip>
  58. <RichTextToggle
  59. checked={richTextAvailable && visual}
  60. disabled={!richTextAvailable}
  61. handleChange={handleChange}
  62. />
  63. </fieldset>
  64. </div>
  65. )
  66. }
  67. const RichTextToggle: FC<{
  68. checked: boolean
  69. disabled: boolean
  70. handleChange: (event: ChangeEvent<HTMLInputElement>) => void
  71. }> = ({ checked, disabled, handleChange }) => {
  72. const { t } = useTranslation()
  73. const toggle = (
  74. <span>
  75. <input
  76. type="radio"
  77. name="editor"
  78. value="rich-text"
  79. id="editor-switch-rich-text"
  80. className="toggle-switch-input"
  81. checked={checked}
  82. onChange={handleChange}
  83. disabled={disabled}
  84. />
  85. <label htmlFor="editor-switch-rich-text" className="toggle-switch-label">
  86. <span>{t('visual_editor')}</span>
  87. </label>
  88. </span>
  89. )
  90. if (disabled) {
  91. return (
  92. <Tooltip
  93. description={t('visual_editor_is_only_available_for_tex_files')}
  94. id="rich-text-toggle-tooltip"
  95. overlayProps={{ placement: 'bottom' }}
  96. tooltipProps={{ className: 'tooltip-wide' }}
  97. >
  98. {toggle}
  99. </Tooltip>
  100. )
  101. }
  102. return toggle
  103. }
  104. export default memo(EditorSwitch)