Skip to content

Commit

Permalink
refactor(behaviors): adjust scroll-canvas (#5686)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca authored Apr 28, 2024
1 parent d4891f7 commit 6986bb6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
26 changes: 17 additions & 9 deletions packages/g6/__tests__/demos/behavior-scroll-canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,28 @@ export const behaviorScrollCanvas: TestCase = async (context) => {
},
behaviors: [
{
key: 'scroll-canvas',
type: 'scroll-canvas',
// direction: 'x',
// direction: 'y',
// sensitivity: 5,
// trigger: {
// up: ['ArrowUp'],
// down: ['ArrowDown'],
// right: ['ArrowRight'],
// left: ['ArrowLeft'],
// },
},
],
});

behaviorScrollCanvas.form = (panel) => {
const config = {
direction: '',
sensitivity: 1,
};

panel.onChange(() => {
graph.updateBehavior({
key: 'scroll-canvas',
...config,
});
});

return [panel.add(config, 'direction', ['xy', 'x', 'y']), panel.add(config, 'sensitivity', 1, 10, 1)];
};

await graph.render();

return graph;
Expand Down
1 change: 1 addition & 0 deletions packages/g6/__tests__/unit/behaviors/scroll-canvas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('behavior scroll canvas', () => {
it('default status', () => {
expect(graph.getBehaviors()).toEqual([
{
key: 'scroll-canvas',
type: 'scroll-canvas',
},
]);
Expand Down
24 changes: 13 additions & 11 deletions packages/g6/src/behaviors/scroll-canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface ScrollCanvasOptions extends BaseBehaviorOptions {
* <zh/> 是否启用滚动画布的功能
*
* <en/> Whether to enable the function of scrolling the canvas
* @defaultValue true
*/
enable?: boolean | ((event: WheelEvent | IKeyboardEvent) => boolean);
/**
Expand All @@ -20,23 +21,30 @@ export interface ScrollCanvasOptions extends BaseBehaviorOptions {
*/
trigger?: CombinationKey;
/**
* <zh/> 允许的滚动方向。选项有:"x"、"y",默认情况下没有限制
* <zh/> 允许的滚动方向
* - 默认情况下没有限制
* - x : 只允许水平滚动
* - y : 只允许垂直滚动
*
* <en/> The allowed rolling direction. The options are "x" and "y", with no restrictions by default
* <en/> The allowed rolling direction
* - by default, there is no restriction
* - x: only allow horizontal scrolling
* - y: only allow vertical scrolling
*/
direction?: 'x' | 'y';
/**
* <zh/> 滚动灵敏度
*
* <en/> Scroll sensitivity
* @defaultValue 1
*/
sensitivity?: number;
/**
* <zh/> 完成滚动时的回调
*
* <en/> Callback when scrolling is completed
*/
onfinish?: () => void;
onFinish?: () => void;
}

type CombinationKey = {
Expand All @@ -46,12 +54,6 @@ type CombinationKey = {
right: ShortcutKey;
};

type EnableOptions = {
node?: boolean;
edge?: boolean;
combo?: boolean;
};

export class ScrollCanvas extends BaseBehavior<ScrollCanvasOptions> {
static defaultOptions: ScrollCanvasOptions = {
enable: true,
Expand Down Expand Up @@ -118,11 +120,11 @@ export class ScrollCanvas extends BaseBehavior<ScrollCanvasOptions> {

private async scroll(value: Point, event: WheelEvent | IKeyboardEvent) {
if (!this.validate(event)) return;
const { onfinish } = this.options;
const { onFinish } = this.options;
const graph = this.context.graph;
const formattedValue = this.formatDisplacement(value);
await graph.translateBy(formattedValue, false);
onfinish?.();
onFinish?.();
}

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

0 comments on commit 6986bb6

Please sign in to comment.