fix(plugins): fix tooltip with incorrect position after graph resize (#6246)

Co-authored-by: antv <antv@antfin.com>
This commit is contained in:
Aaron 2024-08-29 10:00:20 +08:00 committed by GitHub
parent 4fb0fd4bbd
commit 35d917cbf0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 98 additions and 17 deletions

View File

@ -0,0 +1,53 @@
import { Graph } from '@antv/g6';
export const bugTooltipResize: TestCase = async (context) => {
const graph = new Graph({
...context,
data: {
nodes: [{ id: 'node1' }, { id: 'node2' }, { id: 'node3' }, { id: 'node4' }, { id: 'node5' }],
edges: [
{ source: 'node1', target: 'node2' },
{ source: 'node1', target: 'node3' },
{ source: 'node1', target: 'node4' },
{ source: 'node2', target: 'node3' },
{ source: 'node3', target: 'node4' },
{ source: 'node4', target: 'node5' },
],
},
layout: {
type: 'grid',
},
plugins: [
{
type: 'tooltip',
style: {
['.tooltip']: {
transition: 'none',
},
},
},
],
});
await graph.render();
bugTooltipResize.form = (panel) => {
let width = 500;
return [
panel.add(
{
resize: () => {
const newWidth = width === 500 ? 300 : 500;
width = newWidth;
document.querySelector<HTMLDivElement>('#container')!.style.width = `${newWidth}px`;
graph.resize();
graph.fitView();
},
},
'resize',
),
];
};
return graph;
};

View File

@ -19,6 +19,7 @@ export { behaviorLassoSelect } from './behavior-lasso-select';
export { behaviorOptimizeViewportTransform } from './behavior-optimize-viewport-transform'; export { behaviorOptimizeViewportTransform } from './behavior-optimize-viewport-transform';
export { behaviorScrollCanvas } from './behavior-scroll-canvas'; export { behaviorScrollCanvas } from './behavior-scroll-canvas';
export { behaviorZoomCanvas } from './behavior-zoom-canvas'; export { behaviorZoomCanvas } from './behavior-zoom-canvas';
export { bugTooltipResize } from './bug-tooltip-resize';
export { canvasCursor } from './canvas-cursor'; export { canvasCursor } from './canvas-cursor';
export { caseDecisionTree } from './case-decision-tree'; export { caseDecisionTree } from './case-decision-tree';
export { caseIndentedTree } from './case-indented-tree'; export { caseIndentedTree } from './case-indented-tree';

View File

@ -271,6 +271,7 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
}; };
} }
this.tooltipElement.update({ this.tooltipElement.update({
...this.tooltipStyleProps,
x, x,
y, y,
style: { style: {
@ -300,7 +301,7 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
this.tooltipElement.hide(x, y); this.tooltipElement.hide(x, y);
}; };
private initTooltip = () => { private get tooltipStyleProps() {
const { canvas } = this.context; const { canvas } = this.context;
const { center } = canvas.getBounds(); const { center } = canvas.getBounds();
const $container = canvas.getContainer() as HTMLElement; const $container = canvas.getContainer() as HTMLElement;
@ -308,24 +309,24 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
const { style, position, enterable, container = { x: -left, y: -top }, title, offset } = this.options; const { style, position, enterable, container = { x: -left, y: -top }, title, offset } = this.options;
const [x, y] = center; const [x, y] = center;
const [width, height] = canvas.getSize(); const [width, height] = canvas.getSize();
const tooltipElement = new TooltipComponent({
className: 'tooltip', return {
style: {
x, x,
y, y,
container, container,
title, title,
bounding: { bounding: { x: 0, y: 0, width, height },
x: 0,
y: 0,
width,
height,
},
position, position,
enterable, enterable,
offset, offset,
style, style,
}, };
}
private initTooltip = () => {
const tooltipElement = new TooltipComponent({
className: 'tooltip',
style: this.tooltipStyleProps,
}); });
this.container?.appendChild(tooltipElement.HTMLTooltipElement); this.container?.appendChild(tooltipElement.HTMLTooltipElement);
return tooltipElement; return tooltipElement;

View File

@ -0,0 +1,26 @@
import { expect, test } from '@playwright/test';
test.describe('plugin tooltip', () => {
test('bug: tooltip should has correct position after graph resize', async ({ page }) => {
page.goto('/?Demo=bugTooltipResize&Renderer=canvas&GridLine=true&Theme=light&Animation=false');
await page.waitForSelector('.tooltip');
const clip = { x: 0, y: 0, width: 500, height: 500 };
await page.mouse.move(375, 250);
await expect(page).toHaveScreenshot({ clip });
await page.mouse.move(250, 250);
// wait for div content is 'resize'
const resize = page.getByRole('button', { name: 'resize' });
await resize?.click();
await page.mouse.move(285, 250);
await expect(page).toHaveScreenshot({ clip });
});
});