@antv/infographic 0.1.0 → 0.1.2

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.
Files changed (110) hide show
  1. package/README.md +8 -12
  2. package/README.zh-CN.md +8 -9
  3. package/dist/infographic.min.js +45 -34
  4. package/dist/infographic.min.js.map +1 -1
  5. package/esm/designs/components/BtnsGroup.js +1 -1
  6. package/esm/designs/structures/hierarchy-tree.js +13 -6
  7. package/esm/exporter/font.d.ts +18 -0
  8. package/esm/exporter/font.js +202 -0
  9. package/esm/exporter/index.d.ts +3 -0
  10. package/esm/exporter/index.js +2 -0
  11. package/esm/exporter/png.d.ts +2 -0
  12. package/esm/exporter/png.js +42 -0
  13. package/esm/exporter/svg.d.ts +3 -0
  14. package/esm/exporter/svg.js +72 -0
  15. package/esm/exporter/types.d.ts +17 -0
  16. package/esm/index.d.ts +2 -2
  17. package/esm/renderer/composites/text.js +1 -3
  18. package/esm/renderer/fonts/built-in.d.ts +1 -1
  19. package/esm/renderer/fonts/index.d.ts +0 -2
  20. package/esm/renderer/fonts/index.js +0 -1
  21. package/esm/renderer/fonts/loader.js +1 -2
  22. package/esm/renderer/fonts/registry.d.ts +1 -1
  23. package/esm/renderer/fonts/registry.js +1 -1
  24. package/esm/renderer/renderer.js +1 -50
  25. package/esm/resource/utils/ref.js +1 -1
  26. package/esm/runtime/Infographic.d.ts +10 -0
  27. package/esm/runtime/Infographic.js +23 -2
  28. package/esm/types/font.js +1 -0
  29. package/esm/types/index.d.ts +1 -0
  30. package/{lib/renderer/fonts/utils.d.ts → esm/utils/font.d.ts} +1 -1
  31. package/esm/utils/index.d.ts +2 -0
  32. package/esm/utils/index.js +2 -0
  33. package/esm/utils/is-node.d.ts +4 -0
  34. package/esm/utils/is-node.js +6 -0
  35. package/esm/utils/padding.d.ts +1 -0
  36. package/esm/utils/padding.js +70 -0
  37. package/esm/utils/svg.d.ts +0 -1
  38. package/esm/utils/svg.js +3 -18
  39. package/esm/utils/text.js +1 -1
  40. package/esm/utils/viewbox.d.ts +6 -0
  41. package/esm/utils/viewbox.js +12 -0
  42. package/lib/designs/components/BtnsGroup.js +1 -1
  43. package/lib/designs/structures/hierarchy-tree.js +31 -24
  44. package/lib/exporter/font.d.ts +18 -0
  45. package/lib/exporter/font.js +210 -0
  46. package/lib/exporter/index.d.ts +3 -0
  47. package/lib/exporter/index.js +7 -0
  48. package/lib/exporter/png.d.ts +2 -0
  49. package/lib/exporter/png.js +45 -0
  50. package/lib/exporter/svg.d.ts +3 -0
  51. package/lib/exporter/svg.js +76 -0
  52. package/lib/exporter/types.d.ts +17 -0
  53. package/lib/index.d.ts +2 -2
  54. package/lib/renderer/composites/text.js +1 -3
  55. package/lib/renderer/fonts/built-in.d.ts +1 -1
  56. package/lib/renderer/fonts/index.d.ts +0 -2
  57. package/lib/renderer/fonts/index.js +1 -4
  58. package/lib/renderer/fonts/loader.js +1 -2
  59. package/lib/renderer/fonts/registry.d.ts +1 -1
  60. package/lib/renderer/fonts/registry.js +1 -1
  61. package/lib/renderer/renderer.js +1 -50
  62. package/lib/resource/utils/ref.js +1 -1
  63. package/lib/runtime/Infographic.d.ts +10 -0
  64. package/lib/runtime/Infographic.js +23 -2
  65. package/lib/types/font.js +2 -0
  66. package/lib/types/index.d.ts +1 -0
  67. package/{esm/renderer/fonts/utils.d.ts → lib/utils/font.d.ts} +1 -1
  68. package/lib/utils/index.d.ts +2 -0
  69. package/lib/utils/index.js +2 -0
  70. package/lib/utils/is-node.d.ts +4 -0
  71. package/lib/utils/is-node.js +9 -0
  72. package/lib/utils/padding.d.ts +1 -0
  73. package/lib/utils/padding.js +71 -0
  74. package/lib/utils/svg.d.ts +0 -1
  75. package/lib/utils/svg.js +3 -19
  76. package/lib/utils/text.js +2 -2
  77. package/lib/utils/viewbox.d.ts +6 -0
  78. package/lib/utils/viewbox.js +15 -0
  79. package/package.json +5 -2
  80. package/src/designs/components/BtnsGroup.tsx +7 -1
  81. package/src/designs/structures/hierarchy-tree.tsx +14 -8
  82. package/src/exporter/font.ts +273 -0
  83. package/src/exporter/index.ts +7 -0
  84. package/src/exporter/png.ts +58 -0
  85. package/src/exporter/svg.ts +94 -0
  86. package/src/exporter/types.ts +19 -0
  87. package/src/index.ts +7 -3
  88. package/src/renderer/composites/text.ts +1 -2
  89. package/src/renderer/fonts/built-in.ts +1 -1
  90. package/src/renderer/fonts/index.ts +1 -3
  91. package/src/renderer/fonts/loader.ts +1 -2
  92. package/src/renderer/fonts/registry.ts +2 -2
  93. package/src/renderer/renderer.ts +1 -69
  94. package/src/resource/utils/ref.ts +1 -1
  95. package/src/runtime/Infographic.tsx +30 -2
  96. package/src/types/index.ts +1 -0
  97. package/src/{renderer/fonts/utils.ts → utils/font.ts} +1 -1
  98. package/src/utils/index.ts +2 -0
  99. package/src/utils/is-node.ts +8 -0
  100. package/src/utils/padding.ts +79 -0
  101. package/src/utils/svg.ts +5 -19
  102. package/src/utils/text.ts +2 -2
  103. package/src/utils/viewbox.ts +12 -0
  104. /package/esm/{renderer/fonts → exporter}/types.js +0 -0
  105. /package/esm/{renderer/fonts/types.d.ts → types/font.d.ts} +0 -0
  106. /package/esm/{renderer/fonts/utils.js → utils/font.js} +0 -0
  107. /package/lib/{renderer/fonts → exporter}/types.js +0 -0
  108. /package/lib/{renderer/fonts/types.d.ts → types/font.d.ts} +0 -0
  109. /package/lib/{renderer/fonts/utils.js → utils/font.js} +0 -0
  110. /package/src/{renderer/fonts/types.ts → types/font.ts} +0 -0
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.exportToSVGString = exportToSVGString;
4
+ exports.exportToSVG = exportToSVG;
5
+ const utils_1 = require("../utils");
6
+ const font_1 = require("./font");
7
+ async function exportToSVGString(svg, options = {}) {
8
+ const node = await exportToSVG(svg, options);
9
+ const str = new XMLSerializer().serializeToString(node);
10
+ return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(str);
11
+ }
12
+ async function exportToSVG(svg, options = {}) {
13
+ const { embedResources = true } = options;
14
+ const clonedSVG = svg.cloneNode(true);
15
+ const { width, height } = (0, utils_1.getViewBox)(svg);
16
+ (0, utils_1.setAttributes)(clonedSVG, { width, height });
17
+ await embedIcons(clonedSVG);
18
+ await (0, font_1.embedFonts)(clonedSVG, embedResources);
19
+ cleanSVG(clonedSVG);
20
+ return clonedSVG;
21
+ }
22
+ async function embedIcons(svg) {
23
+ const icons = svg.querySelectorAll('use');
24
+ const defs = getDefs(svg);
25
+ icons.forEach((icon) => {
26
+ const href = icon.getAttribute('href');
27
+ if (!href)
28
+ return;
29
+ const existsSymbol = svg.querySelector(href);
30
+ if (!existsSymbol) {
31
+ const symbolElement = document.querySelector(href);
32
+ if (symbolElement)
33
+ defs.appendChild(symbolElement.cloneNode(true));
34
+ }
35
+ });
36
+ }
37
+ function getDefs(svg) {
38
+ const defs = svg.querySelector('defs[id="icon-defs"]');
39
+ if (defs)
40
+ return defs;
41
+ const _defs = (0, utils_1.createElement)('defs', { id: 'icon-defs' });
42
+ svg.prepend(_defs);
43
+ return _defs;
44
+ }
45
+ function cleanSVG(svg) {
46
+ removeBtnGroup(svg);
47
+ removeTransientContainer(svg);
48
+ removeUselessAttrs(svg);
49
+ clearDataset(svg);
50
+ }
51
+ function removeBtnGroup(svg) {
52
+ const btnGroup = svg.querySelector('[data-element-type=btns-group]');
53
+ btnGroup?.remove();
54
+ const btnIconDefs = svg.querySelector('#btn-icon-defs');
55
+ btnIconDefs?.remove();
56
+ }
57
+ function removeTransientContainer(svg) {
58
+ const transientContainer = svg.querySelector('[data-element-type=transient-container]');
59
+ transientContainer?.remove();
60
+ }
61
+ function removeUselessAttrs(svg) {
62
+ const groups = svg.querySelectorAll('g');
63
+ groups.forEach((group) => {
64
+ group.removeAttribute('x');
65
+ group.removeAttribute('y');
66
+ group.removeAttribute('width');
67
+ group.removeAttribute('height');
68
+ });
69
+ }
70
+ function clearDataset(svg) {
71
+ (0, utils_1.traverse)(svg, (node) => {
72
+ for (const key in node.dataset) {
73
+ delete node.dataset[key];
74
+ }
75
+ });
76
+ }
@@ -0,0 +1,17 @@
1
+ export interface SVGExportOptions {
2
+ type: 'svg';
3
+ /**
4
+ * 是否将远程资源嵌入到 SVG 中
5
+ * @default true
6
+ */
7
+ embedResources?: boolean;
8
+ }
9
+ export interface PNGExportOptions {
10
+ type: 'png';
11
+ /**
12
+ * 设备像素比,默认为浏览器的 devicePixelRatio
13
+ * @default globalThis.devicePixelRatio || 2
14
+ */
15
+ dpr?: number;
16
+ }
17
+ export type ExportOptions = SVGExportOptions | PNGExportOptions;
package/lib/index.d.ts CHANGED
@@ -10,7 +10,7 @@ export { getTheme, getThemes, registerTheme } from './themes';
10
10
  export { parseSVG } from './utils';
11
11
  export type { Bounds, ComponentType, DefsProps, EllipseProps, FragmentProps, GroupProps, JSXElement, JSXElementConstructor, JSXNode, PathProps, Point, PolygonProps, RectProps, RenderableNode, SVGAttributes, SVGProps, TextProps, WithChildren, } from './jsx';
12
12
  export type { InfographicOptions, ParsedInfographicOptions } from './options';
13
- export type { Font, FontWeightName, GradientConfig, IRenderer, LinearGradient, Palette, PatternConfig, PatternGenerator, PatternStyle, RadialGradient, RoughConfig, StylizeConfig, TextAlignment, TextHorizontalAlign, TextVerticalAlign, } from './renderer';
13
+ export type { GradientConfig, IRenderer, LinearGradient, Palette, PatternConfig, PatternGenerator, PatternStyle, RadialGradient, RoughConfig, StylizeConfig, TextAlignment, TextHorizontalAlign, TextVerticalAlign, } from './renderer';
14
14
  export type { ParsedTemplateOptions, TemplateOptions } from './templates';
15
15
  export type { ThemeColors, ThemeConfig, ThemeSeed } from './themes';
16
- export type { Data, ImageResource, ItemDatum } from './types';
16
+ export type { Data, Font, FontWeightName, ImageResource, ItemDatum, } from './types';
@@ -5,7 +5,6 @@ exports.renderItemText = renderItemText;
5
5
  exports.renderStaticText = renderStaticText;
6
6
  const lodash_es_1 = require("lodash-es");
7
7
  const utils_1 = require("../../utils");
8
- const fonts_1 = require("../fonts");
9
8
  const utils_2 = require("../utils");
10
9
  function renderText(node, text, attrs = {}) {
11
10
  if (!text)
@@ -17,7 +16,6 @@ function renderText(node, text, attrs = {}) {
17
16
  for (const key in textElement.dataset) {
18
17
  renderedText.setAttribute(`data-${(0, lodash_es_1.kebabCase)(key)}`, textElement.dataset[key]);
19
18
  }
20
- renderedText.setAttribute('id', node.getAttribute('id'));
21
19
  return renderedText;
22
20
  }
23
21
  function renderItemText(type, node, options) {
@@ -35,7 +33,7 @@ function renderStaticText(text, options) {
35
33
  const attrs = options.themeConfig.base?.text || {};
36
34
  (0, utils_1.setAttributes)(text, attrs);
37
35
  if (attrs['font-family']) {
38
- text.setAttribute('font-family', (0, fonts_1.encodeFontFamily)(attrs['font-family']));
36
+ text.setAttribute('font-family', (0, utils_1.encodeFontFamily)(attrs['font-family']));
39
37
  }
40
38
  text.style.userSelect = 'none';
41
39
  text.style.pointerEvents = 'none';
@@ -1,2 +1,2 @@
1
- import { Font } from './types';
1
+ import type { Font } from '../../types';
2
2
  export declare const BUILT_IN_FONTS: Font[];
@@ -1,4 +1,2 @@
1
1
  export { getFontURLs, getWoff2BaseURL, loadFont, loadFonts } from './loader';
2
2
  export { DEFAULT_FONT, getFont, getFonts, registerFont, setDefaultFont, } from './registry';
3
- export type * from './types';
4
- export { decodeFontFamily, encodeFontFamily } from './utils';
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.encodeFontFamily = exports.decodeFontFamily = exports.setDefaultFont = exports.registerFont = exports.getFonts = exports.getFont = exports.DEFAULT_FONT = exports.loadFonts = exports.loadFont = exports.getWoff2BaseURL = exports.getFontURLs = void 0;
3
+ exports.setDefaultFont = exports.registerFont = exports.getFonts = exports.getFont = exports.DEFAULT_FONT = exports.loadFonts = exports.loadFont = exports.getWoff2BaseURL = exports.getFontURLs = void 0;
4
4
  var loader_1 = require("./loader");
5
5
  Object.defineProperty(exports, "getFontURLs", { enumerable: true, get: function () { return loader_1.getFontURLs; } });
6
6
  Object.defineProperty(exports, "getWoff2BaseURL", { enumerable: true, get: function () { return loader_1.getWoff2BaseURL; } });
@@ -12,9 +12,6 @@ Object.defineProperty(exports, "getFont", { enumerable: true, get: function () {
12
12
  Object.defineProperty(exports, "getFonts", { enumerable: true, get: function () { return registry_1.getFonts; } });
13
13
  Object.defineProperty(exports, "registerFont", { enumerable: true, get: function () { return registry_1.registerFont; } });
14
14
  Object.defineProperty(exports, "setDefaultFont", { enumerable: true, get: function () { return registry_1.setDefaultFont; } });
15
- var utils_1 = require("./utils");
16
- Object.defineProperty(exports, "decodeFontFamily", { enumerable: true, get: function () { return utils_1.decodeFontFamily; } });
17
- Object.defineProperty(exports, "encodeFontFamily", { enumerable: true, get: function () { return utils_1.encodeFontFamily; } });
18
15
  const built_in_1 = require("./built-in");
19
16
  const registry_2 = require("./registry");
20
17
  built_in_1.BUILT_IN_FONTS.forEach(registry_2.registerFont);
@@ -6,7 +6,6 @@ exports.loadFont = loadFont;
6
6
  exports.loadFonts = loadFonts;
7
7
  const utils_1 = require("../../utils");
8
8
  const registry_1 = require("./registry");
9
- const utils_2 = require("./utils");
10
9
  function getFontURLs(font) {
11
10
  const config = (0, registry_1.getFont)(font);
12
11
  if (!config)
@@ -20,7 +19,7 @@ function getWoff2BaseURL(font, fontWeightName) {
20
19
  const config = (0, registry_1.getFont)(font);
21
20
  if (!config)
22
21
  return null;
23
- const name = (0, utils_2.normalizeFontWeightName)(fontWeightName);
22
+ const name = (0, utils_1.normalizeFontWeightName)(fontWeightName);
24
23
  const path = config.fontWeight[name];
25
24
  if (!path)
26
25
  return null;
@@ -1,4 +1,4 @@
1
- import type { Font } from './types';
1
+ import type { Font } from '../../types';
2
2
  export declare let DEFAULT_FONT: string;
3
3
  export declare function getFont(font: string): Font | null;
4
4
  export declare function getFonts(): Font[];
@@ -6,7 +6,7 @@ exports.getFonts = getFonts;
6
6
  exports.setDefaultFont = setDefaultFont;
7
7
  exports.registerFont = registerFont;
8
8
  exports.unregisterFont = unregisterFont;
9
- const utils_1 = require("./utils");
9
+ const utils_1 = require("../../utils");
10
10
  const FONT_REGISTRY = new Map();
11
11
  exports.DEFAULT_FONT = 'Alibaba PuHuiTi';
12
12
  function getFont(font) {
@@ -135,55 +135,6 @@ function setView(svg, options) {
135
135
  svg.setAttribute('viewBox', viewBox);
136
136
  }
137
137
  else if (padding !== undefined) {
138
- setSVGPadding(svg, (0, utils_1.parsePadding)(padding));
139
- }
140
- }
141
- function setSVGPadding(svg, padding, options = {}) {
142
- const { preserveAspectRatio = false } = options;
143
- if (!svg.isConnected)
144
- return false;
145
- try {
146
- const bbox = svg.getBBox();
147
- // 检查包围盒是否有效
148
- if (bbox.width === 0 || bbox.height === 0) {
149
- return false;
150
- }
151
- const [widthBaseVal, heightBaseVal] = (0, utils_1.getSizeBaseVal)(svg);
152
- const svgWidth = widthBaseVal || svg.clientWidth || 0;
153
- const svgHeight = heightBaseVal || svg.clientHeight || 0;
154
- const parentElement = svg.parentElement;
155
- const effectiveWidth = svgWidth || (parentElement ? parentElement.clientWidth : 300);
156
- const effectiveHeight = svgHeight || (parentElement ? parentElement.clientHeight : 150);
157
- let viewBoxPadding;
158
- if (effectiveWidth > 0 && effectiveHeight > 0) {
159
- const scaleX = bbox.width / effectiveWidth;
160
- const scaleY = bbox.height / effectiveHeight;
161
- if (preserveAspectRatio) {
162
- const scale = Math.max(scaleX, scaleY);
163
- viewBoxPadding = padding.map((p) => p * scale);
164
- }
165
- else {
166
- viewBoxPadding = [
167
- padding[0] * scaleY,
168
- padding[1] * scaleX,
169
- padding[2] * scaleY,
170
- padding[3] * scaleX,
171
- ];
172
- }
173
- }
174
- else {
175
- viewBoxPadding = [...padding];
176
- }
177
- const newViewBox = [
178
- bbox.x - viewBoxPadding[3],
179
- bbox.y - viewBoxPadding[0],
180
- bbox.width + viewBoxPadding[1] + viewBoxPadding[3],
181
- bbox.height + viewBoxPadding[0] + viewBoxPadding[2],
182
- ].join(' ');
183
- svg.setAttribute('viewBox', newViewBox);
184
- return true;
185
- }
186
- catch {
187
- return false;
138
+ (0, utils_1.setSVGPadding)(svg, (0, utils_1.parsePadding)(padding));
188
139
  }
189
140
  }
@@ -8,7 +8,7 @@ function getResourceId(config) {
8
8
  const cfg = typeof config === 'string' ? (0, data_uri_1.parseDataURI)(config) : config;
9
9
  if (!cfg)
10
10
  return null;
11
- return (0, hash_1.getSimpleHash)(JSON.stringify(cfg));
11
+ return 'rsc-' + (0, hash_1.getSimpleHash)(JSON.stringify(cfg));
12
12
  }
13
13
  function getResourceHref(config) {
14
14
  const id = getResourceId(config);
@@ -1,6 +1,8 @@
1
+ import { ExportOptions } from '../exporter';
1
2
  import { InfographicOptions } from '../options';
2
3
  export declare class Infographic {
3
4
  private options;
5
+ private node;
4
6
  private parsedOptions;
5
7
  constructor(options: InfographicOptions);
6
8
  /**
@@ -12,4 +14,12 @@ export declare class Infographic {
12
14
  */
13
15
  compose(): SVGSVGElement;
14
16
  getTypes(): string;
17
+ /**
18
+ * Export the infographic to data URL
19
+ * @param options Export option
20
+ * @returns Data URL string of the exported infographic
21
+ * @description This method need to be called after `render()` and in a browser environment.
22
+ */
23
+ toDataURL(options?: ExportOptions): Promise<string>;
24
+ destroy(): void;
15
25
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Infographic = void 0;
4
4
  const jsx_runtime_1 = require("@antv/infographic/jsx-runtime");
5
+ const exporter_1 = require("../exporter");
5
6
  const jsx_1 = require("../jsx");
6
7
  const options_1 = require("../options");
7
8
  const renderer_1 = require("../renderer");
@@ -9,6 +10,7 @@ const utils_1 = require("../utils");
9
10
  class Infographic {
10
11
  constructor(options) {
11
12
  this.options = options;
13
+ this.node = null;
12
14
  this.parsedOptions = (0, options_1.parseOptions)(this.options);
13
15
  }
14
16
  /**
@@ -18,8 +20,8 @@ class Infographic {
18
20
  const { container } = this.parsedOptions;
19
21
  const template = this.compose();
20
22
  const renderer = new renderer_1.Renderer(this.parsedOptions, template);
21
- const infographic = renderer.render();
22
- container.replaceChildren(infographic);
23
+ this.node = renderer.render();
24
+ container.replaceChildren(this.node);
23
25
  }
24
26
  /**
25
27
  * Compose the SVG template
@@ -44,5 +46,24 @@ class Infographic {
44
46
  const items = design.items.map((it) => it.composites || []);
45
47
  return (0, utils_1.getTypes)({ structure, items });
46
48
  }
49
+ /**
50
+ * Export the infographic to data URL
51
+ * @param options Export option
52
+ * @returns Data URL string of the exported infographic
53
+ * @description This method need to be called after `render()` and in a browser environment.
54
+ */
55
+ async toDataURL(options) {
56
+ if (!this.node) {
57
+ throw new Error('Infographic is not rendered yet.');
58
+ }
59
+ if (options?.type === 'svg') {
60
+ return await (0, exporter_1.exportToSVGString)(this.node, options);
61
+ }
62
+ return await (0, exporter_1.exportToPNGString)(this.node, options);
63
+ }
64
+ destroy() {
65
+ this.node?.remove();
66
+ this.node = null;
67
+ }
47
68
  }
48
69
  exports.Infographic = Infographic;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,5 +1,6 @@
1
1
  export type * from './attrs';
2
2
  export type * from './data';
3
3
  export type * from './element';
4
+ export type * from './font';
4
5
  export type * from './padding';
5
6
  export type * from './resource';
@@ -1,4 +1,4 @@
1
- import type { FontWeightName } from './types';
1
+ import type { FontWeightName } from '../types';
2
2
  export declare function decodeFontFamily(font: string): string;
3
3
  export declare function encodeFontFamily(font: string): string;
4
4
  export declare function normalizeFontWeightName(fontWeight: string | number): FontWeightName;
@@ -1,5 +1,6 @@
1
1
  export * from './color';
2
2
  export * from './data';
3
+ export * from './font';
3
4
  export * from './get-types';
4
5
  export * from './icon';
5
6
  export * from './item';
@@ -9,3 +10,4 @@ export * from './recognizer';
9
10
  export * from './svg';
10
11
  export * from './text';
11
12
  export * from './uuid';
13
+ export * from './viewbox';
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./color"), exports);
18
18
  __exportStar(require("./data"), exports);
19
+ __exportStar(require("./font"), exports);
19
20
  __exportStar(require("./get-types"), exports);
20
21
  __exportStar(require("./icon"), exports);
21
22
  __exportStar(require("./item"), exports);
@@ -25,3 +26,4 @@ __exportStar(require("./recognizer"), exports);
25
26
  __exportStar(require("./svg"), exports);
26
27
  __exportStar(require("./text"), exports);
27
28
  __exportStar(require("./uuid"), exports);
29
+ __exportStar(require("./viewbox"), exports);
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @see https://github.com/MatthewSH/npm-packages/blob/main/packages/is-node/src/index.ts
3
+ */
4
+ export declare const isNode: boolean;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isNode = void 0;
4
+ /**
5
+ * @see https://github.com/MatthewSH/npm-packages/blob/main/packages/is-node/src/index.ts
6
+ */
7
+ exports.isNode = !!(typeof process !== 'undefined' &&
8
+ process.versions &&
9
+ process.versions.node);
@@ -1,2 +1,3 @@
1
1
  import type { Padding, ParsedPadding } from '../types';
2
2
  export declare function parsePadding(padding: Padding | undefined): ParsedPadding;
3
+ export declare function setSVGPadding(svg: SVGSVGElement, padding: ParsedPadding): void;
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parsePadding = parsePadding;
4
+ exports.setSVGPadding = setSVGPadding;
5
+ const is_node_1 = require("./is-node");
6
+ const svg_1 = require("./svg");
7
+ const viewbox_1 = require("./viewbox");
4
8
  function parsePadding(padding) {
5
9
  if (!padding)
6
10
  return [0, 0, 0, 0];
@@ -21,3 +25,70 @@ function parsePadding(padding) {
21
25
  }
22
26
  return [0, 0, 0, 0];
23
27
  }
28
+ function setSVGPadding(svg, padding) {
29
+ if (is_node_1.isNode) {
30
+ setSVGPaddingInNode(svg, padding);
31
+ }
32
+ else {
33
+ if (document.contains(svg)) {
34
+ setSVGPaddingInBrowser(svg, padding);
35
+ }
36
+ else {
37
+ const observer = new MutationObserver((mutations) => {
38
+ mutations.forEach((mutation) => {
39
+ mutation.addedNodes.forEach((node) => {
40
+ if (node === svg || node.contains(svg)) {
41
+ waitForLayout(svg, () => {
42
+ setSVGPaddingInBrowser(svg, padding);
43
+ });
44
+ observer.disconnect();
45
+ }
46
+ });
47
+ });
48
+ });
49
+ observer.observe(document, {
50
+ childList: true,
51
+ subtree: true,
52
+ });
53
+ }
54
+ }
55
+ }
56
+ function setSVGPaddingInNode(svg, padding) {
57
+ const viewBox = (0, viewbox_1.getViewBox)(svg);
58
+ const [top, right, bottom, left] = padding;
59
+ (0, svg_1.setAttributes)(svg, {
60
+ viewBox: `${viewBox.x - left} ${viewBox.y - top} ${viewBox.width + left + right} ${viewBox.height + top + bottom}`,
61
+ });
62
+ }
63
+ function setSVGPaddingInBrowser(svg, padding) {
64
+ const bbox = svg.getBBox();
65
+ const clientRect = svg.getBoundingClientRect();
66
+ const scaleX = clientRect.width > 0 ? bbox.width / clientRect.width : 1;
67
+ const scaleY = clientRect.height > 0 ? bbox.height / clientRect.height : 1;
68
+ const [topPx, rightPx, bottomPx, leftPx] = padding;
69
+ const topSvg = topPx * scaleY;
70
+ const rightSvg = rightPx * scaleX;
71
+ const bottomSvg = bottomPx * scaleY;
72
+ const leftSvg = leftPx * scaleX;
73
+ const newX = bbox.x - leftSvg;
74
+ const newY = bbox.y - topSvg;
75
+ const newWidth = bbox.width + leftSvg + rightSvg;
76
+ const newHeight = bbox.height + topSvg + bottomSvg;
77
+ (0, svg_1.setAttributes)(svg, {
78
+ viewBox: `${newX} ${newY} ${newWidth} ${newHeight}`,
79
+ });
80
+ }
81
+ function waitForLayout(element, callback) {
82
+ requestAnimationFrame(() => {
83
+ const check = () => {
84
+ const rect = element.getBoundingClientRect();
85
+ if (rect.width > 0 && rect.height > 0) {
86
+ callback();
87
+ }
88
+ else {
89
+ requestAnimationFrame(check);
90
+ }
91
+ };
92
+ check();
93
+ });
94
+ }
@@ -5,4 +5,3 @@ export declare function getAttributes(element: SVGElement, attrs: readonly strin
5
5
  export declare function removeAttributes(element: SVGElement, attrs: string[]): void;
6
6
  export declare function traverse(node: SVGElement, callback: <T extends SVGElement = SVGElement>(child: T) => boolean | void): void;
7
7
  export declare function getOrCreateDefs(svg: SVGSVGElement, defsId?: string): SVGDefsElement;
8
- export declare function getSizeBaseVal(svg: SVGSVGElement): [number, number];
package/lib/utils/svg.js CHANGED
@@ -7,7 +7,6 @@ exports.getAttributes = getAttributes;
7
7
  exports.removeAttributes = removeAttributes;
8
8
  exports.traverse = traverse;
9
9
  exports.getOrCreateDefs = getOrCreateDefs;
10
- exports.getSizeBaseVal = getSizeBaseVal;
11
10
  function createElement(tagName, attributes = {}) {
12
11
  const element = document.createElementNS('http://www.w3.org/2000/svg', tagName);
13
12
  setAttributes(element, attributes);
@@ -24,7 +23,7 @@ function parseSVG(svg) {
24
23
  }
25
24
  function setAttributes(element, attrs) {
26
25
  Object.entries(attrs).forEach(([key, value]) => {
27
- if (value === null) {
26
+ if (value === null || value === undefined) {
28
27
  element.removeAttribute(key);
29
28
  }
30
29
  else {
@@ -35,7 +34,8 @@ function setAttributes(element, attrs) {
35
34
  function getAttributes(element, attrs, ignoreEmpty = true) {
36
35
  return attrs.reduce((acc, attr) => {
37
36
  const value = element.getAttribute(attr);
38
- if (!ignoreEmpty || (value !== null && value !== '')) {
37
+ if (!ignoreEmpty ||
38
+ (value !== null && value !== '' && value !== undefined)) {
39
39
  acc[attr] = value;
40
40
  }
41
41
  return acc;
@@ -65,19 +65,3 @@ function getOrCreateDefs(svg, defsId = 'infographic-defs') {
65
65
  svg.prepend(newDefs);
66
66
  return newDefs;
67
67
  }
68
- function getSizeBaseVal(svg) {
69
- try {
70
- const width = svg.width.baseVal.value || 0;
71
- const height = svg.height.baseVal.value || 0;
72
- return [width, height];
73
- }
74
- catch (error) {
75
- const viewBox = svg.getAttribute('viewBox');
76
- if (viewBox) {
77
- const [x, y, width, height] = viewBox.split(' ').map(Number);
78
- return [width - x, height - y];
79
- }
80
- console.error('Error getting size baseVal from SVG:', error);
81
- return [0, 0];
82
- }
83
- }
package/lib/utils/text.js CHANGED
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.createTextElement = createTextElement;
7
7
  const camelCase_1 = __importDefault(require("lodash-es/camelCase"));
8
- const renderer_1 = require("../renderer");
8
+ const font_1 = require("./font");
9
9
  const svg_1 = require("./svg");
10
10
  function createTextElement(textContent, attributes) {
11
11
  const span = document.createElement('span');
@@ -97,7 +97,7 @@ function getTextStyle(attributes) {
97
97
  if (strokeWidth)
98
98
  style.strokeWidth = `${strokeWidth}px`;
99
99
  if (fontFamily)
100
- style.fontFamily = (0, renderer_1.encodeFontFamily)(fontFamily);
100
+ style.fontFamily = (0, font_1.encodeFontFamily)(fontFamily);
101
101
  return style;
102
102
  }
103
103
  function measureTextSpan(span) {
@@ -0,0 +1,6 @@
1
+ export declare function getViewBox(svg: SVGSVGElement): {
2
+ x: number;
3
+ y: number;
4
+ width: number;
5
+ height: number;
6
+ };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getViewBox = getViewBox;
4
+ function getViewBox(svg) {
5
+ const viewBox = svg.getAttribute('viewBox');
6
+ if (viewBox) {
7
+ const [x, y, width, height] = viewBox.split(' ').map(Number);
8
+ return { x, y, width, height };
9
+ }
10
+ const widthStr = svg.getAttribute('width');
11
+ const heightStr = svg.getAttribute('height');
12
+ const width = Number(widthStr) || 0;
13
+ const height = Number(heightStr) || 0;
14
+ return { x: 0, y: 0, width, height };
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antv/infographic",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "An Infographic Generation and Rendering Framework, bring words to life!",
5
5
  "keywords": [
6
6
  "antv",
@@ -74,6 +74,7 @@
74
74
  "singleQuote": true
75
75
  },
76
76
  "dependencies": {
77
+ "css": "^3.0.0",
77
78
  "culori": "^4.0.2",
78
79
  "d3": "^7.9.0",
79
80
  "lodash-es": "^4.17.21",
@@ -89,6 +90,7 @@
89
90
  "@rollup/plugin-terser": "^0.4.4",
90
91
  "@rollup/plugin-typescript": "^12.3.0",
91
92
  "@size-limit/file": "^11.2.0",
93
+ "@types/css": "^0.0.38",
92
94
  "@types/culori": "^4.0.1",
93
95
  "@types/d3": "^7.4.3",
94
96
  "@types/lodash-es": "^4.17.12",
@@ -133,5 +135,6 @@
133
135
  "limit": "500 Kb",
134
136
  "path": "dist/infographic.min.js"
135
137
  }
136
- ]
138
+ ],
139
+ "repository": "git@github.com:antvis/Infographic.git"
137
140
  }
@@ -5,6 +5,12 @@ export interface BtnsGroupProps extends GroupProps {}
5
5
 
6
6
  export const BtnsGroup = (props: BtnsGroupProps) => {
7
7
  return (
8
- <Group data-element-type="btns-group" width={0} height={0} {...props} />
8
+ <Group
9
+ data-element-type="btns-group"
10
+ width={0}
11
+ height={0}
12
+ {...props}
13
+ display="none"
14
+ />
9
15
  );
10
16
  };