2021-11-30 09:54:14 -05:00
|
|
|
import { useCallback } from 'react'
|
2021-11-03 09:21:14 -04:00
|
|
|
import PropTypes from 'prop-types'
|
2021-12-02 10:18:20 -05:00
|
|
|
import { Dropdown, MenuItem, OverlayTrigger, Tooltip } from 'react-bootstrap'
|
2021-11-03 09:21:14 -04:00
|
|
|
import { Trans, useTranslation } from 'react-i18next'
|
|
|
|
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'
|
2021-12-02 10:18:56 -05:00
|
|
|
import { useCompileContext } from '../../../shared/context/compile-context'
|
2021-11-30 09:54:14 -05:00
|
|
|
import { useLayoutContext } from '../../../shared/context/layout-context'
|
|
|
|
import * as eventTracking from '../../../infrastructure/event-tracking'
|
2021-11-03 09:21:14 -04:00
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
function IconPlaceholder() {
|
2022-01-19 06:56:57 -05:00
|
|
|
return <Icon type="" fw />
|
2021-11-30 09:54:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function IconRefresh() {
|
2022-01-19 06:56:57 -05:00
|
|
|
return <Icon type="refresh" fw spin />
|
2021-11-30 09:54:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function IconLayout() {
|
2022-01-19 06:56:57 -05:00
|
|
|
return <Icon type="columns" fw />
|
2021-11-30 09:54:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function IconDetach() {
|
2022-01-19 06:56:57 -05:00
|
|
|
return <Icon type="window-restore" fw />
|
2021-11-30 09:54:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function IconCheckmark({ iconFor, pdfLayout, view, detachRole }) {
|
|
|
|
if (detachRole === 'detacher') {
|
|
|
|
return <IconPlaceholder />
|
|
|
|
}
|
2021-11-03 09:21:14 -04:00
|
|
|
if (iconFor === 'editorOnly' && pdfLayout === 'flat' && view === 'editor') {
|
|
|
|
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
|
2021-11-30 09:54:14 -05:00
|
|
|
return <IconPlaceholder />
|
2021-11-03 09:21:14 -04:00
|
|
|
}
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
function LayoutDropdownButton() {
|
2021-11-03 09:21:14 -04:00
|
|
|
const { t } = useTranslation()
|
2021-11-15 11:33:57 -05:00
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
const {
|
|
|
|
reattach,
|
|
|
|
detach,
|
|
|
|
detachIsLinked,
|
|
|
|
detachRole,
|
|
|
|
changeLayout,
|
|
|
|
view,
|
|
|
|
pdfLayout,
|
|
|
|
} = useLayoutContext(layoutContextPropTypes)
|
|
|
|
|
2021-12-02 10:18:56 -05:00
|
|
|
const { stopCompile } = useCompileContext()
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
const handleDetach = useCallback(() => {
|
|
|
|
detach()
|
2021-12-02 10:18:56 -05:00
|
|
|
stopCompile()
|
2021-11-30 09:54:14 -05:00
|
|
|
eventTracking.sendMB('project-layout-detach')
|
2021-12-02 10:18:56 -05:00
|
|
|
}, [detach, stopCompile])
|
2021-11-30 09:54:14 -05:00
|
|
|
|
|
|
|
const handleReattach = useCallback(() => {
|
|
|
|
if (detachRole !== 'detacher') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reattach()
|
|
|
|
eventTracking.sendMB('project-layout-reattach')
|
|
|
|
}, [detachRole, reattach])
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
2021-11-03 09:21:14 -04:00
|
|
|
// bsStyle is required for Dropdown.Toggle, but we will override style
|
|
|
|
return (
|
2021-11-15 11:33:57 -05:00
|
|
|
<>
|
2021-11-30 09:54:14 -05:00
|
|
|
{processing && (
|
2021-11-15 11:33:57 -05:00
|
|
|
<div aria-live="assertive" className="sr-only">
|
|
|
|
{t('layout_processing')}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<ControlledDropdown
|
|
|
|
id="layout-dropdown"
|
2021-12-02 10:18:20 -05:00
|
|
|
className="toolbar-item layout-dropdown"
|
2021-11-30 09:54:14 -05:00
|
|
|
pullRight
|
2021-11-15 11:33:57 -05:00
|
|
|
>
|
|
|
|
<Dropdown.Toggle className="btn-full-height" bsStyle="link">
|
2021-11-30 09:54:14 -05:00
|
|
|
{processing ? <IconRefresh /> : <IconLayout />}
|
2021-11-15 11:33:57 -05:00
|
|
|
<span className="toolbar-label">{t('layout')}</span>
|
2021-12-02 10:18:20 -05:00
|
|
|
<OverlayTrigger
|
|
|
|
placement="bottom"
|
|
|
|
overlay={<Tooltip id="pdf-detach-badge">Beta feature</Tooltip>}
|
|
|
|
delayHide={100}
|
|
|
|
>
|
|
|
|
<span className="beta-badge" />
|
|
|
|
</OverlayTrigger>
|
2021-11-15 11:33:57 -05:00
|
|
|
</Dropdown.Toggle>
|
|
|
|
<Dropdown.Menu id="layout-dropdown-list">
|
2021-11-30 09:54:14 -05:00
|
|
|
<MenuItem onSelect={() => handleChangeLayout('sideBySide')}>
|
2021-11-15 11:33:57 -05:00
|
|
|
<IconCheckmark
|
|
|
|
iconFor="sideBySide"
|
|
|
|
pdfLayout={pdfLayout}
|
|
|
|
view={view}
|
2021-11-30 09:54:14 -05:00
|
|
|
detachRole={detachRole}
|
2021-11-15 11:33:57 -05:00
|
|
|
/>
|
|
|
|
<Icon type="columns" />
|
|
|
|
{t('editor_and_pdf')}
|
|
|
|
</MenuItem>
|
|
|
|
|
|
|
|
<MenuItem
|
|
|
|
onSelect={() => handleChangeLayout('flat', 'editor')}
|
|
|
|
className="menu-item-with-svg"
|
|
|
|
>
|
|
|
|
<IconCheckmark
|
|
|
|
iconFor="editorOnly"
|
|
|
|
pdfLayout={pdfLayout}
|
|
|
|
view={view}
|
2021-11-30 09:54:14 -05:00
|
|
|
detachRole={detachRole}
|
2021-11-15 11:33:57 -05:00
|
|
|
/>
|
|
|
|
<IconEditorOnly />
|
2021-12-02 10:18:20 -05:00
|
|
|
<span>
|
|
|
|
<Trans
|
|
|
|
i18nKey="editor_only_hide_pdf"
|
|
|
|
components={[
|
|
|
|
<span key="editor_only_hide_pdf" className="subdued" />,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</span>
|
2021-11-15 11:33:57 -05:00
|
|
|
</MenuItem>
|
|
|
|
|
|
|
|
<MenuItem
|
|
|
|
onSelect={() => handleChangeLayout('flat', 'pdf')}
|
|
|
|
className="menu-item-with-svg"
|
|
|
|
>
|
|
|
|
<IconCheckmark
|
|
|
|
iconFor="pdfOnly"
|
|
|
|
pdfLayout={pdfLayout}
|
|
|
|
view={view}
|
2021-11-30 09:54:14 -05:00
|
|
|
detachRole={detachRole}
|
2021-11-15 11:33:57 -05:00
|
|
|
/>
|
|
|
|
<IconPdfOnly />
|
2021-12-02 10:18:20 -05:00
|
|
|
<span>
|
|
|
|
<Trans
|
|
|
|
i18nKey="pdf_only_hide_editor"
|
|
|
|
components={[
|
|
|
|
<span key="pdf_only_hide_editor" className="subdued" />,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</span>
|
2021-11-15 11:33:57 -05:00
|
|
|
</MenuItem>
|
|
|
|
|
|
|
|
{detachRole === 'detacher' ? (
|
2021-11-30 09:54:14 -05:00
|
|
|
<MenuItem>
|
|
|
|
{detachIsLinked ? <IconChecked /> : <IconRefresh />}
|
|
|
|
<IconDetach />
|
|
|
|
{t('pdf_in_separate_tab')}
|
2021-11-15 11:33:57 -05:00
|
|
|
</MenuItem>
|
|
|
|
) : (
|
2021-11-30 09:54:14 -05:00
|
|
|
<MenuItem onSelect={handleDetach}>
|
|
|
|
<IconPlaceholder />
|
|
|
|
<IconDetach />
|
|
|
|
{t('pdf_in_separate_tab')}
|
2021-11-15 11:33:57 -05:00
|
|
|
</MenuItem>
|
|
|
|
)}
|
2021-12-02 10:18:20 -05:00
|
|
|
|
|
|
|
<MenuItem divider />
|
|
|
|
<div className="pdf-detach-survey">
|
|
|
|
<span>
|
|
|
|
<span className="beta-badge" />
|
|
|
|
</span>
|
|
|
|
<span className="pdf-detach-survey-text">
|
|
|
|
The Layout menu and opening the PDF in a new tab are beta
|
|
|
|
features.{' '}
|
|
|
|
<a
|
|
|
|
href="https://docs.google.com/forms/d/e/1FAIpQLScuxQA8Az9NQwvYgC6FALG7FEtCCj4e8of27e_L0SXGrJFRMw/viewform"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
|
|
|
{t('give_feedback')}.
|
|
|
|
</a>
|
|
|
|
</span>
|
|
|
|
</div>
|
2021-11-15 11:33:57 -05:00
|
|
|
</Dropdown.Menu>
|
|
|
|
</ControlledDropdown>
|
|
|
|
</>
|
2021-11-03 09:21:14 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default LayoutDropdownButton
|
|
|
|
|
|
|
|
IconCheckmark.propTypes = {
|
|
|
|
iconFor: PropTypes.string.isRequired,
|
|
|
|
pdfLayout: PropTypes.string.isRequired,
|
|
|
|
view: PropTypes.string,
|
2021-11-30 09:54:14 -05:00
|
|
|
detachRole: PropTypes.string,
|
2021-11-03 09:21:14 -04:00
|
|
|
}
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
const layoutContextPropTypes = {
|
2021-11-15 11:33:57 -05:00
|
|
|
reattach: PropTypes.func.isRequired,
|
|
|
|
detach: PropTypes.func.isRequired,
|
2021-11-30 09:54:14 -05:00
|
|
|
changeLayout: PropTypes.func.isRequired,
|
|
|
|
detachIsLinked: PropTypes.bool,
|
2021-11-15 11:33:57 -05:00
|
|
|
detachRole: PropTypes.string,
|
2021-11-03 09:21:14 -04:00
|
|
|
pdfLayout: PropTypes.string.isRequired,
|
|
|
|
view: PropTypes.string,
|
|
|
|
}
|