@d3plus/export 3.0.16 → 3.1.1
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 +26 -11
- package/es/src/saveElement.js +16 -15
- package/package.json +18 -9
- package/types/index.d.ts +1 -0
- package/types/src/saveElement.d.ts +25 -0
- package/umd/d3plus-export.full.js +17 -120
- package/umd/d3plus-export.full.js.map +1 -1
- package/umd/d3plus-export.full.min.js +49 -43
- package/umd/d3plus-export.js +17 -120
- package/umd/d3plus-export.js.map +1 -1
- package/umd/d3plus-export.min.js +11 -12
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 D3plus
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# @d3plus/export
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@d3plus/export)
|
|
4
|
+
[](https://codecov.io/gh/d3plus/d3plus/flags)
|
|
5
|
+
|
|
3
6
|
Export methods for transforming and downloading SVG.
|
|
4
7
|
|
|
5
8
|
## Installing
|
|
@@ -7,10 +10,10 @@ Export methods for transforming and downloading SVG.
|
|
|
7
10
|
If using npm, `npm install @d3plus/export`. Otherwise, you can download the [latest release from GitHub](https://github.com/d3plus/d3plus/releases/latest) or load from a [CDN](https://cdn.jsdelivr.net/npm/@d3plus/export).
|
|
8
11
|
|
|
9
12
|
```js
|
|
10
|
-
import
|
|
13
|
+
import {*} from "@d3plus/export";
|
|
11
14
|
```
|
|
12
15
|
|
|
13
|
-
In vanilla
|
|
16
|
+
In a vanilla environment, a `d3plus` global is exported from the pre-bundled version:
|
|
14
17
|
|
|
15
18
|
```html
|
|
16
19
|
<script src="https://cdn.jsdelivr.net/npm/@d3plus/export"></script>
|
|
@@ -21,22 +24,34 @@ In vanilla JavaScript, a `d3plus` global is exported from the pre-bundled versio
|
|
|
21
24
|
|
|
22
25
|
## Examples
|
|
23
26
|
|
|
24
|
-
Live examples can be found on [d3plus.org](https://d3plus.org/), which includes a collection of example visualizations using @d3plus/react.
|
|
27
|
+
Live examples can be found on [d3plus.org](https://d3plus.org/), which includes a collection of example visualizations using [@d3plus/react](https://github.com/d3plus/d3plus/tree/main/packages/react).
|
|
25
28
|
|
|
26
29
|
## API Reference
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
| Functions | Description |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| [`saveElement`](#saveelement) | Downloads an HTML Element as a bitmap PNG image. |
|
|
34
|
+
|
|
35
|
+
## Functions
|
|
36
|
+
|
|
37
|
+
<a id="saveelement"></a>
|
|
38
|
+
|
|
39
|
+
### saveElement()
|
|
30
40
|
|
|
31
|
-
|
|
41
|
+
> **saveElement**(`elem`: `HTMLElement`, `options?`: `SaveElementOptions`, `renderOptions?`: `SaveElementRenderOptions`): `void`
|
|
32
42
|
|
|
33
|
-
|
|
34
|
-
#### d3plus.**saveElement**(elem, [options], [renderOptions]) [<>](https://github.com/d3plus/d3plus/blob/main/packages/export/src/saveElement.js#L9)
|
|
43
|
+
Defined in: [saveElement.ts:36](https://github.com/d3plus/d3plus/blob/fe174c3153bf379a226e4a41b6a8492a86c3a1b7/packages/export/src/saveElement.ts#L36)
|
|
35
44
|
|
|
36
45
|
Downloads an HTML Element as a bitmap PNG image.
|
|
37
46
|
|
|
47
|
+
#### Parameters
|
|
38
48
|
|
|
39
|
-
|
|
49
|
+
| Parameter | Type | Description |
|
|
50
|
+
| ------ | ------ | ------ |
|
|
51
|
+
| `elem` | `HTMLElement` | The DOM element or d3 selection to export. |
|
|
52
|
+
| `options` | `SaveElementOptions` | Additional options to specify. |
|
|
53
|
+
| `renderOptions` | `SaveElementRenderOptions` | Custom options to be passed to the html-to-image function. |
|
|
40
54
|
|
|
41
|
-
|
|
55
|
+
#### Returns
|
|
42
56
|
|
|
57
|
+
`void`
|
package/es/src/saveElement.js
CHANGED
|
@@ -5,28 +5,27 @@ var defaultOptions = {
|
|
|
5
5
|
type: "png"
|
|
6
6
|
};
|
|
7
7
|
/**
|
|
8
|
-
|
|
9
|
-
@
|
|
10
|
-
@param
|
|
11
|
-
@param
|
|
12
|
-
@param
|
|
13
|
-
@param
|
|
14
|
-
@param
|
|
15
|
-
@param {Object} [renderOptions] Custom options to be passed to the html-to-image function.
|
|
8
|
+
Downloads an HTML Element as a bitmap PNG image.
|
|
9
|
+
@param elem The DOM element or d3 selection to export.
|
|
10
|
+
@param options Additional options to specify.
|
|
11
|
+
@param options .filename = "download"] Filename for the downloaded file, without the extension.
|
|
12
|
+
@param options .type = "png"] File type of the saved document. Accepted values are `"png"` and `"jpg"`.
|
|
13
|
+
@param options .callback] Function to be invoked after saving is complete.
|
|
14
|
+
@param renderOptions Custom options to be passed to the html-to-image function.
|
|
16
15
|
*/ export default function(elem) {
|
|
17
16
|
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, renderOptions = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
18
17
|
if (!elem) return;
|
|
19
|
-
|
|
18
|
+
var opts = Object.assign({}, defaultOptions, options);
|
|
20
19
|
// rename renderOptions.background to backgroundColor for backwards compatibility
|
|
21
|
-
|
|
20
|
+
var renderOpts = Object.assign({
|
|
22
21
|
backgroundColor: renderOptions.background
|
|
23
22
|
}, renderOptions);
|
|
24
23
|
function finish(blob) {
|
|
25
|
-
saveAs(blob, "".concat(
|
|
26
|
-
if (
|
|
24
|
+
saveAs(blob, "".concat(opts.filename, ".").concat(opts.type));
|
|
25
|
+
if (opts.callback) opts.callback();
|
|
27
26
|
}
|
|
28
|
-
if (
|
|
29
|
-
toSvg(elem,
|
|
27
|
+
if (opts.type === "svg") {
|
|
28
|
+
toSvg(elem, renderOpts).then(function(dataUrl) {
|
|
30
29
|
var xhr = new XMLHttpRequest();
|
|
31
30
|
xhr.open("GET", dataUrl);
|
|
32
31
|
xhr.responseType = "blob";
|
|
@@ -36,6 +35,8 @@ var defaultOptions = {
|
|
|
36
35
|
xhr.send();
|
|
37
36
|
});
|
|
38
37
|
} else {
|
|
39
|
-
toBlob(elem,
|
|
38
|
+
toBlob(elem, renderOpts).then(function(blob) {
|
|
39
|
+
if (blob) finish(blob);
|
|
40
|
+
});
|
|
40
41
|
}
|
|
41
42
|
}
|
package/package.json
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d3plus/export",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "Export methods for transforming and downloading SVG.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"
|
|
7
|
+
"types": "./types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./types/index.d.ts",
|
|
11
|
+
"default": "./es/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
8
14
|
"browser": "./umd/d3plus-export.full.js",
|
|
9
15
|
"engines": {
|
|
10
|
-
"node": ">=
|
|
16
|
+
"node": ">=20"
|
|
11
17
|
},
|
|
12
18
|
"sideEffects": false,
|
|
13
19
|
"files": [
|
|
14
20
|
"umd",
|
|
15
|
-
"es"
|
|
21
|
+
"es",
|
|
22
|
+
"types"
|
|
16
23
|
],
|
|
17
24
|
"homepage": "https://d3plus.org",
|
|
18
25
|
"repository": {
|
|
@@ -27,14 +34,16 @@
|
|
|
27
34
|
"data",
|
|
28
35
|
"visualization"
|
|
29
36
|
],
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"file-saver": "1.3.3",
|
|
39
|
+
"html-to-image": "^1.11.11"
|
|
40
|
+
},
|
|
30
41
|
"scripts": {
|
|
31
42
|
"build:esm": "node ../../scripts/build-esm.js",
|
|
43
|
+
"build:types": "tsc",
|
|
32
44
|
"build:umd": "node ../../scripts/build-umd.js",
|
|
33
45
|
"dev": "node ../../scripts/dev.js",
|
|
34
|
-
"test": "eslint index.
|
|
35
|
-
|
|
36
|
-
"dependencies": {
|
|
37
|
-
"file-saver": "1.3.3",
|
|
38
|
-
"html-to-image": "^1.11.11"
|
|
46
|
+
"test": "eslint index.ts src/**/*.ts && eslint --global=it test && mocha 'test/**/*-test.js'",
|
|
47
|
+
"test:coverage": "c8 -r text -r lcov --src src mocha 'test/**/*-test.js'"
|
|
39
48
|
}
|
|
40
49
|
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as saveElement } from "./src/saveElement.js";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface SaveElementOptions {
|
|
2
|
+
/** Filename for the downloaded file, without the extension. */
|
|
3
|
+
filename?: string;
|
|
4
|
+
/** File type of the saved document. Accepted values are "png", "jpg", and "svg". */
|
|
5
|
+
type?: "png" | "jpg" | "svg";
|
|
6
|
+
/** Function to be invoked after saving is complete. */
|
|
7
|
+
callback?: () => void;
|
|
8
|
+
}
|
|
9
|
+
export interface SaveElementRenderOptions {
|
|
10
|
+
/** Background color for the rendered image. */
|
|
11
|
+
background?: string;
|
|
12
|
+
/** Background color for the rendered image (alias). */
|
|
13
|
+
backgroundColor?: string;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
Downloads an HTML Element as a bitmap PNG image.
|
|
18
|
+
@param elem The DOM element or d3 selection to export.
|
|
19
|
+
@param options Additional options to specify.
|
|
20
|
+
@param options .filename = "download"] Filename for the downloaded file, without the extension.
|
|
21
|
+
@param options .type = "png"] File type of the saved document. Accepted values are `"png"` and `"jpg"`.
|
|
22
|
+
@param options .callback] Function to be invoked after saving is complete.
|
|
23
|
+
@param renderOptions Custom options to be passed to the html-to-image function.
|
|
24
|
+
*/
|
|
25
|
+
export default function (elem: HTMLElement, options?: SaveElementOptions, renderOptions?: SaveElementRenderOptions): void;
|
|
@@ -1,113 +1,9 @@
|
|
|
1
1
|
/*
|
|
2
|
-
@d3plus/export v3.0
|
|
2
|
+
@d3plus/export v3.1.0
|
|
3
3
|
Export methods for transforming and downloading SVG.
|
|
4
4
|
Copyright (c) 2026 D3plus - https://d3plus.org
|
|
5
5
|
@license MIT
|
|
6
6
|
*/
|
|
7
|
-
|
|
8
|
-
(function (factory) {
|
|
9
|
-
typeof define === 'function' && define.amd ? define(factory) :
|
|
10
|
-
factory();
|
|
11
|
-
})((function () { 'use strict';
|
|
12
|
-
|
|
13
|
-
if (typeof window !== "undefined") {
|
|
14
|
-
(function () {
|
|
15
|
-
try {
|
|
16
|
-
if (typeof SVGElement === 'undefined' || Boolean(SVGElement.prototype.innerHTML)) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
} catch (e) {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function serializeNode (node) {
|
|
24
|
-
switch (node.nodeType) {
|
|
25
|
-
case 1:
|
|
26
|
-
return serializeElementNode(node);
|
|
27
|
-
case 3:
|
|
28
|
-
return serializeTextNode(node);
|
|
29
|
-
case 8:
|
|
30
|
-
return serializeCommentNode(node);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function serializeTextNode (node) {
|
|
35
|
-
return node.textContent.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function serializeCommentNode (node) {
|
|
39
|
-
return '<!--' + node.nodeValue + '-->'
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function serializeElementNode (node) {
|
|
43
|
-
var output = '';
|
|
44
|
-
|
|
45
|
-
output += '<' + node.tagName;
|
|
46
|
-
|
|
47
|
-
if (node.hasAttributes()) {
|
|
48
|
-
[].forEach.call(node.attributes, function(attrNode) {
|
|
49
|
-
output += ' ' + attrNode.name + '="' + attrNode.value + '"';
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
output += '>';
|
|
54
|
-
|
|
55
|
-
if (node.hasChildNodes()) {
|
|
56
|
-
[].forEach.call(node.childNodes, function(childNode) {
|
|
57
|
-
output += serializeNode(childNode);
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
output += '</' + node.tagName + '>';
|
|
62
|
-
|
|
63
|
-
return output;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
Object.defineProperty(SVGElement.prototype, 'innerHTML', {
|
|
67
|
-
get: function () {
|
|
68
|
-
var output = '';
|
|
69
|
-
|
|
70
|
-
[].forEach.call(this.childNodes, function(childNode) {
|
|
71
|
-
output += serializeNode(childNode);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
return output;
|
|
75
|
-
},
|
|
76
|
-
set: function (markup) {
|
|
77
|
-
while (this.firstChild) {
|
|
78
|
-
this.removeChild(this.firstChild);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
try {
|
|
82
|
-
var dXML = new DOMParser();
|
|
83
|
-
dXML.async = false;
|
|
84
|
-
|
|
85
|
-
var sXML = '<svg xmlns=\'http://www.w3.org/2000/svg\' xmlns:xlink=\'http://www.w3.org/1999/xlink\'>' + markup + '</svg>';
|
|
86
|
-
var svgDocElement = dXML.parseFromString(sXML, 'text/xml').documentElement;
|
|
87
|
-
|
|
88
|
-
[].forEach.call(svgDocElement.childNodes, function(childNode) {
|
|
89
|
-
this.appendChild(this.ownerDocument.importNode(childNode, true));
|
|
90
|
-
}.bind(this));
|
|
91
|
-
} catch (e) {
|
|
92
|
-
throw new Error('Error parsing markup string');
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
Object.defineProperty(SVGElement.prototype, 'innerSVG', {
|
|
98
|
-
get: function () {
|
|
99
|
-
return this.innerHTML;
|
|
100
|
-
},
|
|
101
|
-
set: function (markup) {
|
|
102
|
-
this.innerHTML = markup;
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
})();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
}));
|
|
110
|
-
|
|
111
7
|
(function (global, factory) {
|
|
112
8
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
113
9
|
typeof define === 'function' && define.amd ? define('@d3plus/export', ['exports'], factory) :
|
|
@@ -1107,27 +1003,26 @@
|
|
|
1107
1003
|
type: "png"
|
|
1108
1004
|
};
|
|
1109
1005
|
/**
|
|
1110
|
-
|
|
1111
|
-
@
|
|
1112
|
-
@param
|
|
1113
|
-
@param
|
|
1114
|
-
@param
|
|
1115
|
-
@param
|
|
1116
|
-
@param
|
|
1117
|
-
@param {Object} [renderOptions] Custom options to be passed to the html-to-image function.
|
|
1006
|
+
Downloads an HTML Element as a bitmap PNG image.
|
|
1007
|
+
@param elem The DOM element or d3 selection to export.
|
|
1008
|
+
@param options Additional options to specify.
|
|
1009
|
+
@param options .filename = "download"] Filename for the downloaded file, without the extension.
|
|
1010
|
+
@param options .type = "png"] File type of the saved document. Accepted values are `"png"` and `"jpg"`.
|
|
1011
|
+
@param options .callback] Function to be invoked after saving is complete.
|
|
1012
|
+
@param renderOptions Custom options to be passed to the html-to-image function.
|
|
1118
1013
|
*/ function saveElement(elem, options = {}, renderOptions = {}) {
|
|
1119
1014
|
if (!elem) return;
|
|
1120
|
-
|
|
1015
|
+
const opts = Object.assign({}, defaultOptions, options);
|
|
1121
1016
|
// rename renderOptions.background to backgroundColor for backwards compatibility
|
|
1122
|
-
|
|
1017
|
+
const renderOpts = Object.assign({
|
|
1123
1018
|
backgroundColor: renderOptions.background
|
|
1124
1019
|
}, renderOptions);
|
|
1125
1020
|
function finish(blob) {
|
|
1126
|
-
FileSaverExports.saveAs(blob, `${
|
|
1127
|
-
if (
|
|
1021
|
+
FileSaverExports.saveAs(blob, `${opts.filename}.${opts.type}`);
|
|
1022
|
+
if (opts.callback) opts.callback();
|
|
1128
1023
|
}
|
|
1129
|
-
if (
|
|
1130
|
-
toSvg(elem,
|
|
1024
|
+
if (opts.type === "svg") {
|
|
1025
|
+
toSvg(elem, renderOpts).then((dataUrl)=>{
|
|
1131
1026
|
const xhr = new XMLHttpRequest();
|
|
1132
1027
|
xhr.open("GET", dataUrl);
|
|
1133
1028
|
xhr.responseType = "blob";
|
|
@@ -1135,7 +1030,9 @@
|
|
|
1135
1030
|
xhr.send();
|
|
1136
1031
|
});
|
|
1137
1032
|
} else {
|
|
1138
|
-
toBlob(elem,
|
|
1033
|
+
toBlob(elem, renderOpts).then((blob)=>{
|
|
1034
|
+
if (blob) finish(blob);
|
|
1035
|
+
});
|
|
1139
1036
|
}
|
|
1140
1037
|
}
|
|
1141
1038
|
|