Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/mermaid-chat-features/src/webviewManager.ts
5223 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import * as vscode from 'vscode';
6
7
export interface MermaidWebviewInfo {
8
readonly id: string;
9
readonly webview: vscode.Webview;
10
readonly mermaidSource: string;
11
readonly title: string | undefined;
12
readonly type: 'chat' | 'editor';
13
}
14
15
/**
16
* Manages all mermaid webviews (both chat output renderers and editor previews).
17
* Tracks the active webview and provides methods for interacting with webviews.
18
*/
19
export class MermaidWebviewManager {
20
21
private _activeWebviewId: string | undefined;
22
private readonly _webviews = new Map<string, MermaidWebviewInfo>();
23
24
/**
25
* Gets the currently active webview info.
26
*/
27
public get activeWebview(): MermaidWebviewInfo | undefined {
28
return this._activeWebviewId ? this._webviews.get(this._activeWebviewId) : undefined;
29
}
30
31
public registerWebview(id: string, webview: vscode.Webview, mermaidSource: string, title: string | undefined, type: 'chat' | 'editor'): vscode.Disposable {
32
if (this._webviews.has(id)) {
33
throw new Error(`Webview with id ${id} is already registered.`);
34
}
35
36
const info: MermaidWebviewInfo = {
37
id,
38
webview,
39
mermaidSource,
40
title,
41
type
42
};
43
this._webviews.set(id, info);
44
return { dispose: () => this.unregisterWebview(id) };
45
}
46
47
private unregisterWebview(id: string): void {
48
this._webviews.delete(id);
49
50
// Clear active if this was the active webview
51
if (this._activeWebviewId === id) {
52
this._activeWebviewId = undefined;
53
}
54
}
55
56
public setActiveWebview(id: string): void {
57
if (this._webviews.has(id)) {
58
this._activeWebviewId = id;
59
}
60
}
61
62
public getWebview(id: string): MermaidWebviewInfo | undefined {
63
return this._webviews.get(id);
64
}
65
66
/**
67
* Sends a reset pan/zoom message to a specific webview by ID.
68
*/
69
public resetPanZoom(id: string | undefined): void {
70
const target = id ? this._webviews.get(id) : this.activeWebview;
71
target?.webview.postMessage({ type: 'resetPanZoom' });
72
}
73
}
74
75