layout-dropdown-button.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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(layoutContextPropTypes)
  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. className="toolbar-item layout-dropdown"
  134. pullRight
  135. >
  136. <Dropdown.Toggle className="btn-full-height" bsStyle="link">
  137. {processing ? <IconRefresh /> : <IconLayout />}
  138. <span className="toolbar-label">{t('layout')}</span>
  139. <Tooltip
  140. id="pdf-detach-badge"
  141. description="New feature"
  142. overlayProps={{ placement: 'bottom', delayHide: 100 }}
  143. >
  144. <span className="info-badge" />
  145. </Tooltip>
  146. </Dropdown.Toggle>
  147. <Dropdown.Menu className="layout-dropdown-list">
  148. <LayoutMenuItem
  149. onSelect={() => handleChangeLayout('sideBySide')}
  150. checkmark={
  151. <IconCheckmark
  152. iconFor="sideBySide"
  153. pdfLayout={pdfLayout}
  154. view={view}
  155. detachRole={detachRole}
  156. />
  157. }
  158. icon={<IconSplit />}
  159. text={t('editor_and_pdf')}
  160. />
  161. <LayoutMenuItem
  162. onSelect={() => handleChangeLayout('flat', 'editor')}
  163. checkmark={
  164. <IconCheckmark
  165. iconFor="editorOnly"
  166. pdfLayout={pdfLayout}
  167. view={view}
  168. detachRole={detachRole}
  169. />
  170. }
  171. icon={<IconEditorOnly />}
  172. text={
  173. <Trans
  174. i18nKey="editor_only_hide_pdf"
  175. components={[
  176. <span key="editor_only_hide_pdf" className="subdued" />,
  177. ]}
  178. />
  179. }
  180. />
  181. <LayoutMenuItem
  182. onSelect={() => handleChangeLayout('flat', 'pdf')}
  183. checkmark={
  184. <IconCheckmark
  185. iconFor="pdfOnly"
  186. pdfLayout={pdfLayout}
  187. view={view}
  188. detachRole={detachRole}
  189. />
  190. }
  191. icon={<IconPdfOnly />}
  192. text={
  193. <Trans
  194. i18nKey="pdf_only_hide_editor"
  195. components={[
  196. <span key="pdf_only_hide_editor" className="subdued" />,
  197. ]}
  198. />
  199. }
  200. />
  201. {'BroadcastChannel' in window ? (
  202. <LayoutMenuItem
  203. onSelect={() => handleDetach()}
  204. checkmark={
  205. detachRole === 'detacher' ? (
  206. detachIsLinked ? (
  207. <IconChecked />
  208. ) : (
  209. <IconRefresh />
  210. )
  211. ) : (
  212. <IconPlaceholder />
  213. )
  214. }
  215. icon={<IconDetach />}
  216. text={t('pdf_in_separate_tab')}
  217. />
  218. ) : (
  219. <DetachDisabled />
  220. )}
  221. <MenuItem divider />
  222. <div className="pdf-detach-survey">
  223. <span>
  224. <span className="info-badge" />
  225. </span>
  226. <span className="pdf-detach-survey-text">
  227. The Layout menu and opening the PDF in a new tab are new features.{' '}
  228. <a
  229. href="https://docs.google.com/forms/d/1fhc2LUMoun9jXvz9Nd6KkIKAQ8ZBfVZm_w8BpinpWu4"
  230. target="_blank"
  231. rel="noopener noreferrer"
  232. >
  233. {t('give_feedback')}.
  234. </a>
  235. </span>
  236. </div>
  237. </Dropdown.Menu>
  238. </ControlledDropdown>
  239. </>
  240. )
  241. }
  242. export default memo(LayoutDropdownButton)
  243. IconCheckmark.propTypes = {
  244. iconFor: PropTypes.string.isRequired,
  245. pdfLayout: PropTypes.string.isRequired,
  246. view: PropTypes.string,
  247. detachRole: PropTypes.string,
  248. }
  249. const layoutContextPropTypes = {
  250. reattach: PropTypes.func.isRequired,
  251. detach: PropTypes.func.isRequired,
  252. changeLayout: PropTypes.func.isRequired,
  253. detachIsLinked: PropTypes.bool,
  254. detachRole: PropTypes.string,
  255. pdfLayout: PropTypes.string.isRequired,
  256. view: PropTypes.string,
  257. }