editor-and-pdf.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. {/* main */}
  42. <Panel
  43. id="panel-main"
  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 file={fileViewFile(openEntity.entity)} />
  55. )}
  56. {selectedEntityCount > 1 && (
  57. <MultipleSelectionPane selectedEntityCount={selectedEntityCount} />
  58. )}
  59. <EditorPane />
  60. </Panel>
  61. <HorizontalResizeHandle
  62. resizable={pdfLayout === 'sideBySide'}
  63. onDoubleClick={togglePdfPane}
  64. onDragging={setResizing}
  65. className={classNames({
  66. hidden: !editorIsOpen,
  67. })}
  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. {/* pdf */}
  84. <Panel
  85. ref={pdfPanelRef}
  86. id="panel-pdf"
  87. order={2}
  88. defaultSize={50}
  89. minSize={5}
  90. collapsible
  91. onCollapse={handlePdfPaneCollapse}
  92. onExpand={handlePdfPaneExpand}
  93. className="ide-react-panel"
  94. >
  95. {pdfIsOpen && <PdfPreview />}
  96. {/* ensure that "sync to code" is available in PDF only layout */}
  97. {pdfLayout === 'flat' && view === 'pdf' && (
  98. <div className="synctex-controls" hidden>
  99. <DefaultSynctexControl />
  100. </div>
  101. )}
  102. </Panel>
  103. </PanelGroup>
  104. )
  105. }