| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- import { memo, useCallback } from 'react'
- import PropTypes from 'prop-types'
- import { Dropdown, MenuItem } from 'react-bootstrap'
- import { Trans, useTranslation } from 'react-i18next'
- import Tooltip from '../../../shared/components/tooltip'
- import Icon from '../../../shared/components/icon'
- import IconChecked from '../../../shared/components/icon-checked'
- import ControlledDropdown from '../../../shared/components/controlled-dropdown'
- import IconEditorOnly from './icon-editor-only'
- import IconPdfOnly from './icon-pdf-only'
- import { useLayoutContext } from '../../../shared/context/layout-context'
- import * as eventTracking from '../../../infrastructure/event-tracking'
- import useEventListener from '../../../shared/hooks/use-event-listener'
- function IconPlaceholder() {
- return <Icon type="" fw />
- }
- function IconRefresh() {
- return <Icon type="refresh" fw spin />
- }
- function IconLayout() {
- return <Icon type="columns" fw />
- }
- function IconDetach() {
- return <Icon type="window-restore" fw />
- }
- function IconCheckmark({ iconFor, pdfLayout, view, detachRole }) {
- if (detachRole === 'detacher' || view === 'history') {
- return <IconPlaceholder />
- }
- if (
- iconFor === 'editorOnly' &&
- pdfLayout === 'flat' &&
- (view === 'editor' || view === 'file')
- ) {
- return <IconChecked />
- } else if (iconFor === 'pdfOnly' && pdfLayout === 'flat' && view === 'pdf') {
- return <IconChecked />
- } else if (iconFor === 'sideBySide' && pdfLayout === 'sideBySide') {
- return <IconChecked />
- }
- // return empty icon for placeholder
- return <IconPlaceholder />
- }
- function LayoutMenuItem({ checkmark, icon, text, ...props }) {
- return (
- <MenuItem {...props}>
- <div className="layout-menu-item">
- <div className="layout-menu-item-start">
- <div>{checkmark}</div>
- <div>{icon}</div>
- <div>{text}</div>
- </div>
- </div>
- </MenuItem>
- )
- }
- LayoutMenuItem.propTypes = {
- checkmark: PropTypes.node.isRequired,
- icon: PropTypes.node.isRequired,
- text: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
- onSelect: PropTypes.func,
- }
- function DetachDisabled() {
- const { t } = useTranslation()
- return (
- <Tooltip
- id="detach-disabled"
- description={t('your_browser_does_not_support_this_feature')}
- overlayProps={{ placement: 'left' }}
- >
- <LayoutMenuItem
- disabled
- checkmark={<IconPlaceholder />}
- icon={<IconDetach />}
- text={t('pdf_in_separate_tab')}
- />
- </Tooltip>
- )
- }
- function LayoutDropdownButton() {
- const { t } = useTranslation()
- const {
- reattach,
- detach,
- detachIsLinked,
- detachRole,
- changeLayout,
- view,
- pdfLayout,
- } = useLayoutContext(layoutContextPropTypes)
- const handleDetach = useCallback(() => {
- detach()
- eventTracking.sendMB('project-layout-detach')
- }, [detach])
- const handleReattach = useCallback(() => {
- if (detachRole !== 'detacher') {
- return
- }
- reattach()
- eventTracking.sendMB('project-layout-reattach')
- }, [detachRole, reattach])
- // reattach when the PDF pane opens
- useEventListener('ui:pdf-open', handleReattach)
- const handleChangeLayout = useCallback(
- (newLayout, newView) => {
- handleReattach()
- changeLayout(newLayout, newView)
- eventTracking.sendMB('project-layout-change', {
- layout: newLayout,
- view: newView,
- })
- },
- [changeLayout, handleReattach]
- )
- const processing = !detachIsLinked && detachRole === 'detacher'
- // bsStyle is required for Dropdown.Toggle, but we will override style
- return (
- <>
- {processing && (
- <div aria-live="assertive" className="sr-only">
- {t('layout_processing')}
- </div>
- )}
- <ControlledDropdown
- id="layout-dropdown"
- className="toolbar-item layout-dropdown"
- pullRight
- >
- <Dropdown.Toggle className="btn-full-height" bsStyle="link">
- {processing ? <IconRefresh /> : <IconLayout />}
- <span className="toolbar-label">{t('layout')}</span>
- <Tooltip
- id="pdf-detach-badge"
- description="New feature"
- overlayProps={{ placement: 'bottom', delayHide: 100 }}
- >
- <span className="info-badge" />
- </Tooltip>
- </Dropdown.Toggle>
- <Dropdown.Menu className="layout-dropdown-list">
- <LayoutMenuItem
- onSelect={() => handleChangeLayout('sideBySide')}
- checkmark={
- <IconCheckmark
- iconFor="sideBySide"
- pdfLayout={pdfLayout}
- view={view}
- detachRole={detachRole}
- />
- }
- icon={<Icon type="columns" fw />}
- text={t('editor_and_pdf')}
- />
- <LayoutMenuItem
- onSelect={() => handleChangeLayout('flat', 'editor')}
- checkmark={
- <IconCheckmark
- iconFor="editorOnly"
- pdfLayout={pdfLayout}
- view={view}
- detachRole={detachRole}
- />
- }
- icon={
- <i className="fa fa-fw">
- <IconEditorOnly />
- </i>
- }
- text={
- <Trans
- i18nKey="editor_only_hide_pdf"
- components={[
- <span key="editor_only_hide_pdf" className="subdued" />,
- ]}
- />
- }
- />
- <LayoutMenuItem
- onSelect={() => handleChangeLayout('flat', 'pdf')}
- checkmark={
- <IconCheckmark
- iconFor="pdfOnly"
- pdfLayout={pdfLayout}
- view={view}
- detachRole={detachRole}
- />
- }
- icon={
- <i className="fa fa-fw">
- <IconPdfOnly />
- </i>
- }
- text={
- <Trans
- i18nKey="pdf_only_hide_editor"
- components={[
- <span key="pdf_only_hide_editor" className="subdued" />,
- ]}
- />
- }
- />
- {'BroadcastChannel' in window ? (
- <LayoutMenuItem
- onSelect={() => handleDetach()}
- checkmark={
- detachRole === 'detacher' ? (
- detachIsLinked ? (
- <IconChecked />
- ) : (
- <IconRefresh />
- )
- ) : (
- <IconPlaceholder />
- )
- }
- icon={<IconDetach />}
- text={t('pdf_in_separate_tab')}
- />
- ) : (
- <DetachDisabled />
- )}
- <MenuItem divider />
- <div className="pdf-detach-survey">
- <span>
- <span className="info-badge" />
- </span>
- <span className="pdf-detach-survey-text">
- The Layout menu and opening the PDF in a new tab are new features.{' '}
- <a
- href="https://docs.google.com/forms/d/e/1FAIpQLScBc0LSttM02-HgfoUi7jj7MmFT9u3Y4cUDwT_AmDK9to--gg/viewform"
- target="_blank"
- rel="noopener noreferrer"
- >
- {t('give_feedback')}.
- </a>
- </span>
- </div>
- </Dropdown.Menu>
- </ControlledDropdown>
- </>
- )
- }
- export default memo(LayoutDropdownButton)
- IconCheckmark.propTypes = {
- iconFor: PropTypes.string.isRequired,
- pdfLayout: PropTypes.string.isRequired,
- view: PropTypes.string,
- detachRole: PropTypes.string,
- }
- const layoutContextPropTypes = {
- reattach: PropTypes.func.isRequired,
- detach: PropTypes.func.isRequired,
- changeLayout: PropTypes.func.isRequired,
- detachIsLinked: PropTypes.bool,
- detachRole: PropTypes.string,
- pdfLayout: PropTypes.string.isRequired,
- view: PropTypes.string,
- }
|