@onerjs/snapdom 2.7.0
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/LICENSE +21 -0
- package/README.md +969 -0
- package/dist/plugins.mjs +7 -0
- package/dist/preCache.mjs +10 -0
- package/dist/snapdom.js +14 -0
- package/dist/snapdom.mjs +14 -0
- package/package.json +86 -0
- package/types/snapdom.d.ts +375 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* snapDOM – ultra-fast DOM-to-image capture
|
|
3
|
+
* TypeScript definitions (v1.9.14)
|
|
4
|
+
*
|
|
5
|
+
* Notes:
|
|
6
|
+
* - Style compression is internal (no public option).
|
|
7
|
+
* - Icon fonts are always embedded; `embedFonts` controls non-icon fonts only.
|
|
8
|
+
* - This file preserves backward compatibility with earlier 1.9.x defs.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/* =========================
|
|
12
|
+
* Basic MIME / type aliases
|
|
13
|
+
* ========================= */
|
|
14
|
+
|
|
15
|
+
export type RasterMime = "png" | "jpg" | "jpeg" | "webp";
|
|
16
|
+
export type BlobType = "svg" | RasterMime;
|
|
17
|
+
|
|
18
|
+
export type IconFontMatcher = string | RegExp;
|
|
19
|
+
export type CachePolicy = "disabled" | "full" | "auto" | "soft";
|
|
20
|
+
|
|
21
|
+
/* =========================
|
|
22
|
+
* Font & proxy declarations
|
|
23
|
+
* ========================= */
|
|
24
|
+
|
|
25
|
+
export interface LocalFont {
|
|
26
|
+
family: string;
|
|
27
|
+
src: string; // URL or data: URL
|
|
28
|
+
weight?: string | number;
|
|
29
|
+
style?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ExcludeFonts {
|
|
33
|
+
/** Case-insensitive family names to skip (non-icon only). */
|
|
34
|
+
families?: string[];
|
|
35
|
+
/** Host substrings to skip (e.g., "fonts.gstatic.com"). */
|
|
36
|
+
domains?: string[];
|
|
37
|
+
/** Unicode-range subset tags to skip (e.g., "cyrillic-ext"). */
|
|
38
|
+
subsets?: string[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/* =========================
|
|
42
|
+
* Capture options
|
|
43
|
+
* ========================= */
|
|
44
|
+
|
|
45
|
+
export interface SnapdomOptions {
|
|
46
|
+
/** Fast path: skip small idle delays where safe. */
|
|
47
|
+
fast?: boolean;
|
|
48
|
+
/** Output scale multiplier. Takes precedence over width/height. */
|
|
49
|
+
scale?: number;
|
|
50
|
+
/** Device pixel ratio to use for rasterization (defaults to `devicePixelRatio`). */
|
|
51
|
+
dpr?: number;
|
|
52
|
+
/** Target width of the export (keeps aspect if only one dimension is provided). */
|
|
53
|
+
width?: number;
|
|
54
|
+
/** Target height of the export (keeps aspect if only one dimension is provided). */
|
|
55
|
+
height?: number;
|
|
56
|
+
|
|
57
|
+
/** Background fallback color (used esp. for JPEG). Default "#fff". */
|
|
58
|
+
backgroundColor?: string;
|
|
59
|
+
/** Quality for JPEG/WebP (0..1). Default 1. */
|
|
60
|
+
quality?: number;
|
|
61
|
+
|
|
62
|
+
/** Cross-origin proxy prefix (used as a fallback when CORS blocks). */
|
|
63
|
+
useProxy?: string;
|
|
64
|
+
|
|
65
|
+
/** Default Blob type for toBlob() when unspecified. */
|
|
66
|
+
type?: BlobType;
|
|
67
|
+
|
|
68
|
+
/** CSS selector list to filter nodes. */
|
|
69
|
+
exclude?: string[];
|
|
70
|
+
/** How to apply `exclude` ("hide" keeps layout via visibility:hidden; "remove" drops nodes). Default "hide". */
|
|
71
|
+
excludeMode?: "hide" | "remove";
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Custom predicate: return true to keep node, false to exclude.
|
|
75
|
+
* Runs in document order; pairs with `filterMode`.
|
|
76
|
+
*/
|
|
77
|
+
filter?: (el: Element) => boolean;
|
|
78
|
+
/** How to apply `filter` ("hide" or "remove"). Default "hide". */
|
|
79
|
+
filterMode?: "hide" | "remove";
|
|
80
|
+
|
|
81
|
+
/** outerTransforms the root: remove translate/rotate, keep scale/skew. */
|
|
82
|
+
outerTransforms?: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Do not expand root bbox for shadows/blur/outline; also strip shadows/outline
|
|
85
|
+
* from the cloned root to get a tight capture box.
|
|
86
|
+
*/
|
|
87
|
+
outerShadows?: boolean;
|
|
88
|
+
|
|
89
|
+
/** Inline non-icon fonts actually used within the subtree. */
|
|
90
|
+
embedFonts?: boolean;
|
|
91
|
+
/** Provide fonts explicitly to avoid remote discovery. */
|
|
92
|
+
localFonts?: LocalFont[];
|
|
93
|
+
/** Additional matchers for icon font families (strings or regex). */
|
|
94
|
+
iconFonts?: IconFontMatcher | IconFontMatcher[];
|
|
95
|
+
/** Skip specific non-icon fonts (by family/domain/subset). */
|
|
96
|
+
excludeFonts?: ExcludeFonts;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Fallback image when <img> fails to load.
|
|
100
|
+
* Can be a fixed URL or a callback that receives measured dimensions.
|
|
101
|
+
*/
|
|
102
|
+
fallbackURL?:
|
|
103
|
+
| string
|
|
104
|
+
| ((dims: { width?: number; height?: number }) => string);
|
|
105
|
+
|
|
106
|
+
/** Cache policy for resources and style maps. Default "soft". */
|
|
107
|
+
cache?: CachePolicy;
|
|
108
|
+
|
|
109
|
+
/** Show placeholders when resources are missing. Default true. */
|
|
110
|
+
placeholders?: boolean;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Resolve lazy `<picture>` placeholders / `data-src` patterns before clone (default true).
|
|
114
|
+
* Set false to skip; register the `picture-resolver` plugin explicitly if you need overrides while core is off.
|
|
115
|
+
*/
|
|
116
|
+
resolvePicturePlaceholders?: boolean;
|
|
117
|
+
/** Fine-tune the built-in picture/lazy resolver (timeout, concurrency, etc.). */
|
|
118
|
+
pictureResolver?: {
|
|
119
|
+
timeout?: number;
|
|
120
|
+
concurrency?: number;
|
|
121
|
+
resolveLazySrc?: boolean;
|
|
122
|
+
silent?: boolean;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/** Arbitrary plugin configuration at call-site (see PluginUse). */
|
|
126
|
+
plugins?: PluginUse[];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/* =========================
|
|
130
|
+
* Capture context (hook state)
|
|
131
|
+
* ========================= */
|
|
132
|
+
|
|
133
|
+
export interface CaptureContext extends SnapdomOptions {
|
|
134
|
+
/** Input element being captured. */
|
|
135
|
+
element: Element;
|
|
136
|
+
|
|
137
|
+
/** Cloned root (detached), available after `beforeClone`/`afterClone`. */
|
|
138
|
+
clone?: HTMLElement | SVGElement | null;
|
|
139
|
+
|
|
140
|
+
/** Internal style/class caches (opaque to user). */
|
|
141
|
+
classCSS?: string;
|
|
142
|
+
styleCache?: unknown;
|
|
143
|
+
fontsCSS?: string;
|
|
144
|
+
baseCSS?: string;
|
|
145
|
+
|
|
146
|
+
/** Serialized artifacts, available after render. */
|
|
147
|
+
svgString?: string;
|
|
148
|
+
dataURL?: string;
|
|
149
|
+
|
|
150
|
+
/** Current export info during beforeExport/afterExport. */
|
|
151
|
+
export?: {
|
|
152
|
+
/** Export key (e.g., "png", "jpeg", "svg", or any custom key). */
|
|
153
|
+
type: string;
|
|
154
|
+
/** Options passed to the exporter. */
|
|
155
|
+
options?: any;
|
|
156
|
+
/** Canonical SVG data URL of this capture. */
|
|
157
|
+
url: string;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/* =========================
|
|
162
|
+
* Exporter signatures
|
|
163
|
+
* ========================= */
|
|
164
|
+
|
|
165
|
+
export type Exporter = (ctx: CaptureContext, opts?: any) => Promise<any>;
|
|
166
|
+
|
|
167
|
+
/** Map returned by `defineExports`: keys are exposed on the result (e.g., `pdf` → `result.toPdf()` as well as `result['pdf']()`). */
|
|
168
|
+
export type ExportMap = Record<string, Exporter>;
|
|
169
|
+
|
|
170
|
+
/* =========================
|
|
171
|
+
* Plugin system
|
|
172
|
+
* ========================= */
|
|
173
|
+
|
|
174
|
+
export interface SnapdomPlugin {
|
|
175
|
+
/** Unique name for de-dupe/overrides. */
|
|
176
|
+
name: string;
|
|
177
|
+
|
|
178
|
+
/** Hook order follows registration order. All hooks may be async. */
|
|
179
|
+
beforeSnap?(context: CaptureContext): void | Promise<void>;
|
|
180
|
+
beforeClone?(context: CaptureContext): void | Promise<void>;
|
|
181
|
+
afterClone?(context: CaptureContext): void | Promise<void>;
|
|
182
|
+
beforeRender?(context: CaptureContext): void | Promise<void>;
|
|
183
|
+
afterRender?(context: CaptureContext): void | Promise<void>;
|
|
184
|
+
|
|
185
|
+
/** Runs before EACH export. */
|
|
186
|
+
beforeExport?(context: CaptureContext): void | Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* Runs after EACH export; returning a value will be chained to the next plugin
|
|
189
|
+
* (transform pipeline). If undefined is returned, the prior result is preserved.
|
|
190
|
+
*/
|
|
191
|
+
afterExport?(context: CaptureContext, result: any): any | Promise<any>;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Provide custom exporters (e.g., { pdf: async (ctx, opts) => Blob }).
|
|
195
|
+
* Keys are exposed on the capture result as helpers (toPdf()) and as index access (result['pdf']()).
|
|
196
|
+
*/
|
|
197
|
+
defineExports?(context: CaptureContext): ExportMap | Promise<ExportMap>;
|
|
198
|
+
|
|
199
|
+
/** Runs ONCE after the FIRST successful export of this capture (good for cleanup). */
|
|
200
|
+
afterSnap?(context: CaptureContext): void | Promise<void>;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export type PluginFactory = (options?: any) => SnapdomPlugin;
|
|
204
|
+
/** You can pass a plugin instance, a factory, or a tuple with options. */
|
|
205
|
+
export type PluginUse =
|
|
206
|
+
| SnapdomPlugin
|
|
207
|
+
| PluginFactory
|
|
208
|
+
| [PluginFactory, any]
|
|
209
|
+
| { plugin: PluginFactory; options?: any };
|
|
210
|
+
|
|
211
|
+
/* =========================
|
|
212
|
+
* Capture result API
|
|
213
|
+
* ========================= */
|
|
214
|
+
|
|
215
|
+
export interface DownloadOptions {
|
|
216
|
+
filename?: string;
|
|
217
|
+
/** Override default blob type for this download. */
|
|
218
|
+
type?: BlobType;
|
|
219
|
+
/** Quality hint for raster formats. */
|
|
220
|
+
quality?: number;
|
|
221
|
+
/** Target width/height for this export. */
|
|
222
|
+
width?: number;
|
|
223
|
+
height?: number;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export interface BlobOptions {
|
|
227
|
+
type?: BlobType;
|
|
228
|
+
quality?: number;
|
|
229
|
+
width?: number;
|
|
230
|
+
height?: number;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export interface CaptureResult {
|
|
234
|
+
/** Canonical data URL of the SVG snapshot (when available). */
|
|
235
|
+
url: string;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @deprecated Use `toSvg()` for an <img> that renders the SVG snapshot.
|
|
239
|
+
* Historical alias kept for compatibility.
|
|
240
|
+
*/
|
|
241
|
+
toImg(): Promise<HTMLImageElement>;
|
|
242
|
+
|
|
243
|
+
/** Returns an HTMLImageElement that renders the SVG snapshot. */
|
|
244
|
+
toSvg(options?: Partial<SnapdomOptions>): Promise<HTMLImageElement>;
|
|
245
|
+
|
|
246
|
+
/** Returns a Canvas with the rasterized snapshot. */
|
|
247
|
+
toCanvas(options?: Partial<SnapdomOptions>): Promise<HTMLCanvasElement>;
|
|
248
|
+
|
|
249
|
+
/** Returns a Blob of the chosen type (svg/png/jpeg/webp). */
|
|
250
|
+
toBlob(options?: BlobOptions & Partial<SnapdomOptions>): Promise<Blob>;
|
|
251
|
+
|
|
252
|
+
/** Convenience raster exports returning an HTMLImageElement. */
|
|
253
|
+
toPng(options?: Partial<SnapdomOptions>): Promise<HTMLImageElement>;
|
|
254
|
+
toJpeg(options?: Partial<SnapdomOptions>): Promise<HTMLImageElement>;
|
|
255
|
+
/** Alias for `toJpeg()`. */
|
|
256
|
+
toJpg(options?: Partial<SnapdomOptions>): Promise<HTMLImageElement>;
|
|
257
|
+
toWebp(options?: Partial<SnapdomOptions>): Promise<HTMLImageElement>;
|
|
258
|
+
|
|
259
|
+
/** Trigger a client-side download of the snapshot using current/default settings. */
|
|
260
|
+
download(options?: DownloadOptions & Partial<SnapdomOptions>): Promise<void>;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Custom exporters exposed by plugins:
|
|
264
|
+
* - As helpers: a plugin returning { pdf: (...) => ... } also enables result.toPdf(...)
|
|
265
|
+
* - As index access: result["pdf"](...)
|
|
266
|
+
*
|
|
267
|
+
* Since keys are not known ahead of time, we allow index access.
|
|
268
|
+
*/
|
|
269
|
+
[key: string]: any;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/* =========================
|
|
273
|
+
* Main callable & static helpers
|
|
274
|
+
* ========================= */
|
|
275
|
+
|
|
276
|
+
/** Overload: main callable returns a reusable exporter object for the element. */
|
|
277
|
+
export declare function snapdom(
|
|
278
|
+
element: Element,
|
|
279
|
+
options?: SnapdomOptions
|
|
280
|
+
): Promise<CaptureResult>;
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Global plugin registration (chainable).
|
|
284
|
+
* - De-duplicates by `name`.
|
|
285
|
+
* - Execution order = registration order.
|
|
286
|
+
* - Per-capture plugins run before globals and override by `name`.
|
|
287
|
+
*/
|
|
288
|
+
export declare namespace snapdom {
|
|
289
|
+
function plugins(...defs: PluginUse[]): typeof snapdom;
|
|
290
|
+
|
|
291
|
+
/** Shortcut helpers that run a one-off capture+export. */
|
|
292
|
+
|
|
293
|
+
/** @deprecated Returns an SVG <img>; prefer `toSvg`. */
|
|
294
|
+
function toImg(
|
|
295
|
+
element: Element,
|
|
296
|
+
options?: SnapdomOptions
|
|
297
|
+
): Promise<HTMLImageElement>;
|
|
298
|
+
|
|
299
|
+
function toSvg(
|
|
300
|
+
element: Element,
|
|
301
|
+
options?: SnapdomOptions
|
|
302
|
+
): Promise<HTMLImageElement>;
|
|
303
|
+
|
|
304
|
+
function toCanvas(
|
|
305
|
+
element: Element,
|
|
306
|
+
options?: SnapdomOptions
|
|
307
|
+
): Promise<HTMLCanvasElement>;
|
|
308
|
+
|
|
309
|
+
function toBlob(
|
|
310
|
+
element: Element,
|
|
311
|
+
options?: SnapdomOptions & BlobOptions
|
|
312
|
+
): Promise<Blob>;
|
|
313
|
+
|
|
314
|
+
function toPng(
|
|
315
|
+
element: Element,
|
|
316
|
+
options?: SnapdomOptions
|
|
317
|
+
): Promise<HTMLImageElement>;
|
|
318
|
+
|
|
319
|
+
function toJpeg(
|
|
320
|
+
element: Element,
|
|
321
|
+
options?: SnapdomOptions
|
|
322
|
+
): Promise<HTMLImageElement>;
|
|
323
|
+
|
|
324
|
+
/** Alias for `toJpeg`. */
|
|
325
|
+
function toJpg(
|
|
326
|
+
element: Element,
|
|
327
|
+
options?: SnapdomOptions
|
|
328
|
+
): Promise<HTMLImageElement>;
|
|
329
|
+
|
|
330
|
+
function toWebp(
|
|
331
|
+
element: Element,
|
|
332
|
+
options?: SnapdomOptions
|
|
333
|
+
): Promise<HTMLImageElement>;
|
|
334
|
+
|
|
335
|
+
function download(
|
|
336
|
+
element: Element,
|
|
337
|
+
options?: SnapdomOptions & DownloadOptions
|
|
338
|
+
): Promise<void>;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/* =========================
|
|
342
|
+
* preCache helper
|
|
343
|
+
* ========================= */
|
|
344
|
+
|
|
345
|
+
export interface PreCacheOptions {
|
|
346
|
+
/** Root to scan (defaults to `document`). */
|
|
347
|
+
root?: Element | Document;
|
|
348
|
+
/** Try to embed non-icon fonts used under root (see also localFonts). */
|
|
349
|
+
embedFonts?: boolean;
|
|
350
|
+
/** Provide fonts explicitly to avoid remote discovery. */
|
|
351
|
+
localFonts?: LocalFont[];
|
|
352
|
+
/** Additional matchers for icon fonts (strings or regex). */
|
|
353
|
+
iconFonts?: IconFontMatcher | IconFontMatcher[];
|
|
354
|
+
/** Cross-origin proxy prefix (as in SnapdomOptions.useProxy). */
|
|
355
|
+
useProxy?: string;
|
|
356
|
+
/** Cache policy for this preload operation. */
|
|
357
|
+
cache?: CachePolicy;
|
|
358
|
+
|
|
359
|
+
/** Back-compat fields (no-ops if present) */
|
|
360
|
+
/**
|
|
361
|
+
* @deprecated Use `cache` instead.
|
|
362
|
+
*/
|
|
363
|
+
cacheOpt?: CachePolicy;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Preload external resources for a subtree to avoid first-capture stalls.
|
|
368
|
+
* Uses the same discovery heuristics as the main capture path.
|
|
369
|
+
*/
|
|
370
|
+
export declare function preCache(
|
|
371
|
+
root?: Element | Document,
|
|
372
|
+
options?: PreCacheOptions
|
|
373
|
+
): Promise<void>;
|
|
374
|
+
|
|
375
|
+
export {};
|