editor-switch-legacy.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 { sendMB } from '../../../infrastructure/event-tracking'
  5. import getMeta from '../../../utils/meta'
  6. import isValidTeXFile from '../../../main/is-valid-tex-file'
  7. import { useTranslation } from 'react-i18next'
  8. import SplitTestBadge from '../../../shared/components/split-test-badge'
  9. function Badge() {
  10. const content = (
  11. <>
  12. Overleaf has upgraded the source editor.
  13. <br />
  14. You can still use the old editor by selecting "Source (legacy)".
  15. <br />
  16. <br />
  17. Click to learn more and give feedback
  18. </>
  19. )
  20. return (
  21. <Tooltip
  22. id="editor-switch"
  23. description={content}
  24. overlayProps={{
  25. placement: 'bottom',
  26. delayHide: 100,
  27. }}
  28. tooltipProps={{ className: 'tooltip-wide' }}
  29. >
  30. <a
  31. href="https://forms.gle/GmSs6odZRKRp3VX98"
  32. target="_blank"
  33. rel="noopener noreferrer"
  34. className="info-badge"
  35. >
  36. <span className="sr-only">{content}</span>
  37. </a>
  38. </Tooltip>
  39. )
  40. }
  41. const showLegacySourceEditor: boolean = getMeta('ol-showLegacySourceEditor')
  42. function EditorSwitch() {
  43. const { t } = useTranslation()
  44. const [newSourceEditor, setNewSourceEditor] = useScopeValue(
  45. 'editor.newSourceEditor'
  46. )
  47. const [visual, setVisual] = useScopeValue('editor.showVisual')
  48. const [docName] = useScopeValue('editor.open_doc_name')
  49. const richTextAvailable = isValidTeXFile(docName)
  50. // TODO: rename this after legacy & toolbar split tests are complete
  51. const richTextOrVisual = richTextAvailable && visual
  52. const handleChange = useCallback(
  53. event => {
  54. const editorType = event.target.value
  55. switch (editorType) {
  56. case 'ace':
  57. setVisual(false)
  58. setNewSourceEditor(false)
  59. break
  60. case 'cm6':
  61. setVisual(false)
  62. setNewSourceEditor(true)
  63. break
  64. case 'rich-text':
  65. setVisual(true)
  66. setNewSourceEditor(true)
  67. break
  68. }
  69. sendMB('editor-switch-change', { editorType })
  70. },
  71. [setVisual, setNewSourceEditor]
  72. )
  73. return (
  74. <div className="editor-toggle-switch">
  75. {showLegacySourceEditor ? <Badge /> : null}
  76. <fieldset className="toggle-switch">
  77. <legend className="sr-only">Editor mode.</legend>
  78. <input
  79. type="radio"
  80. name="editor"
  81. value="cm6"
  82. id="editor-switch-cm6"
  83. className="toggle-switch-input"
  84. checked={!richTextOrVisual && !!newSourceEditor}
  85. onChange={handleChange}
  86. />
  87. <label htmlFor="editor-switch-cm6" className="toggle-switch-label">
  88. <span>{t('code_editor')}</span>
  89. </label>
  90. {showLegacySourceEditor ? (
  91. <>
  92. <input
  93. type="radio"
  94. name="editor"
  95. value="ace"
  96. id="editor-switch-ace"
  97. className="toggle-switch-input"
  98. checked={!richTextOrVisual && !newSourceEditor}
  99. onChange={handleChange}
  100. />
  101. <label htmlFor="editor-switch-ace" className="toggle-switch-label">
  102. <span>Source (legacy)</span>
  103. </label>
  104. </>
  105. ) : null}
  106. <RichTextToggle
  107. checked={!!richTextOrVisual}
  108. disabled={!richTextAvailable}
  109. handleChange={handleChange}
  110. />
  111. </fieldset>
  112. {!!richTextOrVisual && (
  113. <SplitTestBadge splitTestName="rich-text" displayOnVariants={['cm6']} />
  114. )}
  115. </div>
  116. )
  117. }
  118. const RichTextToggle: FC<{
  119. checked: boolean
  120. disabled: boolean
  121. handleChange: (event: ChangeEvent<HTMLInputElement>) => void
  122. }> = ({ checked, disabled, handleChange }) => {
  123. const { t } = useTranslation()
  124. const toggle = (
  125. <span>
  126. <input
  127. type="radio"
  128. name="editor"
  129. value="rich-text"
  130. id="editor-switch-rich-text"
  131. className="toggle-switch-input"
  132. checked={checked}
  133. onChange={handleChange}
  134. disabled={disabled}
  135. />
  136. <label htmlFor="editor-switch-rich-text" className="toggle-switch-label">
  137. <span>{t('visual_editor')}</span>
  138. </label>
  139. </span>
  140. )
  141. if (disabled) {
  142. return (
  143. <Tooltip
  144. description={t('visual_editor_is_only_available_for_tex_files')}
  145. id="rich-text-toggle-tooltip"
  146. overlayProps={{ placement: 'bottom' }}
  147. tooltipProps={{ className: 'tooltip-wide' }}
  148. >
  149. {toggle}
  150. </Tooltip>
  151. )
  152. }
  153. return toggle
  154. }
  155. export default memo(EditorSwitch)