@antv/g6 5.0.1 → 5.0.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 (281) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/click-select.d.ts +4 -5
  3. package/dist/lib/elements/base-element.d.ts +1 -22
  4. package/dist/lib/elements/combos/base-combo.d.ts +4 -4
  5. package/dist/lib/elements/edges/base-edge.d.ts +4 -2
  6. package/dist/lib/elements/effect.d.ts +17 -0
  7. package/dist/lib/elements/nodes/base-node.d.ts +5 -4
  8. package/dist/lib/elements/nodes/diamond.d.ts +0 -1
  9. package/dist/lib/elements/shapes/badge.d.ts +17 -6
  10. package/dist/lib/elements/shapes/base-shape.d.ts +9 -3
  11. package/dist/lib/elements/shapes/icon.d.ts +15 -3
  12. package/dist/lib/elements/shapes/label.d.ts +18 -7
  13. package/dist/lib/exports.d.ts +3 -2
  14. package/dist/lib/index.d.ts +5 -1
  15. package/dist/lib/layouts/base-layout.d.ts +3 -1
  16. package/dist/lib/plugins/bubble-sets.d.ts +2 -2
  17. package/dist/lib/plugins/hull/index.d.ts +2 -2
  18. package/dist/lib/runtime/canvas.d.ts +15 -2
  19. package/dist/lib/runtime/data.d.ts +7 -16
  20. package/dist/lib/runtime/element.d.ts +18 -2
  21. package/dist/lib/runtime/graph.d.ts +372 -108
  22. package/dist/lib/spec/element/edge.d.ts +1 -1
  23. package/dist/lib/spec/element/node.d.ts +1 -1
  24. package/dist/lib/transforms/process-parallel-edges.d.ts +2 -2
  25. package/dist/lib/types/callable.d.ts +0 -11
  26. package/dist/lib/types/data.d.ts +12 -0
  27. package/dist/lib/types/edge.d.ts +11 -0
  28. package/dist/lib/types/element.d.ts +100 -9
  29. package/dist/lib/types/index.d.ts +0 -1
  30. package/dist/lib/types/point.d.ts +0 -1
  31. package/dist/lib/utils/element.d.ts +2 -2
  32. package/dist/lib/utils/layout.d.ts +2 -2
  33. package/dist/lib/utils/path.d.ts +3 -3
  34. package/dist/lib/utils/position.d.ts +8 -8
  35. package/dist/lib/utils/style.d.ts +13 -8
  36. package/esm/behaviors/click-select.d.ts +4 -5
  37. package/esm/behaviors/click-select.js +59 -50
  38. package/esm/behaviors/click-select.js.map +1 -1
  39. package/esm/behaviors/lasso-select.js.map +1 -1
  40. package/esm/elements/base-element.d.ts +1 -22
  41. package/esm/elements/base-element.js +0 -21
  42. package/esm/elements/base-element.js.map +1 -1
  43. package/esm/elements/combos/base-combo.d.ts +4 -4
  44. package/esm/elements/combos/base-combo.js +27 -4
  45. package/esm/elements/combos/base-combo.js.map +1 -1
  46. package/esm/elements/edges/base-edge.d.ts +4 -2
  47. package/esm/elements/edges/base-edge.js +41 -7
  48. package/esm/elements/edges/base-edge.js.map +1 -1
  49. package/esm/elements/effect.d.ts +17 -0
  50. package/esm/elements/effect.js +70 -0
  51. package/esm/elements/effect.js.map +1 -0
  52. package/esm/elements/nodes/base-node.d.ts +5 -4
  53. package/esm/elements/nodes/base-node.js +33 -14
  54. package/esm/elements/nodes/base-node.js.map +1 -1
  55. package/esm/elements/nodes/circle.js +1 -1
  56. package/esm/elements/nodes/circle.js.map +1 -1
  57. package/esm/elements/nodes/diamond.d.ts +0 -1
  58. package/esm/elements/nodes/diamond.js +0 -6
  59. package/esm/elements/nodes/diamond.js.map +1 -1
  60. package/esm/elements/nodes/ellipse.js +1 -1
  61. package/esm/elements/nodes/ellipse.js.map +1 -1
  62. package/esm/elements/nodes/image.js +2 -2
  63. package/esm/elements/nodes/image.js.map +1 -1
  64. package/esm/elements/nodes/rect.js +1 -1
  65. package/esm/elements/nodes/rect.js.map +1 -1
  66. package/esm/elements/shapes/badge.d.ts +17 -6
  67. package/esm/elements/shapes/badge.js +9 -0
  68. package/esm/elements/shapes/badge.js.map +1 -1
  69. package/esm/elements/shapes/base-shape.d.ts +9 -3
  70. package/esm/elements/shapes/base-shape.js +33 -15
  71. package/esm/elements/shapes/base-shape.js.map +1 -1
  72. package/esm/elements/shapes/icon.d.ts +15 -3
  73. package/esm/elements/shapes/icon.js +12 -4
  74. package/esm/elements/shapes/icon.js.map +1 -1
  75. package/esm/elements/shapes/label.d.ts +18 -7
  76. package/esm/elements/shapes/label.js +10 -0
  77. package/esm/elements/shapes/label.js.map +1 -1
  78. package/esm/elements/shapes/polygon.js +1 -1
  79. package/esm/elements/shapes/polygon.js.map +1 -1
  80. package/esm/exports.d.ts +3 -2
  81. package/esm/exports.js +1 -1
  82. package/esm/exports.js.map +1 -1
  83. package/esm/index.d.ts +5 -1
  84. package/esm/index.js +5 -1
  85. package/esm/index.js.map +1 -1
  86. package/esm/layouts/base-layout.d.ts +3 -1
  87. package/esm/layouts/base-layout.js +3 -1
  88. package/esm/layouts/base-layout.js.map +1 -1
  89. package/esm/plugins/bubble-sets.d.ts +2 -2
  90. package/esm/plugins/bubble-sets.js.map +1 -1
  91. package/esm/plugins/hull/index.d.ts +2 -2
  92. package/esm/plugins/hull/index.js.map +1 -1
  93. package/esm/runtime/canvas.d.ts +15 -2
  94. package/esm/runtime/canvas.js.map +1 -1
  95. package/esm/runtime/data.d.ts +7 -16
  96. package/esm/runtime/data.js +14 -34
  97. package/esm/runtime/data.js.map +1 -1
  98. package/esm/runtime/element.d.ts +18 -2
  99. package/esm/runtime/element.js +68 -20
  100. package/esm/runtime/element.js.map +1 -1
  101. package/esm/runtime/graph.d.ts +372 -108
  102. package/esm/runtime/graph.js +271 -76
  103. package/esm/runtime/graph.js.map +1 -1
  104. package/esm/runtime/layout.js +1 -1
  105. package/esm/runtime/layout.js.map +1 -1
  106. package/esm/spec/element/edge.d.ts +1 -1
  107. package/esm/spec/element/node.d.ts +1 -1
  108. package/esm/themes/base.js +2 -0
  109. package/esm/themes/base.js.map +1 -1
  110. package/esm/transforms/collapse-expand-node.js +14 -4
  111. package/esm/transforms/collapse-expand-node.js.map +1 -1
  112. package/esm/transforms/process-parallel-edges.d.ts +2 -2
  113. package/esm/types/callable.d.ts +0 -11
  114. package/esm/types/data.d.ts +12 -0
  115. package/esm/types/edge.d.ts +11 -0
  116. package/esm/types/element.d.ts +100 -9
  117. package/esm/types/index.d.ts +0 -1
  118. package/esm/types/point.d.ts +0 -1
  119. package/esm/utils/element.d.ts +2 -2
  120. package/esm/utils/element.js.map +1 -1
  121. package/esm/utils/layout.d.ts +2 -2
  122. package/esm/utils/layout.js +4 -4
  123. package/esm/utils/layout.js.map +1 -1
  124. package/esm/utils/path.d.ts +3 -3
  125. package/esm/utils/path.js +1 -1
  126. package/esm/utils/path.js.map +1 -1
  127. package/esm/utils/position.d.ts +8 -8
  128. package/esm/utils/position.js +2 -2
  129. package/esm/utils/position.js.map +1 -1
  130. package/esm/utils/style.d.ts +13 -8
  131. package/esm/utils/style.js +16 -11
  132. package/esm/utils/style.js.map +1 -1
  133. package/lib/behaviors/click-select.d.ts +4 -5
  134. package/lib/behaviors/click-select.js +59 -50
  135. package/lib/behaviors/click-select.js.map +1 -1
  136. package/lib/behaviors/lasso-select.js.map +1 -1
  137. package/lib/elements/base-element.d.ts +1 -22
  138. package/lib/elements/base-element.js +0 -21
  139. package/lib/elements/base-element.js.map +1 -1
  140. package/lib/elements/combos/base-combo.d.ts +4 -4
  141. package/lib/elements/combos/base-combo.js +27 -4
  142. package/lib/elements/combos/base-combo.js.map +1 -1
  143. package/lib/elements/edges/base-edge.d.ts +4 -2
  144. package/lib/elements/edges/base-edge.js +41 -7
  145. package/lib/elements/edges/base-edge.js.map +1 -1
  146. package/lib/elements/effect.d.ts +17 -0
  147. package/lib/elements/effect.js +74 -0
  148. package/lib/elements/effect.js.map +1 -0
  149. package/lib/elements/nodes/base-node.d.ts +5 -4
  150. package/lib/elements/nodes/base-node.js +33 -14
  151. package/lib/elements/nodes/base-node.js.map +1 -1
  152. package/lib/elements/nodes/circle.js +1 -1
  153. package/lib/elements/nodes/circle.js.map +1 -1
  154. package/lib/elements/nodes/diamond.d.ts +0 -1
  155. package/lib/elements/nodes/diamond.js +0 -6
  156. package/lib/elements/nodes/diamond.js.map +1 -1
  157. package/lib/elements/nodes/ellipse.js +1 -1
  158. package/lib/elements/nodes/ellipse.js.map +1 -1
  159. package/lib/elements/nodes/image.js +2 -2
  160. package/lib/elements/nodes/image.js.map +1 -1
  161. package/lib/elements/nodes/rect.js +1 -1
  162. package/lib/elements/nodes/rect.js.map +1 -1
  163. package/lib/elements/shapes/badge.d.ts +17 -6
  164. package/lib/elements/shapes/badge.js +9 -0
  165. package/lib/elements/shapes/badge.js.map +1 -1
  166. package/lib/elements/shapes/base-shape.d.ts +9 -3
  167. package/lib/elements/shapes/base-shape.js +33 -15
  168. package/lib/elements/shapes/base-shape.js.map +1 -1
  169. package/lib/elements/shapes/icon.d.ts +15 -3
  170. package/lib/elements/shapes/icon.js +12 -4
  171. package/lib/elements/shapes/icon.js.map +1 -1
  172. package/lib/elements/shapes/label.d.ts +18 -7
  173. package/lib/elements/shapes/label.js +10 -0
  174. package/lib/elements/shapes/label.js.map +1 -1
  175. package/lib/elements/shapes/polygon.js +1 -1
  176. package/lib/elements/shapes/polygon.js.map +1 -1
  177. package/lib/exports.d.ts +3 -2
  178. package/lib/exports.js +5 -2
  179. package/lib/exports.js.map +1 -1
  180. package/lib/index.d.ts +5 -1
  181. package/lib/index.js +6 -2
  182. package/lib/index.js.map +1 -1
  183. package/lib/layouts/base-layout.d.ts +3 -1
  184. package/lib/layouts/base-layout.js +3 -1
  185. package/lib/layouts/base-layout.js.map +1 -1
  186. package/lib/plugins/bubble-sets.d.ts +2 -2
  187. package/lib/plugins/bubble-sets.js.map +1 -1
  188. package/lib/plugins/hull/index.d.ts +2 -2
  189. package/lib/plugins/hull/index.js.map +1 -1
  190. package/lib/runtime/canvas.d.ts +15 -2
  191. package/lib/runtime/canvas.js.map +1 -1
  192. package/lib/runtime/data.d.ts +7 -16
  193. package/lib/runtime/data.js +14 -34
  194. package/lib/runtime/data.js.map +1 -1
  195. package/lib/runtime/element.d.ts +18 -2
  196. package/lib/runtime/element.js +68 -20
  197. package/lib/runtime/element.js.map +1 -1
  198. package/lib/runtime/graph.d.ts +372 -108
  199. package/lib/runtime/graph.js +271 -76
  200. package/lib/runtime/graph.js.map +1 -1
  201. package/lib/runtime/layout.js +1 -1
  202. package/lib/runtime/layout.js.map +1 -1
  203. package/lib/spec/element/edge.d.ts +1 -1
  204. package/lib/spec/element/node.d.ts +1 -1
  205. package/lib/themes/base.js +2 -0
  206. package/lib/themes/base.js.map +1 -1
  207. package/lib/transforms/collapse-expand-node.js +14 -4
  208. package/lib/transforms/collapse-expand-node.js.map +1 -1
  209. package/lib/transforms/process-parallel-edges.d.ts +2 -2
  210. package/lib/types/callable.d.ts +0 -11
  211. package/lib/types/data.d.ts +12 -0
  212. package/lib/types/edge.d.ts +11 -0
  213. package/lib/types/element.d.ts +100 -9
  214. package/lib/types/index.d.ts +0 -1
  215. package/lib/types/point.d.ts +0 -1
  216. package/lib/utils/element.d.ts +2 -2
  217. package/lib/utils/element.js.map +1 -1
  218. package/lib/utils/layout.d.ts +2 -2
  219. package/lib/utils/layout.js +4 -4
  220. package/lib/utils/layout.js.map +1 -1
  221. package/lib/utils/path.d.ts +3 -3
  222. package/lib/utils/path.js +1 -1
  223. package/lib/utils/path.js.map +1 -1
  224. package/lib/utils/position.d.ts +8 -8
  225. package/lib/utils/position.js +2 -2
  226. package/lib/utils/position.js.map +1 -1
  227. package/lib/utils/style.d.ts +13 -8
  228. package/lib/utils/style.js +18 -13
  229. package/lib/utils/style.js.map +1 -1
  230. package/package.json +9 -9
  231. package/src/behaviors/click-select.ts +59 -60
  232. package/src/behaviors/lasso-select.ts +2 -2
  233. package/src/elements/base-element.ts +1 -25
  234. package/src/elements/combos/base-combo.ts +27 -11
  235. package/src/elements/edges/base-edge.ts +28 -8
  236. package/src/elements/effect.ts +74 -0
  237. package/src/elements/nodes/base-node.ts +23 -18
  238. package/src/elements/nodes/circle.ts +1 -1
  239. package/src/elements/nodes/diamond.ts +0 -7
  240. package/src/elements/nodes/ellipse.ts +1 -1
  241. package/src/elements/nodes/image.ts +2 -2
  242. package/src/elements/nodes/rect.ts +1 -1
  243. package/src/elements/shapes/badge.ts +17 -5
  244. package/src/elements/shapes/base-shape.ts +35 -15
  245. package/src/elements/shapes/icon.ts +18 -7
  246. package/src/elements/shapes/label.ts +19 -6
  247. package/src/elements/shapes/polygon.ts +1 -1
  248. package/src/exports.ts +6 -2
  249. package/src/index.ts +6 -2
  250. package/src/layouts/base-layout.ts +5 -1
  251. package/src/plugins/bubble-sets.ts +2 -2
  252. package/src/plugins/hull/index.ts +2 -2
  253. package/src/runtime/canvas.ts +15 -10
  254. package/src/runtime/data.ts +22 -44
  255. package/src/runtime/element.ts +80 -26
  256. package/src/runtime/graph.ts +398 -126
  257. package/src/runtime/layout.ts +2 -2
  258. package/src/spec/element/edge.ts +1 -1
  259. package/src/spec/element/node.ts +1 -1
  260. package/src/themes/base.ts +2 -0
  261. package/src/transforms/collapse-expand-node.ts +13 -4
  262. package/src/transforms/process-parallel-edges.ts +2 -2
  263. package/src/types/callable.ts +0 -12
  264. package/src/types/data.ts +13 -0
  265. package/src/types/edge.ts +11 -0
  266. package/src/types/element.ts +106 -10
  267. package/src/types/index.ts +0 -1
  268. package/src/types/point.ts +0 -1
  269. package/src/utils/element.ts +3 -13
  270. package/src/utils/layout.ts +6 -6
  271. package/src/utils/path.ts +3 -3
  272. package/src/utils/position.ts +8 -8
  273. package/src/utils/style.ts +19 -12
  274. package/dist/lib/types/position.d.ts +0 -2
  275. package/esm/types/position.d.ts +0 -2
  276. package/esm/types/position.js +0 -2
  277. package/esm/types/position.js.map +0 -1
  278. package/lib/types/position.d.ts +0 -2
  279. package/lib/types/position.js +0 -3
  280. package/lib/types/position.js.map +0 -1
  281. package/src/types/position.ts +0 -3
@@ -3,10 +3,11 @@ import { isFunction } from '@antv/util';
3
3
  import { COMBO_KEY } from '../../constants';
4
4
  import type {
5
5
  CollapsedMarkerStyleProps,
6
+ Combo,
6
7
  ID,
7
8
  NodeLikeData,
8
9
  Padding,
9
- Position,
10
+ Point,
10
11
  Prefix,
11
12
  STDSize,
12
13
  Size,
@@ -19,6 +20,7 @@ import { subStyleProps } from '../../utils/prefix';
19
20
  import { parseSize } from '../../utils/size';
20
21
  import { mergeOptions } from '../../utils/style';
21
22
  import { add, divide } from '../../utils/vector';
23
+ import { effect } from '../effect';
22
24
  import type { BaseNodeStyleProps } from '../nodes';
23
25
  import { BaseNode } from '../nodes';
24
26
  import { Icon, IconStyleProps } from '../shapes';
@@ -83,7 +85,10 @@ export interface BaseComboStyleProps
83
85
  *
84
86
  * <en/> When customizing a combo, it is recommended to use this class as the base class. In this way, users only need to focus on the logic of drawing keyShape
85
87
  */
86
- export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStyleProps> extends BaseNode<S> {
88
+ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStyleProps>
89
+ extends BaseNode<S>
90
+ implements Combo
91
+ {
87
92
  public type = 'combo';
88
93
 
89
94
  static defaultStyleProps: Partial<BaseComboStyleProps> = {
@@ -101,6 +106,7 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
101
106
  };
102
107
  constructor(options: DisplayObjectConfig<BaseComboStyleProps>) {
103
108
  super(mergeOptions({ style: BaseCombo.defaultStyleProps }, options));
109
+ this.updateComboPosition(this.parsedAttributes);
104
110
  }
105
111
 
106
112
  /**
@@ -131,15 +137,23 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
131
137
 
132
138
  protected getContentBBox(attributes: Required<S>): AABB {
133
139
  const { context, childrenNode = [], padding } = attributes;
134
- const childrenBBox = getCombinedBBox(
135
- childrenNode.map((id) => context!.element!.getElement(id)).map((child) => child!.getBounds()),
136
- );
140
+ const children = childrenNode.map((id) => context!.element!.getElement(id)).filter(Boolean);
141
+ if (children.length === 0) {
142
+ const bbox = new AABB();
143
+ const { x = 0, y = 0, size } = attributes;
144
+ const [width, height] = parseSize(size);
145
+ bbox.setMinMax([x - width / 2, y - height / 2, 0], [x + width / 2, y + height / 2, 0]);
146
+ return bbox;
147
+ }
148
+
149
+ const childrenBBox = getCombinedBBox(children.map((child) => child!.getBounds()));
137
150
 
138
151
  if (!padding) return childrenBBox;
139
152
 
140
153
  return getExpandedBBox(childrenBBox, padding);
141
154
  }
142
155
 
156
+ @effect((self, attributes) => self.getCollapsedMarkerStyle(attributes))
143
157
  protected drawCollapsedMarkerShape(attributes: Required<S>, container: Group): void {
144
158
  this.upsert('collapsed-marker', Icon, this.getCollapsedMarkerStyle(attributes), container);
145
159
  }
@@ -184,7 +198,7 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
184
198
  return ancestors.length;
185
199
  }
186
200
 
187
- public getComboPosition(attributes: Required<S>): Position {
201
+ public getComboPosition(attributes: Required<S>): Point {
188
202
  const { x = 0, y = 0, collapsed, context, childrenData = [] } = attributes;
189
203
 
190
204
  if (childrenData.length === 0) return [+x, +y, 0];
@@ -195,7 +209,7 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
195
209
 
196
210
  if (descendants.length > 0) {
197
211
  // combo 被收起,返回平均中心位置 / combo is collapsed, return the average center position
198
- const totalPosition = descendants.reduce((acc, datum) => add(acc, positionOf(datum)), [0, 0, 0] as Position);
212
+ const totalPosition = descendants.reduce((acc, datum) => add(acc, positionOf(datum)), [0, 0, 0] as Point);
199
213
  return divide(totalPosition, descendants.length);
200
214
  }
201
215
  // empty combo
@@ -215,20 +229,22 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
215
229
  protected updateComboPosition(attributes: Required<S>) {
216
230
  const comboStyle = this.getComboStyle(attributes);
217
231
  Object.assign(this.style, comboStyle);
218
-
219
232
  // Sync combo position to model
220
- attributes.context!.model.syncComboDatum({ id: this.id, style: comboStyle });
233
+ const { x, y } = comboStyle;
234
+ attributes.context!.model.syncComboDatum({ id: this.id, style: { x, y } });
221
235
  }
222
236
 
223
237
  public render(attributes: Required<S>, container: Group = this) {
224
238
  super.render(attributes, container);
225
- this.updateComboPosition(attributes);
226
239
 
227
240
  // collapsed marker
228
241
  this.drawCollapsedMarkerShape(attributes, container);
229
242
  }
230
243
 
231
- protected hostingAnimation = false;
244
+ public update(attr: Partial<S> = {}): void {
245
+ super.update(attr);
246
+ this.updateComboPosition(this.parsedAttributes);
247
+ }
232
248
 
233
249
  protected onframe() {
234
250
  super.onframe();
@@ -4,6 +4,7 @@ import type { PathArray } from '@antv/util';
4
4
  import { isFunction, pick } from '@antv/util';
5
5
  import type {
6
6
  BaseElementStyleProps,
7
+ Edge,
7
8
  EdgeArrowStyleProps,
8
9
  EdgeBadgeStyleProps,
9
10
  EdgeKey,
@@ -24,6 +25,7 @@ import { mergeOptions } from '../../utils/style';
24
25
  import * as Symbol from '../../utils/symbol';
25
26
  import { getWordWrapWidthByEnds } from '../../utils/text';
26
27
  import { BaseElement } from '../base-element';
28
+ import { effect } from '../effect';
27
29
  import type { BadgeStyleProps, LabelStyleProps } from '../shapes';
28
30
  import { Badge, Label } from '../shapes';
29
31
 
@@ -170,7 +172,7 @@ type ParsedBaseEdgeStyleProps = Required<BaseEdgeStyleProps>;
170
172
  *
171
173
  * <en/> Base class of the edge
172
174
  */
173
- export abstract class BaseEdge extends BaseElement<BaseEdgeStyleProps> {
175
+ export abstract class BaseEdge extends BaseElement<BaseEdgeStyleProps> implements Edge {
174
176
  public type = 'edge';
175
177
 
176
178
  static defaultStyleProps: Partial<BaseEdgeStyleProps> = {
@@ -360,42 +362,60 @@ export abstract class BaseEdge extends BaseElement<BaseEdgeStyleProps> {
360
362
  );
361
363
  }
362
364
 
365
+ @effect((self, attributes) => self.getLabelStyle(attributes))
363
366
  protected drawLabelShape(attributes: ParsedBaseEdgeStyleProps, container: Group) {
364
367
  this.upsert('label', Label, this.getLabelStyle(attributes), container);
365
368
  }
366
369
 
370
+ @effect((self, attributes) => self.getHaloStyle(attributes))
367
371
  protected drawHaloShape(attributes: ParsedBaseEdgeStyleProps, container: Group) {
368
372
  this.upsert('halo', Path, this.getHaloStyle(attributes), container);
369
373
  }
370
374
 
375
+ @effect((self, attributes) => self.getBadgeStyle(attributes))
371
376
  protected drawBadgeShape(attributes: ParsedBaseEdgeStyleProps, container: Group) {
372
377
  this.upsert('badge', Badge, this.getBadgeStyle(attributes), container);
373
378
  }
374
379
 
375
- protected drawKeyShape(attributes: ParsedBaseEdgeStyleProps, container: Group): Path | undefined {
376
- const key = this.upsert('key', Path, this.getKeyStyle(attributes), container);
380
+ @effect((self, attributes) => self.getArrowStyle(attributes, 'start'))
381
+ protected drawSourceArrow(attributes: ParsedBaseEdgeStyleProps) {
377
382
  this.drawArrow(attributes, 'start');
383
+ }
384
+
385
+ @effect((self, attributes) => self.getArrowStyle(attributes, 'end'))
386
+ protected drawTargetArrow(attributes: ParsedBaseEdgeStyleProps) {
378
387
  this.drawArrow(attributes, 'end');
388
+ }
389
+
390
+ @effect((self, attributes) => self.getKeyStyle(attributes))
391
+ protected drawKeyShape(attributes: ParsedBaseEdgeStyleProps, container: Group): Path | undefined {
392
+ const key = this.upsert('key', Path, this.getKeyStyle(attributes), container);
379
393
  return key;
380
394
  }
381
395
 
382
396
  public render(attributes = this.parsedAttributes, container: Group = this): void {
383
397
  // 1. key shape
384
- const keyShape = this.drawKeyShape(attributes, container);
385
- if (!keyShape) return;
398
+ this.drawKeyShape(attributes, container);
399
+ if (!this.getShape('key')) return;
400
+
401
+ // 2. arrows
402
+ this.drawSourceArrow(attributes);
403
+ this.drawTargetArrow(attributes);
386
404
 
387
- // 2. label
405
+ // 3. label
388
406
  this.drawLabelShape(attributes, container);
389
407
 
390
- // 3. halo
408
+ // 4. halo
391
409
  this.drawHaloShape(attributes, container);
392
410
 
393
- // 4. badge
411
+ // 5. badges
394
412
  this.drawBadgeShape(attributes, container);
395
413
  }
396
414
 
397
415
  protected onframe() {
398
416
  this.drawKeyShape(this.parsedAttributes, this);
417
+ this.drawSourceArrow(this.parsedAttributes);
418
+ this.drawTargetArrow(this.parsedAttributes);
399
419
  this.drawHaloShape(this.parsedAttributes, this);
400
420
  this.drawLabelShape(this.parsedAttributes, this);
401
421
  this.drawBadgeShape(this.parsedAttributes, this);
@@ -0,0 +1,74 @@
1
+ import type { Element } from '../types';
2
+ import { getCachedStyle, setCacheStyle } from '../utils/cache';
3
+
4
+ /**
5
+ * <zh/> 基于样式属性是否变化控制函数是否执行
6
+ *
7
+ * <en/> Control whether the function is executed based on whether the style attribute changes
8
+ * @param styler - <zh/> 获取样式属性函数 | <en/> Get style attribute function
9
+ * @returns <zh/> 装饰器 | <en/> Decorator
10
+ * @remarks
11
+ * <zh/> 仅指定 getStyle 的情况下,会分别使用当前的 attributes 和 新的 attributes 调用函数,若两者相同,则不执行函数。
12
+ *
13
+ * 如果指定了 shapeKey, 则会直接获取该图形的 attributes 作为原始样式属性,通常在 getStyle 函数中获取了包围盒时使用。
14
+ *
15
+ * <en/> Only when getStyle is specified, the function will be called with the current attributes and the new attributes respectively. If they are the same, the function will not be executed.
16
+ *
17
+ * If shapeKey is specified, the attributes of the shape will be directly obtained as the original style attributes, which is usually used when the bounding box of the element is used in the getStyle function.
18
+ */
19
+ export function effect(styler: (self: any, attributes: Record<string, unknown>) => Record<string, unknown>) {
20
+ return function (target: Element, propertyKey: string, descriptor: PropertyDescriptor) {
21
+ const fn = descriptor.value;
22
+
23
+ descriptor.value = function (this: Element, attr: Record<string, unknown>, ...rest: unknown[]) {
24
+ // 初始化后需要执行首次调用 / First call after initialization
25
+ const initKey = `${propertyKey}_invoke`;
26
+ if (!getCachedStyle(this, initKey)) {
27
+ setCacheStyle(this, initKey, true);
28
+ return fn.call(this, attr, ...rest);
29
+ }
30
+
31
+ const styleKey = `${propertyKey}_style`;
32
+ const original = getCachedStyle(this, styleKey);
33
+ const modified = styler(this, attr);
34
+ setCacheStyle(this, styleKey, modified);
35
+
36
+ if (isStyleEqual(original, modified)) return null;
37
+
38
+ return fn.call(this, attr, ...rest);
39
+ };
40
+ return descriptor;
41
+ };
42
+ }
43
+
44
+ /**
45
+ * <zh/> 比较两个样式属性是否相等
46
+ *
47
+ * <en/> Compare whether two style attributes are equal
48
+ * @param a - <zh/> 样式属性 a | <en/> Style attribute a
49
+ * @param b - <zh/> 样式属性 b | <en/> Style attribute b
50
+ * @param depth - <zh/> 比较深度 | <en/> Comparison depth
51
+ * @returns <zh/> 是否相等 | <en/> Whether they are equal
52
+ * @remarks
53
+ * <zh/> 进行第二层浅比较用于比较 badges、ports 等复合图形属性
54
+ *
55
+ * <en/> Perform a second-level shallow comparison to compare complex shape attributes such as badges and ports
56
+ */
57
+ const isStyleEqual = (a: Record<string, unknown>, b: Record<string, unknown>, depth = 2): boolean => {
58
+ if (typeof a !== 'object' || typeof b !== 'object') return a === b;
59
+
60
+ const keys1 = Object.keys(a);
61
+ const keys2 = Object.keys(b);
62
+
63
+ if (keys1.length !== keys2.length) return false;
64
+
65
+ for (const key of keys1) {
66
+ const val1 = a[key];
67
+ const val2 = b[key];
68
+ if (depth > 1 && typeof val1 === 'object' && typeof val2 === 'object') {
69
+ if (!isStyleEqual(val1 as Record<string, unknown>, val2 as Record<string, unknown>, depth - 1)) return false;
70
+ } else if (val1 !== val2) return false;
71
+ }
72
+
73
+ return true;
74
+ };
@@ -5,6 +5,7 @@ import type { NodeData } from '../../spec';
5
5
  import type {
6
6
  BaseElementStyleProps,
7
7
  ID,
8
+ Node,
8
9
  NodeBadgeStyleProps,
9
10
  NodeLabelStyleProps,
10
11
  NodePortStyleProps,
@@ -19,13 +20,12 @@ import { getPortXYByPlacement, getTextStyleByPlacement, isSimplePort } from '../
19
20
  import { inferIconStyle } from '../../utils/node';
20
21
  import { getPaletteColors } from '../../utils/palette';
21
22
  import { getRectIntersectPoint } from '../../utils/point';
22
- import { getXYByPlacement } from '../../utils/position';
23
23
  import { omitStyleProps, subObject, subStyleProps } from '../../utils/prefix';
24
24
  import { parseSize } from '../../utils/size';
25
25
  import { mergeOptions } from '../../utils/style';
26
26
  import { getWordWrapWidthByBox } from '../../utils/text';
27
- import { replaceTranslateInTransform } from '../../utils/transform';
28
27
  import { BaseElement } from '../base-element';
28
+ import { effect } from '../effect';
29
29
  import type { BadgeStyleProps, IconStyleProps, LabelStyleProps } from '../shapes';
30
30
  import { Badge, Icon, Label } from '../shapes';
31
31
 
@@ -46,13 +46,13 @@ export interface BaseNodeStyleProps
46
46
  *
47
47
  * <en/> The x-coordinate of node
48
48
  */
49
- x?: number | string;
49
+ x?: number;
50
50
  /**
51
51
  * <zh/> y 坐标
52
52
  *
53
53
  * <en/> The y-coordinate of node
54
54
  */
55
- y?: number | string;
55
+ y?: number;
56
56
  /**
57
57
  * <zh/> z 坐标
58
58
  *
@@ -185,7 +185,10 @@ export interface BaseNodeStyleProps
185
185
  *
186
186
  * <en/> Design document: https://www.yuque.com/antv/g6/gl1iof1xpzg6ed98
187
187
  */
188
- export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps> extends BaseElement<S> {
188
+ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps>
189
+ extends BaseElement<S>
190
+ implements Node
191
+ {
189
192
  public type = 'node';
190
193
 
191
194
  static defaultStyleProps: Partial<BaseNodeStyleProps> = {
@@ -240,8 +243,7 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
240
243
  this.getGraphicStyle(attributes),
241
244
  'label',
242
245
  );
243
- const keyShape = this.getShape('key');
244
- const keyBounds = keyShape.getLocalBounds();
246
+ const keyBounds = this.getShape('key').getLocalBounds();
245
247
 
246
248
  return Object.assign(
247
249
  getTextStyleByPlacement(keyBounds, placement, offsetX, offsetY),
@@ -263,10 +265,8 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
263
265
  if (attributes.icon === false || (!attributes.iconText && !attributes.iconSrc)) return false;
264
266
 
265
267
  const iconStyle = subStyleProps(this.getGraphicStyle(attributes), 'icon');
266
- const keyShape = this.getShape('key');
267
- const [x, y] = getXYByPlacement(keyShape.getLocalBounds(), 'center');
268
268
 
269
- return Object.assign({ x, y }, inferIconStyle(attributes.size!, iconStyle), iconStyle);
269
+ return Object.assign(inferIconStyle(attributes.size!, iconStyle), iconStyle);
270
270
  }
271
271
 
272
272
  protected getBadgesStyle(attributes: Required<S>): Record<string, NodeBadgeStyleProps | false> {
@@ -358,6 +358,7 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
358
358
  return getRectIntersectPoint(point, keyShapeBounds);
359
359
  }
360
360
 
361
+ @effect((self, attributes) => self.getHaloStyle(attributes))
361
362
  protected drawHaloShape(attributes: Required<S>, container: Group): void {
362
363
  const keyShape = this.getShape('key');
363
364
  this.upsert(
@@ -368,10 +369,12 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
368
369
  );
369
370
  }
370
371
 
372
+ @effect((self, attributes) => self.getIconStyle(attributes))
371
373
  protected drawIconShape(attributes: Required<S>, container: Group): void {
372
374
  this.upsert('icon', Icon, this.getIconStyle(attributes), container);
373
375
  }
374
376
 
377
+ @effect((self, attributes) => self.getBadgesStyle(attributes))
375
378
  protected drawBadgeShapes(attributes: Required<S>, container: Group): void {
376
379
  const badgesStyle = this.getBadgesStyle(attributes);
377
380
  Object.keys(badgesStyle).forEach((key) => {
@@ -379,6 +382,7 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
379
382
  });
380
383
  }
381
384
 
385
+ @effect((self, attributes) => self.getPortsStyle(attributes))
382
386
  protected drawPortShapes(attributes: Required<S>, container: Group): void {
383
387
  const portsStyle = this.getPortsStyle(attributes);
384
388
  Object.keys(portsStyle).forEach((key) => {
@@ -386,22 +390,23 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
386
390
  });
387
391
  }
388
392
 
393
+ @effect((self, attributes) => self.getLabelStyle(attributes))
389
394
  protected drawLabelShape(attributes: Required<S>, container: Group): void {
390
395
  this.upsert('label', Label, this.getLabelStyle(attributes), container);
391
396
  }
392
397
 
393
398
  protected abstract drawKeyShape(attributes: Required<S>, container: Group): DisplayObject | undefined;
394
399
 
395
- public render(attributes = this.parsedAttributes, container: Group = this) {
396
- // Use `transform: translate3d()` instead of `x/y/z`
397
- const { x = 0, y = 0, z = 0, transform } = attributes;
398
- if (x !== 0 || y !== 0 || z !== 0) {
399
- this.style.transform = replaceTranslateInTransform(x as number, y as number, z as number, transform);
400
- }
400
+ // 用于装饰抽象方法 / Used to decorate abstract methods
401
+ @effect((self, attributes) => self.getKeyStyle(attributes))
402
+ private _drawKeyShape(attributes: Required<S>, container: Group) {
403
+ return this.drawKeyShape(attributes, container);
404
+ }
401
405
 
406
+ public render(attributes = this.parsedAttributes, container: Group = this) {
402
407
  // 1. key shape
403
- const keyShape = this.drawKeyShape(attributes, container);
404
- if (!keyShape) return;
408
+ this._drawKeyShape(attributes, container);
409
+ if (!this.getShape('key')) return;
405
410
 
406
411
  // 2. halo, use shape same with keyShape
407
412
  this.drawHaloShape(attributes, container);
@@ -40,7 +40,7 @@ export class Circle extends BaseNode {
40
40
 
41
41
  protected getIconStyle(attributes: Required<CircleStyleProps>): false | IconStyleProps {
42
42
  const style = super.getIconStyle(attributes);
43
- const { r } = this.getKeyStyle(attributes);
43
+ const { r } = this.getShape<GCircle>('key').attributes;
44
44
  const size = (r as number) * 2 * ICON_SIZE_RATIO;
45
45
  return style ? ({ width: size, height: size, ...style } as IconStyleProps) : false;
46
46
  }
@@ -1,7 +1,6 @@
1
1
  import type { DisplayObjectConfig } from '@antv/g';
2
2
  import type { Point } from '../../types';
3
3
  import { getDiamondPoints } from '../../utils/element';
4
- import { getPolygonIntersectPoint } from '../../utils/point';
5
4
  import type { PolygonStyleProps } from '../shapes';
6
5
  import { Polygon } from '../shapes/polygon';
7
6
 
@@ -26,10 +25,4 @@ export class Diamond extends Polygon {
26
25
  const [width, height] = this.getSize(attributes);
27
26
  return getDiamondPoints(width, height);
28
27
  }
29
-
30
- public getIntersectPoint(point: Point): Point {
31
- const { points } = this.getKeyStyle(this.parsedAttributes);
32
- const center = [this.attributes.x, this.attributes.y] as Point;
33
- return getPolygonIntersectPoint(point, center, points).point;
34
- }
35
28
  }
@@ -45,7 +45,7 @@ export class Ellipse extends BaseNode {
45
45
 
46
46
  protected getIconStyle(attributes: Required<EllipseStyleProps>): false | IconStyleProps {
47
47
  const style = super.getIconStyle(attributes);
48
- const { rx, ry } = this.getKeyStyle(attributes);
48
+ const { rx, ry } = this.getShape<GEllipse>('key').attributes;
49
49
  const size = Math.min(+rx, +ry) * 2 * ICON_SIZE_RATIO;
50
50
 
51
51
  return style ? ({ width: size, height: size, ...style } as IconStyleProps) : false;
@@ -58,12 +58,12 @@ export class Image extends BaseNode<ImageStyleProps> {
58
58
 
59
59
  protected getHaloStyle(attributes: Required<ImageStyleProps>): false | GRectStyleProps {
60
60
  if (attributes.halo === false) return false;
61
- const { fill: keyStyleFill, stroke: keyStyleStroke, ...keyStyle } = this.getKeyStyle(attributes);
61
+ const { fill: keyStyleFill, stroke: keyStyleStroke, ...keyStyle } = this.getShape<GRect>('key').attributes;
62
62
  const haloStyle = subStyleProps(this.getGraphicStyle(attributes), 'halo');
63
63
  const haloLineWidth = Number(haloStyle.lineWidth);
64
64
  const [width, height] = add(this.getSize(attributes), [haloLineWidth, haloLineWidth]);
65
65
  const fill = 'transparent';
66
- return { ...keyStyle, ...haloStyle, width, height, fill, x: -width / 2, y: -height / 2 };
66
+ return { ...haloStyle, width, height, fill, x: -width / 2, y: -height / 2 };
67
67
  }
68
68
 
69
69
  protected getIconStyle(attributes: Required<ImageStyleProps>): false | IconStyleProps {
@@ -36,7 +36,7 @@ export class Rect extends BaseNode<RectStyleProps> {
36
36
 
37
37
  protected getIconStyle(attributes: ParsedRectStyleProps): false | IconStyleProps {
38
38
  const style = super.getIconStyle(attributes);
39
- const { width, height } = this.getKeyStyle(attributes);
39
+ const { width, height } = this.getShape<GRect>('key').attributes;
40
40
 
41
41
  return style
42
42
  ? ({
@@ -4,10 +4,22 @@ import { BaseShape } from './base-shape';
4
4
  import type { LabelStyleProps } from './label';
5
5
  import { Label } from './label';
6
6
 
7
+ /**
8
+ * <zh/> 徽标样式
9
+ *
10
+ * <en/> Badge style
11
+ */
7
12
  export interface BadgeStyleProps extends LabelStyleProps {}
8
- type ParsedBadgeStyleProps = Required<BadgeStyleProps>;
9
- type BadgeOptions = DisplayObjectConfig<BadgeStyleProps>;
10
13
 
14
+ /**
15
+ * <zh/> 徽标
16
+ *
17
+ * <en/> Badge
18
+ * @remarks
19
+ * <zh/> 徽标是一种特殊的标签,通常用于展示数量或状态信息。
20
+ *
21
+ * <en/> Badge is a special label, usually used to display quantity or status information.
22
+ */
11
23
  export class Badge extends BaseShape<BadgeStyleProps> {
12
24
  static defaultStyleProps: Partial<BadgeStyleProps> = {
13
25
  padding: [2, 4, 2, 4],
@@ -17,15 +29,15 @@ export class Badge extends BaseShape<BadgeStyleProps> {
17
29
  backgroundOpacity: 1,
18
30
  };
19
31
 
20
- constructor(options: BadgeOptions) {
32
+ constructor(options: DisplayObjectConfig<BadgeStyleProps>) {
21
33
  super(mergeOptions({ style: Badge.defaultStyleProps }, options));
22
34
  }
23
35
 
24
- protected getBadgeStyle(attributes: ParsedBadgeStyleProps) {
36
+ protected getBadgeStyle(attributes: Required<BadgeStyleProps>) {
25
37
  return this.getGraphicStyle(attributes);
26
38
  }
27
39
 
28
- public render(attributes: ParsedBadgeStyleProps = this.parsedAttributes, container: Group = this) {
40
+ public render(attributes: Required<BadgeStyleProps> = this.parsedAttributes, container: Group = this) {
29
41
  this.upsert('label', Label, this.getBadgeStyle(attributes), container);
30
42
  }
31
43
 
@@ -5,6 +5,8 @@ import type { Keyframe } from '../../types';
5
5
  import { createAnimationsProxy, preprocessKeyframes } from '../../utils/animation';
6
6
  import { updateStyle } from '../../utils/element';
7
7
  import { subObject } from '../../utils/prefix';
8
+ import { getSubShapeStyle } from '../../utils/style';
9
+ import { replaceTranslateInTransform } from '../../utils/transform';
8
10
  import { setVisibility } from '../../utils/visibility';
9
11
 
10
12
  export interface BaseShapeStyleProps extends BaseStyleProps {}
@@ -12,6 +14,7 @@ export interface BaseShapeStyleProps extends BaseStyleProps {}
12
14
  export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends CustomElement<StyleProps> {
13
15
  constructor(options: DisplayObjectConfig<StyleProps>) {
14
16
  super(options);
17
+ this.transformPosition(this.attributes);
15
18
  this.render(this.attributes as Required<StyleProps>, this);
16
19
  this.setVisibility();
17
20
  this.bindEvents();
@@ -63,7 +66,7 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
63
66
  }
64
67
 
65
68
  // create
66
- if (!target) {
69
+ if (!target || target.destroyed) {
67
70
  const instance = new Ctor({ className, style });
68
71
  container.appendChild(instance);
69
72
  this.shapeMap[className] = instance;
@@ -76,9 +79,25 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
76
79
  return target;
77
80
  }
78
81
 
82
+ /**
83
+ * <zh/> 使用 transform 更新图形位置
84
+ *
85
+ * <en/> Update the position of the shape using transform
86
+ * @param attributes - <zh/> 样式属性 | <en/> style attributes
87
+ */
88
+ protected transformPosition(attributes: Partial<StyleProps>) {
89
+ // Use `transform: translate3d()` instead of `x/y/z`
90
+ const { x = 0, y = 0, z = 0, transform } = attributes as any;
91
+ if (x !== 0 || y !== 0 || z !== 0) {
92
+ this.style.transform = replaceTranslateInTransform(+x, +y, +z, transform);
93
+ }
94
+ }
95
+
79
96
  public update(attr: Partial<StyleProps> = {}): void {
80
- this.attr(Object.assign({}, this.attributes, attr) as StyleProps);
81
- this.render(this.attributes as Required<StyleProps>, this);
97
+ const attributes = Object.assign({}, this.attributes, attr) as Required<StyleProps>;
98
+ this.attr(attributes);
99
+ this.render(attributes, this);
100
+ this.transformPosition(attributes);
82
101
  this.setVisibility();
83
102
  }
84
103
 
@@ -98,14 +117,13 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
98
117
  *
99
118
  * <en/> Extracts the graphic style properties from a given attribute object.
100
119
  * Removes specific properties like position, transformation, and class name.
101
- * @param attributes - <zh/> 属性对象 | <en/> attribute object
120
+ * @param style - <zh/> 属性对象 | <en/> attribute object
102
121
  * @returns <zh/> 仅包含样式属性的对象 | <en/> An object containing only the style properties.
103
122
  */
104
123
  public getGraphicStyle<T extends Record<string, any>>(
105
- attributes: T,
124
+ style: T,
106
125
  ): Omit<T, 'x' | 'y' | 'z' | 'transform' | 'transformOrigin' | 'className' | 'class' | 'context' | 'zIndex'> {
107
- const { x, y, z, class: cls, className, transform, transformOrigin, context, zIndex, ...style } = attributes;
108
- return style;
126
+ return getSubShapeStyle(style);
109
127
  }
110
128
 
111
129
  /**
@@ -119,19 +137,21 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
119
137
  ];
120
138
  }
121
139
 
122
- /*
123
- * <zh/> 是否自动托管动画
124
- *
125
- * <en/> Whether to automatically host animation
126
- */
127
- protected hostingAnimation = true;
128
-
129
140
  public animate(keyframes: Keyframe[], options?: number | KeyframeAnimationOptions): IAnimation | null {
130
141
  if (keyframes.length === 0) return null;
131
142
  const animationMap: IAnimation[] = [];
132
143
 
144
+ // 如果 keyframes 中存在 x/y/z ,替换为 transform
145
+ // if x/y/z exists in keyframes, replace them with transform
146
+ if (keyframes[0].x !== undefined || keyframes[0].y !== undefined || keyframes[0].z !== undefined) {
147
+ const { x: _x = 0, y: _y = 0, z: _z = 0 } = this.attributes as Record<string, any>;
148
+ keyframes.forEach((keyframe) => {
149
+ const { x = _x, y = _y, z = _z } = keyframe;
150
+ Object.assign(keyframe, { transform: replaceTranslateInTransform(+x, +y, +z) });
151
+ });
152
+ }
153
+
133
154
  const result = super.animate(keyframes, options);
134
- if (!this.hostingAnimation) return result;
135
155
  if (result) animationMap.push(result);
136
156
 
137
157
  if (Array.isArray(keyframes) && keyframes.length > 0) {
@@ -2,12 +2,24 @@ import { DisplayObjectConfig, Image as GImage, Text as GText, Group, ImageStyleP
2
2
  import type { BaseShapeStyleProps } from './base-shape';
3
3
  import { BaseShape } from './base-shape';
4
4
 
5
+ /**
6
+ * <zh/> 图标样式
7
+ *
8
+ * <en/> Icon style
9
+ */
5
10
  export interface IconStyleProps extends BaseShapeStyleProps, Partial<TextStyleProps>, Omit<ImageStyleProps, 'z'> {}
6
11
 
7
- type IconOptions = DisplayObjectConfig<IconStyleProps>;
8
-
12
+ /**
13
+ * <zh/> 图标
14
+ *
15
+ * <en/> Icon
16
+ * @remarks
17
+ * <zh/> 图标是一种特殊的图形,可以是图片或者文字。传入 src 属性时,会渲染图片;传入 text 属性时,会渲染文字。
18
+ *
19
+ * <en/> Icon is a special shape, which can be an image or text. When the src attribute is passed in, an image will be rendered; when the text attribute is passed in, text will be rendered.
20
+ */
9
21
  export class Icon extends BaseShape<IconStyleProps> {
10
- constructor(options: IconOptions) {
22
+ constructor(options: DisplayObjectConfig<IconStyleProps>) {
11
23
  super(options);
12
24
  }
13
25
 
@@ -16,13 +28,12 @@ export class Icon extends BaseShape<IconStyleProps> {
16
28
  }
17
29
 
18
30
  protected getIconStyle(attributes: IconStyleProps = this.attributes): IconStyleProps {
19
- const { x = 0, y = 0 } = attributes;
31
+ const { width = 0, height = 0 } = attributes;
20
32
  const style = this.getGraphicStyle(attributes);
21
- const { width, height } = style;
22
33
  if (this.isGImage()) {
23
34
  return {
24
- x: (x as number) - (width as number) / 2,
25
- y: (y as number) - (height as number) / 2,
35
+ x: -width / 2,
36
+ y: -height / 2,
26
37
  ...style,
27
38
  };
28
39
  }