editor-switch.tsx 2.9 KB

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