@marimo-team/frontend 0.19.7-dev31 → 0.19.7-dev34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/{JsonOutput-CaYsBlIL.js → JsonOutput-DvKIRGOg.js} +10 -10
- package/dist/assets/{add-cell-with-ai-DGWXuion.js → add-cell-with-ai-BHgqYu8P.js} +1 -1
- package/dist/assets/{agent-panel-C_CZ3wsS.js → agent-panel-C9codfcr.js} +1 -1
- package/dist/assets/{cell-editor-DrfE1YS-.js → cell-editor-BxhibLVM.js} +1 -1
- package/dist/assets/{chat-display-BIHBJe4Q.js → chat-display-B0625p01.js} +1 -1
- package/dist/assets/{chat-panel-C4V8R2vf.js → chat-panel-CkHdco_X.js} +1 -1
- package/dist/assets/{column-preview-DfgAxcnz.js → column-preview-C6jEPj3t.js} +1 -1
- package/dist/assets/{command-palette-D4dxpptj.js → command-palette-D1g3pU47.js} +1 -1
- package/dist/assets/{dependency-graph-panel-BbRSXJOA.js → dependency-graph-panel-BWvUSpJI.js} +1 -1
- package/dist/assets/{download-Cphyle83.js → download-vBVDTXQk.js} +2 -2
- package/dist/assets/{edit-page-BiEfjNLG.js → edit-page-UqaWbc_J.js} +3 -3
- package/dist/assets/{file-explorer-panel-Dkqk7q5V.js → file-explorer-panel-y7F8Uqi-.js} +1 -1
- package/dist/assets/hooks-D_OOStv3.js +1 -0
- package/dist/assets/html-to-image-Cdx1xsbU.js +2 -0
- package/dist/assets/{index-0-J_BSHl.js → index-BSBPZDCV.js} +3 -3
- package/dist/assets/index-DmMvDRRC.css +2 -0
- package/dist/assets/{layout-BJ9AHITN.js → layout-Xf51uwDc.js} +1 -1
- package/dist/assets/{markdown-renderer-aprG4bjs.js → markdown-renderer-DSY-ElEE.js} +1 -1
- package/dist/assets/{panels-DDBrdHNH.js → panels-D-yo_63g.js} +1 -1
- package/dist/assets/{readonly-python-code-BwtDUnWb.js → readonly-python-code-B1UBDfCT.js} +1 -1
- package/dist/assets/{run-page-BS9QQKCJ.js → run-page-CoCql9Xm.js} +1 -1
- package/dist/assets/{scratchpad-panel-_4qqJHnW.js → scratchpad-panel-DPnpCcOn.js} +1 -1
- package/dist/assets/{session-panel-sueNTZq5.js → session-panel-CjoiPf6W.js} +1 -1
- package/dist/assets/{useAddCell-B7_Lgd-h.js → useAddCell-M6_IuI4C.js} +1 -1
- package/dist/assets/{useCellActionButton-BxQHrbUZ.js → useCellActionButton-Ddy42rre.js} +1 -1
- package/dist/assets/{useDependencyPanelTab-oTszzY42.js → useDependencyPanelTab-RSRpRVKD.js} +1 -1
- package/dist/assets/{useNotebookActions-DFKZ18Bu.js → useNotebookActions-XKe-QMLa.js} +1 -1
- package/dist/assets/{utilities.esm-BeoTGe8r.js → utilities.esm-mPQPstBT.js} +1 -1
- package/dist/index.html +12 -12
- package/package.json +1 -1
- package/src/components/editor/Output.tsx +6 -10
- package/src/components/editor/actions/useNotebookActions.tsx +2 -2
- package/src/core/export/__tests__/hooks.test.ts +72 -33
- package/src/core/export/hooks.ts +17 -8
- package/src/utils/__tests__/download.test.tsx +18 -18
- package/src/utils/__tests__/mime-types.test.ts +326 -0
- package/src/utils/download.ts +9 -11
- package/src/utils/html-to-image.ts +139 -0
- package/src/utils/mime-types.ts +181 -0
- package/dist/assets/hooks-COmcm0X5.js +0 -1
- package/dist/assets/html-to-image-DiZMGN_d.js +0 -2
- package/dist/assets/index-DHsXOI_M.css +0 -2
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import type { MimeType } from "@/components/editor/Output";
|
|
4
|
+
import {
|
|
5
|
+
applyHidingRules,
|
|
6
|
+
createMimeConfig,
|
|
7
|
+
getDefaultMimeConfig,
|
|
8
|
+
processMimeBundle,
|
|
9
|
+
sortByPrecedence,
|
|
10
|
+
} from "../mime-types";
|
|
11
|
+
|
|
12
|
+
/** Helper to build a hiding rules Map inline */
|
|
13
|
+
function hidingRules(
|
|
14
|
+
rules: Record<string, MimeType[]>,
|
|
15
|
+
): ReadonlyMap<MimeType, ReadonlySet<MimeType>> {
|
|
16
|
+
const map = new Map<MimeType, ReadonlySet<MimeType>>();
|
|
17
|
+
for (const [trigger, toHide] of Object.entries(rules)) {
|
|
18
|
+
map.set(trigger as MimeType, new Set(toHide));
|
|
19
|
+
}
|
|
20
|
+
return map;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Helper to build a precedence Map inline */
|
|
24
|
+
function precedenceMap(types: MimeType[]): ReadonlyMap<MimeType, number> {
|
|
25
|
+
const map = new Map<MimeType, number>();
|
|
26
|
+
types.forEach((mime, i) => map.set(mime, i));
|
|
27
|
+
return map;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("mime-types", () => {
|
|
31
|
+
describe("applyHidingRules", () => {
|
|
32
|
+
it("should return all visible when no rules match", () => {
|
|
33
|
+
const mimeTypes = new Set<MimeType>(["text/plain", "text/markdown"]);
|
|
34
|
+
const rules = hidingRules({ "text/html": ["image/png"] });
|
|
35
|
+
|
|
36
|
+
const result = applyHidingRules(mimeTypes, rules);
|
|
37
|
+
|
|
38
|
+
expect(result.visible).toEqual(new Set(["text/plain", "text/markdown"]));
|
|
39
|
+
expect(result.hidden.size).toBe(0);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should hide mime types when trigger is present", () => {
|
|
43
|
+
const mimeTypes = new Set<MimeType>([
|
|
44
|
+
"text/html",
|
|
45
|
+
"image/png",
|
|
46
|
+
"text/plain",
|
|
47
|
+
]);
|
|
48
|
+
const rules = hidingRules({ "text/html": ["image/png"] });
|
|
49
|
+
|
|
50
|
+
const result = applyHidingRules(mimeTypes, rules);
|
|
51
|
+
|
|
52
|
+
expect(result.visible).toEqual(new Set(["text/html", "text/plain"]));
|
|
53
|
+
expect(result.hidden).toEqual(new Set(["image/png"]));
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should not hide markdown when html is present (per requirements)", () => {
|
|
57
|
+
const mimeTypes = new Set<MimeType>([
|
|
58
|
+
"text/html",
|
|
59
|
+
"text/markdown",
|
|
60
|
+
"image/png",
|
|
61
|
+
]);
|
|
62
|
+
const rules = hidingRules({ "text/html": ["image/png"] });
|
|
63
|
+
|
|
64
|
+
const result = applyHidingRules(mimeTypes, rules);
|
|
65
|
+
|
|
66
|
+
expect(result.visible.has("text/markdown")).toBe(true);
|
|
67
|
+
expect(result.visible.has("text/html")).toBe(true);
|
|
68
|
+
expect(result.hidden.has("image/png")).toBe(true);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("should handle multiple matching rules", () => {
|
|
72
|
+
const mimeTypes = new Set<MimeType>([
|
|
73
|
+
"text/html",
|
|
74
|
+
"application/vnd.vegalite.v5+json",
|
|
75
|
+
"image/png",
|
|
76
|
+
"image/jpeg",
|
|
77
|
+
]);
|
|
78
|
+
const rules = hidingRules({
|
|
79
|
+
"text/html": ["image/png"],
|
|
80
|
+
"application/vnd.vegalite.v5+json": ["image/jpeg"],
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const result = applyHidingRules(mimeTypes, rules);
|
|
84
|
+
|
|
85
|
+
expect(result.hidden).toEqual(new Set(["image/png", "image/jpeg"]));
|
|
86
|
+
expect(result.visible).toEqual(
|
|
87
|
+
new Set(["text/html", "application/vnd.vegalite.v5+json"]),
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("should handle empty mime types", () => {
|
|
92
|
+
const mimeTypes = new Set<MimeType>();
|
|
93
|
+
const rules = hidingRules({ "text/html": ["image/png"] });
|
|
94
|
+
|
|
95
|
+
const result = applyHidingRules(mimeTypes, rules);
|
|
96
|
+
|
|
97
|
+
expect(result.visible.size).toBe(0);
|
|
98
|
+
expect(result.hidden.size).toBe(0);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should handle empty rules", () => {
|
|
102
|
+
const mimeTypes = new Set<MimeType>(["text/html", "image/png"]);
|
|
103
|
+
const rules = hidingRules({});
|
|
104
|
+
|
|
105
|
+
const result = applyHidingRules(mimeTypes, rules);
|
|
106
|
+
|
|
107
|
+
expect(result.visible).toEqual(mimeTypes);
|
|
108
|
+
expect(result.hidden.size).toBe(0);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should hide a type that is also a trigger if configured", () => {
|
|
112
|
+
const mimeTypes = new Set<MimeType>(["text/html", "text/plain"]);
|
|
113
|
+
const rules = hidingRules({ "text/html": ["text/html"] });
|
|
114
|
+
|
|
115
|
+
const result = applyHidingRules(mimeTypes, rules);
|
|
116
|
+
|
|
117
|
+
expect(result.hidden.has("text/html")).toBe(true);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("should not hide types that are not present", () => {
|
|
121
|
+
const mimeTypes = new Set<MimeType>(["text/html"]);
|
|
122
|
+
const rules = hidingRules({
|
|
123
|
+
"text/html": ["image/png", "image/jpeg"],
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const result = applyHidingRules(mimeTypes, rules);
|
|
127
|
+
|
|
128
|
+
expect(result.hidden.size).toBe(0);
|
|
129
|
+
expect(result.visible).toEqual(new Set(["text/html"]));
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe("sortByPrecedence", () => {
|
|
134
|
+
it("should sort entries by precedence order", () => {
|
|
135
|
+
const entries: Array<[MimeType, string]> = [
|
|
136
|
+
["text/plain", "plain"],
|
|
137
|
+
["text/html", "html"],
|
|
138
|
+
["image/png", "png"],
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
const result = sortByPrecedence(
|
|
142
|
+
entries,
|
|
143
|
+
precedenceMap(["text/html", "image/png", "text/plain"]),
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
expect(result.map(([m]) => m)).toEqual([
|
|
147
|
+
"text/html",
|
|
148
|
+
"image/png",
|
|
149
|
+
"text/plain",
|
|
150
|
+
]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("should place unknown mime types at the end", () => {
|
|
154
|
+
const entries: Array<[MimeType, string]> = [
|
|
155
|
+
["text/plain", "plain"],
|
|
156
|
+
["text/html", "html"],
|
|
157
|
+
["application/json", "json"],
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
const result = sortByPrecedence(entries, precedenceMap(["text/html"]));
|
|
161
|
+
|
|
162
|
+
expect(result[0][0]).toBe("text/html");
|
|
163
|
+
expect(result.slice(1).map(([m]) => m)).toEqual([
|
|
164
|
+
"text/plain",
|
|
165
|
+
"application/json",
|
|
166
|
+
]);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("should handle empty entries", () => {
|
|
170
|
+
const result = sortByPrecedence([], precedenceMap(["text/html"]));
|
|
171
|
+
|
|
172
|
+
expect(result).toEqual([]);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("should handle empty precedence", () => {
|
|
176
|
+
const entries: Array<[MimeType, string]> = [
|
|
177
|
+
["text/plain", "plain"],
|
|
178
|
+
["text/html", "html"],
|
|
179
|
+
];
|
|
180
|
+
|
|
181
|
+
const result = sortByPrecedence(entries, precedenceMap([]));
|
|
182
|
+
|
|
183
|
+
expect(result.map(([m]) => m)).toEqual(["text/plain", "text/html"]);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("should not mutate original array", () => {
|
|
187
|
+
const entries: Array<[MimeType, string]> = [
|
|
188
|
+
["text/plain", "plain"],
|
|
189
|
+
["text/html", "html"],
|
|
190
|
+
];
|
|
191
|
+
const original = [...entries];
|
|
192
|
+
|
|
193
|
+
sortByPrecedence(entries, precedenceMap(["text/html", "text/plain"]));
|
|
194
|
+
|
|
195
|
+
expect(entries).toEqual(original);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("processMimeBundle", () => {
|
|
200
|
+
it("should filter and sort mime entries", () => {
|
|
201
|
+
const entries: Array<[MimeType, string]> = [
|
|
202
|
+
["text/plain", "plain"],
|
|
203
|
+
["text/html", "html"],
|
|
204
|
+
["image/png", "png"],
|
|
205
|
+
];
|
|
206
|
+
|
|
207
|
+
const config = createMimeConfig({
|
|
208
|
+
precedence: ["text/html", "text/plain"],
|
|
209
|
+
hidingRules: { "text/html": ["image/png"] },
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
const result = processMimeBundle(entries, config);
|
|
213
|
+
|
|
214
|
+
expect(result.entries.map(([m]) => m)).toEqual([
|
|
215
|
+
"text/html",
|
|
216
|
+
"text/plain",
|
|
217
|
+
]);
|
|
218
|
+
expect(result.hidden).toEqual(["image/png"]);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("should handle empty entries", () => {
|
|
222
|
+
const result = processMimeBundle([]);
|
|
223
|
+
|
|
224
|
+
expect(result.entries).toEqual([]);
|
|
225
|
+
expect(result.hidden).toEqual([]);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("should use default config when not provided", () => {
|
|
229
|
+
const entries: Array<[MimeType, string]> = [
|
|
230
|
+
["text/html", "html"],
|
|
231
|
+
["image/png", "png"],
|
|
232
|
+
["text/markdown", "md"],
|
|
233
|
+
];
|
|
234
|
+
|
|
235
|
+
const result = processMimeBundle(entries);
|
|
236
|
+
|
|
237
|
+
expect(result.entries.map(([m]) => m)).not.toContain("image/png");
|
|
238
|
+
expect(result.entries.map(([m]) => m)).toContain("text/markdown");
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("should preserve data associated with mime types", () => {
|
|
242
|
+
const htmlData = { content: "<h1>Hello</h1>" };
|
|
243
|
+
const entries: Array<[MimeType, typeof htmlData]> = [
|
|
244
|
+
["text/html", htmlData],
|
|
245
|
+
];
|
|
246
|
+
|
|
247
|
+
const result = processMimeBundle(entries);
|
|
248
|
+
|
|
249
|
+
expect(result.entries[0][1]).toBe(htmlData);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("should sort by precedence after filtering", () => {
|
|
253
|
+
const entries: Array<[MimeType, string]> = [
|
|
254
|
+
["text/plain", "plain"],
|
|
255
|
+
["text/markdown", "md"],
|
|
256
|
+
["text/html", "html"],
|
|
257
|
+
];
|
|
258
|
+
|
|
259
|
+
const result = processMimeBundle(entries);
|
|
260
|
+
|
|
261
|
+
expect(result.entries[0][0]).toBe("text/html");
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
describe("getDefaultMimeConfig", () => {
|
|
266
|
+
const config = getDefaultMimeConfig();
|
|
267
|
+
|
|
268
|
+
it("should have text/html as highest precedence", () => {
|
|
269
|
+
expect(config.precedence.get("text/html")).toBe(0);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("should hide image types when html is present", () => {
|
|
273
|
+
const htmlHides = config.hidingRules.get("text/html");
|
|
274
|
+
|
|
275
|
+
expect(htmlHides).toBeDefined();
|
|
276
|
+
expect(htmlHides?.has("image/png")).toBe(true);
|
|
277
|
+
expect(htmlHides?.has("image/jpeg")).toBe(true);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it("should NOT hide markdown when html is present", () => {
|
|
281
|
+
const htmlHides = config.hidingRules.get("text/html");
|
|
282
|
+
|
|
283
|
+
expect(htmlHides?.has("text/markdown")).toBeFalsy();
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("should hide images when vega charts are present", () => {
|
|
287
|
+
const vegaHides = config.hidingRules.get(
|
|
288
|
+
"application/vnd.vegalite.v5+json",
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
expect(vegaHides).toBeDefined();
|
|
292
|
+
expect(vegaHides?.has("image/png")).toBe(true);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("should return the same instance on repeated calls", () => {
|
|
296
|
+
expect(getDefaultMimeConfig()).toBe(config);
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
describe("createMimeConfig", () => {
|
|
301
|
+
it("should compile precedence array into a Map", () => {
|
|
302
|
+
const config = createMimeConfig({
|
|
303
|
+
precedence: ["text/html", "image/png"],
|
|
304
|
+
hidingRules: {},
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
expect(config.precedence.get("text/html")).toBe(0);
|
|
308
|
+
expect(config.precedence.get("image/png")).toBe(1);
|
|
309
|
+
expect(config.precedence.has("text/plain")).toBe(false);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("should compile hiding rules into Map<MimeType, Set>", () => {
|
|
313
|
+
const config = createMimeConfig({
|
|
314
|
+
precedence: [],
|
|
315
|
+
hidingRules: {
|
|
316
|
+
"text/html": ["image/png", "image/jpeg"],
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
const htmlHides = config.hidingRules.get("text/html");
|
|
321
|
+
expect(htmlHides).toBeInstanceOf(Set);
|
|
322
|
+
expect(htmlHides?.has("image/png")).toBe(true);
|
|
323
|
+
expect(htmlHides?.has("image/jpeg")).toBe(true);
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
});
|
package/src/utils/download.ts
CHANGED
|
@@ -70,16 +70,15 @@ function releaseBodyPrinting() {
|
|
|
70
70
|
* Prepare a cell element for screenshot capture.
|
|
71
71
|
*
|
|
72
72
|
* @param element - The cell output element to prepare
|
|
73
|
-
* @param
|
|
74
|
-
* This can cause layout shifts that cause the page to scroll.
|
|
73
|
+
* @param snappy - When true, avoids layout shifts and speeds up the capture.
|
|
75
74
|
* @returns A cleanup function to restore the element's original state
|
|
76
75
|
*/
|
|
77
76
|
function prepareCellElementForScreenshot(
|
|
78
77
|
element: HTMLElement,
|
|
79
|
-
|
|
78
|
+
snappy: boolean,
|
|
80
79
|
) {
|
|
81
80
|
element.classList.add("printing-output");
|
|
82
|
-
if (
|
|
81
|
+
if (!snappy) {
|
|
83
82
|
acquireBodyPrinting();
|
|
84
83
|
}
|
|
85
84
|
const originalOverflow = element.style.overflow;
|
|
@@ -87,7 +86,7 @@ function prepareCellElementForScreenshot(
|
|
|
87
86
|
|
|
88
87
|
return () => {
|
|
89
88
|
element.classList.remove("printing-output");
|
|
90
|
-
if (
|
|
89
|
+
if (!snappy) {
|
|
91
90
|
releaseBodyPrinting();
|
|
92
91
|
}
|
|
93
92
|
element.style.overflow = originalOverflow;
|
|
@@ -100,13 +99,12 @@ const THRESHOLD_TIME_MS = 500;
|
|
|
100
99
|
* Capture a cell output as a PNG data URL.
|
|
101
100
|
*
|
|
102
101
|
* @param cellId - The ID of the cell to capture
|
|
103
|
-
* @param
|
|
104
|
-
* This can cause layout shifts that cause the page to scroll.
|
|
102
|
+
* @param snappy - When true, uses a faster method to capture the image. Avoids layout shifts.
|
|
105
103
|
* @returns The PNG as a data URL, or undefined if the cell element wasn't found
|
|
106
104
|
*/
|
|
107
105
|
export async function getImageDataUrlForCell(
|
|
108
106
|
cellId: CellId,
|
|
109
|
-
|
|
107
|
+
snappy = false,
|
|
110
108
|
): Promise<string | undefined> {
|
|
111
109
|
const element = findElementForCell(cellId);
|
|
112
110
|
if (!element) {
|
|
@@ -118,11 +116,11 @@ export async function getImageDataUrlForCell(
|
|
|
118
116
|
return iframeDataUrl;
|
|
119
117
|
}
|
|
120
118
|
|
|
121
|
-
const cleanup = prepareCellElementForScreenshot(element,
|
|
119
|
+
const cleanup = prepareCellElementForScreenshot(element, snappy);
|
|
122
120
|
|
|
123
121
|
try {
|
|
124
122
|
const startTime = Date.now();
|
|
125
|
-
const dataUrl = await toPng(element);
|
|
123
|
+
const dataUrl = await toPng(element, undefined, snappy);
|
|
126
124
|
const timeTaken = Date.now() - startTime;
|
|
127
125
|
if (timeTaken > THRESHOLD_TIME_MS) {
|
|
128
126
|
Logger.debug(
|
|
@@ -159,7 +157,7 @@ export async function downloadCellOutputAsImage(
|
|
|
159
157
|
await downloadHTMLAsImage({
|
|
160
158
|
element,
|
|
161
159
|
filename,
|
|
162
|
-
prepare: () => prepareCellElementForScreenshot(element,
|
|
160
|
+
prepare: () => prepareCellElementForScreenshot(element, false),
|
|
163
161
|
});
|
|
164
162
|
}
|
|
165
163
|
|
|
@@ -4,6 +4,143 @@ import { Logger } from "./Logger";
|
|
|
4
4
|
|
|
5
5
|
export type HtmlToImageOptions = Parameters<typeof htmlToImageToPng>[1];
|
|
6
6
|
|
|
7
|
+
// For improved performance, we include these styles that are likely to be present on the element.
|
|
8
|
+
export const necessaryStyleProperties = [
|
|
9
|
+
// Sizing
|
|
10
|
+
"width",
|
|
11
|
+
"height",
|
|
12
|
+
"min-width",
|
|
13
|
+
"min-height",
|
|
14
|
+
"max-width",
|
|
15
|
+
"max-height",
|
|
16
|
+
"box-sizing",
|
|
17
|
+
"aspect-ratio",
|
|
18
|
+
|
|
19
|
+
// Display & Layout
|
|
20
|
+
"display",
|
|
21
|
+
"position",
|
|
22
|
+
"top",
|
|
23
|
+
"left",
|
|
24
|
+
"bottom",
|
|
25
|
+
"right",
|
|
26
|
+
"z-index",
|
|
27
|
+
"float",
|
|
28
|
+
"clear",
|
|
29
|
+
|
|
30
|
+
// Flexbox
|
|
31
|
+
"flex",
|
|
32
|
+
"flex-direction",
|
|
33
|
+
"flex-wrap",
|
|
34
|
+
"flex-grow",
|
|
35
|
+
"flex-shrink",
|
|
36
|
+
"flex-basis",
|
|
37
|
+
"align-items",
|
|
38
|
+
"align-self",
|
|
39
|
+
"justify-content",
|
|
40
|
+
"gap",
|
|
41
|
+
"order",
|
|
42
|
+
|
|
43
|
+
// Grid
|
|
44
|
+
"grid-template-columns",
|
|
45
|
+
"grid-template-rows",
|
|
46
|
+
"grid-column",
|
|
47
|
+
"grid-row",
|
|
48
|
+
"row-gap",
|
|
49
|
+
"column-gap",
|
|
50
|
+
|
|
51
|
+
// Spacing
|
|
52
|
+
"margin",
|
|
53
|
+
"margin-top",
|
|
54
|
+
"margin-right",
|
|
55
|
+
"margin-bottom",
|
|
56
|
+
"margin-left",
|
|
57
|
+
"padding",
|
|
58
|
+
"padding-top",
|
|
59
|
+
"padding-right",
|
|
60
|
+
"padding-bottom",
|
|
61
|
+
"padding-left",
|
|
62
|
+
|
|
63
|
+
// Typography
|
|
64
|
+
"font",
|
|
65
|
+
"font-family",
|
|
66
|
+
"font-size",
|
|
67
|
+
"font-weight",
|
|
68
|
+
"font-style",
|
|
69
|
+
"line-height",
|
|
70
|
+
"letter-spacing",
|
|
71
|
+
"word-spacing",
|
|
72
|
+
"text-align",
|
|
73
|
+
"text-decoration",
|
|
74
|
+
"text-transform",
|
|
75
|
+
"text-indent",
|
|
76
|
+
"text-shadow",
|
|
77
|
+
"white-space",
|
|
78
|
+
"text-wrap",
|
|
79
|
+
"word-break",
|
|
80
|
+
"text-overflow",
|
|
81
|
+
"vertical-align",
|
|
82
|
+
"color",
|
|
83
|
+
|
|
84
|
+
// Background
|
|
85
|
+
"background",
|
|
86
|
+
"background-color",
|
|
87
|
+
"background-image",
|
|
88
|
+
"background-size",
|
|
89
|
+
"background-position",
|
|
90
|
+
"background-repeat",
|
|
91
|
+
"background-clip",
|
|
92
|
+
|
|
93
|
+
// Borders
|
|
94
|
+
"border",
|
|
95
|
+
"border-width",
|
|
96
|
+
"border-style",
|
|
97
|
+
"border-color",
|
|
98
|
+
"border-top",
|
|
99
|
+
"border-right",
|
|
100
|
+
"border-bottom",
|
|
101
|
+
"border-left",
|
|
102
|
+
"border-radius",
|
|
103
|
+
"outline",
|
|
104
|
+
|
|
105
|
+
// Effects
|
|
106
|
+
"box-shadow",
|
|
107
|
+
"text-shadow",
|
|
108
|
+
"opacity",
|
|
109
|
+
"filter",
|
|
110
|
+
"backdrop-filter",
|
|
111
|
+
"mix-blend-mode",
|
|
112
|
+
"transform",
|
|
113
|
+
"clip-path",
|
|
114
|
+
|
|
115
|
+
// Overflow & Visibility
|
|
116
|
+
// We don't include overflow properties because they can include scrollbars
|
|
117
|
+
// "overflow",
|
|
118
|
+
// "overflow-x",
|
|
119
|
+
// "overflow-y",
|
|
120
|
+
"visibility",
|
|
121
|
+
|
|
122
|
+
// SVG
|
|
123
|
+
"fill",
|
|
124
|
+
"stroke",
|
|
125
|
+
"stroke-width",
|
|
126
|
+
|
|
127
|
+
// Images & Objects
|
|
128
|
+
"object-fit",
|
|
129
|
+
"object-position",
|
|
130
|
+
|
|
131
|
+
// Lists
|
|
132
|
+
"list-style",
|
|
133
|
+
"list-style-type",
|
|
134
|
+
|
|
135
|
+
// Tables
|
|
136
|
+
"border-collapse",
|
|
137
|
+
"border-spacing",
|
|
138
|
+
|
|
139
|
+
// Misc
|
|
140
|
+
"content",
|
|
141
|
+
"cursor",
|
|
142
|
+
];
|
|
143
|
+
|
|
7
144
|
/**
|
|
8
145
|
* Default options for html-to-image conversions.
|
|
9
146
|
* These handle common edge cases like filtering out toolbars and logging errors.
|
|
@@ -39,9 +176,11 @@ export const defaultHtmlToImageOptions: HtmlToImageOptions = {
|
|
|
39
176
|
export async function toPng(
|
|
40
177
|
element: HTMLElement,
|
|
41
178
|
options?: HtmlToImageOptions,
|
|
179
|
+
snappy?: boolean,
|
|
42
180
|
): Promise<string> {
|
|
43
181
|
return htmlToImageToPng(element, {
|
|
44
182
|
...defaultHtmlToImageOptions,
|
|
183
|
+
includeStyleProperties: snappy ? necessaryStyleProperties : undefined,
|
|
45
184
|
...options,
|
|
46
185
|
});
|
|
47
186
|
}
|