Skip to content

Commit 6067a89

Browse files
authored
Merge pull request #3321 from VisActor/refactor/tooltip-dom
refactor: refactor dom tooltip of vchart
2 parents 7b5fcd7 + b127cd2 commit 6067a89

30 files changed

+688
-1555
lines changed

packages/vchart/src/plugin/components/tooltip-handler/base.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { DEFAULT_CHART_WIDTH, DEFAULT_CHART_HEIGHT } from '../../../constant/base';
2-
import type { Options } from './constants';
32
// eslint-disable-next-line no-duplicate-imports
43
import { DEFAULT_OPTIONS } from './constants';
54
import type { Maybe, IPoint, RenderMode } from '../../../typings';
@@ -28,21 +27,20 @@ import {
2827
import type { IGroup } from '@visactor/vrender-core';
2928
import type { AABBBounds } from '@visactor/vutils';
3029
// eslint-disable-next-line no-duplicate-imports
31-
import { isNumber, isObject, isValidNumber, isValid, throttle, isNil, isFunction } from '@visactor/vutils';
30+
import { isNumber, isObject, isValidNumber, isValid, isFunction } from '@visactor/vutils';
3231
import type { IElement } from '@visactor/vgrammar-core';
3332
import type { ILayoutModel } from '../../../model/interface';
3433
import type { Compiler } from '../../../compile/compiler';
3534
import type { IContainerSize } from '@visactor/vrender-components';
36-
import { getTooltipAttributes } from './utils/attribute';
3735
import type { IChartOption } from '../../../chart/interface';
3836
import type { ITooltipSpec, Tooltip, TooltipHandlerParams } from '../../../component/tooltip';
3937
// eslint-disable-next-line no-duplicate-imports
4038
import { TooltipResult } from '../../../component/tooltip';
4139
import type { IComponentPlugin, IComponentPluginService } from '../interface';
4240
import { BasePlugin } from '../../base/base-plugin';
43-
import type { ITooltipAttributes } from './interface';
4441
import { getTooltipPatternValue } from '../../../component/tooltip/utils';
4542
import type { IDimensionData, IDimensionInfo } from '../../../event/events/dimension/interface';
43+
import type { ITooltipHandlerOptions } from './interface';
4644

4745
type ChangeTooltipFunc = (visible: boolean, params: TooltipHandlerParams, data?: TooltipData) => TooltipResult;
4846

@@ -62,7 +60,7 @@ export abstract class BaseTooltipHandler extends BasePlugin implements ITooltipH
6260

6361
// protected _style: ITooltipStyle = {};
6462

65-
protected _option: Options;
63+
protected _option: ITooltipHandlerOptions;
6664

6765
protected _chartOption: IChartOption;
6866

@@ -72,7 +70,6 @@ export abstract class BaseTooltipHandler extends BasePlugin implements ITooltipH
7270
}
7371

7472
protected _component: Tooltip;
75-
protected _attributes?: ITooltipAttributes | null = null;
7673

7774
protected _chartContainer: Maybe<HTMLElement>;
7875
protected _compiler: Compiler;
@@ -192,16 +189,17 @@ export abstract class BaseTooltipHandler extends BasePlugin implements ITooltipH
192189

193190
protected abstract _updateTooltip(visible: boolean, params: TooltipHandlerParams): void;
194191
protected abstract _removeTooltip(): void;
192+
protected abstract _getTooltipBoxSize(
193+
actualTooltip: ITooltipActual,
194+
changePositionOnly: boolean
195+
): IContainerSize | undefined;
195196

196197
/* -----需要子类继承的方法结束----- */
197198

198-
protected _getDefaultOption(): Options {
199+
protected _getDefaultOption(): ITooltipHandlerOptions {
199200
const { offset } = this._component.getSpec();
200-
return {
201-
...DEFAULT_OPTIONS,
202-
offsetX: offset?.x ?? DEFAULT_OPTIONS.offsetX,
203-
offsetY: offset?.y ?? DEFAULT_OPTIONS.offsetY
204-
};
201+
202+
return offset ? { ...DEFAULT_OPTIONS, ...offset } : DEFAULT_OPTIONS;
205203
}
206204

207205
/**
@@ -444,23 +442,6 @@ export abstract class BaseTooltipHandler extends BasePlugin implements ITooltipH
444442
return result;
445443
};
446444

447-
// 计算 tooltip 内容区域的宽高,并缓存结果
448-
protected _getTooltipBoxSize(actualTooltip: ITooltipActual, changePositionOnly: boolean): IContainerSize | undefined {
449-
if (!changePositionOnly || isNil(this._attributes)) {
450-
const chartTheme = this._chartOption?.getTheme() ?? {};
451-
this._attributes = getTooltipAttributes(actualTooltip, this._component.getSpec(), chartTheme);
452-
}
453-
const { panel, panelDomHeight } = this._attributes ?? {};
454-
// canvas模式下, size需要考虑border size, 目的是为了精准判断边界是否超出画布,达到confine效果
455-
// html模式不提供confine, 所以不考虑精准计算size
456-
const isCanvas = this._component.getSpec().renderMode === 'canvas';
457-
458-
return {
459-
width: panel?.width + (isCanvas ? panel.lineWidth : 0),
460-
height: (panelDomHeight ?? panel?.height) + (isCanvas ? panel.lineWidth : 0)
461-
};
462-
}
463-
464445
protected _getParentElement(spec: ITooltipSpec): HTMLElement {
465446
return spec.parentElement as any;
466447
}

packages/vchart/src/plugin/components/tooltip-handler/canvas/canvas-tooltip-handler.ts renamed to packages/vchart/src/plugin/components/tooltip-handler/canvas-tooltip-handler.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import type { ILayer, INode, Stage } from '@visactor/vrender-core';
2-
import { BaseTooltipHandler } from '../base';
2+
import { BaseTooltipHandler } from './base';
33
import { Tooltip as TooltipComponent } from '@visactor/vrender-components';
4-
import { isValid } from '@visactor/vutils';
5-
import type { TooltipHandlerParams } from '../../../../component/tooltip';
6-
import type { IComponentPluginService } from '../../interface';
7-
import { registerComponentPlugin } from '../../register';
8-
import { TooltipHandlerType } from '../../../../component/tooltip/constant';
4+
import { isValid, isNil } from '@visactor/vutils';
5+
import type { TooltipHandlerParams } from '../../../component/tooltip';
6+
import type { IComponentPluginService } from '../interface';
7+
import { registerComponentPlugin } from '../register';
8+
import { TooltipHandlerType } from '../../../component/tooltip/constant';
9+
import type { ITooltipActual } from '../../../typings';
10+
import type { IContainerSize } from '@visactor/vrender-components';
11+
import { getTooltipAttributes } from './utils/attribute';
12+
import type { ITooltipAttributes } from './interface';
913

1014
/**
1115
* The tooltip handler class.
@@ -18,6 +22,7 @@ export class CanvasTooltipHandler extends BaseTooltipHandler {
1822
protected _el?: HTMLCanvasElement;
1923
protected _tooltipCanvasId?: string;
2024
protected _tooltipComponent: TooltipComponent;
25+
protected _attributes?: ITooltipAttributes | null = null;
2126

2227
constructor() {
2328
super(CanvasTooltipHandler.type);
@@ -55,6 +60,22 @@ export class CanvasTooltipHandler extends BaseTooltipHandler {
5560
return this._layer;
5661
}
5762

63+
// 计算 tooltip 内容区域的宽高,并缓存结果
64+
protected _getTooltipBoxSize(actualTooltip: ITooltipActual, changePositionOnly: boolean): IContainerSize | undefined {
65+
if (!changePositionOnly || isNil(this._attributes)) {
66+
const chartTheme = this._chartOption?.getTheme() ?? {};
67+
this._attributes = getTooltipAttributes(actualTooltip, this._component.getSpec(), chartTheme);
68+
}
69+
const { panel, panelDomHeight } = this._attributes ?? {};
70+
// canvas模式下, size需要考虑border size, 目的是为了精准判断边界是否超出画布,达到confine效果
71+
// html模式不提供confine, 所以不考虑精准计算size
72+
73+
return {
74+
width: panel.width + panel.lineWidth,
75+
height: (panelDomHeight ?? panel.height) + panel.lineWidth
76+
};
77+
}
78+
5879
protected _removeTooltip() {
5980
if (this._layer) {
6081
this._layer.removeAllChild();

packages/vchart/src/plugin/components/tooltip-handler/canvas/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { escapeHTML } from './utils/common';
1+
import type { ITooltipHandlerOptions } from './interface';
22

3-
export const TOOLTIP_CONTAINER_EL_CLASS_NAME = 'vchart-tooltip-container';
3+
export const TOOLTIP_PREFIX = 'vchart-tooltip';
4+
export const TOOLTIP_CONTAINER_EL_CLASS_NAME = `${TOOLTIP_PREFIX}-container`;
5+
export const TOOLTIP_CONTENT_BOX_CLASS_NAME = `${TOOLTIP_PREFIX}-content-box`;
46
export const TOOLTIP_EMPTY_STRING = '';
57

6-
export const DEFAULT_OPTIONS = {
8+
export const DEFAULT_OPTIONS: ITooltipHandlerOptions = {
79
/**
810
* X offset.
911
*/
@@ -12,15 +14,6 @@ export const DEFAULT_OPTIONS = {
1214
/**
1315
* Y offset.
1416
*/
15-
offsetY: 10,
16-
17-
/**
18-
* HTML sanitizer function that removes dangerous HTML to prevent XSS.
19-
*
20-
* This should be a function from string to string. You may replace it with a formatter such as a markdown formatter.
21-
*/
22-
sanitize: escapeHTML
17+
offsetY: 10
2318
};
24-
25-
// FIXME: 命名规范
26-
export type Options = typeof DEFAULT_OPTIONS;
19+
export const DEFAULT_TOOLTIP_Z_INDEX = '99999999999999';

0 commit comments

Comments
 (0)