pdf-compile-button.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. import * as eventTracking from '../../../infrastructure/event-tracking'
  9. import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
  10. import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
  11. import {
  12. DropdownToggleCustom,
  13. Dropdown,
  14. DropdownDivider,
  15. DropdownHeader,
  16. DropdownItem,
  17. DropdownMenu,
  18. DropdownToggle,
  19. } from '@/features/ui/components/bootstrap-5/dropdown-menu'
  20. import OLButton from '@/features/ui/components/ol/ol-button'
  21. import OLButtonGroup from '@/features/ui/components/ol/ol-button-group'
  22. import { bsVersion, isBootstrap5 } from '@/features/utils/bootstrap-5'
  23. import { useLayoutContext } from '@/shared/context/layout-context'
  24. const modifierKey = /Mac/i.test(navigator.platform) ? 'Cmd' : 'Ctrl'
  25. function sendEventAndSet<T extends boolean>(
  26. value: T,
  27. setter: (value: T) => void,
  28. settingName: string
  29. ) {
  30. eventTracking.sendMB('recompile-setting-changed', {
  31. setting: settingName,
  32. settingVal: value,
  33. })
  34. setter(value)
  35. }
  36. function PdfCompileButton() {
  37. const {
  38. animateCompileDropdownArrow,
  39. autoCompile,
  40. compiling,
  41. draft,
  42. hasChanges,
  43. setAnimateCompileDropdownArrow,
  44. setAutoCompile,
  45. setDraft,
  46. setStopOnValidationError,
  47. stopOnFirstError,
  48. stopOnValidationError,
  49. startCompile,
  50. stopCompile,
  51. recompileFromScratch,
  52. } = useCompileContext()
  53. const { enableStopOnFirstError, disableStopOnFirstError } =
  54. useStopOnFirstError({ eventSource: 'dropdown' })
  55. const { t } = useTranslation()
  56. const { detachRole } = useLayoutContext()
  57. const fromScratchWithEvent = () => {
  58. eventTracking.sendMB('recompile-setting-changed', {
  59. setting: 'from-scratch',
  60. })
  61. recompileFromScratch()
  62. }
  63. const compileButtonLabel = compiling ? `${t('compiling')}…` : t('recompile')
  64. const tooltipElement = (
  65. <>
  66. {t('recompile_pdf')}{' '}
  67. <span className="keyboard-shortcut">({modifierKey} + Enter)</span>
  68. </>
  69. )
  70. const dropdownToggleClassName = classNames(
  71. {
  72. 'detach-compile-button-animate': animateCompileDropdownArrow,
  73. 'btn-striped-animated': hasChanges,
  74. },
  75. 'no-left-border',
  76. bsVersion({ bs5: 'dropdown-button-toggle' })
  77. )
  78. const buttonClassName = classNames(
  79. {
  80. 'btn-striped-animated': hasChanges,
  81. 'align-items-center py-0': isBootstrap5(),
  82. },
  83. 'no-left-radius px-3'
  84. )
  85. return (
  86. <BootstrapVersionSwitcher
  87. bs3={
  88. <SplitMenu
  89. bsStyle="primary"
  90. bsSize="xs"
  91. disabled={compiling}
  92. button={{
  93. tooltip: {
  94. description: tooltipElement,
  95. id: 'compile',
  96. tooltipProps: { className: 'keyboard-tooltip' },
  97. overlayProps: {
  98. delayShow: 500,
  99. placement: detachRole === 'detached' ? 'bottom' : undefined,
  100. },
  101. },
  102. icon: { type: 'refresh', spin: compiling },
  103. onClick: () => startCompile(),
  104. text: compileButtonLabel,
  105. className: buttonClassName,
  106. }}
  107. dropdownToggle={{
  108. 'aria-label': t('toggle_compile_options_menu'),
  109. handleAnimationEnd: () => setAnimateCompileDropdownArrow(false),
  110. className: dropdownToggleClassName,
  111. }}
  112. dropdown={{
  113. id: 'pdf-recompile-dropdown',
  114. }}
  115. >
  116. <SplitMenu.Item header>{t('auto_compile')}</SplitMenu.Item>
  117. <SplitMenu.Item
  118. onSelect={() =>
  119. sendEventAndSet(true, setAutoCompile, 'auto-compile')
  120. }
  121. >
  122. <Icon type={autoCompile ? 'check' : ''} fw />
  123. {t('on')}
  124. </SplitMenu.Item>
  125. <SplitMenu.Item
  126. onSelect={() =>
  127. sendEventAndSet(false, setAutoCompile, 'auto-compile')
  128. }
  129. >
  130. <Icon type={!autoCompile ? 'check' : ''} fw />
  131. {t('off')}
  132. </SplitMenu.Item>
  133. <SplitMenu.Item header>{t('compile_mode')}</SplitMenu.Item>
  134. <SplitMenu.Item
  135. onSelect={() => sendEventAndSet(false, setDraft, 'compile-mode')}
  136. >
  137. <Icon type={!draft ? 'check' : ''} fw />
  138. {t('normal')}
  139. </SplitMenu.Item>
  140. <SplitMenu.Item
  141. onSelect={() => sendEventAndSet(true, setDraft, 'compile-mode')}
  142. >
  143. <Icon type={draft ? 'check' : ''} fw />
  144. {t('fast')} <span className="subdued">[draft]</span>
  145. </SplitMenu.Item>
  146. <SplitMenu.Item header>Syntax Checks</SplitMenu.Item>
  147. <SplitMenu.Item
  148. onSelect={() =>
  149. sendEventAndSet(true, setStopOnValidationError, 'syntax-check')
  150. }
  151. >
  152. <Icon type={stopOnValidationError ? 'check' : ''} fw />
  153. {t('stop_on_validation_error')}
  154. </SplitMenu.Item>
  155. <SplitMenu.Item
  156. onSelect={() =>
  157. sendEventAndSet(false, setStopOnValidationError, 'syntax-check')
  158. }
  159. >
  160. <Icon type={!stopOnValidationError ? 'check' : ''} fw />
  161. {t('ignore_validation_errors')}
  162. </SplitMenu.Item>
  163. <SplitMenu.Item header>{t('compile_error_handling')}</SplitMenu.Item>
  164. <SplitMenu.Item onSelect={enableStopOnFirstError}>
  165. <Icon type={stopOnFirstError ? 'check' : ''} fw />
  166. {t('stop_on_first_error')}
  167. </SplitMenu.Item>
  168. <SplitMenu.Item onSelect={disableStopOnFirstError}>
  169. <Icon type={!stopOnFirstError ? 'check' : ''} fw />
  170. {t('try_to_compile_despite_errors')}
  171. </SplitMenu.Item>
  172. <SplitMenu.Item divider />
  173. <SplitMenu.Item
  174. onSelect={() => stopCompile()}
  175. disabled={!compiling}
  176. aria-disabled={!compiling}
  177. >
  178. {t('stop_compile')}
  179. </SplitMenu.Item>
  180. <SplitMenu.Item
  181. onSelect={fromScratchWithEvent}
  182. disabled={compiling}
  183. aria-disabled={compiling}
  184. >
  185. {t('recompile_from_scratch')}
  186. </SplitMenu.Item>
  187. </SplitMenu>
  188. }
  189. bs5={
  190. <Dropdown
  191. as={OLButtonGroup}
  192. autoClose="outside"
  193. className="compile-button-group"
  194. >
  195. <OLTooltip
  196. description={tooltipElement}
  197. id="compile"
  198. tooltipProps={{ className: 'keyboard-tooltip' }}
  199. overlayProps={{
  200. delay: { show: 500, hide: 0 },
  201. placement: detachRole === 'detached' ? 'bottom' : undefined,
  202. }}
  203. >
  204. <OLButton
  205. variant="primary"
  206. disabled={compiling}
  207. isLoading={compiling}
  208. onClick={() => startCompile()}
  209. className={buttonClassName}
  210. >
  211. {t('recompile')}
  212. </OLButton>
  213. </OLTooltip>
  214. <DropdownToggle
  215. as={DropdownToggleCustom}
  216. split
  217. variant="primary"
  218. id="pdf-recompile-dropdown"
  219. size="sm"
  220. aria-label={t('toggle_compile_options_menu')}
  221. className={dropdownToggleClassName}
  222. />
  223. <DropdownMenu>
  224. <DropdownHeader>{t('auto_compile')}</DropdownHeader>
  225. <li role="none">
  226. <DropdownItem
  227. as="button"
  228. onClick={() =>
  229. sendEventAndSet(true, setAutoCompile, 'auto-compile')
  230. }
  231. trailingIcon={autoCompile ? 'check' : null}
  232. >
  233. {t('on')}
  234. </DropdownItem>
  235. </li>
  236. <li role="none">
  237. <DropdownItem
  238. as="button"
  239. onClick={() =>
  240. sendEventAndSet(false, setAutoCompile, 'auto-compile')
  241. }
  242. trailingIcon={!autoCompile ? 'check' : null}
  243. >
  244. {t('off')}
  245. </DropdownItem>
  246. </li>
  247. <DropdownDivider />
  248. <DropdownHeader>{t('compile_mode')}</DropdownHeader>
  249. <li role="none">
  250. <DropdownItem
  251. as="button"
  252. onClick={() => sendEventAndSet(false, setDraft, 'compile-mode')}
  253. trailingIcon={!draft ? 'check' : null}
  254. >
  255. {t('normal')}
  256. </DropdownItem>
  257. </li>
  258. <li role="none">
  259. <DropdownItem
  260. as="button"
  261. onClick={() => sendEventAndSet(true, setDraft, 'compile-mode')}
  262. trailingIcon={draft ? 'check' : null}
  263. >
  264. {t('fast')}&nbsp;<span className="subdued">[draft]</span>
  265. </DropdownItem>
  266. </li>
  267. <DropdownDivider />
  268. <DropdownHeader>Syntax Checks</DropdownHeader>
  269. <li role="none">
  270. <DropdownItem
  271. as="button"
  272. onClick={() =>
  273. sendEventAndSet(
  274. true,
  275. setStopOnValidationError,
  276. 'syntax-check'
  277. )
  278. }
  279. trailingIcon={stopOnValidationError ? 'check' : null}
  280. >
  281. {t('stop_on_validation_error')}
  282. </DropdownItem>
  283. </li>
  284. <li role="none">
  285. <DropdownItem
  286. as="button"
  287. onClick={() =>
  288. sendEventAndSet(
  289. false,
  290. setStopOnValidationError,
  291. 'syntax-check'
  292. )
  293. }
  294. trailingIcon={!stopOnValidationError ? 'check' : null}
  295. >
  296. {t('ignore_validation_errors')}
  297. </DropdownItem>
  298. </li>
  299. <DropdownDivider />
  300. <DropdownHeader>{t('compile_error_handling')}</DropdownHeader>
  301. <li role="none">
  302. <DropdownItem
  303. as="button"
  304. onClick={enableStopOnFirstError}
  305. trailingIcon={stopOnFirstError ? 'check' : null}
  306. >
  307. {t('stop_on_first_error')}
  308. </DropdownItem>
  309. </li>
  310. <li role="none">
  311. <DropdownItem
  312. as="button"
  313. onClick={disableStopOnFirstError}
  314. trailingIcon={!stopOnFirstError ? 'check' : null}
  315. >
  316. {t('try_to_compile_despite_errors')}
  317. </DropdownItem>
  318. </li>
  319. <DropdownDivider />
  320. <li role="none">
  321. <DropdownItem
  322. as="button"
  323. onClick={() => stopCompile()}
  324. disabled={!compiling}
  325. aria-disabled={!compiling}
  326. >
  327. {t('stop_compile')}
  328. </DropdownItem>
  329. </li>
  330. <li role="none">
  331. <DropdownItem
  332. as="button"
  333. onClick={fromScratchWithEvent}
  334. disabled={compiling}
  335. aria-disabled={compiling}
  336. >
  337. {t('recompile_from_scratch')}
  338. </DropdownItem>
  339. </li>
  340. </DropdownMenu>
  341. </Dropdown>
  342. }
  343. />
  344. )
  345. }
  346. export default memo(PdfCompileButton)