Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/mermaid-chat-features/chat-webview-src/index.ts
3520 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 mermaid, { MermaidConfig } from 'mermaid';
6
7
function getMermaidTheme() {
8
return document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
9
? 'dark'
10
: 'default';
11
}
12
13
type State = {
14
readonly diagramText: string;
15
readonly theme: 'dark' | 'default';
16
};
17
18
let state: State | undefined = undefined;
19
20
function init() {
21
const diagram = document.querySelector('.mermaid');
22
if (!diagram) {
23
return;
24
}
25
26
const theme = getMermaidTheme();
27
state = {
28
diagramText: diagram.textContent ?? '',
29
theme
30
};
31
32
const config: MermaidConfig = {
33
startOnLoad: true,
34
theme,
35
};
36
mermaid.initialize(config);
37
}
38
39
function tryUpdate() {
40
const newTheme = getMermaidTheme();
41
if (state?.theme === newTheme) {
42
return;
43
}
44
45
const diagramNode = document.querySelector('.mermaid');
46
if (!diagramNode || !(diagramNode instanceof HTMLElement)) {
47
return;
48
}
49
50
state = {
51
diagramText: state?.diagramText ?? '',
52
theme: newTheme
53
};
54
55
// Re-render
56
diagramNode.textContent = state?.diagramText ?? '';
57
delete diagramNode.dataset.processed;
58
59
mermaid.initialize({
60
theme: newTheme,
61
});
62
mermaid.run({
63
nodes: [diagramNode]
64
});
65
}
66
67
// Update when theme changes
68
new MutationObserver(() => {
69
tryUpdate();
70
}).observe(document.body, { attributes: true, attributeFilter: ['class'] });
71
72
init();
73
74
75