-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.ts
82 lines (72 loc) · 2.23 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { BrowserWindow } from "@electron/remote";
import { ChartData, MermaidRenderer } from "@markdown-confluence/lib";
import MermaidRendererClient from "mermaid_renderer.esbuild";
let mermaidRenderHtml: string;
export class ElectronMermaidRenderer implements MermaidRenderer {
async captureMermaidCharts(
charts: ChartData[]
): Promise<Map<string, Buffer>> {
if (!mermaidRenderHtml) {
mermaidRenderHtml = URL.createObjectURL(
new Blob([MermaidRendererClient], { type: "text/html" })
);
}
const capturedCharts = new Map<string, Buffer>();
const promises = charts.map(async (chart) => {
const chartWindow = new BrowserWindow({
width: 800,
height: 600,
show: false,
frame: false,
});
await chartWindow.loadURL(mermaidRenderHtml);
const mermaidConfig = {
theme: "base",
themeVariables: {
background: "#ffffff",
mainBkg: "#ddebff",
primaryColor: "#ddebff",
primaryTextColor: "#192b50",
primaryBorderColor: "#0052cc",
secondaryColor: "#ff8f73",
secondaryTextColor: "#192b50",
secondaryBorderColor: "#df360c",
tertiaryColor: "#c0b6f3",
tertiaryTextColor: "#fefefe",
tertiaryBorderColor: "#5243aa",
noteBkgColor: "#ffc403",
noteTextColor: "#182a4e",
textColor: "#ff0000",
titleColor: "#0052cc",
},
};
// Render the chart and get the dimensions
const dimensions = await chartWindow.webContents.executeJavaScript(`
renderMermaidChart(\`${chart.data}\`, ${JSON.stringify(mermaidConfig)});
`);
// Resize the window to fit the chart dimensions
const { width, height } = dimensions;
chartWindow.setSize(width, height);
try {
// Capture the chart as a NativeImage
const image = await chartWindow.webContents.capturePage(
dimensions,
{ stayHidden: true, stayAwake: true }
);
// Convert the NativeImage to a PNG buffer
const imageBuffer = image.toPNG();
// Clean up the window
chartWindow.close();
// Add the buffer to the capturedCharts map
capturedCharts.set(chart.name, imageBuffer);
// Resolve the promise
} catch (error) {
// Handle errors and clean up
chartWindow.close();
throw error;
}
});
await Promise.all(promises);
return capturedCharts;
}
}