Skip to content

Commit

Permalink
feat: support direction for drag-canvas behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
yvonneyx committed Aug 20, 2024
1 parent ab97b6a commit 2813259
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/g6/__tests__/unit/behaviors/drag-canvas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ describe('behavior drag canvas', () => {
await expect(graph).toMatchSnapshot(__filename);
});

it('drag in the x-axis direction', () => {
graph.updateBehavior({ key: 'drag-canvas', direction: 'x' });

const [x, y] = graph.getPosition();
graph.emit(CommonEvent.KEY_DOWN, { key: 'ArrowRight' });
graph.emit(CommonEvent.KEY_UP, { key: 'ArrowRight' });
graph.emit(CommonEvent.KEY_DOWN, { key: 'ArrowDown' });
graph.emit(CommonEvent.KEY_UP, { key: 'ArrowDown' });

expect(graph.getPosition()).toBeCloseTo([x + 20, y]);
});

it('drag in the y-axis direction', () => {
graph.updateBehavior({ key: 'drag-canvas', direction: 'y' });

const [x, y] = graph.getPosition();
graph.emit(CommonEvent.KEY_DOWN, { key: 'ArrowRight' });
graph.emit(CommonEvent.KEY_UP, { key: 'ArrowRight' });
graph.emit(CommonEvent.KEY_DOWN, { key: 'ArrowDown' });
graph.emit(CommonEvent.KEY_UP, { key: 'ArrowDown' });

expect(graph.getPosition()).toBeCloseTo([x, y + 20]);
graph.updateBehavior({ key: 'drag-canvas', direction: 'both' });
});

it('onFinish with key', async () => {
const onFinish = jest.fn();
graph.updateBehavior({ key: 'drag-canvas', onFinish });
Expand Down
25 changes: 24 additions & 1 deletion packages/g6/src/behaviors/drag-canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ export interface DragCanvasOptions extends BaseBehaviorOptions {
* @defaultValue true
*/
enable?: boolean | ((event: IPointerEvent | IKeyboardEvent) => boolean);
/**
* <zh/> 允许拖拽方向
* - `'x'`: 只允许水平拖拽
* - `'y'`: 只允许垂直拖拽
* - `'both'`: 不受限制,允许水平和垂直拖拽
*
* <en/> Allowed drag direction
* - `'x'`: Only allow horizontal drag
* - `'y'`: Only allow vertical drag
* - `'both'`: Allow horizontal and vertical drag
* @defaultValue `'both'`
*/
direction?: 'x' | 'y' | 'both';
/**
* <zh/> 触发拖拽的方式,默认使用指针按下拖拽
*
Expand Down Expand Up @@ -66,6 +79,7 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
return true;
},
sensitivity: 10,
direction: 'both',
};

private shortcut: Shortcut;
Expand Down Expand Up @@ -155,7 +169,16 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
* @internal
*/
protected async translate(offset: Vector2, animation?: ViewportAnimationEffectTiming) {
await this.context.graph.translateBy(offset, animation);
let [dx, dy] = offset;

const { direction } = this.options;
if (direction === 'x') {
dy = 0;
} else if (direction === 'y') {
dx = 0;
}

await this.context.graph.translateBy([dx, dy], animation);
}

private validate(event: IPointerEvent | IKeyboardEvent) {
Expand Down

0 comments on commit 2813259

Please sign in to comment.