pdf-compile-button.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. 'no-left-radius': true,
  43. })
  44. return (
  45. <SplitMenu
  46. bsStyle="primary"
  47. bsSize="xs"
  48. disabled={compiling}
  49. button={{
  50. tooltip: {
  51. description: tooltipElement,
  52. id: 'compile',
  53. tooltipProps: { className: 'keyboard-tooltip' },
  54. overlayProps: { delayShow: 500 },
  55. },
  56. icon: { type: 'refresh', spin: compiling },
  57. onClick: () => startCompile(),
  58. text: compileButtonLabel,
  59. className: buttonClassName,
  60. }}
  61. dropdownToggle={{
  62. 'aria-label': t('toggle_compile_options_menu'),
  63. handleAnimationEnd: () => setAnimateCompileDropdownArrow(false),
  64. className: dropdownToggleClassName,
  65. }}
  66. dropdown={{
  67. id: 'pdf-recompile-dropdown',
  68. }}
  69. >
  70. <SplitMenu.Item header>{t('auto_compile')}</SplitMenu.Item>
  71. <SplitMenu.Item onSelect={() => setAutoCompile(true)}>
  72. <Icon type={autoCompile ? 'check' : ''} fw />
  73. {t('on')}
  74. </SplitMenu.Item>
  75. <SplitMenu.Item onSelect={() => setAutoCompile(false)}>
  76. <Icon type={!autoCompile ? 'check' : ''} fw />
  77. {t('off')}
  78. </SplitMenu.Item>
  79. <SplitMenu.Item header>{t('compile_mode')}</SplitMenu.Item>
  80. <SplitMenu.Item onSelect={() => setDraft(false)}>
  81. <Icon type={!draft ? 'check' : ''} fw />
  82. {t('normal')}
  83. </SplitMenu.Item>
  84. <SplitMenu.Item onSelect={() => setDraft(true)}>
  85. <Icon type={draft ? 'check' : ''} fw />
  86. {t('fast')} <span className="subdued">[draft]</span>
  87. </SplitMenu.Item>
  88. <SplitMenu.Item header>Syntax Checks</SplitMenu.Item>
  89. <SplitMenu.Item onSelect={() => setStopOnValidationError(true)}>
  90. <Icon type={stopOnValidationError ? 'check' : ''} fw />
  91. {t('stop_on_validation_error')}
  92. </SplitMenu.Item>
  93. <SplitMenu.Item onSelect={() => setStopOnValidationError(false)}>
  94. <Icon type={!stopOnValidationError ? 'check' : ''} fw />
  95. {t('ignore_validation_errors')}
  96. </SplitMenu.Item>
  97. <SplitMenu.Item header>{t('compile_error_handling')}</SplitMenu.Item>
  98. <SplitMenu.Item onSelect={enableStopOnFirstError}>
  99. <Icon type={stopOnFirstError ? 'check' : ''} fw />
  100. {t('stop_on_first_error')}
  101. </SplitMenu.Item>
  102. <SplitMenu.Item onSelect={disableStopOnFirstError}>
  103. <Icon type={!stopOnFirstError ? 'check' : ''} fw />
  104. {t('try_to_compile_despite_errors')}
  105. </SplitMenu.Item>
  106. <SplitMenu.Item divider />
  107. <SplitMenu.Item
  108. onSelect={() => stopCompile()}
  109. disabled={!compiling}
  110. aria-disabled={!compiling}
  111. >
  112. {t('stop_compile')}
  113. </SplitMenu.Item>
  114. <SplitMenu.Item
  115. onSelect={() => recompileFromScratch()}
  116. disabled={compiling}
  117. aria-disabled={compiling}
  118. >
  119. {t('recompile_from_scratch')}
  120. </SplitMenu.Item>
  121. </SplitMenu>
  122. )
  123. }
  124. export default memo(PdfCompileButton)