editor-and-sidebar.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import React, { useCallback, useEffect, useRef, useState } from 'react'
  2. import TwoColumnMainContent from '@/features/ide-react/components/layout/two-column-main-content'
  3. import EditorSidebar from '@/features/ide-react/components/editor-sidebar'
  4. import History from '@/features/ide-react/components/history'
  5. import { HistoryProvider } from '@/features/history/context/history-context'
  6. import { useProjectContext } from '@/shared/context/project-context'
  7. import { useLayoutContext } from '@/shared/context/layout-context'
  8. import {
  9. FileTreeDeleteHandler,
  10. FileTreeDocumentFindResult,
  11. FileTreeFileRefFindResult,
  12. FileTreeFindResult,
  13. } from '@/features/ide-react/types/file-tree'
  14. import FileView from '@/features/file-view/components/file-view'
  15. import { FileRef } from '../../../../../types/file-ref'
  16. import { EditorPane } from '@/features/ide-react/components/editor/editor-pane'
  17. import EditorAndPdf from '@/features/ide-react/components/editor-and-pdf'
  18. import MultipleSelectionPane from '@/features/ide-react/components/editor/multiple-selection-pane'
  19. import NoSelectionPane from '@/features/ide-react/components/editor/no-selection-pane'
  20. import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context'
  21. import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context'
  22. import NoOpenDocPane from '@/features/ide-react/components/editor/no-open-doc-pane'
  23. import { debugConsole } from '@/utils/debugging'
  24. import { HistorySidebar } from '@/features/ide-react/components/history-sidebar'
  25. import { BinaryFile } from '@/features/file-view/types/binary-file'
  26. import useScopeValue from '@/shared/hooks/use-scope-value'
  27. import { useSelectFileTreeEntity } from '@/features/ide-react/hooks/use-select-file-tree-entity'
  28. type EditorAndSidebarProps = {
  29. shouldPersistLayout: boolean
  30. leftColumnDefaultSize: number
  31. setLeftColumnDefaultSize: React.Dispatch<React.SetStateAction<number>>
  32. }
  33. function convertFileRefToBinaryFile(fileRef: FileRef): BinaryFile {
  34. return {
  35. _id: fileRef._id,
  36. name: fileRef.name,
  37. id: fileRef._id,
  38. type: 'file',
  39. selected: true,
  40. linkedFileData: fileRef.linkedFileData,
  41. created: fileRef.created ? new Date(fileRef.created) : new Date(),
  42. }
  43. }
  44. // `FileViewHeader`, which is TypeScript, expects a BinaryFile, which has a
  45. // `created` property of type `Date`, while `TPRFileViewInfo`, written in JS,
  46. // into which `FileViewHeader` passes its BinaryFile, expects a file object with
  47. // `created` property of type `string`, which is a mismatch. `TPRFileViewInfo`
  48. // is the only one making runtime complaints and it seems that other uses of
  49. // `FileViewHeader` pass in a string for `created`, so that's what this function
  50. // does too.
  51. function fileViewFile(fileRef: FileRef) {
  52. return {
  53. ...convertFileRefToBinaryFile(fileRef),
  54. created: fileRef.created,
  55. }
  56. }
  57. export function EditorAndSidebar({
  58. shouldPersistLayout,
  59. leftColumnDefaultSize,
  60. setLeftColumnDefaultSize,
  61. }: EditorAndSidebarProps) {
  62. const [leftColumnIsOpen, setLeftColumnIsOpen] = useState(true)
  63. const { rootDocId } = useProjectContext()
  64. const { eventEmitter } = useIdeReactContext()
  65. const {
  66. openDocId: openDocWithId,
  67. currentDocumentId: openDocId,
  68. openInitialDoc,
  69. } = useEditorManagerContext()
  70. const { view } = useLayoutContext()
  71. const { projectJoined } = useIdeReactContext()
  72. const { selectEntity } = useSelectFileTreeEntity()
  73. const [, setOpenFile] = useScopeValue<BinaryFile | null>('openFile')
  74. const historyIsOpen = view === 'history'
  75. const [openEntity, setOpenEntity] = useState<
  76. FileTreeDocumentFindResult | FileTreeFileRefFindResult | null
  77. >(null)
  78. const [selectedEntityCount, setSelectedEntityCount] = useState(0)
  79. const [fileTreeReady, setFileTreeReady] = useState(false)
  80. const handleFileTreeInit = useCallback(() => {
  81. setFileTreeReady(true)
  82. }, [])
  83. // Open a document in the editor when one is selected in the file tree
  84. const handleFileTreeSelect = useCallback(
  85. (selectedEntities: FileTreeFindResult[]) => {
  86. debugConsole.log('File tree selection changed', selectedEntities)
  87. setSelectedEntityCount(selectedEntities.length)
  88. if (selectedEntities.length !== 1) {
  89. setOpenEntity(null)
  90. return
  91. }
  92. const [selected] = selectedEntities
  93. if (selected.type === 'folder') {
  94. return
  95. }
  96. setOpenEntity(selected)
  97. if (selected.type === 'doc' && fileTreeReady) {
  98. openDocWithId(selected.entity._id)
  99. }
  100. // Keep openFile scope value in sync with the file tree
  101. const openFile =
  102. selected.type === 'fileRef'
  103. ? convertFileRefToBinaryFile(selected.entity)
  104. : null
  105. setOpenFile(openFile)
  106. if (openFile) {
  107. window.dispatchEvent(new CustomEvent('file-view:file-opened'))
  108. }
  109. },
  110. [fileTreeReady, setOpenFile, openDocWithId]
  111. )
  112. const handleFileTreeDelete: FileTreeDeleteHandler = useCallback(
  113. entity => {
  114. eventEmitter.emit('entity:deleted', entity)
  115. // Select the root document if the current document was deleted
  116. if (entity.entity._id === openDocId) {
  117. openDocWithId(rootDocId)
  118. }
  119. },
  120. [eventEmitter, openDocId, openDocWithId, rootDocId]
  121. )
  122. // Synchronize the file tree when openDoc or openDocId is called on the editor
  123. // manager context from elsewhere. If the file tree does change, it will
  124. // trigger the onSelect handler in this component, which will update the local
  125. // state.
  126. useEffect(() => {
  127. debugConsole.log(`openDocId changed to ${openDocId}`)
  128. if (openDocId !== null) {
  129. selectEntity(openDocId)
  130. }
  131. }, [openDocId, selectEntity])
  132. // Open a document once the file tree and project are ready
  133. const initialOpenDoneRef = useRef(false)
  134. useEffect(() => {
  135. if (fileTreeReady && projectJoined && !initialOpenDoneRef.current) {
  136. initialOpenDoneRef.current = true
  137. openInitialDoc(rootDocId)
  138. }
  139. }, [fileTreeReady, openInitialDoc, projectJoined, rootDocId])
  140. // Keep the editor file tree around so that it is available and up to date
  141. // when restoring a file
  142. const leftColumnContent = (
  143. <>
  144. <EditorSidebar
  145. shouldShow={!historyIsOpen}
  146. shouldPersistLayout={shouldPersistLayout}
  147. onFileTreeInit={handleFileTreeInit}
  148. onFileTreeSelect={handleFileTreeSelect}
  149. onFileTreeDelete={handleFileTreeDelete}
  150. />
  151. {historyIsOpen ? <HistorySidebar /> : null}
  152. </>
  153. )
  154. let rightColumnContent
  155. if (historyIsOpen) {
  156. rightColumnContent = (
  157. <HistoryProvider>
  158. <History />
  159. </HistoryProvider>
  160. )
  161. } else {
  162. let editorContent = null
  163. // Always have the editor mounted when not in history view, and hide and
  164. // show it as necessary
  165. const editorPane = (
  166. <EditorPane
  167. shouldPersistLayout={shouldPersistLayout}
  168. show={openEntity?.type === 'doc' && selectedEntityCount === 1}
  169. />
  170. )
  171. if (openDocId === undefined) {
  172. rightColumnContent = <NoOpenDocPane />
  173. } else if (selectedEntityCount === 0) {
  174. rightColumnContent = (
  175. <>
  176. {editorPane}
  177. <NoSelectionPane />
  178. </>
  179. )
  180. } else if (selectedEntityCount > 1) {
  181. editorContent = (
  182. <>
  183. {editorPane}
  184. <MultipleSelectionPane selectedEntityCount={selectedEntityCount} />
  185. </>
  186. )
  187. } else if (openEntity) {
  188. editorContent =
  189. openEntity.type === 'doc' ? (
  190. editorPane
  191. ) : (
  192. <>
  193. {editorPane}
  194. <FileView file={fileViewFile(openEntity.entity)} />
  195. </>
  196. )
  197. }
  198. if (editorContent) {
  199. rightColumnContent = (
  200. <EditorAndPdf
  201. editorContent={editorContent}
  202. shouldPersistLayout={shouldPersistLayout}
  203. />
  204. )
  205. }
  206. }
  207. return (
  208. <TwoColumnMainContent
  209. leftColumnId="editor-left-column"
  210. leftColumnContent={leftColumnContent}
  211. leftColumnDefaultSize={leftColumnDefaultSize}
  212. setLeftColumnDefaultSize={setLeftColumnDefaultSize}
  213. rightColumnContent={rightColumnContent}
  214. leftColumnIsOpen={leftColumnIsOpen}
  215. setLeftColumnIsOpen={setLeftColumnIsOpen}
  216. shouldPersistLayout={shouldPersistLayout}
  217. />
  218. )
  219. }