editor-switch.tsx 4.3 KB

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