Skip to content

Commit

Permalink
feat: implement session restoration for MathPreviewPanel with tab man…
Browse files Browse the repository at this point in the history
…agement
  • Loading branch information
tamuratak committed Jan 23, 2025
1 parent 30f4c79 commit acb4d87
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/components/mathpreviewpanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function resourcesFolder(extensionRoot: string) {
return vscode.Uri.file(folder)
}

let restored = false

export class MathPreviewPanelSerializer implements vscode.WebviewPanelSerializer {

constructor(private readonly extension: {
Expand All @@ -34,6 +36,7 @@ export class MathPreviewPanelSerializer implements vscode.WebviewPanelSerializer
}
panel.webview.html = this.extension.mathPreviewPanel.getHtml(panel.webview)
this.extension.logger.info('Math preview panel: restored')
restored = true
return Promise.resolve()
}

Expand Down Expand Up @@ -63,6 +66,33 @@ export class MathPreviewPanel {
}),
new vscode.Disposable(() => void this.panel?.dispose())
)
setTimeout(() => this.reopenPanelOnNewSession(), 1000)
}

private findPanelTabs() {
return vscode.window.tabGroups.all.flatMap(group =>
group.tabs.filter(tab => {
return tab.input instanceof vscode.TabInputWebview && tab.input.viewType.includes('latex-toybox-mathpreview')
})
)
}

// When the extension host reloads due to an extension update or other reasons,
// the connection with the webview is lost. Therefore, we close the old panel
// and open a new panel.
private async reopenPanelOnNewSession() {
if (restored || this.panel) {
return
}
const oldPanelTab = this.findPanelTabs()[0]
if (oldPanelTab) {
await this.open()
// We need to locate the old tab again because the oldPanelTab object becomes invalid after a tab operation.
const theOldPanelTab = this.findPanelTabs()[0]
if (theOldPanelTab) {
await vscode.window.tabGroups.close(theOldPanelTab)
}
}
}

private get mathPreview() {
Expand Down

0 comments on commit acb4d87

Please sign in to comment.