@antv/component 0.8.32-beta.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/esm/abstract/html-component.js +1 -9
  2. package/esm/abstract/html-component.js.map +1 -1
  3. package/esm/legend/category.js +12 -3
  4. package/esm/legend/category.js.map +1 -1
  5. package/esm/slider/constant.d.ts +44 -1
  6. package/esm/slider/constant.js +48 -4
  7. package/esm/slider/constant.js.map +1 -1
  8. package/esm/slider/foreground.d.ts +71 -0
  9. package/esm/slider/foreground.js +104 -0
  10. package/esm/slider/foreground.js.map +1 -0
  11. package/esm/slider/handler.d.ts +3 -8
  12. package/esm/slider/handler.js +8 -15
  13. package/esm/slider/handler.js.map +1 -1
  14. package/esm/slider/slider.d.ts +16 -4
  15. package/esm/slider/slider.js +218 -40
  16. package/esm/slider/slider.js.map +1 -1
  17. package/esm/tooltip/css-const.d.ts +0 -1
  18. package/esm/tooltip/css-const.js +0 -1
  19. package/esm/tooltip/css-const.js.map +1 -1
  20. package/esm/tooltip/html-theme.d.ts +0 -22
  21. package/esm/tooltip/html-theme.js +0 -7
  22. package/esm/tooltip/html-theme.js.map +1 -1
  23. package/esm/tooltip/html.d.ts +0 -23
  24. package/esm/tooltip/html.js +11 -27
  25. package/esm/tooltip/html.js.map +1 -1
  26. package/esm/util/event.d.ts +9 -1
  27. package/esm/util/event.js +12 -0
  28. package/esm/util/event.js.map +1 -1
  29. package/esm/util/mask.d.ts +9 -0
  30. package/esm/util/mask.js +34 -0
  31. package/esm/util/mask.js.map +1 -0
  32. package/esm/util/util.d.ts +7 -0
  33. package/esm/util/util.js +59 -1
  34. package/esm/util/util.js.map +1 -1
  35. package/lib/abstract/html-component.js +1 -9
  36. package/lib/abstract/html-component.js.map +1 -1
  37. package/lib/legend/category.js +12 -3
  38. package/lib/legend/category.js.map +1 -1
  39. package/lib/slider/constant.d.ts +44 -1
  40. package/lib/slider/constant.js +49 -5
  41. package/lib/slider/constant.js.map +1 -1
  42. package/lib/slider/foreground.d.ts +71 -0
  43. package/lib/slider/foreground.js +107 -0
  44. package/lib/slider/foreground.js.map +1 -0
  45. package/lib/slider/handler.d.ts +3 -8
  46. package/lib/slider/handler.js +9 -16
  47. package/lib/slider/handler.js.map +1 -1
  48. package/lib/slider/slider.d.ts +16 -4
  49. package/lib/slider/slider.js +215 -37
  50. package/lib/slider/slider.js.map +1 -1
  51. package/lib/tooltip/css-const.d.ts +0 -1
  52. package/lib/tooltip/css-const.js +1 -2
  53. package/lib/tooltip/css-const.js.map +1 -1
  54. package/lib/tooltip/html-theme.d.ts +0 -22
  55. package/lib/tooltip/html-theme.js +0 -7
  56. package/lib/tooltip/html-theme.js.map +1 -1
  57. package/lib/tooltip/html.d.ts +0 -23
  58. package/lib/tooltip/html.js +11 -27
  59. package/lib/tooltip/html.js.map +1 -1
  60. package/lib/util/event.d.ts +9 -1
  61. package/lib/util/event.js +14 -1
  62. package/lib/util/event.js.map +1 -1
  63. package/lib/util/mask.d.ts +9 -0
  64. package/lib/util/mask.js +40 -0
  65. package/lib/util/mask.js.map +1 -0
  66. package/lib/util/util.d.ts +7 -0
  67. package/lib/util/util.js +61 -1
  68. package/lib/util/util.js.map +1 -1
  69. package/package.json +6 -18
  70. package/src/abstract/html-component.ts +1 -12
  71. package/src/legend/category.ts +21 -12
  72. package/src/slider/constant.ts +55 -4
  73. package/src/slider/foreground.ts +165 -0
  74. package/src/slider/handler.ts +10 -20
  75. package/src/slider/slider.ts +249 -55
  76. package/src/tooltip/css-const.ts +0 -1
  77. package/src/tooltip/html-theme.ts +0 -8
  78. package/src/tooltip/html.ts +11 -29
  79. package/src/util/event.ts +14 -1
  80. package/src/util/mask.ts +41 -0
  81. package/src/util/util.ts +64 -2
@@ -24,14 +24,6 @@ export default {
24
24
  lineHeight: '20px',
25
25
  padding: '10px 10px 6px 10px',
26
26
  },
27
- [`${CssConst.CONTAINER_CLASS_CUSTOM}`]: {
28
- position: 'absolute',
29
- zIndex: 8,
30
- transition:
31
- 'visibility 0.2s cubic-bezier(0.23, 1, 0.32, 1), ' +
32
- 'left 0.4s cubic-bezier(0.23, 1, 0.32, 1), ' +
33
- 'top 0.4s cubic-bezier(0.23, 1, 0.32, 1)',
34
- },
35
27
  [`${CssConst.TITLE_CLASS}`]: {
36
28
  marginBottom: '4px',
37
29
  },
@@ -10,8 +10,6 @@ import TooltipTheme from './html-theme';
10
10
 
11
11
  import { ILocation } from '../interfaces';
12
12
  import { getAlignPoint } from '../util/align';
13
- import { hasClass } from '../util/util';
14
- import { CONTAINER_CLASS, CONTAINER_CLASS_CUSTOM } from './css-const';
15
13
 
16
14
  function hasOneKey(obj, keys) {
17
15
  let result = false;
@@ -133,13 +131,6 @@ class Tooltip<T extends TooltipCfg = TooltipCfg> extends HtmlComponent implement
133
131
  });
134
132
  }
135
133
 
136
- private setCustomContainer(): void {
137
- const customContainer = document.createElement('div');
138
- customContainer.className = CONTAINER_CLASS_CUSTOM;
139
- this.set('container', customContainer);
140
- this.set('containerClassName', CONTAINER_CLASS_CUSTOM);
141
- }
142
-
143
134
  // 如有 customContent 则根据 customContent 设置 container
144
135
  protected initContainer() {
145
136
  super.initContainer();
@@ -147,11 +138,9 @@ class Tooltip<T extends TooltipCfg = TooltipCfg> extends HtmlComponent implement
147
138
  if (this.get('container')) {
148
139
  this.get('container').remove();
149
140
  }
150
- this.setCustomContainer();
151
- const newContainer = this.getHtmlContentNode();
152
- const customContainer: HTMLElement = this.get('container');
153
- customContainer.appendChild(newContainer);
154
- this.get('parent').appendChild(customContainer);
141
+ const container = this.getHtmlContentNode();
142
+ this.get('parent').appendChild(container);
143
+ this.set('container', container);
155
144
  this.resetStyles();
156
145
  this.applyStyles();
157
146
  }
@@ -208,22 +197,15 @@ class Tooltip<T extends TooltipCfg = TooltipCfg> extends HtmlComponent implement
208
197
 
209
198
  // 根据 customContent 渲染
210
199
  private renderCustomContent() {
211
- const newContainer = this.getHtmlContentNode();
212
- let oldContainer: HTMLElement = this.get('container');
213
- // 如果一个图表的 tooltip 一开始是默认的, 而后手动设置了 customContent 方法, 则会导致 oldContainer 是 'g2-tooltip', 但其实应该 'g2-tooltip-custom'
214
- let existG2TooltipClass = hasClass(oldContainer, CONTAINER_CLASS);
215
- if (existG2TooltipClass) {
216
- this.setCustomContainer();
217
- oldContainer = this.get('container');
218
- const parent: HTMLElement = this.get('parent');
219
- const target = parent.querySelector(`.${CONTAINER_CLASS}`);
220
- parent.removeChild(target);
221
- }
222
- oldContainer.innerHTML = '';
223
- oldContainer.appendChild(newContainer);
224
- if (existG2TooltipClass) {
225
- this.get('parent').appendChild(oldContainer);
200
+ const node = this.getHtmlContentNode();
201
+ const parent: HTMLElement = this.get('parent');
202
+ const curContainer: HTMLElement = this.get('container');
203
+ if (curContainer && curContainer.parentNode === parent) {
204
+ parent.replaceChild(node, curContainer);
205
+ } else {
206
+ parent.appendChild(node);
226
207
  }
208
+ this.set('container', node);
227
209
  this.resetStyles();
228
210
  this.applyStyles();
229
211
  }
package/src/util/event.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { IGroup } from '@antv/g-base';
1
+ import { Event, IGroup } from '@antv/g-base';
2
2
  import { Event as GraphEvent } from '@antv/g-base';
3
+ import { get } from '@antv/util';
3
4
  import { LooseObject } from '../types';
4
5
 
5
6
  /**
@@ -22,3 +23,15 @@ export function propagationDelegate(group: IGroup, eventName: string, eventObjec
22
23
  parent = parent.getParent() as IGroup;
23
24
  }
24
25
  }
26
+
27
+ /**
28
+ *
29
+ * @param e 事件对象
30
+ */
31
+ export function getCurrentPoint(e: Event) {
32
+ const event = e.originalEvent as MouseEvent;
33
+ return {
34
+ x: get(event, 'touches.0.pageX', event.pageX),
35
+ y: get(event, 'touches.0.pageY', event.pageY),
36
+ };
37
+ }
@@ -0,0 +1,41 @@
1
+ import { BBox, IGroup, IShape, LooseObject, Point } from '@antv/g-base';
2
+
3
+ export function createMask(group: IGroup, maskType: string, maskAttrs: LooseObject) {
4
+ const maskShape = group.addShape({
5
+ type: maskType,
6
+ name: 'component-mask',
7
+ attrs: {
8
+ fill: '#569CFF',
9
+ opacity: 0.16,
10
+ ...maskAttrs,
11
+ },
12
+ });
13
+ return maskShape;
14
+ }
15
+
16
+ export function updateMask(shape: IShape, maskAttrs: LooseObject) {
17
+ shape.attr(maskAttrs);
18
+ return shape;
19
+ }
20
+
21
+ // export function overBBoxLimit(bbox: BBox, curPoint: Point) {
22
+ // const { minX, maxX, minY, maxY } = bbox;
23
+ // const { x, y } = curPoint;
24
+ // if (Math.min(minX, maxX) > x || Math.max(minX, maxX) < x || Math.min(minY, maxY) > y || Math.max(minY, maxY) < y) {
25
+ // return true;
26
+ // }
27
+ // return false;
28
+ // }
29
+
30
+ export function getRectMaskAttrs(start: Point, end: Point) {
31
+ const x = Math.min(start.x, end.x);
32
+ const y = Math.min(start.y, end.y);
33
+ const width = Math.abs(end.x - start.x);
34
+ const height = Math.abs(end.y - start.y);
35
+ return {
36
+ x,
37
+ y,
38
+ width,
39
+ height,
40
+ };
41
+ }
package/src/util/util.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { IElement, IGroup } from '@antv/g-base';
2
- import { each, isArray, isNil, isNumber } from '@antv/util';
1
+ import { getOffScreenContext, IElement, IGroup } from '@antv/g-base';
2
+ import { each, isArray, isNil, isNumber, isString } from '@antv/util';
3
3
  import { BBox, Point, Region } from '../types';
4
4
 
5
5
  export function formatPadding(padding: number | number[]): number[] {
@@ -235,3 +235,65 @@ export function getTextPoint(start: Point, end: Point, position: string, offset:
235
235
  y: getValueByPercent(start.y, end.y, percent),
236
236
  };
237
237
  }
238
+
239
+ /**
240
+ * 获取字体宽度
241
+ * @param text 文本
242
+ * @param font 字体
243
+ */
244
+ export function getTextWidth(text: string, font: string) {
245
+ const context = getOffScreenContext(); // 获取离屏的 ctx 进行计算
246
+ let width = 0;
247
+ // null 或者 undefined 时,宽度为 0
248
+ if (isNil(text) || text === '') {
249
+ return width;
250
+ }
251
+ context.save();
252
+ context.font = font;
253
+ if (isString(text) && text.includes('\n')) {
254
+ const textArr = text.split('\n');
255
+ each(textArr, (subText) => {
256
+ const measureWidth = context.measureText(subText).width;
257
+ if (width < measureWidth) {
258
+ width = measureWidth;
259
+ }
260
+ });
261
+ } else {
262
+ width = context.measureText(text).width;
263
+ }
264
+ context.restore();
265
+ return width;
266
+ }
267
+
268
+ export function clipText(str: string, maxWidth: number, font: string) {
269
+ // 日期字段根据空格拆为两组:'2022-10-12 08:23' => ['2022-10-12', '08:23']
270
+ if (Date.parse(str)) {
271
+ return str.split(' ').join('\n');
272
+ }
273
+
274
+ function mapStr(str: string, callback: (i: number) => void) {
275
+ for (let i = 0; i < str.length; i++) {
276
+ const curStr = str.slice(0, i + 1);
277
+ const curWidth = getTextWidth(curStr, '12');
278
+ if (curWidth > maxWidth) {
279
+ callback(i);
280
+ return;
281
+ }
282
+ }
283
+ }
284
+
285
+ let firstStr = '';
286
+ let secondStr = '';
287
+ // 文字拆分为两组
288
+ mapStr(str, (i: number) => {
289
+ firstStr = str.slice(0, i);
290
+ secondStr = str.slice(i);
291
+ });
292
+
293
+ // 第二行超出宽度时加省略号
294
+ mapStr(secondStr, (i: number) => {
295
+ secondStr = secondStr.slice(0, i - 1) + '...';
296
+ });
297
+
298
+ return firstStr + '\n' + secondStr;
299
+ }