layout-dropdown-button.jsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { memo, useCallback } from 'react'
  2. import PropTypes from 'prop-types'
  3. import { Dropdown, MenuItem } from 'react-bootstrap'
  4. import { Trans, useTranslation } from 'react-i18next'
  5. import Tooltip from '../../../shared/components/tooltip'
  6. import Icon from '../../../shared/components/icon'
  7. import IconChecked from '../../../shared/components/icon-checked'
  8. import ControlledDropdown from '../../../shared/components/controlled-dropdown'
  9. import { useLayoutContext } from '../../../shared/context/layout-context'
  10. import * as eventTracking from '../../../infrastructure/event-tracking'
  11. import useEventListener from '../../../shared/hooks/use-event-listener'
  12. function IconPlaceholder() {
  13. return <Icon type="" fw />
  14. }
  15. function IconRefresh() {
  16. return <Icon type="refresh" fw spin />
  17. }
  18. function IconLayout() {
  19. return <Icon type="columns" fw />
  20. }
  21. function IconSplit() {
  22. return <Icon type="columns" fw />
  23. }
  24. function IconDetach() {
  25. return <Icon type="window-restore" fw />
  26. }
  27. function IconEditorOnly() {
  28. return <Icon type="code" fw />
  29. }
  30. function IconPdfOnly() {
  31. return <Icon type="file-pdf-o" fw />
  32. }
  33. function IconCheckmark({ iconFor, pdfLayout, view, detachRole }) {
  34. if (detachRole === 'detacher' || view === 'history') {
  35. return <IconPlaceholder />
  36. }
  37. if (
  38. iconFor === 'editorOnly' &&
  39. pdfLayout === 'flat' &&
  40. (view === 'editor' || view === 'file')
  41. ) {
  42. return <IconChecked />
  43. } else if (iconFor === 'pdfOnly' && pdfLayout === 'flat' && view === 'pdf') {
  44. return <IconChecked />
  45. } else if (iconFor === 'sideBySide' && pdfLayout === 'sideBySide') {
  46. return <IconChecked />
  47. }
  48. // return empty icon for placeholder
  49. return <IconPlaceholder />
  50. }
  51. function LayoutMenuItem({ checkmark, icon, text, ...props }) {
  52. return (
  53. <MenuItem {...props}>
  54. <div className="layout-menu-item">
  55. <div className="layout-menu-item-start">
  56. <div>{checkmark}</div>
  57. <div>{icon}</div>
  58. <div>{text}</div>
  59. </div>
  60. </div>
  61. </MenuItem>
  62. )
  63. }
  64. LayoutMenuItem.propTypes = {
  65. checkmark: PropTypes.node.isRequired,
  66. icon: PropTypes.node.isRequired,
  67. text: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
  68. onSelect: PropTypes.func,
  69. }
  70. function DetachDisabled() {
  71. const { t } = useTranslation()
  72. return (
  73. <Tooltip
  74. id="detach-disabled"
  75. description={t('your_browser_does_not_support_this_feature')}
  76. overlayProps={{ placement: 'left' }}
  77. >
  78. <LayoutMenuItem
  79. disabled
  80. checkmark={<IconPlaceholder />}
  81. icon={<IconDetach />}
  82. text={t('pdf_in_separate_tab')}
  83. />
  84. </Tooltip>
  85. )
  86. }
  87. function LayoutDropdownButton() {
  88. const { t } = useTranslation()
  89. const {
  90. reattach,
  91. detach,
  92. detachIsLinked,
  93. detachRole,
  94. changeLayout,
  95. view,
  96. pdfLayout,
  97. } = useLayoutContext()
  98. const handleDetach = useCallback(() => {
  99. detach()
  100. eventTracking.sendMB('project-layout-detach')
  101. }, [detach])
  102. const handleReattach = useCallback(() => {
  103. if (detachRole !== 'detacher') {
  104. return
  105. }
  106. reattach()
  107. eventTracking.sendMB('project-layout-reattach')
  108. }, [detachRole, reattach])
  109. // reattach when the PDF pane opens
  110. useEventListener('ui:pdf-open', handleReattach)
  111. const handleChangeLayout = useCallback(
  112. (newLayout, newView) => {
  113. handleReattach()
  114. changeLayout(newLayout, newView)
  115. eventTracking.sendMB('project-layout-change', {
  116. layout: newLayout,
  117. view: newView,
  118. })
  119. },
  120. [changeLayout, handleReattach]
  121. )
  122. const processing = !detachIsLinked && detachRole === 'detacher'
  123. // bsStyle is required for Dropdown.Toggle, but we will override style
  124. return (
  125. <>
  126. {processing && (
  127. <div aria-live="assertive" className="sr-only">
  128. {t('layout_processing')}
  129. </div>
  130. )}
  131. <ControlledDropdown
  132. id="layout-dropdown"
  133. onMainButtonClick={() => {
  134. eventTracking.sendMB('navigation-clicked-layout')
  135. }}
  136. className="toolbar-item layout-dropdown"
  137. pullRight
  138. >
  139. <Dropdown.Toggle className="btn-full-height" bsStyle="link">
  140. {processing ? <IconRefresh /> : <IconLayout />}
  141. <span className="toolbar-label">{t('layout')}</span>
  142. </Dropdown.Toggle>
  143. <Dropdown.Menu className="layout-dropdown-list">
  144. <LayoutMenuItem
  145. onSelect={() => handleChangeLayout('sideBySide')}
  146. checkmark={
  147. <IconCheckmark
  148. iconFor="sideBySide"
  149. pdfLayout={pdfLayout}
  150. view={view}
  151. detachRole={detachRole}
  152. />
  153. }
  154. icon={<IconSplit />}
  155. text={<Trans i18nKey="editor_and_pdf">&</Trans>}
  156. />
  157. <LayoutMenuItem
  158. onSelect={() => handleChangeLayout('flat', 'editor')}
  159. checkmark={
  160. <IconCheckmark
  161. iconFor="editorOnly"
  162. pdfLayout={pdfLayout}
  163. view={view}
  164. detachRole={detachRole}
  165. />
  166. }
  167. icon={<IconEditorOnly />}
  168. text={
  169. <Trans
  170. i18nKey="editor_only_hide_pdf"
  171. components={[
  172. <span key="editor_only_hide_pdf" className="subdued" />,
  173. ]}
  174. />
  175. }
  176. />
  177. <LayoutMenuItem
  178. onSelect={() => handleChangeLayout('flat', 'pdf')}
  179. checkmark={
  180. <IconCheckmark
  181. iconFor="pdfOnly"
  182. pdfLayout={pdfLayout}
  183. view={view}
  184. detachRole={detachRole}
  185. />
  186. }
  187. icon={<IconPdfOnly />}
  188. text={
  189. <Trans
  190. i18nKey="pdf_only_hide_editor"
  191. components={[
  192. <span key="pdf_only_hide_editor" className="subdued" />,
  193. ]}
  194. />
  195. }
  196. />
  197. {'BroadcastChannel' in window ? (
  198. <LayoutMenuItem
  199. onSelect={() => handleDetach()}
  200. checkmark={
  201. detachRole === 'detacher' ? (
  202. detachIsLinked ? (
  203. <IconChecked />
  204. ) : (
  205. <IconRefresh />
  206. )
  207. ) : (
  208. <IconPlaceholder />
  209. )
  210. }
  211. icon={<IconDetach />}
  212. text={t('pdf_in_separate_tab')}
  213. />
  214. ) : (
  215. <DetachDisabled />
  216. )}
  217. </Dropdown.Menu>
  218. </ControlledDropdown>
  219. </>
  220. )
  221. }
  222. export default memo(LayoutDropdownButton)
  223. IconCheckmark.propTypes = {
  224. iconFor: PropTypes.string.isRequired,
  225. pdfLayout: PropTypes.string.isRequired,
  226. view: PropTypes.string,
  227. detachRole: PropTypes.string,
  228. }