2023-11-02 07:36:04 -04:00
|
|
|
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
2023-10-26 04:57:00 -04:00
|
|
|
import TwoColumnMainContent from '@/features/ide-react/components/layout/two-column-main-content'
|
|
|
|
import EditorSidebar from '@/features/ide-react/components/editor-sidebar'
|
2023-10-27 06:50:57 -04:00
|
|
|
import History from '@/features/ide-react/components/history'
|
|
|
|
import { HistoryProvider } from '@/features/history/context/history-context'
|
2023-10-26 04:57:00 -04:00
|
|
|
import { useProjectContext } from '@/shared/context/project-context'
|
2023-10-27 06:50:57 -04:00
|
|
|
import { useLayoutContext } from '@/shared/context/layout-context'
|
2023-11-02 07:36:04 -04:00
|
|
|
import {
|
|
|
|
FileTreeDeleteHandler,
|
|
|
|
FileTreeDocumentFindResult,
|
|
|
|
FileTreeFileRefFindResult,
|
|
|
|
FileTreeFindResult,
|
|
|
|
} from '@/features/ide-react/types/file-tree'
|
|
|
|
import FileView from '@/features/file-view/components/file-view'
|
|
|
|
import { FileRef } from '../../../../../types/file-ref'
|
|
|
|
import { EditorPane } from '@/features/ide-react/components/editor/editor-pane'
|
|
|
|
import EditorAndPdf from '@/features/ide-react/components/editor-and-pdf'
|
|
|
|
import MultipleSelectionPane from '@/features/ide-react/components/editor/multiple-selection-pane'
|
|
|
|
import NoSelectionPane from '@/features/ide-react/components/editor/no-selection-pane'
|
|
|
|
import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context'
|
|
|
|
import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context'
|
|
|
|
import NoOpenDocPane from '@/features/ide-react/components/editor/no-open-doc-pane'
|
|
|
|
import { debugConsole } from '@/utils/debugging'
|
|
|
|
import { HistorySidebar } from '@/features/ide-react/components/history-sidebar'
|
2023-11-03 08:15:36 -04:00
|
|
|
import { BinaryFile } from '@/features/file-view/types/binary-file'
|
|
|
|
import useScopeValue from '@/shared/hooks/use-scope-value'
|
2023-10-26 04:57:00 -04:00
|
|
|
|
|
|
|
type EditorAndSidebarProps = {
|
|
|
|
shouldPersistLayout: boolean
|
|
|
|
leftColumnDefaultSize: number
|
|
|
|
setLeftColumnDefaultSize: React.Dispatch<React.SetStateAction<number>>
|
|
|
|
}
|
|
|
|
|
2023-11-03 08:15:36 -04:00
|
|
|
function convertFileRefToBinaryFile(fileRef: FileRef): BinaryFile {
|
|
|
|
return {
|
|
|
|
_id: fileRef._id,
|
|
|
|
name: fileRef.name,
|
|
|
|
id: fileRef._id,
|
|
|
|
type: 'file',
|
|
|
|
selected: true,
|
|
|
|
linkedFileData: fileRef.linkedFileData,
|
|
|
|
created: fileRef.created ? new Date(fileRef.created) : new Date(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 07:36:04 -04:00
|
|
|
// `FileViewHeader`, which is TypeScript, expects a BinaryFile, which has a
|
|
|
|
// `created` property of type `Date`, while `TPRFileViewInfo`, written in JS,
|
|
|
|
// into which `FileViewHeader` passes its BinaryFile, expects a file object with
|
|
|
|
// `created` property of type `string`, which is a mismatch. `TPRFileViewInfo`
|
|
|
|
// is the only one making runtime complaints and it seems that other uses of
|
|
|
|
// `FileViewHeader` pass in a string for `created`, so that's what this function
|
|
|
|
// does too.
|
|
|
|
function fileViewFile(fileRef: FileRef) {
|
|
|
|
return {
|
2023-11-03 08:15:36 -04:00
|
|
|
...convertFileRefToBinaryFile(fileRef),
|
2023-11-02 07:36:04 -04:00
|
|
|
created: fileRef.created,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 04:57:00 -04:00
|
|
|
export function EditorAndSidebar({
|
|
|
|
shouldPersistLayout,
|
|
|
|
leftColumnDefaultSize,
|
|
|
|
setLeftColumnDefaultSize,
|
|
|
|
}: EditorAndSidebarProps) {
|
|
|
|
const [leftColumnIsOpen, setLeftColumnIsOpen] = useState(true)
|
2023-11-03 08:15:36 -04:00
|
|
|
const { rootDocId } = useProjectContext()
|
2023-11-02 07:36:04 -04:00
|
|
|
const { eventEmitter } = useIdeReactContext()
|
2023-11-03 08:15:36 -04:00
|
|
|
const {
|
|
|
|
openDocId: openDocWithId,
|
|
|
|
currentDocumentId: openDocId,
|
|
|
|
openInitialDoc,
|
|
|
|
} = useEditorManagerContext()
|
2023-10-27 06:50:57 -04:00
|
|
|
const { view } = useLayoutContext()
|
2023-11-08 07:10:07 -05:00
|
|
|
const { projectJoined } = useIdeReactContext()
|
2023-11-03 08:15:36 -04:00
|
|
|
const [, setOpenFile] = useScopeValue<BinaryFile | null>('openFile')
|
|
|
|
|
2023-10-27 06:50:57 -04:00
|
|
|
const historyIsOpen = view === 'history'
|
2023-10-26 04:57:00 -04:00
|
|
|
|
2023-11-02 07:36:04 -04:00
|
|
|
const [openEntity, setOpenEntity] = useState<
|
|
|
|
FileTreeDocumentFindResult | FileTreeFileRefFindResult | null
|
|
|
|
>(null)
|
|
|
|
const [selectedEntityCount, setSelectedEntityCount] = useState(0)
|
2023-10-26 04:57:00 -04:00
|
|
|
const [fileTreeReady, setFileTreeReady] = useState(false)
|
|
|
|
|
|
|
|
const handleFileTreeInit = useCallback(() => {
|
|
|
|
setFileTreeReady(true)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handleFileTreeSelect = useCallback(
|
|
|
|
(selectedEntities: FileTreeFindResult[]) => {
|
2023-11-02 07:36:04 -04:00
|
|
|
debugConsole.log('File tree selection changed', selectedEntities)
|
|
|
|
setSelectedEntityCount(selectedEntities.length)
|
|
|
|
if (selectedEntities.length !== 1) {
|
|
|
|
setOpenEntity(null)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const [selected] = selectedEntities
|
|
|
|
|
|
|
|
if (selected.type === 'folder') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setOpenEntity(selected)
|
2023-11-03 08:15:36 -04:00
|
|
|
if (selected.type === 'doc' && fileTreeReady) {
|
|
|
|
openDocWithId(selected.entity._id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep openFile scope value in sync with the file tree
|
|
|
|
const openFile =
|
|
|
|
selected.type === 'fileRef'
|
|
|
|
? convertFileRefToBinaryFile(selected.entity)
|
|
|
|
: null
|
|
|
|
setOpenFile(openFile)
|
|
|
|
if (openFile) {
|
|
|
|
window.dispatchEvent(new CustomEvent('file-view:file-opened'))
|
2023-11-02 07:36:04 -04:00
|
|
|
}
|
2023-10-26 04:57:00 -04:00
|
|
|
},
|
2023-11-03 08:15:36 -04:00
|
|
|
[fileTreeReady, setOpenFile, openDocWithId]
|
2023-10-26 04:57:00 -04:00
|
|
|
)
|
|
|
|
|
2023-11-02 07:36:04 -04:00
|
|
|
const handleFileTreeDelete: FileTreeDeleteHandler = useCallback(
|
|
|
|
entity => {
|
|
|
|
eventEmitter.emit('entity:deleted', entity)
|
|
|
|
// Select the root document if the current document was deleted
|
|
|
|
if (entity.entity._id === openDocId) {
|
|
|
|
openDocWithId(rootDocId)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[eventEmitter, openDocId, openDocWithId, rootDocId]
|
|
|
|
)
|
|
|
|
|
|
|
|
// Synchronize the file tree when openDoc or openDocId is called on the editor
|
|
|
|
// manager context from elsewhere. If the file tree does change, it will
|
|
|
|
// trigger the onSelect handler in this component, which will update the local
|
|
|
|
// state.
|
|
|
|
useEffect(() => {
|
2023-11-03 08:15:36 -04:00
|
|
|
debugConsole.log(`openDocId changed to ${openDocId}`)
|
|
|
|
if (openDocId === null) {
|
2023-11-02 07:36:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
window.dispatchEvent(
|
2023-11-03 08:15:36 -04:00
|
|
|
new CustomEvent('editor.openDoc', { detail: openDocId })
|
2023-11-02 07:36:04 -04:00
|
|
|
)
|
2023-11-03 08:15:36 -04:00
|
|
|
}, [openDocId])
|
2023-11-02 07:36:04 -04:00
|
|
|
|
2023-11-08 07:10:07 -05:00
|
|
|
// Open a document once the file tree and project are ready
|
2023-11-03 08:15:36 -04:00
|
|
|
const initialOpenDoneRef = useRef(false)
|
2023-11-02 07:36:04 -04:00
|
|
|
useEffect(() => {
|
2023-11-08 07:10:07 -05:00
|
|
|
if (fileTreeReady && projectJoined && !initialOpenDoneRef.current) {
|
2023-11-03 08:15:36 -04:00
|
|
|
initialOpenDoneRef.current = true
|
|
|
|
openInitialDoc(rootDocId)
|
2023-11-02 07:36:04 -04:00
|
|
|
}
|
2023-11-08 07:10:07 -05:00
|
|
|
}, [fileTreeReady, openInitialDoc, projectJoined, rootDocId])
|
2023-11-03 08:15:36 -04:00
|
|
|
|
|
|
|
// Keep the editor file tree around so that it is available and up to date
|
|
|
|
// when restoring a file
|
|
|
|
const leftColumnContent = (
|
|
|
|
<>
|
|
|
|
<EditorSidebar
|
|
|
|
shouldShow={!historyIsOpen}
|
|
|
|
shouldPersistLayout={shouldPersistLayout}
|
|
|
|
onFileTreeInit={handleFileTreeInit}
|
|
|
|
onFileTreeSelect={handleFileTreeSelect}
|
|
|
|
onFileTreeDelete={handleFileTreeDelete}
|
|
|
|
/>
|
|
|
|
{historyIsOpen ? <HistorySidebar /> : null}
|
|
|
|
</>
|
2023-10-26 04:57:00 -04:00
|
|
|
)
|
2023-10-27 06:50:57 -04:00
|
|
|
|
2023-11-02 07:36:04 -04:00
|
|
|
let rightColumnContent
|
|
|
|
|
|
|
|
if (historyIsOpen) {
|
|
|
|
rightColumnContent = (
|
|
|
|
<HistoryProvider>
|
|
|
|
<History />
|
|
|
|
</HistoryProvider>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let editorContent = null
|
|
|
|
|
|
|
|
// Always have the editor mounted when not in history view, and hide and
|
|
|
|
// show it as necessary
|
|
|
|
const editorPane = (
|
|
|
|
<EditorPane
|
2023-10-27 06:50:57 -04:00
|
|
|
shouldPersistLayout={shouldPersistLayout}
|
2023-11-02 07:36:04 -04:00
|
|
|
show={openEntity?.type === 'doc' && selectedEntityCount === 1}
|
2023-10-27 06:50:57 -04:00
|
|
|
/>
|
2023-11-02 07:36:04 -04:00
|
|
|
)
|
|
|
|
if (openDocId === undefined) {
|
|
|
|
rightColumnContent = <NoOpenDocPane />
|
|
|
|
} else if (selectedEntityCount === 0) {
|
|
|
|
rightColumnContent = (
|
|
|
|
<>
|
|
|
|
{editorPane}
|
|
|
|
<NoSelectionPane />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
} else if (selectedEntityCount > 1) {
|
|
|
|
editorContent = (
|
|
|
|
<>
|
|
|
|
{editorPane}
|
|
|
|
<MultipleSelectionPane selectedEntityCount={selectedEntityCount} />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
} else if (openEntity) {
|
|
|
|
editorContent =
|
|
|
|
openEntity.type === 'doc' ? (
|
|
|
|
editorPane
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{editorPane}
|
|
|
|
<FileView file={fileViewFile(openEntity.entity)} />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (editorContent) {
|
|
|
|
rightColumnContent = (
|
|
|
|
<EditorAndPdf
|
|
|
|
editorContent={editorContent}
|
|
|
|
shouldPersistLayout={shouldPersistLayout}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-10-26 04:57:00 -04:00
|
|
|
|
|
|
|
return (
|
|
|
|
<TwoColumnMainContent
|
|
|
|
leftColumnId="editor-left-column"
|
|
|
|
leftColumnContent={leftColumnContent}
|
|
|
|
leftColumnDefaultSize={leftColumnDefaultSize}
|
|
|
|
setLeftColumnDefaultSize={setLeftColumnDefaultSize}
|
|
|
|
rightColumnContent={rightColumnContent}
|
|
|
|
leftColumnIsOpen={leftColumnIsOpen}
|
|
|
|
setLeftColumnIsOpen={setLeftColumnIsOpen}
|
|
|
|
shouldPersistLayout={shouldPersistLayout}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|