layout-dropdown-button.js 7.8 KB

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