pdf-compile-button.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { useTranslation } from 'react-i18next'
  2. import { memo } from 'react'
  3. import classNames from 'classnames'
  4. import { useDetachCompileContext as useCompileContext } from '../../../shared/context/detach-compile-context'
  5. import { useStopOnFirstError } from '../../../shared/hooks/use-stop-on-first-error'
  6. import SplitMenu from '../../../shared/components/split-menu'
  7. import Icon from '../../../shared/components/icon'
  8. const modifierKey = /Mac/i.test(navigator.platform) ? 'Cmd' : 'Ctrl'
  9. function PdfCompileButton() {
  10. const {
  11. animateCompileDropdownArrow,
  12. autoCompile,
  13. compiling,
  14. draft,
  15. hasChanges,
  16. setAnimateCompileDropdownArrow,
  17. setAutoCompile,
  18. setDraft,
  19. setStopOnValidationError,
  20. stopOnFirstError,
  21. stopOnValidationError,
  22. startCompile,
  23. stopCompile,
  24. recompileFromScratch,
  25. } = useCompileContext()
  26. const { enableStopOnFirstError, disableStopOnFirstError } =
  27. useStopOnFirstError({ eventSource: 'dropdown' })
  28. const { t } = useTranslation()
  29. const compileButtonLabel = compiling ? `${t('compiling')}…` : t('recompile')
  30. const tooltipElement = (
  31. <>
  32. {t('recompile_pdf')}{' '}
  33. <span className="keyboard-shortcut">({modifierKey} + Enter)</span>
  34. </>
  35. )
  36. const dropdownToggleClassName = classNames({
  37. 'detach-compile-button-animate': animateCompileDropdownArrow,
  38. 'btn-striped-animated': hasChanges,
  39. })
  40. const buttonClassName = classNames({
  41. 'btn-striped-animated': hasChanges,
  42. })
  43. return (
  44. <SplitMenu
  45. bsStyle="primary"
  46. bsSize="xs"
  47. disabled={compiling}
  48. button={{
  49. tooltip: {
  50. description: tooltipElement,
  51. id: 'logs-toggle',
  52. tooltipProps: { className: 'keyboard-tooltip' },
  53. overlayProps: { delayShow: 500 },
  54. },
  55. icon: { type: 'refresh', spin: compiling },
  56. onClick: () => startCompile(),
  57. text: compileButtonLabel,
  58. className: buttonClassName,
  59. }}
  60. dropdownToggle={{
  61. 'aria-label': t('toggle_compile_options_menu'),
  62. handleAnimationEnd: () => setAnimateCompileDropdownArrow(false),
  63. className: dropdownToggleClassName,
  64. }}
  65. dropdown={{
  66. id: 'pdf-recompile-dropdown',
  67. }}
  68. >
  69. <SplitMenu.Item header>{t('auto_compile')}</SplitMenu.Item>
  70. <SplitMenu.Item onSelect={() => setAutoCompile(true)}>
  71. <Icon type={autoCompile ? 'check' : ''} fw />
  72. {t('on')}
  73. </SplitMenu.Item>
  74. <SplitMenu.Item onSelect={() => setAutoCompile(false)}>
  75. <Icon type={!autoCompile ? 'check' : ''} fw />
  76. {t('off')}
  77. </SplitMenu.Item>
  78. <SplitMenu.Item header>{t('compile_mode')}</SplitMenu.Item>
  79. <SplitMenu.Item onSelect={() => setDraft(false)}>
  80. <Icon type={!draft ? 'check' : ''} fw />
  81. {t('normal')}
  82. </SplitMenu.Item>
  83. <SplitMenu.Item onSelect={() => setDraft(true)}>
  84. <Icon type={draft ? 'check' : ''} fw />
  85. {t('fast')} <span className="subdued">[draft]</span>
  86. </SplitMenu.Item>
  87. <SplitMenu.Item header>Syntax Checks</SplitMenu.Item>
  88. <SplitMenu.Item onSelect={() => setStopOnValidationError(true)}>
  89. <Icon type={stopOnValidationError ? 'check' : ''} fw />
  90. {t('stop_on_validation_error')}
  91. </SplitMenu.Item>
  92. <SplitMenu.Item onSelect={() => setStopOnValidationError(false)}>
  93. <Icon type={!stopOnValidationError ? 'check' : ''} fw />
  94. {t('ignore_validation_errors')}
  95. </SplitMenu.Item>
  96. <SplitMenu.Item header>{t('compile_error_handling')}</SplitMenu.Item>
  97. <SplitMenu.Item onSelect={enableStopOnFirstError}>
  98. <Icon type={stopOnFirstError ? 'check' : ''} fw />
  99. {t('stop_on_first_error')}
  100. </SplitMenu.Item>
  101. <SplitMenu.Item onSelect={disableStopOnFirstError}>
  102. <Icon type={!stopOnFirstError ? 'check' : ''} fw />
  103. {t('try_to_compile_despite_errors')}
  104. </SplitMenu.Item>
  105. <SplitMenu.Item divider />
  106. <SplitMenu.Item
  107. onSelect={() => stopCompile()}
  108. disabled={!compiling}
  109. aria-disabled={!compiling}
  110. >
  111. {t('stop_compile')}
  112. </SplitMenu.Item>
  113. <SplitMenu.Item
  114. onSelect={() => recompileFromScratch()}
  115. disabled={compiling}
  116. aria-disabled={compiling}
  117. >
  118. {t('recompile_from_scratch')}
  119. </SplitMenu.Item>
  120. </SplitMenu>
  121. )
  122. }
  123. export default memo(PdfCompileButton)