editor-switch.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 isValidTeXFile from '../../../main/is-valid-tex-file'
  6. import { useTranslation } from 'react-i18next'
  7. import { PromotionOverlay } from './table-generator/promotion/popover'
  8. function EditorSwitch() {
  9. const { t } = useTranslation()
  10. const [visual, setVisual] = useScopeValue('editor.showVisual')
  11. const [docName] = useScopeValue('editor.open_doc_name')
  12. const richTextAvailable = isValidTeXFile(docName)
  13. const handleChange = useCallback(
  14. event => {
  15. const editorType = event.target.value
  16. switch (editorType) {
  17. case 'cm6':
  18. setVisual(false)
  19. break
  20. case 'rich-text':
  21. setVisual(true)
  22. break
  23. }
  24. sendMB('editor-switch-change', { editorType })
  25. },
  26. [setVisual]
  27. )
  28. return (
  29. <div className="editor-toggle-switch">
  30. <fieldset className="toggle-switch">
  31. <legend className="sr-only">Editor mode.</legend>
  32. <input
  33. type="radio"
  34. name="editor"
  35. value="cm6"
  36. id="editor-switch-cm6"
  37. className="toggle-switch-input"
  38. checked={!richTextAvailable || !visual}
  39. onChange={handleChange}
  40. />
  41. <label htmlFor="editor-switch-cm6" className="toggle-switch-label">
  42. <span>{t('code_editor')}</span>
  43. </label>
  44. <RichTextToggle
  45. checked={richTextAvailable && visual}
  46. disabled={!richTextAvailable}
  47. handleChange={handleChange}
  48. />
  49. </fieldset>
  50. </div>
  51. )
  52. }
  53. const RichTextToggle: FC<{
  54. checked: boolean
  55. disabled: boolean
  56. handleChange: (event: ChangeEvent<HTMLInputElement>) => void
  57. }> = ({ checked, disabled, handleChange }) => {
  58. const { t } = useTranslation()
  59. const toggle = (
  60. <span>
  61. <input
  62. type="radio"
  63. name="editor"
  64. value="rich-text"
  65. id="editor-switch-rich-text"
  66. className="toggle-switch-input"
  67. checked={checked}
  68. onChange={handleChange}
  69. disabled={disabled}
  70. />
  71. <label htmlFor="editor-switch-rich-text" className="toggle-switch-label">
  72. <span>{t('visual_editor')}</span>
  73. </label>
  74. </span>
  75. )
  76. if (disabled) {
  77. return (
  78. <Tooltip
  79. description={t('visual_editor_is_only_available_for_tex_files')}
  80. id="rich-text-toggle-tooltip"
  81. overlayProps={{ placement: 'bottom' }}
  82. tooltipProps={{ className: 'tooltip-wide' }}
  83. >
  84. {toggle}
  85. </Tooltip>
  86. )
  87. }
  88. return <PromotionOverlay>{toggle}</PromotionOverlay>
  89. }
  90. export default memo(EditorSwitch)