preview-recompile-button.js 5.4 KB

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