editor-and-pdf.tsx 3.8 KB

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