2023-11-15 06:55:05 -05:00
|
|
|
import { ReactNode, useRef, useState } from 'react'
|
2023-10-26 04:57:00 -04:00
|
|
|
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'
|
2023-11-02 07:36:04 -04:00
|
|
|
import PdfPreview from '@/features/pdf-preview/components/pdf-preview'
|
|
|
|
import { DefaultSynctexControl } from '@/features/pdf-preview/components/detach-synctex-control'
|
2023-10-27 06:50:57 -04:00
|
|
|
import classnames from 'classnames'
|
2023-10-26 04:57:00 -04:00
|
|
|
|
|
|
|
export type EditorProps = {
|
|
|
|
shouldPersistLayout?: boolean
|
2023-11-02 07:36:04 -04:00
|
|
|
editorContent: ReactNode
|
2023-10-26 04:57:00 -04:00
|
|
|
}
|
|
|
|
|
2023-11-02 07:36:04 -04:00
|
|
|
export default function EditorAndPdf({
|
2023-10-26 04:57:00 -04:00
|
|
|
shouldPersistLayout = false,
|
2023-11-02 07:36:04 -04:00
|
|
|
editorContent,
|
2023-10-26 04:57:00 -04:00
|
|
|
}: EditorProps) {
|
|
|
|
const { t } = useTranslation()
|
2023-11-15 06:35:18 -05:00
|
|
|
const { view, pdfLayout, changeLayout, detachRole, reattach } =
|
|
|
|
useLayoutContext()
|
2023-11-15 06:55:05 -05:00
|
|
|
const [resizing, setResizing] = useState(false)
|
2023-10-26 04:57:00 -04:00
|
|
|
|
|
|
|
const pdfPanelRef = useRef<ImperativePanelHandle>(null)
|
|
|
|
const isDualPane = pdfLayout === 'sideBySide'
|
2023-11-15 06:35:18 -05:00
|
|
|
const editorIsVisible = isDualPane || view === 'editor' || view === 'file'
|
2023-10-26 04:57:00 -04:00
|
|
|
const pdfIsOpen = isDualPane || view === 'pdf'
|
|
|
|
|
|
|
|
useCollapsiblePanel(pdfIsOpen, pdfPanelRef)
|
|
|
|
|
2023-10-27 06:50:57 -04:00
|
|
|
const historyIsOpen = view === 'history'
|
2023-10-26 04:57:00 -04:00
|
|
|
|
|
|
|
function setPdfIsOpen(isOpen: boolean) {
|
|
|
|
if (isOpen) {
|
2023-11-15 06:35:18 -05:00
|
|
|
if (detachRole === 'detacher') {
|
|
|
|
reattach()
|
|
|
|
}
|
2023-10-26 04:57:00 -04:00
|
|
|
changeLayout('sideBySide')
|
|
|
|
} else {
|
|
|
|
changeLayout('flat', 'editor')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PanelGroup
|
|
|
|
autoSaveId={
|
|
|
|
shouldPersistLayout ? 'ide-react-editor-and-pdf-layout' : undefined
|
|
|
|
}
|
|
|
|
direction="horizontal"
|
2023-11-15 06:55:05 -05:00
|
|
|
className={classnames('ide-react-editor-and-pdf', {
|
|
|
|
hide: historyIsOpen,
|
|
|
|
'ide-react-editor-and-pdf-resizing': resizing,
|
|
|
|
})}
|
2023-10-26 04:57:00 -04:00
|
|
|
>
|
|
|
|
{editorIsVisible ? (
|
2023-11-02 07:36:04 -04:00
|
|
|
<Panel
|
|
|
|
id="editor"
|
|
|
|
order={1}
|
|
|
|
defaultSize={50}
|
|
|
|
className="ide-react-panel"
|
|
|
|
>
|
2023-11-15 06:55:05 -05:00
|
|
|
<div className="ide-react-editor-content full-size">
|
|
|
|
{editorContent}
|
|
|
|
</div>
|
2023-10-26 04:57:00 -04:00
|
|
|
</Panel>
|
|
|
|
) : null}
|
2023-11-13 05:54:23 -05:00
|
|
|
<HorizontalResizeHandle
|
|
|
|
resizable={isDualPane}
|
|
|
|
onDoubleClick={() => setPdfIsOpen(!pdfIsOpen)}
|
2023-11-15 06:55:05 -05:00
|
|
|
onDragging={setResizing}
|
2023-11-13 05:54:23 -05:00
|
|
|
>
|
2023-11-06 08:09:10 -05:00
|
|
|
<HorizontalToggler
|
|
|
|
id="editor-pdf"
|
|
|
|
togglerType="east"
|
|
|
|
isOpen={pdfIsOpen}
|
2023-11-13 05:54:23 -05:00
|
|
|
setIsOpen={setPdfIsOpen}
|
2023-11-06 08:09:10 -05:00
|
|
|
tooltipWhenOpen={t('tooltip_hide_pdf')}
|
|
|
|
tooltipWhenClosed={t('tooltip_show_pdf')}
|
|
|
|
/>
|
|
|
|
{isDualPane ? (
|
2023-11-02 07:36:04 -04:00
|
|
|
<div className="synctex-controls">
|
|
|
|
<DefaultSynctexControl />
|
|
|
|
</div>
|
2023-11-06 08:09:10 -05:00
|
|
|
) : null}
|
|
|
|
</HorizontalResizeHandle>
|
2023-11-15 06:55:05 -05:00
|
|
|
{pdfIsOpen || resizing ? (
|
2023-10-26 04:57:00 -04:00
|
|
|
<Panel
|
|
|
|
ref={pdfPanelRef}
|
|
|
|
id="pdf"
|
|
|
|
order={2}
|
|
|
|
defaultSize={50}
|
|
|
|
minSize={5}
|
|
|
|
collapsible
|
|
|
|
onCollapse={collapsed => setPdfIsOpen(!collapsed)}
|
2023-11-02 07:36:04 -04:00
|
|
|
className="ide-react-panel"
|
2023-10-26 04:57:00 -04:00
|
|
|
>
|
2023-11-02 07:36:04 -04:00
|
|
|
<PdfPreview />
|
2023-10-26 04:57:00 -04:00
|
|
|
</Panel>
|
|
|
|
) : null}
|
|
|
|
</PanelGroup>
|
|
|
|
)
|
|
|
|
}
|