pdf-compile-button.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 as={OLButtonGroup} className="compile-button-group">
  191. <OLTooltip
  192. description={tooltipElement}
  193. id="compile"
  194. tooltipProps={{ className: 'keyboard-tooltip' }}
  195. overlayProps={{
  196. delay: { show: 500, hide: 0 },
  197. placement: detachRole === 'detached' ? 'bottom' : undefined,
  198. }}
  199. >
  200. <OLButton
  201. variant="primary"
  202. disabled={compiling}
  203. isLoading={compiling}
  204. onClick={() => startCompile()}
  205. className={buttonClassName}
  206. >
  207. {t('recompile')}
  208. </OLButton>
  209. </OLTooltip>
  210. <DropdownToggle
  211. as={DropdownToggleCustom}
  212. split
  213. variant="primary"
  214. id="pdf-recompile-dropdown"
  215. size="sm"
  216. aria-label={t('toggle_compile_options_menu')}
  217. className={dropdownToggleClassName}
  218. />
  219. <DropdownMenu>
  220. <DropdownHeader>{t('auto_compile')}</DropdownHeader>
  221. <li role="none">
  222. <DropdownItem
  223. as="button"
  224. onClick={() =>
  225. sendEventAndSet(true, setAutoCompile, 'auto-compile')
  226. }
  227. trailingIcon={autoCompile ? 'check' : null}
  228. >
  229. {t('on')}
  230. </DropdownItem>
  231. </li>
  232. <li role="none">
  233. <DropdownItem
  234. as="button"
  235. onClick={() =>
  236. sendEventAndSet(false, setAutoCompile, 'auto-compile')
  237. }
  238. trailingIcon={!autoCompile ? 'check' : null}
  239. >
  240. {t('off')}
  241. </DropdownItem>
  242. </li>
  243. <DropdownDivider />
  244. <DropdownHeader>{t('compile_mode')}</DropdownHeader>
  245. <li role="none">
  246. <DropdownItem
  247. as="button"
  248. onClick={() => sendEventAndSet(false, setDraft, 'compile-mode')}
  249. trailingIcon={!draft ? 'check' : null}
  250. >
  251. {t('normal')}
  252. </DropdownItem>
  253. </li>
  254. <li role="none">
  255. <DropdownItem
  256. as="button"
  257. onClick={() => sendEventAndSet(true, setDraft, 'compile-mode')}
  258. trailingIcon={draft ? 'check' : null}
  259. >
  260. {t('fast')}&nbsp;<span className="subdued">[draft]</span>
  261. </DropdownItem>
  262. </li>
  263. <DropdownDivider />
  264. <DropdownHeader>Syntax Checks</DropdownHeader>
  265. <li role="none">
  266. <DropdownItem
  267. as="button"
  268. onClick={() =>
  269. sendEventAndSet(
  270. true,
  271. setStopOnValidationError,
  272. 'syntax-check'
  273. )
  274. }
  275. trailingIcon={stopOnValidationError ? 'check' : null}
  276. >
  277. {t('stop_on_validation_error')}
  278. </DropdownItem>
  279. </li>
  280. <li role="none">
  281. <DropdownItem
  282. as="button"
  283. onClick={() =>
  284. sendEventAndSet(
  285. false,
  286. setStopOnValidationError,
  287. 'syntax-check'
  288. )
  289. }
  290. trailingIcon={!stopOnValidationError ? 'check' : null}
  291. >
  292. {t('ignore_validation_errors')}
  293. </DropdownItem>
  294. </li>
  295. <DropdownDivider />
  296. <DropdownHeader>{t('compile_error_handling')}</DropdownHeader>
  297. <li role="none">
  298. <DropdownItem
  299. as="button"
  300. onClick={enableStopOnFirstError}
  301. trailingIcon={stopOnFirstError ? 'check' : null}
  302. >
  303. {t('stop_on_first_error')}
  304. </DropdownItem>
  305. </li>
  306. <li role="none">
  307. <DropdownItem
  308. as="button"
  309. onClick={disableStopOnFirstError}
  310. trailingIcon={!stopOnFirstError ? 'check' : null}
  311. >
  312. {t('try_to_compile_despite_errors')}
  313. </DropdownItem>
  314. </li>
  315. <DropdownDivider />
  316. <li role="none">
  317. <DropdownItem
  318. as="button"
  319. onClick={() => stopCompile()}
  320. disabled={!compiling}
  321. aria-disabled={!compiling}
  322. >
  323. {t('stop_compile')}
  324. </DropdownItem>
  325. </li>
  326. <li role="none">
  327. <DropdownItem
  328. as="button"
  329. onClick={fromScratchWithEvent}
  330. disabled={compiling}
  331. aria-disabled={compiling}
  332. >
  333. {t('recompile_from_scratch')}
  334. </DropdownItem>
  335. </li>
  336. </DropdownMenu>
  337. </Dropdown>
  338. }
  339. />
  340. )
  341. }
  342. export default memo(PdfCompileButton)