@antv/infographic 0.2.16 → 0.2.18

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 (131) hide show
  1. package/README.md +1 -1
  2. package/README.zh-CN.md +1 -1
  3. package/dist/infographic.min.js +121 -120
  4. package/dist/infographic.min.js.map +1 -1
  5. package/esm/constants/service.d.ts +1 -1
  6. package/esm/constants/service.js +1 -1
  7. package/esm/designs/structures/chart-line.js +7 -4
  8. package/esm/editor/interactions/dblclick-edit-text.js +3 -3
  9. package/esm/editor/managers/interaction.js +6 -4
  10. package/esm/editor/plugins/components/button.d.ts +2 -1
  11. package/esm/editor/plugins/components/button.js +4 -4
  12. package/esm/editor/plugins/components/color-picker.d.ts +1 -0
  13. package/esm/editor/plugins/components/color-picker.js +3 -3
  14. package/esm/editor/plugins/components/popover.d.ts +3 -1
  15. package/esm/editor/plugins/components/popover.js +29 -9
  16. package/esm/editor/plugins/edit-bar/edit-bar.d.ts +3 -1
  17. package/esm/editor/plugins/edit-bar/edit-bar.js +17 -7
  18. package/esm/editor/plugins/edit-bar/edit-items/align-elements.js +6 -4
  19. package/esm/editor/plugins/edit-bar/edit-items/font-align.js +8 -5
  20. package/esm/editor/plugins/edit-bar/edit-items/font-color.js +7 -4
  21. package/esm/editor/plugins/edit-bar/edit-items/font-family.js +11 -9
  22. package/esm/editor/plugins/edit-bar/edit-items/font-size.js +8 -5
  23. package/esm/editor/plugins/edit-bar/edit-items/icon-color.js +7 -4
  24. package/esm/editor/plugins/edit-bar/edit-items/types.d.ts +5 -1
  25. package/esm/editor/plugins/reset-viewbox.d.ts +4 -1
  26. package/esm/editor/plugins/reset-viewbox.js +12 -6
  27. package/esm/editor/utils/index.d.ts +1 -0
  28. package/esm/editor/utils/index.js +1 -0
  29. package/esm/editor/utils/root.d.ts +3 -0
  30. package/esm/editor/utils/root.js +18 -0
  31. package/esm/exporter/svg.js +229 -3
  32. package/esm/options/parser.js +8 -6
  33. package/esm/options/types.d.ts +3 -3
  34. package/esm/renderer/renderer.js +1 -1
  35. package/esm/resource/loaders/search.js +2 -6
  36. package/esm/runtime/options.js +1 -1
  37. package/esm/syntax/index.js +56 -10
  38. package/esm/syntax/mapper.js +20 -6
  39. package/esm/syntax/parser.js +9 -0
  40. package/esm/syntax/types.d.ts +1 -1
  41. package/esm/templates/registry.d.ts +1 -0
  42. package/esm/templates/registry.js +6 -0
  43. package/esm/templates/utils.d.ts +1 -0
  44. package/esm/templates/utils.js +68 -0
  45. package/esm/themes/built-in.js +3 -0
  46. package/esm/utils/padding.js +1 -1
  47. package/esm/utils/style.d.ts +3 -1
  48. package/esm/utils/style.js +27 -4
  49. package/esm/version.d.ts +1 -1
  50. package/esm/version.js +1 -1
  51. package/lib/constants/service.d.ts +1 -1
  52. package/lib/constants/service.js +1 -1
  53. package/lib/designs/structures/chart-line.js +7 -4
  54. package/lib/editor/interactions/dblclick-edit-text.js +3 -3
  55. package/lib/editor/managers/interaction.js +7 -5
  56. package/lib/editor/plugins/components/button.d.ts +2 -1
  57. package/lib/editor/plugins/components/button.js +4 -4
  58. package/lib/editor/plugins/components/color-picker.d.ts +1 -0
  59. package/lib/editor/plugins/components/color-picker.js +3 -3
  60. package/lib/editor/plugins/components/popover.d.ts +3 -1
  61. package/lib/editor/plugins/components/popover.js +32 -12
  62. package/lib/editor/plugins/edit-bar/edit-bar.d.ts +3 -1
  63. package/lib/editor/plugins/edit-bar/edit-bar.js +17 -7
  64. package/lib/editor/plugins/edit-bar/edit-items/align-elements.js +6 -4
  65. package/lib/editor/plugins/edit-bar/edit-items/font-align.js +8 -5
  66. package/lib/editor/plugins/edit-bar/edit-items/font-color.js +7 -4
  67. package/lib/editor/plugins/edit-bar/edit-items/font-family.js +11 -9
  68. package/lib/editor/plugins/edit-bar/edit-items/font-size.js +8 -5
  69. package/lib/editor/plugins/edit-bar/edit-items/icon-color.js +7 -4
  70. package/lib/editor/plugins/edit-bar/edit-items/types.d.ts +5 -1
  71. package/lib/editor/plugins/reset-viewbox.d.ts +4 -1
  72. package/lib/editor/plugins/reset-viewbox.js +12 -6
  73. package/lib/editor/utils/index.d.ts +1 -0
  74. package/lib/editor/utils/index.js +1 -0
  75. package/lib/editor/utils/root.d.ts +3 -0
  76. package/lib/editor/utils/root.js +22 -0
  77. package/lib/exporter/svg.js +229 -3
  78. package/lib/options/parser.js +7 -5
  79. package/lib/options/types.d.ts +3 -3
  80. package/lib/renderer/renderer.js +1 -1
  81. package/lib/resource/loaders/search.js +2 -6
  82. package/lib/runtime/options.js +1 -1
  83. package/lib/syntax/index.js +56 -10
  84. package/lib/syntax/mapper.js +20 -6
  85. package/lib/syntax/parser.js +9 -0
  86. package/lib/syntax/types.d.ts +1 -1
  87. package/lib/templates/registry.d.ts +1 -0
  88. package/lib/templates/registry.js +7 -0
  89. package/lib/templates/utils.d.ts +1 -0
  90. package/lib/templates/utils.js +71 -0
  91. package/lib/themes/built-in.js +3 -0
  92. package/lib/utils/padding.js +1 -1
  93. package/lib/utils/style.d.ts +3 -1
  94. package/lib/utils/style.js +27 -4
  95. package/lib/version.d.ts +1 -1
  96. package/lib/version.js +1 -1
  97. package/package.json +1 -1
  98. package/src/constants/service.ts +1 -1
  99. package/src/designs/structures/chart-line.tsx +8 -4
  100. package/src/editor/interactions/dblclick-edit-text.ts +3 -2
  101. package/src/editor/managers/interaction.ts +9 -7
  102. package/src/editor/plugins/components/button.ts +5 -2
  103. package/src/editor/plugins/components/color-picker.ts +4 -2
  104. package/src/editor/plugins/components/popover.ts +31 -12
  105. package/src/editor/plugins/edit-bar/edit-bar.ts +26 -11
  106. package/src/editor/plugins/edit-bar/edit-items/align-elements.ts +7 -2
  107. package/src/editor/plugins/edit-bar/edit-items/font-align.ts +8 -3
  108. package/src/editor/plugins/edit-bar/edit-items/font-color.ts +7 -2
  109. package/src/editor/plugins/edit-bar/edit-items/font-family.ts +11 -7
  110. package/src/editor/plugins/edit-bar/edit-items/font-size.ts +8 -3
  111. package/src/editor/plugins/edit-bar/edit-items/icon-color.ts +7 -2
  112. package/src/editor/plugins/edit-bar/edit-items/types.ts +6 -1
  113. package/src/editor/plugins/reset-viewbox.ts +17 -8
  114. package/src/editor/utils/index.ts +1 -0
  115. package/src/editor/utils/root.ts +26 -0
  116. package/src/exporter/svg.ts +274 -3
  117. package/src/options/parser.ts +7 -6
  118. package/src/options/types.ts +3 -3
  119. package/src/renderer/renderer.ts +1 -1
  120. package/src/resource/loaders/search.ts +2 -5
  121. package/src/runtime/options.ts +1 -1
  122. package/src/syntax/index.ts +71 -10
  123. package/src/syntax/mapper.ts +20 -6
  124. package/src/syntax/parser.ts +10 -0
  125. package/src/syntax/types.ts +1 -0
  126. package/src/templates/registry.ts +6 -0
  127. package/src/templates/utils.ts +111 -0
  128. package/src/themes/built-in.ts +4 -0
  129. package/src/utils/padding.ts +1 -1
  130. package/src/utils/style.ts +31 -4
  131. package/src/version.ts +1 -1
@@ -23,6 +23,13 @@ function getIndentInfo(line) {
23
23
  function stripComments(content) {
24
24
  return content.trimEnd();
25
25
  }
26
+ function isCommentLine(content) {
27
+ const trimmed = content.trimStart();
28
+ return trimmed.startsWith('#') || trimmed.startsWith('//');
29
+ }
30
+ function isCodeFenceLine(content) {
31
+ return /^```[\w-]*\s*$/.test(content.trim());
32
+ }
26
33
  function looksLikeRelationExpression(text) {
27
34
  return /[<>=o.x-]{2,}/.test(text);
28
35
  }
@@ -133,6 +140,8 @@ export function parseSyntaxToAst(input) {
133
140
  if (!line.trim())
134
141
  return;
135
142
  const { indent, content } = getIndentInfo(line);
143
+ if (isCommentLine(content) || isCodeFenceLine(content))
144
+ return;
136
145
  const stripped = stripComments(content);
137
146
  if (!stripped.trim())
138
147
  return;
@@ -16,7 +16,7 @@ export interface ArrayNode {
16
16
  line: number;
17
17
  items: SyntaxNode[];
18
18
  }
19
- export type SyntaxErrorCode = 'unknown_key' | 'schema_mismatch' | 'invalid_value' | 'bad_indent' | 'bad_list' | 'bad_syntax';
19
+ export type SyntaxErrorCode = 'implicit_template' | 'unknown_key' | 'schema_mismatch' | 'invalid_value' | 'bad_indent' | 'bad_list' | 'bad_syntax';
20
20
  export interface SyntaxError {
21
21
  path: string;
22
22
  line: number;
@@ -1,4 +1,5 @@
1
1
  import type { TemplateOptions } from './types';
2
2
  export declare function registerTemplate(type: string, template: TemplateOptions): void;
3
+ export declare function resolveTemplateKey(type: string): string | undefined;
3
4
  export declare function getTemplate(type: string): TemplateOptions | undefined;
4
5
  export declare function getTemplates(): string[];
@@ -1,7 +1,13 @@
1
+ import { findClosestTemplateKey } from './utils.js';
1
2
  const TEMPLATE_REGISTRY = new Map();
2
3
  export function registerTemplate(type, template) {
3
4
  TEMPLATE_REGISTRY.set(type, template);
4
5
  }
6
+ export function resolveTemplateKey(type) {
7
+ if (TEMPLATE_REGISTRY.has(type))
8
+ return type;
9
+ return findClosestTemplateKey(type, TEMPLATE_REGISTRY.keys());
10
+ }
5
11
  export function getTemplate(type) {
6
12
  return TEMPLATE_REGISTRY.get(type);
7
13
  }
@@ -0,0 +1 @@
1
+ export declare function findClosestTemplateKey(type: string, keys: Iterable<string>): string | undefined;
@@ -0,0 +1,68 @@
1
+ function normalizeTemplateKey(type) {
2
+ return type
3
+ .trim()
4
+ .toLowerCase()
5
+ .replace(/[_\s]+/g, '-')
6
+ .replace(/-+/g, '-');
7
+ }
8
+ function getLevenshteinDistance(source, target) {
9
+ if (source === target)
10
+ return 0;
11
+ if (!source.length)
12
+ return target.length;
13
+ if (!target.length)
14
+ return source.length;
15
+ const previous = Array.from({ length: target.length + 1 }, (_, index) => index);
16
+ const current = new Array(target.length + 1);
17
+ for (let sourceIndex = 1; sourceIndex <= source.length; sourceIndex += 1) {
18
+ current[0] = sourceIndex;
19
+ const sourceCode = source.charCodeAt(sourceIndex - 1);
20
+ for (let targetIndex = 1; targetIndex <= target.length; targetIndex += 1) {
21
+ const replaceCost = sourceCode === target.charCodeAt(targetIndex - 1) ? 0 : 1;
22
+ current[targetIndex] = Math.min(previous[targetIndex] + 1, current[targetIndex - 1] + 1, previous[targetIndex - 1] + replaceCost);
23
+ }
24
+ for (let index = 0; index < current.length; index += 1) {
25
+ previous[index] = current[index];
26
+ }
27
+ }
28
+ return previous[target.length];
29
+ }
30
+ function getCommonPrefixLength(source, target) {
31
+ const limit = Math.min(source.length, target.length);
32
+ let index = 0;
33
+ while (index < limit &&
34
+ source.charCodeAt(index) === target.charCodeAt(index)) {
35
+ index += 1;
36
+ }
37
+ return index;
38
+ }
39
+ function isBetterMatch(bestMatch, bestDistance, bestPrefixLength, candidateKey, candidateDistance, candidatePrefixLength) {
40
+ return (candidateDistance < bestDistance ||
41
+ (candidateDistance === bestDistance &&
42
+ candidatePrefixLength > bestPrefixLength) ||
43
+ (candidateDistance === bestDistance &&
44
+ candidatePrefixLength === bestPrefixLength &&
45
+ (!bestMatch || candidateKey < bestMatch)));
46
+ }
47
+ export function findClosestTemplateKey(type, keys) {
48
+ const normalizedType = normalizeTemplateKey(type);
49
+ if (!normalizedType)
50
+ return undefined;
51
+ let bestMatch;
52
+ let bestDistance = Number.POSITIVE_INFINITY;
53
+ let bestPrefixLength = -1;
54
+ for (const key of keys) {
55
+ const normalizedKey = normalizeTemplateKey(key);
56
+ if (normalizedKey === normalizedType) {
57
+ return key;
58
+ }
59
+ const distance = getLevenshteinDistance(normalizedType, normalizedKey);
60
+ const prefixLength = getCommonPrefixLength(normalizedType, normalizedKey);
61
+ if (isBetterMatch(bestMatch, bestDistance, bestPrefixLength, key, distance, prefixLength)) {
62
+ bestMatch = key;
63
+ bestDistance = distance;
64
+ bestPrefixLength = prefixLength;
65
+ }
66
+ }
67
+ return bestMatch;
68
+ }
@@ -1,4 +1,7 @@
1
1
  import { registerTheme } from './registry.js';
2
+ registerTheme('light', {
3
+ colorBg: '#ffffff',
4
+ });
2
5
  registerTheme('dark', {
3
6
  colorBg: '#1F1F1F',
4
7
  base: {
@@ -26,7 +26,7 @@ export function setSVGPadding(svg, padding) {
26
26
  setSVGPaddingInNode(svg, padding);
27
27
  }
28
28
  else {
29
- if (document.contains(svg)) {
29
+ if (svg.isConnected) {
30
30
  setSVGPaddingInBrowser(svg, padding);
31
31
  }
32
32
  else {
@@ -1 +1,3 @@
1
- export declare function injectStyleOnce(id: string, styles: string): void;
1
+ type StyleTarget = Document | ShadowRoot | Node | null | undefined;
2
+ export declare function injectStyleOnce(id: string, styles: string, target?: StyleTarget): void;
3
+ export {};
@@ -1,8 +1,31 @@
1
- export function injectStyleOnce(id, styles) {
2
- if (document.getElementById(id))
1
+ function resolveStyleRoot(target) {
2
+ if (!target)
3
+ return document;
4
+ if (target instanceof Document || target instanceof ShadowRoot)
5
+ return target;
6
+ if (!target.isConnected)
7
+ return document;
8
+ const root = target.getRootNode();
9
+ return root instanceof ShadowRoot ? root : document;
10
+ }
11
+ function hasStyle(root, id) {
12
+ if (root instanceof Document)
13
+ return Boolean(root.getElementById(id));
14
+ return Boolean(root.querySelector(`#${id}`));
15
+ }
16
+ export function injectStyleOnce(id, styles, target) {
17
+ var _a;
18
+ const root = resolveStyleRoot(target);
19
+ if (hasStyle(root, id))
3
20
  return;
4
- const style = document.createElement('style');
21
+ const doc = root instanceof Document ? root : ((_a = root.ownerDocument) !== null && _a !== void 0 ? _a : document);
22
+ const style = doc.createElement('style');
5
23
  style.id = id;
6
24
  style.textContent = styles;
7
- document.head.appendChild(style);
25
+ if (root instanceof Document) {
26
+ root.head.appendChild(style);
27
+ }
28
+ else {
29
+ root.appendChild(style);
30
+ }
8
31
  }
package/esm/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.2.16";
1
+ export declare const VERSION = "0.2.18";
package/esm/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.2.16';
1
+ export const VERSION = '0.2.18';
@@ -1 +1 @@
1
- export declare const ICON_SERVICE_URL = "https://www.weavefox.cn/api/open/v1/icon";
1
+ export declare const ICON_SERVICE_URL = "https://www.weavefox.cn/api/v1/infographic/icon";
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ICON_SERVICE_URL = void 0;
4
- exports.ICON_SERVICE_URL = 'https://www.weavefox.cn/api/open/v1/icon';
4
+ exports.ICON_SERVICE_URL = 'https://www.weavefox.cn/api/v1/infographic/icon';
@@ -9,6 +9,8 @@ const components_1 = require("../components");
9
9
  const layouts_1 = require("../layouts");
10
10
  const utils_2 = require("../utils");
11
11
  const registry_1 = require("./registry");
12
+ const ITEM_POSITION_H = 'center';
13
+ const ITEM_POSITION_V = 'normal';
12
14
  const ChartLine = (props) => {
13
15
  const { Title, Item, data, width, height = 260, gap = 10, padding = 24, showValue = true, options, valueFormatter = (value) => value.toString(), } = props;
14
16
  const { title, desc, items = [], xTitle, yTitle } = data;
@@ -21,8 +23,8 @@ const ChartLine = (props) => {
21
23
  indexes: [0],
22
24
  datum: items[0],
23
25
  data,
24
- positionH: 'center',
25
- positionV: 'normal',
26
+ positionH: ITEM_POSITION_H,
27
+ positionV: ITEM_POSITION_V,
26
28
  };
27
29
  const sampleBounds = (0, jsx_1.getElementBounds)((0, jsx_runtime_1.jsx)(Item, Object.assign({}, itemProps)));
28
30
  const labelWidth = sampleBounds.width;
@@ -70,10 +72,11 @@ const ChartLine = (props) => {
70
72
  const titleElements = [];
71
73
  const tickElements = [];
72
74
  const ticksY = scaleY.ticks(6);
75
+ const formatTickY = scaleY.tickFormat(6);
73
76
  ticksY.forEach((tick) => {
74
77
  const yPos = chartOriginY + scaleY(tick);
75
78
  gridElements.push((0, jsx_runtime_1.jsx)(jsx_1.Path, { d: `M ${chartOriginX} ${yPos} L ${chartOriginX + derivedChartWidth} ${yPos}`, width: derivedChartWidth, height: 1, stroke: axisColor, strokeWidth: 1, "data-element-type": "shape", opacity: 0.08 }));
76
- tickElements.push((0, jsx_runtime_1.jsx)(jsx_1.Text, { x: chartOriginX - 8, y: yPos, alignHorizontal: "right", alignVertical: "middle", fontSize: 12, fill: axisColor, children: Number.isInteger(tick) ? tick.toString() : tick.toFixed(1) }));
79
+ tickElements.push((0, jsx_runtime_1.jsx)(jsx_1.Text, { x: chartOriginX - 8, y: yPos, alignHorizontal: "right", alignVertical: "middle", fontSize: 12, fill: axisColor, children: formatTickY(tick) }));
77
80
  });
78
81
  const xLabels = [];
79
82
  const pointPositions = [];
@@ -161,7 +164,7 @@ const ChartLine = (props) => {
161
164
  if (yTitle) {
162
165
  titleElements.push((0, jsx_runtime_1.jsx)(jsx_1.Text, { x: paddingLeft + yTitleSpace / 2, y: chartOriginY + height / 2, alignHorizontal: "center", alignVertical: "middle", fontSize: 14, fontWeight: "bold", fill: axisColor, children: yTitle }));
163
166
  }
164
- return ((0, jsx_runtime_1.jsxs)(layouts_1.FlexLayout, { id: "infographic-container", flexDirection: "column", justifyContent: "center", alignItems: "center", children: [titleContent, (0, jsx_runtime_1.jsxs)(jsx_1.Group, { width: totalWidth, height: totalHeight, children: [(0, jsx_runtime_1.jsxs)(jsx_1.Defs, { children: [(0, jsx_runtime_1.jsx)("linearGradient", { id: gradientStrokeId, x1: "0%", y1: "0%", x2: "100%", y2: "0%", children: gradientStops }), (0, jsx_runtime_1.jsxs)("linearGradient", { id: gradientAreaId, x1: "0%", y1: "0%", x2: "100%", y2: "0%", children: [areaStops, (0, jsx_runtime_1.jsx)("stop", { offset: "100%", stopColor: colorPrimary, stopOpacity: "0.04" })] })] }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: gridElements }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: [...axisElements, ...tickElements] }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: lineElements }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: pointElements }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: valueElements }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: titleElements }), (0, jsx_runtime_1.jsx)(components_1.ItemsGroup, { children: xLabels })] })] }));
167
+ return ((0, jsx_runtime_1.jsxs)(layouts_1.FlexLayout, { id: "infographic-container", flexDirection: "column", justifyContent: "center", alignItems: "center", children: [titleContent, (0, jsx_runtime_1.jsxs)(jsx_1.Group, { width: totalWidth, height: totalHeight, children: [(0, jsx_runtime_1.jsxs)(jsx_1.Defs, { children: [(0, jsx_runtime_1.jsx)("linearGradient", { id: gradientStrokeId, x1: "0%", y1: "0%", x2: "100%", y2: "0%", children: gradientStops }), (0, jsx_runtime_1.jsx)("linearGradient", { id: gradientAreaId, x1: "0%", y1: "0%", x2: "100%", y2: "0%", children: areaStops })] }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: gridElements }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: [...axisElements, ...tickElements] }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: lineElements }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: pointElements }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: valueElements }), (0, jsx_runtime_1.jsx)(jsx_1.Group, { children: titleElements }), (0, jsx_runtime_1.jsx)(components_1.ItemsGroup, { children: xLabels })] })] }));
165
168
  };
166
169
  exports.ChartLine = ChartLine;
167
170
  (0, registry_1.registerStructure)('chart-line', {
@@ -76,7 +76,7 @@ function editText(text, options) {
76
76
  const entity = (0, utils_1.getTextEntity)(text);
77
77
  if (!entity)
78
78
  return;
79
- ensureEditorStyles();
79
+ ensureEditorStyles(entity);
80
80
  new InlineTextEditor(entity, options).start();
81
81
  }
82
82
  class InlineTextEditor {
@@ -207,7 +207,7 @@ class InlineTextEditor {
207
207
  return this.entity.textContent || '';
208
208
  }
209
209
  }
210
- function ensureEditorStyles() {
210
+ function ensureEditorStyles(target) {
211
211
  (0, utils_1.injectStyleOnce)(EDITOR_STYLE_ID, `
212
212
  .${EDITOR_BASE_CLASS} {
213
213
  margin: 0;
@@ -220,5 +220,5 @@ function ensureEditorStyles() {
220
220
  .${EDITOR_BASE_CLASS}::selection {
221
221
  background-color: #b3d4fc;
222
222
  }
223
- `);
223
+ `, target);
224
224
  }
@@ -12,9 +12,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.InteractionManager = void 0;
13
13
  const utils_1 = require("../../utils");
14
14
  const utils_2 = require("../utils");
15
+ const utils_3 = require("../utils");
15
16
  class InteractionManager {
16
17
  constructor() {
17
- this.extensions = new utils_2.Extension();
18
+ this.extensions = new utils_3.Extension();
18
19
  this.interactions = [];
19
20
  this.active = false;
20
21
  this.running = null;
@@ -22,14 +23,15 @@ class InteractionManager {
22
23
  this.selection = new Set();
23
24
  this.handleClick = (event) => {
24
25
  const doc = this.editor.getDocument();
25
- const target = event.target;
26
- if (!target) {
26
+ const path = typeof event.composedPath === 'function' ? event.composedPath() : [];
27
+ const insideInfographic = (0, utils_2.eventPathContains)(event, doc) ||
28
+ path.some((node) => node instanceof HTMLElement && (0, utils_1.isInfographicComponent)(node));
29
+ if (!event.target) {
27
30
  this.deactivate();
28
31
  return;
29
32
  }
30
33
  // 点击画布 SVG 或者标记为组件的元素
31
- if (doc.contains(target) ||
32
- (0, utils_1.isInfographicComponent)(target))
34
+ if (insideInfographic)
33
35
  this.activate();
34
36
  else
35
37
  this.deactivate();
@@ -4,9 +4,10 @@ export interface IconButtonProps {
4
4
  icon: Icon;
5
5
  onClick?: () => void;
6
6
  activate?: boolean;
7
+ root?: Node;
7
8
  }
8
9
  export interface IconButtonHandle {
9
10
  activate: boolean;
10
11
  setActivate: (activate: boolean) => void;
11
12
  }
12
- export declare const IconButton: ({ icon, onClick, activate, }: IconButtonProps) => Button;
13
+ export declare const IconButton: ({ icon, onClick, activate, root, }: IconButtonProps) => Button;
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.IconButton = void 0;
4
4
  const utils_1 = require("../../../utils");
5
- const IconButton = ({ icon, onClick, activate = false, }) => {
6
- ensureIconButtonStyle();
5
+ const IconButton = ({ icon, onClick, activate = false, root, }) => {
6
+ ensureIconButtonStyle(root);
7
7
  const button = document.createElement('button');
8
8
  button.type = 'button';
9
9
  button.classList.add(ICON_BUTTON_CLASS);
@@ -27,7 +27,7 @@ const IconButton = ({ icon, onClick, activate = false, }) => {
27
27
  exports.IconButton = IconButton;
28
28
  const ICON_BUTTON_CLASS = 'infographic-edit-bar-icon-btn';
29
29
  const ICON_BUTTON_STYLE_ID = 'infographic-edit-bar-icon-btn-style';
30
- function ensureIconButtonStyle() {
30
+ function ensureIconButtonStyle(target) {
31
31
  (0, utils_1.injectStyleOnce)(ICON_BUTTON_STYLE_ID, `
32
32
  .${ICON_BUTTON_CLASS} {
33
33
  padding: 0;
@@ -53,5 +53,5 @@ function ensureIconButtonStyle() {
53
53
  .${ICON_BUTTON_CLASS}[data-activate="true"] {
54
54
  background-color: #d9d9d9;
55
55
  }
56
- `);
56
+ `, target);
57
57
  }
@@ -2,6 +2,7 @@ export type ColorPickerProps = {
2
2
  value?: string;
3
3
  swatches?: string[];
4
4
  onChange?: (value: string) => void;
5
+ root?: Node;
5
6
  };
6
7
  export type ColorPickerHandle = {
7
8
  setValue: (value?: string) => void;
@@ -49,7 +49,7 @@ function ColorPicker(props) {
49
49
  if (typeof document === 'undefined') {
50
50
  throw new Error('ColorPicker can only be used in the browser.');
51
51
  }
52
- ensureColorPickerStyles();
52
+ ensureColorPickerStyles(props.root);
53
53
  const container = document.createElement('div');
54
54
  container.classList.add(COLOR_PICKER_CLASS);
55
55
  const swatchContainer = document.createElement('div');
@@ -242,7 +242,7 @@ function createSwitchLabel(text) {
242
242
  span.classList.add(COLOR_PICKER_SWITCH_LABEL_CLASS);
243
243
  return span;
244
244
  }
245
- function ensureColorPickerStyles() {
245
+ function ensureColorPickerStyles(target) {
246
246
  (0, utils_1.injectStyleOnce)(COLOR_PICKER_STYLE_ID, `
247
247
  .${COLOR_PICKER_CLASS} {
248
248
  width: 240px;
@@ -353,5 +353,5 @@ function ensureColorPickerStyles() {
353
353
  .${COLOR_PICKER_SWITCH_CLASS}[data-format="rgba"] .${COLOR_PICKER_SWITCH_LABEL_CLASS}:last-child {
354
354
  color: #ffffff;
355
355
  }
356
- `);
356
+ `, target);
357
357
  }
@@ -3,7 +3,7 @@ export type PopoverPlacementPreference = PopoverPlacement | PopoverPlacement[];
3
3
  export interface PopoverProps {
4
4
  content: HTMLElement | string | (() => HTMLElement | string);
5
5
  target?: HTMLElement;
6
- getContainer?: HTMLElement | (() => HTMLElement);
6
+ getContainer?: OverlayRoot | (() => OverlayRoot);
7
7
  placement?: PopoverPlacementPreference;
8
8
  padding?: number | string;
9
9
  open?: boolean;
@@ -13,6 +13,7 @@ export interface PopoverProps {
13
13
  mouseLeaveDelay?: number;
14
14
  offset?: number;
15
15
  }
16
+ type OverlayRoot = HTMLElement | ShadowRoot;
16
17
  export interface PopoverHandle {
17
18
  setOpen: (open: boolean) => void;
18
19
  toggle: () => void;
@@ -21,3 +22,4 @@ export interface PopoverHandle {
21
22
  destroy: () => void;
22
23
  }
23
24
  export declare function Popover(props: PopoverProps): HTMLDivElement & PopoverHandle;
25
+ export {};
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Popover = Popover;
4
+ const utils_1 = require("../../utils");
4
5
  const constants_1 = require("../../../constants");
5
- const utils_1 = require("../../../utils");
6
+ const utils_2 = require("../../../utils");
6
7
  const POPOVER_CLASS = 'infographic-edit-popover';
7
8
  const POPOVER_CONTENT_CLASS = `${POPOVER_CLASS}__content`;
8
9
  const POPOVER_ARROW_CLASS = `${POPOVER_CLASS}__arrow`;
9
10
  const POPOVER_STYLE_ID = 'infographic-edit-popover-style';
10
11
  function Popover(props) {
11
12
  var _a, _b, _c, _d, _e, _f, _g, _h;
12
- ensurePopoverStyle();
13
13
  const placement = (_a = props.placement) !== null && _a !== void 0 ? _a : 'top';
14
14
  const closeOnOutsideClick = (_b = props.closeOnOutsideClick) !== null && _b !== void 0 ? _b : true;
15
15
  const triggerActions = Array.isArray(props.trigger)
@@ -31,11 +31,11 @@ function Popover(props) {
31
31
  const next = typeof props.getContainer === 'function'
32
32
  ? props.getContainer()
33
33
  : props.getContainer;
34
- return next !== null && next !== void 0 ? next : document.body;
34
+ return next !== null && next !== void 0 ? next : (0, utils_1.getOverlayContainer)(trigger);
35
35
  };
36
36
  const content = document.createElement('div');
37
37
  content.classList.add(POPOVER_CONTENT_CLASS);
38
- (0, utils_1.setElementRole)(content, constants_1.COMPONENT_ROLE);
38
+ (0, utils_2.setElementRole)(content, constants_1.COMPONENT_ROLE);
39
39
  content.dataset.placement = Array.isArray(placement)
40
40
  ? placement[0]
41
41
  : placement;
@@ -44,7 +44,18 @@ function Popover(props) {
44
44
  content.style.setProperty('--popover-arrow-size', `${arrowSize}px`);
45
45
  content.style.setProperty('--popover-arrow-inner-size', `${arrowInnerSize}px`);
46
46
  const contentContainer = getContentContainer();
47
+ ensurePopoverStyle(contentContainer);
47
48
  const isPortal = contentContainer !== container;
49
+ const getPortalOffsetParent = () => {
50
+ const offsetParent = content.offsetParent;
51
+ if (offsetParent)
52
+ return offsetParent;
53
+ if (contentContainer instanceof ShadowRoot)
54
+ return contentContainer.host;
55
+ if (contentContainer instanceof HTMLElement)
56
+ return contentContainer;
57
+ return document.documentElement;
58
+ };
48
59
  const arrow = document.createElement('div');
49
60
  arrow.classList.add(POPOVER_ARROW_CLASS);
50
61
  content.appendChild(arrow);
@@ -122,8 +133,17 @@ function Popover(props) {
122
133
  if (!isPortal)
123
134
  return;
124
135
  ({ left, top } = position);
125
- content.style.left = `${left}px`;
126
- content.style.top = `${top}px`;
136
+ const offsetParent = getPortalOffsetParent();
137
+ if (offsetParent === document.body ||
138
+ offsetParent === document.documentElement) {
139
+ content.style.left = `${left}px`;
140
+ content.style.top = `${top}px`;
141
+ }
142
+ else {
143
+ const parentRect = offsetParent.getBoundingClientRect();
144
+ content.style.left = `${left - parentRect.left + offsetParent.scrollLeft}px`;
145
+ content.style.top = `${top - parentRect.top + offsetParent.scrollTop}px`;
146
+ }
127
147
  content.style.right = 'auto';
128
148
  content.style.bottom = 'auto';
129
149
  content.style.transform = 'translate(0, 0)';
@@ -167,9 +187,9 @@ function Popover(props) {
167
187
  };
168
188
  const toggle = () => setOpen(!open);
169
189
  const handleOutsideClick = (event) => {
170
- const targetNode = event.target;
171
- if (!container.contains(targetNode) &&
172
- (isPortal ? !content.contains(targetNode) : true)) {
190
+ const insideTrigger = (0, utils_1.eventPathContains)(event, container);
191
+ const insideContent = (0, utils_1.eventPathContains)(event, content);
192
+ if (!insideTrigger && !insideContent) {
173
193
  setOpen(false);
174
194
  }
175
195
  };
@@ -237,8 +257,8 @@ function Popover(props) {
237
257
  };
238
258
  return Object.assign(container, api);
239
259
  }
240
- function ensurePopoverStyle() {
241
- (0, utils_1.injectStyleOnce)(POPOVER_STYLE_ID, `
260
+ function ensurePopoverStyle(target) {
261
+ (0, utils_2.injectStyleOnce)(POPOVER_STYLE_ID, `
242
262
  .${POPOVER_CLASS} {
243
263
  position: relative;
244
264
  display: inline-flex;
@@ -380,5 +400,5 @@ function ensurePopoverStyle() {
380
400
  .${POPOVER_CONTENT_CLASS}[data-placement="right"][data-open="true"] {
381
401
  transform: translate(0, -50%);
382
402
  }
383
- `);
403
+ `, target);
384
404
  }
@@ -4,8 +4,9 @@ import { Plugin } from '../base';
4
4
  export interface EditBarOptions {
5
5
  style?: Partial<CSSStyleDeclaration>;
6
6
  className?: string;
7
- getContainer?: HTMLElement | (() => HTMLElement);
7
+ getContainer?: OverlayRoot | (() => OverlayRoot);
8
8
  }
9
+ type OverlayRoot = HTMLElement | ShadowRoot;
9
10
  type EditItem = HTMLElement;
10
11
  export declare class EditBar extends Plugin implements IPlugin {
11
12
  private options?;
@@ -27,6 +28,7 @@ export declare class EditBar extends Plugin implements IPlugin {
27
28
  protected getGeometryEditItems(_selection: Selection): EditItem[];
28
29
  protected getGeometryCollectionEditItems(selection: Selection): EditItem[];
29
30
  protected getElementCollectionEditItems(selection: Selection): EditItem[];
31
+ private resolveOverlayRoot;
30
32
  private placeEditBar;
31
33
  }
32
34
  export {};
@@ -4,6 +4,7 @@ exports.EditBar = void 0;
4
4
  const constants_1 = require("../../../constants");
5
5
  const jsx_1 = require("../../../jsx");
6
6
  const utils_1 = require("../../../utils");
7
+ const utils_2 = require("../../utils");
7
8
  const base_1 = require("../base");
8
9
  const edit_items_1 = require("./edit-items");
9
10
  class EditBar extends base_1.Plugin {
@@ -112,29 +113,31 @@ class EditBar extends base_1.Plugin {
112
113
  }
113
114
  (0, utils_1.setElementRole)(container, constants_1.COMPONENT_ROLE);
114
115
  this.container = container;
115
- const { getContainer } = this.options || {};
116
- const resolvedContainer = typeof getContainer === 'function' ? getContainer() : getContainer;
117
- const containerParent = resolvedContainer !== null && resolvedContainer !== void 0 ? resolvedContainer : document.body;
116
+ const containerParent = this.resolveOverlayRoot();
118
117
  containerParent === null || containerParent === void 0 ? void 0 : containerParent.appendChild(container);
119
118
  return container;
120
119
  }
121
120
  getTextEditItems(text) {
122
121
  const { attributes = {} } = (0, utils_1.getTextElementProps)(text);
123
- return [edit_items_1.FontColor, edit_items_1.FontSize, edit_items_1.FontAlign, edit_items_1.FontFamily].map((item) => item([text], attributes, this.commander));
122
+ const root = this.resolveOverlayRoot();
123
+ return [edit_items_1.FontColor, edit_items_1.FontSize, edit_items_1.FontAlign, edit_items_1.FontFamily].map((item) => item([text], attributes, this.commander, { root }));
124
124
  }
125
125
  getTextCollectionEditItems(selection) {
126
126
  const attrs = (0, utils_1.getCommonAttrs)(selection.map((text) => (0, utils_1.getTextElementProps)(text).attributes || {}));
127
- const items = [edit_items_1.FontColor, edit_items_1.FontSize, edit_items_1.FontAlign, edit_items_1.FontFamily].map((item) => item(selection, attrs, this.commander));
127
+ const root = this.resolveOverlayRoot();
128
+ const items = [edit_items_1.FontColor, edit_items_1.FontSize, edit_items_1.FontAlign, edit_items_1.FontFamily].map((item) => item(selection, attrs, this.commander, { root }));
128
129
  const commonItems = this.getElementCollectionEditItems(selection);
129
130
  return [...items, ...commonItems];
130
131
  }
131
132
  getIconEditItems(selection) {
132
133
  const attrs = (0, utils_1.getIconAttrs)(selection[0]);
133
- return [edit_items_1.IconColor].map((item) => item(selection, attrs, this.commander));
134
+ const root = this.resolveOverlayRoot();
135
+ return [edit_items_1.IconColor].map((item) => item(selection, attrs, this.commander, { root }));
134
136
  }
135
137
  getIconCollectionEditItems(selection) {
136
138
  const attrs = (0, utils_1.getCommonAttrs)(selection.map((icon) => (0, utils_1.getIconAttrs)(icon)));
137
- return [edit_items_1.IconColor].map((item) => item(selection, attrs, this.commander));
139
+ const root = this.resolveOverlayRoot();
140
+ return [edit_items_1.IconColor].map((item) => item(selection, attrs, this.commander, { root }));
138
141
  }
139
142
  getGeometryEditItems(_selection) {
140
143
  return [];
@@ -146,12 +149,19 @@ class EditBar extends base_1.Plugin {
146
149
  getElementCollectionEditItems(selection) {
147
150
  if (selection.length <= 1)
148
151
  return [];
152
+ const root = this.resolveOverlayRoot();
149
153
  return [
150
154
  (0, edit_items_1.ElementAlign)(selection, {}, this.commander, {
151
155
  enableDistribution: selection.length > 2,
156
+ root,
152
157
  }),
153
158
  ];
154
159
  }
160
+ resolveOverlayRoot() {
161
+ const { getContainer } = this.options || {};
162
+ const resolvedContainer = typeof getContainer === 'function' ? getContainer() : getContainer;
163
+ return resolvedContainer !== null && resolvedContainer !== void 0 ? resolvedContainer : (0, utils_2.getOverlayContainer)(this.editor.getDocument());
164
+ }
155
165
  placeEditBar(container, selection) {
156
166
  var _a;
157
167
  if (selection.length === 0)
@@ -32,18 +32,19 @@ const ALIGN_OPTIONS = [
32
32
  ];
33
33
  const ElementAlign = (selection, _attrs, commander, options) => {
34
34
  var _a;
35
- (0, utils_1.injectStyleOnce)(GRID_STYLE_ID, GRID_STYLES);
35
+ (0, utils_1.injectStyleOnce)(GRID_STYLE_ID, GRID_STYLES, options === null || options === void 0 ? void 0 : options.root);
36
36
  const enableDistribution = (_a = options === null || options === void 0 ? void 0 : options.enableDistribution) !== null && _a !== void 0 ? _a : true;
37
- const content = createAlignContent((action) => alignSelection(selection, action, commander), enableDistribution);
37
+ const content = createAlignContent((action) => alignSelection(selection, action, commander), enableDistribution, options === null || options === void 0 ? void 0 : options.root);
38
38
  return (0, components_1.Popover)({
39
- target: (0, components_1.IconButton)({ icon: components_1.ELEMENT_ICONS.align }),
39
+ target: (0, components_1.IconButton)({ icon: components_1.ELEMENT_ICONS.align, root: options === null || options === void 0 ? void 0 : options.root }),
40
40
  content,
41
+ getContainer: options === null || options === void 0 ? void 0 : options.root,
41
42
  placement: 'top',
42
43
  offset: 12,
43
44
  });
44
45
  };
45
46
  exports.ElementAlign = ElementAlign;
46
- function createAlignContent(onSelect, enableDistribution) {
47
+ function createAlignContent(onSelect, enableDistribution, root) {
47
48
  const content = document.createElement('div');
48
49
  content.classList.add(GRID_CLASS);
49
50
  if (!enableDistribution)
@@ -53,6 +54,7 @@ function createAlignContent(onSelect, enableDistribution) {
53
54
  : ALIGN_OPTIONS.filter(({ action }) => action !== 'H_DISTRIBUTE' && action !== 'V_DISTRIBUTE');
54
55
  visibleOptions.forEach(({ icon, action }) => {
55
56
  const button = (0, components_1.IconButton)({
57
+ root,
56
58
  icon,
57
59
  onClick: () => onSelect(action),
58
60
  });