From 3e11ee6e5a40e73f5345a2d5f5a2a0463b024953 Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Thu, 22 Feb 2024 13:20:37 +0000 Subject: [PATCH] Ensure that the editor state is preserved when switching views (#17238) GitOrigin-RevId: 946b82cb633afbea13a72e30a9ce432aa1a8196d --- .../ide-react/components/editor-and-pdf.tsx | 90 +++++++++---------- .../components/editor/editor-pane.tsx | 10 ++- .../components/layout/main-layout.tsx | 12 +-- .../hooks/use-codemirror-scope.ts | 3 + 4 files changed, 58 insertions(+), 57 deletions(-) diff --git a/services/web/frontend/js/features/ide-react/components/editor-and-pdf.tsx b/services/web/frontend/js/features/ide-react/components/editor-and-pdf.tsx index dfd892bd44..53ba5de77d 100644 --- a/services/web/frontend/js/features/ide-react/components/editor-and-pdf.tsx +++ b/services/web/frontend/js/features/ide-react/components/editor-and-pdf.tsx @@ -13,10 +13,9 @@ import { useTranslation } from 'react-i18next' import classNames from 'classnames' import { fileViewFile } from '@/features/ide-react/util/file-view' import { useFileTreeOpenContext } from '@/features/ide-react/context/file-tree-open-context' +import { EditorPane } from '@/features/ide-react/components/editor/editor-pane' -export const EditorAndPdf: FC<{ - editorPane: React.ReactNode -}> = ({ editorPane }) => { +export const EditorAndPdf: FC = () => { const [resizing, setResizing] = useState(false) const { t } = useTranslation() @@ -43,54 +42,53 @@ export const EditorAndPdf: FC<{ direction="horizontal" className={classNames({ 'ide-panel-group-resizing': resizing, + hidden: view === 'history', })} > {/* main */} - {editorIsOpen && ( - <> - - {selectedEntityCount === 0 && } - {selectedEntityCount === 1 && openEntity?.type === 'fileRef' && ( - - )} - {selectedEntityCount === 1 && editorPane} - {selectedEntityCount > 1 && ( - - )} - + + {selectedEntityCount === 0 && } + {selectedEntityCount === 1 && openEntity?.type === 'fileRef' && ( + + )} + {selectedEntityCount > 1 && ( + + )} + + - - + + - {pdfLayout === 'sideBySide' && ( -
- -
- )} -
- - )} + {pdfLayout === 'sideBySide' && ( +
+ +
+ )} +
{/* pdf */} { const [editor] = useScopeValue('editor') const { selectedEntityCount, openEntity } = useFileTreeOpenContext() + const { currentDocumentId } = useEditorManagerContext() + + if (!currentDocumentId) { + return null + } const isLoading = Boolean( (!editor.sharejs_doc || editor.opening) && diff --git a/services/web/frontend/js/features/ide-react/components/layout/main-layout.tsx b/services/web/frontend/js/features/ide-react/components/layout/main-layout.tsx index fbaa8f520d..7da8e4060d 100644 --- a/services/web/frontend/js/features/ide-react/components/layout/main-layout.tsx +++ b/services/web/frontend/js/features/ide-react/components/layout/main-layout.tsx @@ -10,12 +10,10 @@ import { HistorySidebar } from '@/features/ide-react/components/history-sidebar' import { HistoryProvider } from '@/features/history/context/history-context' import History from '@/features/ide-react/components/history' import EditorSidebar from '@/features/ide-react/components/editor-sidebar' -import { EditorPane } from '@/features/ide-react/components/editor/editor-pane' import { useTranslation } from 'react-i18next' import { useSidebarPane } from '@/features/ide-react/hooks/use-sidebar-pane' import { useChatPane } from '@/features/ide-react/hooks/use-chat-pane' import { EditorAndPdf } from '@/features/ide-react/components/editor-and-pdf' -import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context' export const MainLayout: FC = () => { const { view } = useLayoutContext() @@ -41,13 +39,8 @@ export const MainLayout: FC = () => { handlePaneExpand: handleChatExpand, } = useChatPane() - const { currentDocumentId } = useEditorManagerContext() - const { t } = useTranslation() - // keep the editor pane open when a doc is open, even if the history view is open - const editorPane = currentDocumentId ? : null - return (
@@ -93,13 +86,12 @@ export const MainLayout: FC = () => { - {view === 'history' ? ( + {view === 'history' && ( - ) : ( - )} + { if (currentDoc) { + debugConsole.log('creating new editor state') + const state = EditorState.create({ doc: currentDoc.getSnapshot(), extensions: createExtensions({