Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: snapline and brush-select keep lineWidth after canvas zoom #6198

Merged
merged 6 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/g6/__tests__/demos/plugin-snapline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const pluginSnapline: TestCase = async (context) => {
labelText: (datum) => datum.id,
},
},
behaviors: ['drag-element', 'drag-canvas'],
behaviors: ['drag-element', 'drag-canvas', 'zoom-canvas'],
plugins: [
{
type: 'snapline',
Expand All @@ -36,6 +36,7 @@ export const pluginSnapline: TestCase = async (context) => {
const config = {
filter: false,
offset: 20,
autoSnap: false,
};

pluginSnapline.form = (panel) => {
Expand All @@ -48,7 +49,6 @@ export const pluginSnapline: TestCase = async (context) => {
key: 'snapline',
filter: (node: Node) => (filter ? node.id !== 'node3' : true),
});
graph.render();
}),
panel
.add(config, 'offset', [0, 20, Infinity])
Expand All @@ -58,7 +58,15 @@ export const pluginSnapline: TestCase = async (context) => {
key: 'snapline',
offset,
});
graph.render();
}),
panel
.add(config, 'autoSnap')
.name('Auto Snap')
.onChange((autoSnap: boolean) => {
graph.updatePlugin({
key: 'snapline',
autoSnap,
});
}),
];
};
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions packages/g6/__tests__/snapshots/plugins/snapline/zoom-5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions packages/g6/__tests__/unit/behaviors/brush-select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ describe('behavior brush select', () => {

graph.emit(CanvasEvent.CLICK);
await expect(graph).toMatchSnapshot(__filename, 'brush-clear-mode-intersect');

// zoom to test line width
graph.zoomTo(5);
graph.emit(CommonEvent.POINTER_DOWN, { canvas: { x: 100, y: 100 }, targetType: 'canvas' });
graph.emit(CommonEvent.POINTER_MOVE, { canvas: { x: 250, y: 400 } });

await expect(graph).toMatchSnapshot(__filename, 'brush-selecting-zoom');
graph.emit(CommonEvent.POINTER_UP, { canvas: { x: 250, y: 400 } });
});

afterAll(() => {
Expand Down
13 changes: 12 additions & 1 deletion packages/g6/__tests__/unit/plugins/snapline.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Node, NodeEvent, type Graph } from '@/src';
import type { Graph, Node } from '@/src';
import { NodeEvent } from '@/src';
import { pluginSnapline } from '@@/demos';
import { createDemoGraph } from '../../utils';

Expand Down Expand Up @@ -75,6 +76,16 @@ describe('plugin snapline', () => {
graph.emit(NodeEvent.DRAG, { target: node, dx: 0, dy: 0 });
await expect(graph).toMatchSnapshot(__filename, `auto-snap`);
graph.emit(NodeEvent.DRAG_END, { target: node });

// zoom to test lineWidth
graph.zoomTo(5, false, [300, 300]);
graph.updatePlugin({ key: 'snapline', autoSnap: false });
graph.updateNodeData([{ id: 'node3', style: { x: 260, y: 300 } }]);
graph.render();
graph.emit(NodeEvent.DRAG_START, { target: node, targetType: 'node' });
graph.emit(NodeEvent.DRAG, { target: node, dx: 0, dy: 0 });
await expect(graph).toMatchSnapshot(__filename, 'zoom-5');
graph.emit(NodeEvent.DRAG_END, { target: node });
});

afterAll(() => {
Expand Down
18 changes: 18 additions & 0 deletions packages/g6/__tests__/unit/utils/shortcut.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ describe('shortcut', () => {
expect(drag).toHaveBeenCalledTimes(1);
expect(drag.mock.calls[0][0].deltaX).toBe(10);
expect(drag.mock.calls[0][0].deltaY).toBe(0);

drag.mockClear();

// shift drag
emitter.emit(CommonEvent.KEY_DOWN, { key: 'Shift' });
emitter.emit(CommonEvent.DRAG, { deltaX: 10, deltaY: 0 });
emitter.emit(CommonEvent.KEY_UP, { key: 'Shift' });
expect(drag).toHaveBeenCalledTimes(0);

shortcut.unbindAll();
shortcut.bind(['Shift', 'drag'], drag);

emitter.emit(CommonEvent.KEY_DOWN, { key: 'Shift' });
emitter.emit(CommonEvent.DRAG, { deltaX: 10, deltaY: 0 });
emitter.emit(CommonEvent.KEY_UP, { key: 'Shift' });
expect(drag).toHaveBeenCalledTimes(1);
expect(drag.mock.calls[0][0].deltaX).toBe(10);
expect(drag.mock.calls[0][0].deltaY).toBe(0);
});

it('focus', () => {
Expand Down
17 changes: 12 additions & 5 deletions packages/g6/src/behaviors/brush-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,16 @@ export class BrushSelect extends BaseBehavior<BrushSelectOptions> {
*/
protected onPointerDown(event: IPointerEvent) {
if (!this.validate(event) || !this.isKeydown() || this.startPoint) return;
const { canvas } = this.context;
const { canvas, graph } = this.context;
const style = { ...this.options.style };

this.rectShape = new Rect({ id: 'g6-brush-select', style: this.options.style });
// 根据缩放比例调整 lineWidth
// Adjust lineWidth according to the zoom ratio
if (this.options.style.lineWidth) {
style.lineWidth = +this.options.style.lineWidth / graph.getZoom();
}

this.rectShape = new Rect({ id: 'g6-brush-select', style });
canvas.appendChild(this.rectShape);

this.startPoint = [event.canvas.x, event.canvas.y];
Expand Down Expand Up @@ -327,8 +334,7 @@ export class BrushSelect extends BaseBehavior<BrushSelectOptions> {
protected isKeydown(): boolean {
const { trigger } = this.options;
const keys = (Array.isArray(trigger) ? trigger : [trigger]) as string[];
if (!trigger || keys.includes('drag')) return true;
return this.shortcut!.match(keys);
return this.shortcut!.match(keys.filter((key) => key !== 'drag'));
}

/**
Expand All @@ -348,7 +354,6 @@ export class BrushSelect extends BaseBehavior<BrushSelectOptions> {

private bindEvents() {
const { graph } = this.context;
this.unbindEvents();

graph.on(CommonEvent.POINTER_DOWN, this.onPointerDown);
graph.on(CommonEvent.POINTER_MOVE, this.onPointerMove);
Expand All @@ -373,7 +378,9 @@ export class BrushSelect extends BaseBehavior<BrushSelectOptions> {
* @internal
*/
public update(options: Partial<BrushSelectOptions>) {
this.unbindEvents();
this.options = deepMix(this.options, options);
this.bindEvents();
}

/**
Expand Down
Loading
Loading