editor-switch.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. function Badge() {
  6. const content = (
  7. <>
  8. Overleaf has upgraded the source editor. You can still use the old editor
  9. by selecting "Source (legacy)".
  10. <br />
  11. <br />
  12. Click to learn more and give feedback
  13. </>
  14. )
  15. return (
  16. <Tooltip
  17. id="editor-switch"
  18. description={content}
  19. overlayProps={{
  20. placement: 'bottom',
  21. delayHide: 100,
  22. }}
  23. tooltipProps={{ className: 'tooltip-wide' }}
  24. >
  25. <a
  26. href="https://forms.gle/GmSs6odZRKRp3VX98"
  27. target="_blank"
  28. rel="noopener noreferrer"
  29. className="badge info-badge"
  30. >
  31. <span className="sr-only">{content}</span>
  32. </a>
  33. </Tooltip>
  34. )
  35. }
  36. function EditorSwitch() {
  37. const [newSourceEditor, setNewSourceEditor] = useScopeValue(
  38. 'editor.newSourceEditor'
  39. )
  40. const [richText, setRichText] = useScopeValue('editor.showRichText')
  41. const handleChange = useCallback(
  42. event => {
  43. const editorType = event.target.value
  44. switch (editorType) {
  45. case 'ace':
  46. setRichText(false)
  47. setNewSourceEditor(false)
  48. break
  49. case 'cm6':
  50. setRichText(false)
  51. setNewSourceEditor(true)
  52. break
  53. case 'rich-text':
  54. setRichText(true)
  55. break
  56. }
  57. sendMB('editor-switch-change', { editorType })
  58. },
  59. [setRichText, setNewSourceEditor]
  60. )
  61. return (
  62. <div className="editor-toggle-switch">
  63. <Badge />
  64. <fieldset className="toggle-switch">
  65. <legend className="sr-only">Editor mode.</legend>
  66. <input
  67. type="radio"
  68. name="editor"
  69. value="cm6"
  70. id="editor-switch-cm6"
  71. className="toggle-switch-input"
  72. checked={!richText && !!newSourceEditor}
  73. onChange={handleChange}
  74. />
  75. <label htmlFor="editor-switch-cm6" className="toggle-switch-label">
  76. <span>Source</span>
  77. </label>
  78. <input
  79. type="radio"
  80. name="editor"
  81. value="ace"
  82. id="editor-switch-ace"
  83. className="toggle-switch-input"
  84. checked={!richText && !newSourceEditor}
  85. onChange={handleChange}
  86. />
  87. <label htmlFor="editor-switch-ace" className="toggle-switch-label">
  88. <span>Source (legacy)</span>
  89. </label>
  90. <input
  91. type="radio"
  92. name="editor"
  93. value="rich-text"
  94. id="editor-switch-rich-text"
  95. className="toggle-switch-input"
  96. checked={!!richText}
  97. onChange={handleChange}
  98. />
  99. <label
  100. htmlFor="editor-switch-rich-text"
  101. className="toggle-switch-label"
  102. >
  103. <span>Rich Text</span>
  104. </label>
  105. </fieldset>
  106. </div>
  107. )
  108. }
  109. export default memo(EditorSwitch)