editor-and-pdf.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. tagName="section"
  52. aria-label={t('editor')}
  53. >
  54. {selectedEntityCount === 0 && <NoSelectionPane />}
  55. {selectedEntityCount === 1 && openEntity?.type === 'fileRef' && (
  56. <FileView
  57. file={fileViewFile(openEntity.entity)}
  58. key={openEntity.entity._id}
  59. />
  60. )}
  61. {selectedEntityCount > 1 && (
  62. <MultipleSelectionPane selectedEntityCount={selectedEntityCount} />
  63. )}
  64. <EditorPane />
  65. </Panel>
  66. <HorizontalResizeHandle
  67. resizable={pdfLayout === 'sideBySide'}
  68. onDoubleClick={togglePdfPane}
  69. onDragging={setResizing}
  70. className={classNames({
  71. hidden: !editorIsOpen,
  72. })}
  73. hitAreaMargins={{ coarse: 0, fine: 0 }}
  74. >
  75. <HorizontalToggler
  76. id="editor-pdf"
  77. togglerType="east"
  78. isOpen={pdfIsOpen}
  79. setIsOpen={setPdfIsOpen}
  80. tooltipWhenOpen={t('tooltip_hide_pdf')}
  81. tooltipWhenClosed={t('tooltip_show_pdf')}
  82. />
  83. {pdfLayout === 'sideBySide' && (
  84. <div className="synctex-controls">
  85. <DefaultSynctexControl />
  86. </div>
  87. )}
  88. </HorizontalResizeHandle>
  89. {/* pdf */}
  90. <Panel
  91. ref={pdfPanelRef}
  92. id="panel-pdf"
  93. order={2}
  94. defaultSize={50}
  95. minSize={5}
  96. collapsible
  97. onCollapse={handlePdfPaneCollapse}
  98. onExpand={handlePdfPaneExpand}
  99. className="ide-react-panel"
  100. tagName="section"
  101. aria-label={t('pdf_preview_logs')}
  102. >
  103. <PdfPreview />
  104. {/* ensure that "sync to code" is available in PDF only layout */}
  105. {pdfLayout === 'flat' && view === 'pdf' && (
  106. <div className="synctex-controls" hidden>
  107. <DefaultSynctexControl />
  108. </div>
  109. )}
  110. </Panel>
  111. </PanelGroup>
  112. )
  113. }