From abe62315693d23f110c4e6b44d6bc7d0b79a8aa8 Mon Sep 17 00:00:00 2001 From: Matthew Runyon Date: Thu, 30 Mar 2023 13:55:39 -0500 Subject: [PATCH] Fix replacing existing preview panel --- .../src/ConsolePlugin.tsx | 75 +++++++++---------- packages/dashboard/src/layout/LayoutUtils.ts | 2 + 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/packages/dashboard-core-plugins/src/ConsolePlugin.tsx b/packages/dashboard-core-plugins/src/ConsolePlugin.tsx index 4cf5c7813a..a2ff7eb48d 100644 --- a/packages/dashboard-core-plugins/src/ConsolePlugin.tsx +++ b/packages/dashboard-core-plugins/src/ConsolePlugin.tsx @@ -446,47 +446,24 @@ export function ConsolePlugin( const isFileOpen = fileIsOpen(fileMetadata); const isFileOpenAsPreview = fileIsOpenAsPreview(fileMetadata); - // If the file is already open, just show and focus it if necessary - if (isFileOpen) { - showFilePanel(fileMetadata); - if (shouldFocus) { - const panelId = getPanelIdForFileMetadata(fileMetadata); - focusPanelById(panelId); - } - return; + const [previewId] = [...previewFileMap.values()]; + const isPreview = !shouldFocus; + const panelId = + isPreview && previewId + ? previewId + : getPanelIdForFileMetadata(fileMetadata); + + if (!panelId) { + throw new Error( + 'Unable to retrieve or create panelId for metadata', + fileMetadata + ); } - // If the file is already open as a preview and we're not focusing it, just show it - // If we're focusing it, that means we need to replace the panel anyway with a non-preview panel, so just fall into the logic below - if (isFileOpenAsPreview && !shouldFocus) { + // If the file is already open, show it + if (isFileOpen || isFileOpenAsPreview) { showFilePanel(fileMetadata); - return; - } - - // By this point, the user has double clicked the panel - - const [previewTabName] = Array.from(previewFileMap.keys()); - const previewTabId = previewFileMap.get(previewTabName); - let panelId: string | undefined; - if (previewTabId != null) { - panelId = previewTabId; - - const stack = LayoutUtils.getStackForConfig(layout.root, { - component: NotebookPanel.COMPONENT, - id: panelId, - }); - - const item = LayoutUtils.getContentItemInStack(stack, { - component: NotebookPanel.COMPONENT, - id: previewTabId, - }); - if (item && isComponent(item)) { - item.container.emit(NotebookEvent.PROMOTE_FROM_PREVIEW); - deletePreviewFileMapEntry(previewTabName); - addOpenFileMapEntry(previewTabName, previewTabId); - } } else { - panelId = getPanelIdForFileMetadata(fileMetadata); const stack = LayoutUtils.getStackForComponentTypes(layout.root, [ NotebookPanel.COMPONENT, ]); @@ -497,14 +474,34 @@ export function ConsolePlugin( fileMetadata, session, sessionLanguage, - isPreview: !shouldFocus, + isPreview, }); + // This will replace the existing preview by panelId if needed LayoutUtils.openComponentInStack(stack, config); } + // If the file is open as a preview and focused, promote to non-preview + if (isFileOpenAsPreview && shouldFocus) { + const fileId = fileMetadata.id; + const stack = LayoutUtils.getStackForConfig(layout.root, { + component: NotebookPanel.COMPONENT, + id: panelId, + }); + + const item = LayoutUtils.getContentItemInStack(stack, { + component: NotebookPanel.COMPONENT, + id: panelId, + }); + if (item && isComponent(item)) { + item.container.emit(NotebookEvent.PROMOTE_FROM_PREVIEW); + deletePreviewFileMapEntry(fileId); + addOpenFileMapEntry(fileId, panelId); + } + } + if (shouldFocus) { - // Focus the tab we just opened if we're supposed to + // Focus the tab if we're supposed to focusPanelById(panelId); } }, diff --git a/packages/dashboard/src/layout/LayoutUtils.ts b/packages/dashboard/src/layout/LayoutUtils.ts index b1382116c8..d49cf62e87 100644 --- a/packages/dashboard/src/layout/LayoutUtils.ts +++ b/packages/dashboard/src/layout/LayoutUtils.ts @@ -559,6 +559,8 @@ class LayoutUtils { searchConfig ); + console.log(oldContentItem, searchConfig); + if (replaceExisting && oldContentItem && stack) { const index = stack?.contentItems.indexOf(oldContentItem);