@open-artifact/export-kit 0.0.0-stage → 0.2.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 +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +36 -4
- package/src/capture-safe.d.ts +32 -0
- package/src/capture-safe.js +190 -0
- package/src/capture.d.ts +87 -0
- package/src/capture.js +183 -0
- package/src/fonts.d.ts +21 -0
- package/src/fonts.js +53 -0
- package/src/index.d.ts +20 -0
- package/src/index.js +20 -0
- package/src/measure.d.ts +53 -0
- package/src/measure.js +116 -0
- package/src/pictures.d.ts +45 -0
- package/src/pictures.js +125 -0
- package/src/text-layer.d.ts +55 -0
- package/src/text-layer.js +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @open-artifact/export-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
What the editors' exports share in the browser: DOM capture to SVG and pictures, the PDF text layer measured from rendered pages, picture shrinking and font stylesheet helpers.
|
|
4
|
+
|
|
5
|
+
Part of [Open Artifact](https://github.com/JimLiu/open-artifact): slide decks, design canvases and whiteboards an agent makes
|
|
6
|
+
and you view, edit and comment on in one local page. Source: [`packages/export-kit`](https://github.com/JimLiu/open-artifact/tree/main/packages/export-kit).
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifact/export-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "What the editors' exports share in the browser: DOM capture to SVG and pictures, the PDF text layer measured from rendered pages, picture shrinking and font stylesheet helpers.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"default": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"NOTICE"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"dom-to-svg": "^0.12.2"
|
|
20
|
+
},
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Jim Liu 宝玉",
|
|
24
|
+
"url": "https://github.com/JimLiu"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/JimLiu/open-artifact#readme",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/JimLiu/open-artifact.git",
|
|
30
|
+
"directory": "packages/export-kit"
|
|
31
|
+
},
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/JimLiu/open-artifact/issues"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Making a page readable by dom-to-svg for the length of one capture.
|
|
3
|
+
*
|
|
4
|
+
* dom-to-svg reads computed styles and SVG attributes as they are, and two
|
|
5
|
+
* things in them break it:
|
|
6
|
+
*
|
|
7
|
+
* - a `linear-gradient()` written with CSS Color 4: an interpolation space
|
|
8
|
+
* (`in oklch`) or stops in `oklch()`, `lab()`, `color()`... Its gradient
|
|
9
|
+
* parser only knows `rgb()`, `rgba()`, hex and names, and throws on the
|
|
10
|
+
* rest, which fails the whole capture. {@link safeBackgroundImage} rewrites
|
|
11
|
+
* such a value with plain `rgba()` stops;
|
|
12
|
+
* - a shape inside an inline `<svg>` sized in percentages (`<circle r="50%">`).
|
|
13
|
+
* The shape is copied into the drawing's own coordinates, where the
|
|
14
|
+
* percentage is taken of the whole drawing. {@link withCaptureSafeDom}
|
|
15
|
+
* pins those lengths to user units while the capture runs.
|
|
16
|
+
*/
|
|
17
|
+
/** Splits `text` at commas that are not inside parentheses. */
|
|
18
|
+
export declare function splitTopLevel(text: string): string[];
|
|
19
|
+
/**
|
|
20
|
+
* A computed `background-image` with each `linear-gradient()` rewritten to
|
|
21
|
+
* the syntax dom-to-svg parses: no interpolation space, `rgba()` stops.
|
|
22
|
+
* Other layers are left as they are. `toRgba` turns any CSS colour into
|
|
23
|
+
* `rgba(r, g, b, a)`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function safeBackgroundImage(value: string, toRgba: (color: string) => string): string;
|
|
26
|
+
/**
|
|
27
|
+
* Runs `run` (a synchronous dom-to-svg pass over `root`) with the page made
|
|
28
|
+
* readable by it: `getComputedStyle` hands out capture-safe background
|
|
29
|
+
* images, and percentage lengths in inline SVG are pinned to user units.
|
|
30
|
+
* Everything is put back before returning.
|
|
31
|
+
*/
|
|
32
|
+
export declare function withCaptureSafeDom<T>(root: Element, run: () => T): T;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Making a page readable by dom-to-svg for the length of one capture.
|
|
3
|
+
*
|
|
4
|
+
* dom-to-svg reads computed styles and SVG attributes as they are, and two
|
|
5
|
+
* things in them break it:
|
|
6
|
+
*
|
|
7
|
+
* - a `linear-gradient()` written with CSS Color 4: an interpolation space
|
|
8
|
+
* (`in oklch`) or stops in `oklch()`, `lab()`, `color()`... Its gradient
|
|
9
|
+
* parser only knows `rgb()`, `rgba()`, hex and names, and throws on the
|
|
10
|
+
* rest, which fails the whole capture. {@link safeBackgroundImage} rewrites
|
|
11
|
+
* such a value with plain `rgba()` stops;
|
|
12
|
+
* - a shape inside an inline `<svg>` sized in percentages (`<circle r="50%">`).
|
|
13
|
+
* The shape is copied into the drawing's own coordinates, where the
|
|
14
|
+
* percentage is taken of the whole drawing. {@link withCaptureSafeDom}
|
|
15
|
+
* pins those lengths to user units while the capture runs.
|
|
16
|
+
*/
|
|
17
|
+
/** Splits `text` at commas that are not inside parentheses. */
|
|
18
|
+
export function splitTopLevel(text) {
|
|
19
|
+
const parts = [];
|
|
20
|
+
let depth = 0;
|
|
21
|
+
let start = 0;
|
|
22
|
+
for (let i = 0; i < text.length; i++) {
|
|
23
|
+
const c = text[i];
|
|
24
|
+
if (c === '(')
|
|
25
|
+
depth++;
|
|
26
|
+
else if (c === ')')
|
|
27
|
+
depth--;
|
|
28
|
+
else if (c === ',' && depth === 0) {
|
|
29
|
+
parts.push(text.slice(start, i).trim());
|
|
30
|
+
start = i + 1;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
parts.push(text.slice(start).trim());
|
|
34
|
+
return parts;
|
|
35
|
+
}
|
|
36
|
+
/** Colour functions dom-to-svg's gradient parser reads. */
|
|
37
|
+
const PARSED_COLOR = /^(rgba?)$/i;
|
|
38
|
+
/** `in <space> [<hue method> hue]` in a gradient's first argument. */
|
|
39
|
+
const INTERPOLATION = /\s*\bin\s+[a-z-]+(?:\s+(?:shorter|longer|increasing|decreasing)\s+hue)?\s*/i;
|
|
40
|
+
/** Replaces every colour function the parser does not read with `toRgba(fn)`. */
|
|
41
|
+
function plainColors(argument, toRgba) {
|
|
42
|
+
let out = '';
|
|
43
|
+
let i = 0;
|
|
44
|
+
while (i < argument.length) {
|
|
45
|
+
const match = /[a-z-]+\(/i.exec(argument.slice(i));
|
|
46
|
+
if (!match) {
|
|
47
|
+
out += argument.slice(i);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
const at = i + match.index;
|
|
51
|
+
const name = match[0].slice(0, -1);
|
|
52
|
+
let depth = 0;
|
|
53
|
+
let end = at + name.length;
|
|
54
|
+
for (; end < argument.length; end++) {
|
|
55
|
+
if (argument[end] === '(')
|
|
56
|
+
depth++;
|
|
57
|
+
else if (argument[end] === ')' && --depth === 0)
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
const call = argument.slice(at, end + 1);
|
|
61
|
+
out += argument.slice(i, at) + (PARSED_COLOR.test(name) ? call : toRgba(call));
|
|
62
|
+
i = end + 1;
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
/** Rewrites one `linear-gradient(...)` so dom-to-svg can parse it. */
|
|
67
|
+
function safeLinearGradient(layer, toRgba) {
|
|
68
|
+
const open = layer.indexOf('(');
|
|
69
|
+
const name = layer.slice(0, open);
|
|
70
|
+
const args = splitTopLevel(layer.slice(open + 1, layer.lastIndexOf(')')));
|
|
71
|
+
const first = args[0] ?? '';
|
|
72
|
+
// The first argument is a direction or angle (with or without an interpolation space), or an interpolation space alone.
|
|
73
|
+
if (/^(to\b|-?[\d.]+(deg|rad|grad|turn)\b|in\b)/i.test(first)) {
|
|
74
|
+
const direction = first.replace(INTERPOLATION, ' ').trim();
|
|
75
|
+
if (direction)
|
|
76
|
+
args[0] = direction;
|
|
77
|
+
else
|
|
78
|
+
args.shift();
|
|
79
|
+
}
|
|
80
|
+
return `${name}(${args.map((arg) => plainColors(arg, toRgba)).join(', ')})`;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* A computed `background-image` with each `linear-gradient()` rewritten to
|
|
84
|
+
* the syntax dom-to-svg parses: no interpolation space, `rgba()` stops.
|
|
85
|
+
* Other layers are left as they are. `toRgba` turns any CSS colour into
|
|
86
|
+
* `rgba(r, g, b, a)`.
|
|
87
|
+
*/
|
|
88
|
+
export function safeBackgroundImage(value, toRgba) {
|
|
89
|
+
if (!/linear-gradient\(/i.test(value))
|
|
90
|
+
return value;
|
|
91
|
+
return splitTopLevel(value)
|
|
92
|
+
.map((layer) => (/^(-webkit-)?(repeating-)?linear-gradient\(/i.test(layer) ? safeLinearGradient(layer, toRgba) : layer))
|
|
93
|
+
.join(', ');
|
|
94
|
+
}
|
|
95
|
+
/** Turns any CSS colour into `rgba(r, g, b, a)` by drawing it, in `view`'s document. */
|
|
96
|
+
function colorConverter(view) {
|
|
97
|
+
const canvas = view.document.createElement('canvas');
|
|
98
|
+
canvas.width = canvas.height = 1;
|
|
99
|
+
const context = canvas.getContext('2d', { willReadFrequently: true });
|
|
100
|
+
const cache = new Map();
|
|
101
|
+
return (color) => {
|
|
102
|
+
let rgba = cache.get(color);
|
|
103
|
+
if (rgba)
|
|
104
|
+
return rgba;
|
|
105
|
+
rgba = 'rgba(0, 0, 0, 0)';
|
|
106
|
+
if (context) {
|
|
107
|
+
context.clearRect(0, 0, 1, 1);
|
|
108
|
+
context.fillStyle = 'rgba(0, 0, 0, 0)';
|
|
109
|
+
context.fillStyle = color;
|
|
110
|
+
context.fillRect(0, 0, 1, 1);
|
|
111
|
+
const [r, g, b, a] = context.getImageData(0, 0, 1, 1).data;
|
|
112
|
+
rgba = `rgba(${r}, ${g}, ${b}, ${Math.round((a / 255) * 1000) / 1000})`;
|
|
113
|
+
}
|
|
114
|
+
cache.set(color, rgba);
|
|
115
|
+
return rgba;
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/** Geometry attributes of SVG elements that may be percentages of the viewport. */
|
|
119
|
+
const LENGTH_ATTRIBUTES = ['x', 'y', 'width', 'height', 'cx', 'cy', 'r', 'rx', 'ry', 'x1', 'y1', 'x2', 'y2'];
|
|
120
|
+
/** Pins percentage lengths of shapes inside inline `<svg>`s under `root` to user units; returns the undo. */
|
|
121
|
+
function pinSvgPercentages(root) {
|
|
122
|
+
const saved = [];
|
|
123
|
+
const shapes = root.ownerDocument.querySelectorAll('svg *');
|
|
124
|
+
for (const shape of shapes) {
|
|
125
|
+
if (!root.contains(shape))
|
|
126
|
+
continue;
|
|
127
|
+
for (const name of LENGTH_ATTRIBUTES) {
|
|
128
|
+
const value = shape.getAttribute(name);
|
|
129
|
+
if (!value?.includes('%'))
|
|
130
|
+
continue;
|
|
131
|
+
const length = shape[name];
|
|
132
|
+
if (!length?.baseVal)
|
|
133
|
+
continue;
|
|
134
|
+
let resolved;
|
|
135
|
+
try {
|
|
136
|
+
resolved = length.baseVal.value;
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
saved.push([shape, name, value]);
|
|
142
|
+
shape.setAttribute(name, String(resolved));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return () => {
|
|
146
|
+
for (const [shape, name, value] of saved)
|
|
147
|
+
shape.setAttribute(name, value);
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Runs `run` (a synchronous dom-to-svg pass over `root`) with the page made
|
|
152
|
+
* readable by it: `getComputedStyle` hands out capture-safe background
|
|
153
|
+
* images, and percentage lengths in inline SVG are pinned to user units.
|
|
154
|
+
* Everything is put back before returning.
|
|
155
|
+
*/
|
|
156
|
+
export function withCaptureSafeDom(root, run) {
|
|
157
|
+
const view = root.ownerDocument.defaultView;
|
|
158
|
+
if (!view)
|
|
159
|
+
return run();
|
|
160
|
+
const toRgba = colorConverter(view);
|
|
161
|
+
const original = view.getComputedStyle;
|
|
162
|
+
const patched = function (element, pseudo) {
|
|
163
|
+
const styles = original.call(view, element, pseudo);
|
|
164
|
+
const backgroundImage = styles.backgroundImage;
|
|
165
|
+
if (!backgroundImage || !backgroundImage.includes('linear-gradient('))
|
|
166
|
+
return styles;
|
|
167
|
+
const safe = safeBackgroundImage(backgroundImage, toRgba);
|
|
168
|
+
if (safe === backgroundImage)
|
|
169
|
+
return styles;
|
|
170
|
+
return new Proxy(styles, {
|
|
171
|
+
get(target, property) {
|
|
172
|
+
if (property === 'backgroundImage')
|
|
173
|
+
return safe;
|
|
174
|
+
if (property === 'getPropertyValue')
|
|
175
|
+
return (name) => (name === 'background-image' ? safe : target.getPropertyValue(name));
|
|
176
|
+
const value = target[property];
|
|
177
|
+
return typeof value === 'function' ? value.bind(target) : value;
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
const unpin = pinSvgPercentages(root);
|
|
182
|
+
view.getComputedStyle = patched;
|
|
183
|
+
try {
|
|
184
|
+
return run();
|
|
185
|
+
}
|
|
186
|
+
finally {
|
|
187
|
+
view.getComputedStyle = original;
|
|
188
|
+
unpin();
|
|
189
|
+
}
|
|
190
|
+
}
|
package/src/capture.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capturing rendered DOM as pictures, two ways:
|
|
3
|
+
*
|
|
4
|
+
* - through an SVG `<foreignObject>`: a copy of the markup with every
|
|
5
|
+
* picture inlined as a `data:` URL ({@link inlinedCopy}) is wrapped in an
|
|
6
|
+
* SVG document ({@link foreignObjectSvg}), loaded as an image
|
|
7
|
+
* ({@link loadSvgImage}) and drawn onto a canvas. It keeps everything the
|
|
8
|
+
* browser can draw, but some browsers refuse to read the canvas back;
|
|
9
|
+
* - with dom-to-svg ({@link elementSvg}), which rebuilds the element as
|
|
10
|
+
* plain SVG shapes: a vector drawing that pastes into design tools, and
|
|
11
|
+
* the fallback when the first way fails.
|
|
12
|
+
*
|
|
13
|
+
* What is captured (a slide, an artboard's element) and how the result is
|
|
14
|
+
* drawn stays with each editor.
|
|
15
|
+
*/
|
|
16
|
+
/** A transparent 1×1 GIF: stands in for pictures a capture leaves out. */
|
|
17
|
+
export declare const BLANK_GIF = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
|
|
18
|
+
export declare const SVG_NS = "http://www.w3.org/2000/svg";
|
|
19
|
+
export declare const XHTML_NS = "http://www.w3.org/1999/xhtml";
|
|
20
|
+
/** How large a capture may be. */
|
|
21
|
+
export interface CaptureLimits {
|
|
22
|
+
/** Largest side, px. */
|
|
23
|
+
maxSide: number;
|
|
24
|
+
/** Most pixels in one capture. */
|
|
25
|
+
maxPixels: number;
|
|
26
|
+
}
|
|
27
|
+
/** The largest scale an area of `width` × `height` can be drawn at within `limits`. */
|
|
28
|
+
export declare function maxCaptureScale(width: number, height: number, limits: CaptureLimits): number;
|
|
29
|
+
/** Resolves once every picture in the element loaded (or failed, or took longer than `timeout` ms) and decoded. */
|
|
30
|
+
export declare function picturesReady(root: ParentNode, timeout: number): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Hides everything under `root` but `target` (live elements or a copy), so a
|
|
33
|
+
* capture draws only the target where it sits. `bare` also hides what the
|
|
34
|
+
* target holds (its background and border only). Returns an undo.
|
|
35
|
+
*/
|
|
36
|
+
export declare function hideOutside(root: HTMLElement, target: Element | undefined, bare: boolean): () => void;
|
|
37
|
+
/** How {@link inlinedCopy} reads and replaces pictures. */
|
|
38
|
+
export interface InlineOptions {
|
|
39
|
+
/** A picture's address as a `data:` URL, or null when it cannot be read. */
|
|
40
|
+
dataUrl: (src: string) => Promise<string | null>;
|
|
41
|
+
/**
|
|
42
|
+
* What becomes of a picture that cannot be read. `leave`: an `<img>`
|
|
43
|
+
* loses its `src`; an SVG `<image>` and a style's `url(…)` keep their
|
|
44
|
+
* address. `blank`: each becomes {@link BLANK_GIF}.
|
|
45
|
+
*/
|
|
46
|
+
unreadable: 'leave' | 'blank';
|
|
47
|
+
/** Called with each `<img>` source and style `url(…)` that could not be read. */
|
|
48
|
+
onMissing?: (src: string) => void;
|
|
49
|
+
/** Inlines only pictures in this element; `<img>`s outside it are blanked (their size kept) so the copy stays small, and it alone is shown. */
|
|
50
|
+
only?: Element;
|
|
51
|
+
/** With `only`: hides what the element holds (see {@link hideOutside}). */
|
|
52
|
+
bare?: boolean;
|
|
53
|
+
}
|
|
54
|
+
/** Replaces `url(…)` references in an element's inline style with `data:` URLs. */
|
|
55
|
+
export declare function inlineStyleUrls(el: Element, options: Pick<InlineOptions, 'dataUrl' | 'unreadable' | 'onMissing'>): Promise<void>;
|
|
56
|
+
/** A copy of the element's markup with every picture inlined, ready to be drawn in isolation. */
|
|
57
|
+
export declare function inlinedCopy(root: HTMLElement, options: InlineOptions): Promise<HTMLElement>;
|
|
58
|
+
/** Escapes text (such as CSS) for an XML text node. */
|
|
59
|
+
export declare function xmlText(text: string): string;
|
|
60
|
+
/** The size of a {@link foreignObjectSvg} drawing. */
|
|
61
|
+
export interface ForeignObjectSize {
|
|
62
|
+
/** The markup's layout size, CSS px (also the view box). */
|
|
63
|
+
width: number;
|
|
64
|
+
height: number;
|
|
65
|
+
/** The SVG's own size (default: the layout size). */
|
|
66
|
+
outerWidth?: number;
|
|
67
|
+
outerHeight?: number;
|
|
68
|
+
}
|
|
69
|
+
/** An SVG document drawing serialized XHTML `markup`, styled by `css`, through a `<foreignObject>`. */
|
|
70
|
+
export declare function foreignObjectSvg(markup: string, css: string, size: ForeignObjectSize): string;
|
|
71
|
+
/** Loads an SVG document as an image (from a `data:` URL), decoded; rejects with `error` when it will not load. */
|
|
72
|
+
export declare function loadSvgImage(svg: string, options: {
|
|
73
|
+
error: string;
|
|
74
|
+
decoding?: HTMLImageElement['decoding'];
|
|
75
|
+
}): Promise<HTMLImageElement>;
|
|
76
|
+
/** An area to capture, CSS px in the element's window. */
|
|
77
|
+
export interface CaptureArea {
|
|
78
|
+
x: number;
|
|
79
|
+
y: number;
|
|
80
|
+
width: number;
|
|
81
|
+
height: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The element rebuilt as plain SVG by dom-to-svg (links dropped), its
|
|
85
|
+
* resources inlined. `area` defaults to the element's own box.
|
|
86
|
+
*/
|
|
87
|
+
export declare function elementSvg(element: Element, area?: CaptureArea): Promise<XMLDocument>;
|