editor-and-pdf.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 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. hitAreaMargins={{ coarse: 0, fine: 0 }}
  69. >
  70. <HorizontalToggler
  71. id="editor-pdf"
  72. togglerType="east"
  73. isOpen={pdfIsOpen}
  74. setIsOpen={setPdfIsOpen}
  75. tooltipWhenOpen={t('tooltip_hide_pdf')}
  76. tooltipWhenClosed={t('tooltip_show_pdf')}
  77. />
  78. {pdfLayout === 'sideBySide' && (
  79. <div className="synctex-controls">
  80. <DefaultSynctexControl />
  81. </div>
  82. )}
  83. </HorizontalResizeHandle>
  84. {/* pdf */}
  85. <Panel
  86. ref={pdfPanelRef}
  87. id="panel-pdf"
  88. order={2}
  89. defaultSize={50}
  90. minSize={5}
  91. collapsible
  92. onCollapse={handlePdfPaneCollapse}
  93. onExpand={handlePdfPaneExpand}
  94. className="ide-react-panel"
  95. >
  96. {pdfIsOpen && <PdfPreview />}
  97. {/* ensure that "sync to code" is available in PDF only layout */}
  98. {pdfLayout === 'flat' && view === 'pdf' && (
  99. <div className="synctex-controls" hidden>
  100. <DefaultSynctexControl />
  101. </div>
  102. )}
  103. </Panel>
  104. </PanelGroup>
  105. )
  106. }