@d3plus/export 3.0.16 → 3.1.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 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,5 @@
1
1
  # @d3plus/export
2
-
2
+
3
3
  Export methods for transforming and downloading SVG.
4
4
 
5
5
  ## Installing
@@ -7,10 +7,10 @@ Export methods for transforming and downloading SVG.
7
7
  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
8
 
9
9
  ```js
10
- import modules from "@d3plus/export";
10
+ import {*} from "@d3plus/export";
11
11
  ```
12
12
 
13
- In vanilla JavaScript, a `d3plus` global is exported from the pre-bundled version:
13
+ In a vanilla environment, a `d3plus` global is exported from the pre-bundled version:
14
14
 
15
15
  ```html
16
16
  <script src="https://cdn.jsdelivr.net/npm/@d3plus/export"></script>
@@ -21,22 +21,34 @@ In vanilla JavaScript, a `d3plus` global is exported from the pre-bundled versio
21
21
 
22
22
  ## Examples
23
23
 
24
- Live examples can be found on [d3plus.org](https://d3plus.org/), which includes a collection of example visualizations using @d3plus/react.
24
+ 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
25
 
26
26
  ## API Reference
27
27
 
28
- #####
29
- * [saveElement](#saveElement) - Downloads an HTML Element as a bitmap PNG image.
28
+ | Functions | Description |
29
+ | --- | --- |
30
+ | [`saveElement`](#saveelement) | Downloads an HTML Element as a bitmap PNG image. |
31
+
32
+ ## Functions
33
+
34
+ <a id="saveelement"></a>
35
+
36
+ ### saveElement()
30
37
 
31
- ---
38
+ > **saveElement**(`elem`: `HTMLElement`, `options?`: `SaveElementOptions`, `renderOptions?`: `SaveElementRenderOptions`): `void`
32
39
 
33
- <a name="saveElement"></a>
34
- #### d3plus.**saveElement**(elem, [options], [renderOptions]) [<>](https://github.com/d3plus/d3plus/blob/main/packages/export/src/saveElement.js#L9)
40
+ Defined in: [saveElement.ts:36](https://github.com/d3plus/d3plus/blob/e9db3c74352143cd7b6bdc8d0786477ea971eb6d/packages/export/src/saveElement.ts#L36)
35
41
 
36
42
  Downloads an HTML Element as a bitmap PNG image.
37
43
 
44
+ #### Parameters
38
45
 
39
- This is a global function
46
+ | Parameter | Type | Description |
47
+ | ------ | ------ | ------ |
48
+ | `elem` | `HTMLElement` | The DOM element or d3 selection to export. |
49
+ | `options` | `SaveElementOptions` | Additional options to specify. |
50
+ | `renderOptions` | `SaveElementRenderOptions` | Custom options to be passed to the html-to-image function. |
40
51
 
41
- ---
52
+ #### Returns
42
53
 
54
+ `void`
@@ -5,28 +5,27 @@ var defaultOptions = {
5
5
  type: "png"
6
6
  };
7
7
  /**
8
- @function saveElement
9
- @desc Downloads an HTML Element as a bitmap PNG image.
10
- @param {HTMLElement} elem A single element to be saved to one file.
11
- @param {Object} [options] Additional options to specify.
12
- @param {String} [options.filename = "download"] Filename for the downloaded file, without the extension.
13
- @param {String} [options.type = "png"] File type of the saved document. Accepted values are `"png"` and `"jpg"`.
14
- @param {Function} [options.callback] Function to be invoked after saving is complete.
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
- options = Object.assign({}, defaultOptions, options);
18
+ var opts = Object.assign({}, defaultOptions, options);
20
19
  // rename renderOptions.background to backgroundColor for backwards compatibility
21
- renderOptions = Object.assign({
20
+ var renderOpts = Object.assign({
22
21
  backgroundColor: renderOptions.background
23
22
  }, renderOptions);
24
23
  function finish(blob) {
25
- saveAs(blob, "".concat(options.filename, ".").concat(options.type));
26
- if (options.callback) options.callback();
24
+ saveAs(blob, "".concat(opts.filename, ".").concat(opts.type));
25
+ if (opts.callback) opts.callback();
27
26
  }
28
- if (options.type === "svg") {
29
- toSvg(elem, renderOptions).then(function(dataUrl) {
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, renderOptions).then(finish);
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.0.16",
3
+ "version": "3.1.0",
4
4
  "description": "Export methods for transforming and downloading SVG.",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
- "exports": "./es/index.js",
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": ">=18"
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,15 @@
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.js src/**/*.js && eslint --global=it test && mocha 'test/**/*-test.js'"
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'"
39
47
  }
40
48
  }
@@ -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;
@@ -4,110 +4,6 @@
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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
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
- @function saveElement
1111
- @desc Downloads an HTML Element as a bitmap PNG image.
1112
- @param {HTMLElement} elem A single element to be saved to one file.
1113
- @param {Object} [options] Additional options to specify.
1114
- @param {String} [options.filename = "download"] Filename for the downloaded file, without the extension.
1115
- @param {String} [options.type = "png"] File type of the saved document. Accepted values are `"png"` and `"jpg"`.
1116
- @param {Function} [options.callback] Function to be invoked after saving is complete.
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
- options = Object.assign({}, defaultOptions, options);
1015
+ const opts = Object.assign({}, defaultOptions, options);
1121
1016
  // rename renderOptions.background to backgroundColor for backwards compatibility
1122
- renderOptions = Object.assign({
1017
+ const renderOpts = Object.assign({
1123
1018
  backgroundColor: renderOptions.background
1124
1019
  }, renderOptions);
1125
1020
  function finish(blob) {
1126
- FileSaverExports.saveAs(blob, `${options.filename}.${options.type}`);
1127
- if (options.callback) options.callback();
1021
+ FileSaverExports.saveAs(blob, `${opts.filename}.${opts.type}`);
1022
+ if (opts.callback) opts.callback();
1128
1023
  }
1129
- if (options.type === "svg") {
1130
- toSvg(elem, renderOptions).then((dataUrl)=>{
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, renderOptions).then(finish);
1033
+ toBlob(elem, renderOpts).then((blob)=>{
1034
+ if (blob) finish(blob);
1035
+ });
1139
1036
  }
1140
1037
  }
1141
1038