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