editor-and-pdf.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React, { FC, useState } from 'react'
  2. import { Panel, PanelGroup } from 'react-resizable-panels'
  3. import NoSelectionPane from '@/features/ide-react/components/editor/no-selection-pane'
  4. import FileView from '@/features/file-view/components/file-view'
  5. import MultipleSelectionPane from '@/features/ide-react/components/editor/multiple-selection-pane'
  6. import { HorizontalResizeHandle } from '@/features/ide-react/components/resize/horizontal-resize-handle'
  7. import { HorizontalToggler } from '@/features/ide-react/components/resize/horizontal-toggler'
  8. import { DefaultSynctexControl } from '@/features/pdf-preview/components/detach-synctex-control'
  9. import PdfPreview from '@/features/pdf-preview/components/pdf-preview'
  10. import { usePdfPane } from '@/features/ide-react/hooks/use-pdf-pane'
  11. import { useLayoutContext } from '@/shared/context/layout-context'
  12. import { useTranslation } from 'react-i18next'
  13. import classNames from 'classnames'
  14. import { fileViewFile } from '@/features/ide-react/util/file-view'
  15. import { useFileTreeOpenContext } from '@/features/ide-react/context/file-tree-open-context'
  16. export const EditorAndPdf: FC<{
  17. editorPane: React.ReactNode
  18. }> = ({ editorPane }) => {
  19. const [resizing, setResizing] = useState(false)
  20. const { t } = useTranslation()
  21. const {
  22. togglePdfPane,
  23. handlePdfPaneExpand,
  24. handlePdfPaneCollapse,
  25. setPdfIsOpen,
  26. pdfIsOpen,
  27. pdfPanelRef,
  28. } = usePdfPane()
  29. const { view, pdfLayout } = useLayoutContext()
  30. const { selectedEntityCount, openEntity } = useFileTreeOpenContext()
  31. const editorIsOpen =
  32. view === 'editor' || view === 'file' || pdfLayout === 'sideBySide'
  33. return (
  34. <PanelGroup
  35. autoSaveId="ide-editor-pdf-layout"
  36. direction="horizontal"
  37. className={classNames({
  38. 'ide-panel-group-resizing': resizing,
  39. })}
  40. >
  41. {/* main */}
  42. {editorIsOpen && (
  43. <>
  44. <Panel
  45. id="panel-main"
  46. order={1}
  47. defaultSize={50}
  48. minSize={5}
  49. className={classNames('ide-react-panel', {
  50. 'ide-panel-group-resizing': resizing,
  51. })}
  52. >
  53. {selectedEntityCount === 0 && <NoSelectionPane />}
  54. {selectedEntityCount === 1 && openEntity?.type === 'fileRef' && (
  55. <FileView file={fileViewFile(openEntity.entity)} />
  56. )}
  57. {selectedEntityCount === 1 && editorPane}
  58. {selectedEntityCount > 1 && (
  59. <MultipleSelectionPane
  60. selectedEntityCount={selectedEntityCount}
  61. />
  62. )}
  63. </Panel>
  64. <HorizontalResizeHandle
  65. resizable={pdfLayout === 'sideBySide'}
  66. onDoubleClick={togglePdfPane}
  67. onDragging={setResizing}
  68. >
  69. <HorizontalToggler
  70. id="editor-pdf"
  71. togglerType="east"
  72. isOpen={pdfIsOpen}
  73. setIsOpen={setPdfIsOpen}
  74. tooltipWhenOpen={t('tooltip_hide_pdf')}
  75. tooltipWhenClosed={t('tooltip_show_pdf')}
  76. />
  77. {pdfLayout === 'sideBySide' && (
  78. <div className="synctex-controls">
  79. <DefaultSynctexControl />
  80. </div>
  81. )}
  82. </HorizontalResizeHandle>
  83. </>
  84. )}
  85. {/* pdf */}
  86. <Panel
  87. ref={pdfPanelRef}
  88. id="panel-pdf"
  89. order={2}
  90. defaultSize={50}
  91. minSize={5}
  92. collapsible
  93. onCollapse={handlePdfPaneCollapse}
  94. onExpand={handlePdfPaneExpand}
  95. className="ide-react-panel"
  96. >
  97. {pdfIsOpen && <PdfPreview />}
  98. {/* ensure that "sync to code" is available in PDF only layout */}
  99. {pdfLayout === 'flat' && view === 'pdf' && (
  100. <div className="synctex-controls" hidden>
  101. <DefaultSynctexControl />
  102. </div>
  103. )}
  104. </Panel>
  105. </PanelGroup>
  106. )
  107. }