pdf-compile-button.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 * as eventTracking from '../../../infrastructure/event-tracking'
  7. import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
  8. import {
  9. DropdownToggleCustom,
  10. Dropdown,
  11. DropdownDivider,
  12. DropdownHeader,
  13. DropdownItem,
  14. DropdownMenu,
  15. DropdownToggle,
  16. } from '@/features/ui/components/bootstrap-5/dropdown-menu'
  17. import OLButton from '@/features/ui/components/ol/ol-button'
  18. import OLButtonGroup from '@/features/ui/components/ol/ol-button-group'
  19. import { useLayoutContext } from '@/shared/context/layout-context'
  20. const modifierKey = /Mac/i.test(navigator.platform) ? 'Cmd' : 'Ctrl'
  21. function sendEventAndSet<T extends boolean>(
  22. value: T,
  23. setter: (value: T) => void,
  24. settingName: string
  25. ) {
  26. eventTracking.sendMB('recompile-setting-changed', {
  27. setting: settingName,
  28. settingVal: value,
  29. })
  30. setter(value)
  31. }
  32. function PdfCompileButton() {
  33. const {
  34. animateCompileDropdownArrow,
  35. autoCompile,
  36. compiling,
  37. draft,
  38. hasChanges,
  39. setAutoCompile,
  40. setDraft,
  41. setStopOnValidationError,
  42. stopOnFirstError,
  43. stopOnValidationError,
  44. startCompile,
  45. stopCompile,
  46. recompileFromScratch,
  47. } = useCompileContext()
  48. const { enableStopOnFirstError, disableStopOnFirstError } =
  49. useStopOnFirstError({ eventSource: 'dropdown' })
  50. const { t } = useTranslation()
  51. const { detachRole } = useLayoutContext()
  52. const fromScratchWithEvent = () => {
  53. eventTracking.sendMB('recompile-setting-changed', {
  54. setting: 'from-scratch',
  55. })
  56. recompileFromScratch()
  57. }
  58. const tooltipElement = (
  59. <>
  60. {t('recompile_pdf')}{' '}
  61. <span className="keyboard-shortcut">({modifierKey} + Enter)</span>
  62. </>
  63. )
  64. const dropdownToggleClassName = classNames(
  65. {
  66. 'detach-compile-button-animate': animateCompileDropdownArrow,
  67. 'btn-striped-animated': hasChanges,
  68. },
  69. 'no-left-border',
  70. 'dropdown-button-toggle',
  71. 'compile-dropdown-toggle'
  72. )
  73. const buttonClassName = classNames(
  74. 'align-items-center py-0 no-left-radius px-3',
  75. 'compile-button',
  76. {
  77. 'btn-striped-animated': hasChanges,
  78. }
  79. )
  80. return (
  81. <Dropdown as={OLButtonGroup} className="compile-button-group">
  82. <OLTooltip
  83. description={tooltipElement}
  84. id="compile"
  85. tooltipProps={{ className: 'keyboard-tooltip' }}
  86. overlayProps={{
  87. delay: { show: 500, hide: 0 },
  88. placement: detachRole === 'detached' ? 'bottom' : undefined,
  89. }}
  90. >
  91. <OLButton
  92. variant="primary"
  93. disabled={compiling}
  94. isLoading={compiling}
  95. onClick={() => startCompile()}
  96. className={buttonClassName}
  97. loadingLabel={`${t('compiling')}…`}
  98. >
  99. {t('recompile')}
  100. </OLButton>
  101. </OLTooltip>
  102. <DropdownToggle
  103. as={DropdownToggleCustom}
  104. split
  105. variant="primary"
  106. id="pdf-recompile-dropdown"
  107. size="sm"
  108. aria-label={t('toggle_compile_options_menu')}
  109. className={dropdownToggleClassName}
  110. />
  111. <DropdownMenu>
  112. <DropdownHeader>{t('auto_compile')}</DropdownHeader>
  113. <li role="none">
  114. <DropdownItem
  115. as="button"
  116. onClick={() =>
  117. sendEventAndSet(true, setAutoCompile, 'auto-compile')
  118. }
  119. trailingIcon={autoCompile ? 'check' : null}
  120. >
  121. {t('on')}
  122. </DropdownItem>
  123. </li>
  124. <li role="none">
  125. <DropdownItem
  126. as="button"
  127. onClick={() =>
  128. sendEventAndSet(false, setAutoCompile, 'auto-compile')
  129. }
  130. trailingIcon={!autoCompile ? 'check' : null}
  131. >
  132. {t('off')}
  133. </DropdownItem>
  134. </li>
  135. <DropdownDivider />
  136. <DropdownHeader>{t('compile_mode')}</DropdownHeader>
  137. <li role="none">
  138. <DropdownItem
  139. as="button"
  140. onClick={() => sendEventAndSet(false, setDraft, 'compile-mode')}
  141. trailingIcon={!draft ? 'check' : null}
  142. >
  143. {t('normal')}
  144. </DropdownItem>
  145. </li>
  146. <li role="none">
  147. <DropdownItem
  148. as="button"
  149. onClick={() => sendEventAndSet(true, setDraft, 'compile-mode')}
  150. trailingIcon={draft ? 'check' : null}
  151. >
  152. {t('fast')}&nbsp;<span className="subdued">[draft]</span>
  153. </DropdownItem>
  154. </li>
  155. <DropdownDivider />
  156. <DropdownHeader>Syntax Checks</DropdownHeader>
  157. <li role="none">
  158. <DropdownItem
  159. as="button"
  160. onClick={() =>
  161. sendEventAndSet(true, setStopOnValidationError, 'syntax-check')
  162. }
  163. trailingIcon={stopOnValidationError ? 'check' : null}
  164. >
  165. {t('stop_on_validation_error')}
  166. </DropdownItem>
  167. </li>
  168. <li role="none">
  169. <DropdownItem
  170. as="button"
  171. onClick={() =>
  172. sendEventAndSet(false, setStopOnValidationError, 'syntax-check')
  173. }
  174. trailingIcon={!stopOnValidationError ? 'check' : null}
  175. >
  176. {t('ignore_validation_errors')}
  177. </DropdownItem>
  178. </li>
  179. <DropdownDivider />
  180. <DropdownHeader>{t('compile_error_handling')}</DropdownHeader>
  181. <li role="none">
  182. <DropdownItem
  183. as="button"
  184. onClick={enableStopOnFirstError}
  185. trailingIcon={stopOnFirstError ? 'check' : null}
  186. >
  187. {t('stop_on_first_error')}
  188. </DropdownItem>
  189. </li>
  190. <li role="none">
  191. <DropdownItem
  192. as="button"
  193. onClick={disableStopOnFirstError}
  194. trailingIcon={!stopOnFirstError ? 'check' : null}
  195. >
  196. {t('try_to_compile_despite_errors')}
  197. </DropdownItem>
  198. </li>
  199. <DropdownDivider />
  200. <li role="none">
  201. <DropdownItem
  202. as="button"
  203. onClick={() => stopCompile()}
  204. disabled={!compiling}
  205. aria-disabled={!compiling}
  206. >
  207. {t('stop_compile')}
  208. </DropdownItem>
  209. </li>
  210. <li role="none">
  211. <DropdownItem
  212. as="button"
  213. onClick={fromScratchWithEvent}
  214. disabled={compiling}
  215. aria-disabled={compiling}
  216. >
  217. {t('recompile_from_scratch')}
  218. </DropdownItem>
  219. </li>
  220. </DropdownMenu>
  221. </Dropdown>
  222. )
  223. }
  224. export default memo(PdfCompileButton)