Path: blob/main/extensions/mermaid-chat-features/src/webviewManager.ts
5223 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import * as vscode from 'vscode';56export interface MermaidWebviewInfo {7readonly id: string;8readonly webview: vscode.Webview;9readonly mermaidSource: string;10readonly title: string | undefined;11readonly type: 'chat' | 'editor';12}1314/**15* Manages all mermaid webviews (both chat output renderers and editor previews).16* Tracks the active webview and provides methods for interacting with webviews.17*/18export class MermaidWebviewManager {1920private _activeWebviewId: string | undefined;21private readonly _webviews = new Map<string, MermaidWebviewInfo>();2223/**24* Gets the currently active webview info.25*/26public get activeWebview(): MermaidWebviewInfo | undefined {27return this._activeWebviewId ? this._webviews.get(this._activeWebviewId) : undefined;28}2930public registerWebview(id: string, webview: vscode.Webview, mermaidSource: string, title: string | undefined, type: 'chat' | 'editor'): vscode.Disposable {31if (this._webviews.has(id)) {32throw new Error(`Webview with id ${id} is already registered.`);33}3435const info: MermaidWebviewInfo = {36id,37webview,38mermaidSource,39title,40type41};42this._webviews.set(id, info);43return { dispose: () => this.unregisterWebview(id) };44}4546private unregisterWebview(id: string): void {47this._webviews.delete(id);4849// Clear active if this was the active webview50if (this._activeWebviewId === id) {51this._activeWebviewId = undefined;52}53}5455public setActiveWebview(id: string): void {56if (this._webviews.has(id)) {57this._activeWebviewId = id;58}59}6061public getWebview(id: string): MermaidWebviewInfo | undefined {62return this._webviews.get(id);63}6465/**66* Sends a reset pan/zoom message to a specific webview by ID.67*/68public resetPanZoom(id: string | undefined): void {69const target = id ? this._webviews.get(id) : this.activeWebview;70target?.webview.postMessage({ type: 'resetPanZoom' });71}72}737475