overleaf/services/web/frontend/js/features/ide-react/components/editor-and-pdf.tsx
Tim Down 4b86a54241 Merge pull request #15754 from overleaf/td-ide-page-pdf-detach-fixes
React IDE page: fix file preview with detached PDF and make PDF resizer toggler reattach PDF

GitOrigin-RevId: e28bf753174fa445af70e5d3efae05f89aa5a21c
2023-11-16 09:03:08 +00:00

101 lines
2.9 KiB
TypeScript

import { ReactNode, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import {
ImperativePanelHandle,
Panel,
PanelGroup,
} from 'react-resizable-panels'
import { HorizontalResizeHandle } from '@/features/ide-react/components/resize/horizontal-resize-handle'
import { HorizontalToggler } from '@/features/ide-react/components/resize/horizontal-toggler'
import useCollapsiblePanel from '@/features/ide-react/hooks/use-collapsible-panel'
import { useLayoutContext } from '@/shared/context/layout-context'
import PdfPreview from '@/features/pdf-preview/components/pdf-preview'
import { DefaultSynctexControl } from '@/features/pdf-preview/components/detach-synctex-control'
import classnames from 'classnames'
export type EditorProps = {
shouldPersistLayout?: boolean
editorContent: ReactNode
}
export default function EditorAndPdf({
shouldPersistLayout = false,
editorContent,
}: EditorProps) {
const { t } = useTranslation()
const { view, pdfLayout, changeLayout, detachRole, reattach } =
useLayoutContext()
const pdfPanelRef = useRef<ImperativePanelHandle>(null)
const isDualPane = pdfLayout === 'sideBySide'
const editorIsVisible = isDualPane || view === 'editor' || view === 'file'
const pdfIsOpen = isDualPane || view === 'pdf'
useCollapsiblePanel(pdfIsOpen, pdfPanelRef)
const historyIsOpen = view === 'history'
function setPdfIsOpen(isOpen: boolean) {
if (isOpen) {
if (detachRole === 'detacher') {
reattach()
}
changeLayout('sideBySide')
} else {
changeLayout('flat', 'editor')
}
}
return (
<PanelGroup
autoSaveId={
shouldPersistLayout ? 'ide-react-editor-and-pdf-layout' : undefined
}
direction="horizontal"
className={classnames({ hide: historyIsOpen })}
>
{editorIsVisible ? (
<Panel
id="editor"
order={1}
defaultSize={50}
className="ide-react-panel"
>
{editorContent}
</Panel>
) : null}
<HorizontalResizeHandle
resizable={isDualPane}
onDoubleClick={() => setPdfIsOpen(!pdfIsOpen)}
>
<HorizontalToggler
id="editor-pdf"
togglerType="east"
isOpen={pdfIsOpen}
setIsOpen={setPdfIsOpen}
tooltipWhenOpen={t('tooltip_hide_pdf')}
tooltipWhenClosed={t('tooltip_show_pdf')}
/>
{isDualPane ? (
<div className="synctex-controls">
<DefaultSynctexControl />
</div>
) : null}
</HorizontalResizeHandle>
{pdfIsOpen ? (
<Panel
ref={pdfPanelRef}
id="pdf"
order={2}
defaultSize={50}
minSize={5}
collapsible
onCollapse={collapsed => setPdfIsOpen(!collapsed)}
className="ide-react-panel"
>
<PdfPreview />
</Panel>
) : null}
</PanelGroup>
)
}