preview-recompile-button.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import React from 'react'
  2. import PropTypes from 'prop-types'
  3. import { Dropdown, MenuItem, OverlayTrigger, Tooltip } from 'react-bootstrap'
  4. import { useTranslation } from 'react-i18next'
  5. import Icon from '../../../shared/components/icon'
  6. function PreviewRecompileButton({
  7. compilerState: {
  8. isAutoCompileOn,
  9. isCompiling,
  10. isDraftModeOn,
  11. isSyntaxCheckOn
  12. },
  13. onRecompile,
  14. onRecompileFromScratch,
  15. onRunSyntaxCheckNow,
  16. onStopCompilation,
  17. onSetAutoCompile,
  18. onSetDraftMode,
  19. onSetSyntaxCheck,
  20. showText
  21. }) {
  22. const { t } = useTranslation()
  23. function handleSelectAutoCompileOn() {
  24. onSetAutoCompile(true)
  25. }
  26. function handleSelectAutoCompileOff() {
  27. onSetAutoCompile(false)
  28. }
  29. function handleSelectDraftModeOn() {
  30. onSetDraftMode(true)
  31. }
  32. function handleSelectDraftModeOff() {
  33. onSetDraftMode(false)
  34. }
  35. function handleSelectSyntaxCheckOn() {
  36. onSetSyntaxCheck(true)
  37. }
  38. function handleSelectSyntaxCheckOff() {
  39. onSetSyntaxCheck(false)
  40. }
  41. let compilingProps = {}
  42. let recompileProps = {}
  43. function _hideText(keepAria) {
  44. return {
  45. 'aria-hidden': !keepAria,
  46. style: {
  47. position: 'absolute',
  48. right: '-100vw'
  49. }
  50. }
  51. }
  52. if (!showText) {
  53. compilingProps = _hideText(isCompiling)
  54. recompileProps = _hideText(!isCompiling)
  55. } else if (isCompiling) {
  56. recompileProps = _hideText()
  57. } else {
  58. compilingProps = _hideText()
  59. }
  60. const buttonElement = (
  61. <Dropdown
  62. id="pdf-recompile-dropdown"
  63. className="btn-recompile-group toolbar-item"
  64. >
  65. <button className="btn btn-recompile" onClick={onRecompile}>
  66. <Icon type="refresh" spin={isCompiling} />
  67. <span id="text-compiling" className="toolbar-text" {...compilingProps}>
  68. {t('compiling')}
  69. &hellip;
  70. </span>
  71. <span id="text-recompile" className="toolbar-text" {...recompileProps}>
  72. {t('recompile')}
  73. </span>
  74. </button>
  75. <Dropdown.Toggle
  76. aria-label={t('toggle_compile_options_menu')}
  77. className="btn btn-recompile"
  78. bsStyle="success"
  79. />
  80. <Dropdown.Menu>
  81. <MenuItem header>{t('auto_compile')}</MenuItem>
  82. <MenuItem onSelect={handleSelectAutoCompileOn}>
  83. <Icon type={isAutoCompileOn ? 'check' : ''} modifier="fw" />
  84. {t('on')}
  85. </MenuItem>
  86. <MenuItem onSelect={handleSelectAutoCompileOff}>
  87. <Icon type={!isAutoCompileOn ? 'check' : ''} modifier="fw" />
  88. {t('off')}
  89. </MenuItem>
  90. <MenuItem header>{t('compile_mode')}</MenuItem>
  91. <MenuItem onSelect={handleSelectDraftModeOff}>
  92. <Icon type={!isDraftModeOn ? 'check' : ''} modifier="fw" />
  93. {t('normal')}
  94. </MenuItem>
  95. <MenuItem onSelect={handleSelectDraftModeOn}>
  96. <Icon type={isDraftModeOn ? 'check' : ''} modifier="fw" />
  97. {t('fast')} <span className="subdued">[draft]</span>
  98. </MenuItem>
  99. <MenuItem header>Syntax Checks</MenuItem>
  100. <MenuItem onSelect={handleSelectSyntaxCheckOn}>
  101. <Icon type={isSyntaxCheckOn ? 'check' : ''} modifier="fw" />
  102. {t('stop_on_validation_error')}
  103. </MenuItem>
  104. <MenuItem onSelect={handleSelectSyntaxCheckOff}>
  105. <Icon type={!isSyntaxCheckOn ? 'check' : ''} modifier="fw" />
  106. {t('ignore_validation_errors')}
  107. </MenuItem>
  108. <MenuItem onSelect={onRunSyntaxCheckNow}>
  109. <Icon type="" modifier="fw" />
  110. {t('run_syntax_check_now')}
  111. </MenuItem>
  112. <MenuItem className={!isCompiling ? 'hidden' : ''} divider />
  113. <MenuItem
  114. onSelect={onStopCompilation}
  115. className={!isCompiling ? 'hidden' : ''}
  116. disabled={!isCompiling}
  117. aria-disabled={!isCompiling}
  118. >
  119. {t('stop_compile')}
  120. </MenuItem>
  121. <MenuItem divider />
  122. <MenuItem
  123. onSelect={onRecompileFromScratch}
  124. disabled={isCompiling}
  125. aria-disabled={!!isCompiling}
  126. >
  127. {t('recompile_from_scratch')}
  128. </MenuItem>
  129. </Dropdown.Menu>
  130. </Dropdown>
  131. )
  132. return showText ? (
  133. buttonElement
  134. ) : (
  135. <OverlayTrigger
  136. placement="bottom"
  137. overlay={
  138. <Tooltip id="tooltip-download-pdf">
  139. {isCompiling ? t('compiling') : t('recompile')}
  140. </Tooltip>
  141. }
  142. >
  143. {buttonElement}
  144. </OverlayTrigger>
  145. )
  146. }
  147. PreviewRecompileButton.propTypes = {
  148. compilerState: PropTypes.shape({
  149. isAutoCompileOn: PropTypes.bool.isRequired,
  150. isCompiling: PropTypes.bool.isRequired,
  151. isDraftModeOn: PropTypes.bool.isRequired,
  152. isSyntaxCheckOn: PropTypes.bool.isRequired,
  153. logEntries: PropTypes.object.isRequired
  154. }),
  155. onRecompile: PropTypes.func.isRequired,
  156. onRecompileFromScratch: PropTypes.func.isRequired,
  157. onRunSyntaxCheckNow: PropTypes.func.isRequired,
  158. onSetAutoCompile: PropTypes.func.isRequired,
  159. onSetDraftMode: PropTypes.func.isRequired,
  160. onSetSyntaxCheck: PropTypes.func.isRequired,
  161. onStopCompilation: PropTypes.func.isRequired,
  162. showText: PropTypes.bool.isRequired
  163. }
  164. export default PreviewRecompileButton