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