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

feat: panning modifiers add space #4166

Merged
merged 2 commits into from
Jan 24, 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
30 changes: 28 additions & 2 deletions packages/x6/src/graph/panning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class PanningManager extends Base {
private clientX: number
private clientY: number
private mousewheelHandle: Dom.MouseWheelHandle
private isSpaceKeyPressed: boolean

protected get widgetOptions() {
return this.options.panning
Expand All @@ -17,6 +18,8 @@ export class PanningManager extends Base {

protected init() {
this.onRightMouseDown = this.onRightMouseDown.bind(this)
this.onSpaceKeyDown = this.onSpaceKeyDown.bind(this)
this.onSpaceKeyUp = this.onSpaceKeyUp.bind(this)
this.startListening()
this.updateClassName()
}
Expand All @@ -26,6 +29,10 @@ export class PanningManager extends Base {
this.graph.on('node:unhandled:mousedown', this.onMouseDown, this)
this.graph.on('edge:unhandled:mousedown', this.onMouseDown, this)
Dom.Event.on(this.graph.container, 'mousedown', this.onRightMouseDown)
Dom.Event.on(document.body, {
keydown: this.onSpaceKeyDown,
keyup: this.onSpaceKeyUp,
})
this.mousewheelHandle = new Dom.MouseWheelHandle(
this.graph.container,
this.onMouseWheel.bind(this),
Expand All @@ -39,15 +46,24 @@ export class PanningManager extends Base {
this.graph.off('node:unhandled:mousedown', this.onMouseDown, this)
this.graph.off('edge:unhandled:mousedown', this.onMouseDown, this)
Dom.Event.off(this.graph.container, 'mousedown', this.onRightMouseDown)
Dom.Event.off(document.body, {
keydown: this.onSpaceKeyDown,
keyup: this.onSpaceKeyUp,
})
if (this.mousewheelHandle) {
this.mousewheelHandle.disable()
}
}

allowPanning(e: Dom.MouseDownEvent, strict?: boolean) {
;(e as any).spaceKey = this.isSpaceKeyPressed
return (
this.pannable &&
ModifierKey.isMatch(e, this.widgetOptions.modifiers, strict)
ModifierKey.isMatch(
e,
this.widgetOptions.modifiers as ModifierKey,
strict,
)
)
}

Expand Down Expand Up @@ -129,6 +145,16 @@ export class PanningManager extends Base {
this.graph.translateBy(-deltaX, -deltaY)
}

protected onSpaceKeyDown(e: Dom.KeyDownEvent) {
if (e.which === 32) {
this.isSpaceKeyPressed = true
}
}
protected onSpaceKeyUp(e: Dom.KeyUpEvent) {
if (e.which === 32) {
this.isSpaceKeyPressed = false
}
}
protected allowBlankMouseDown(e: Dom.MouseDownEvent) {
const eventTypes = this.widgetOptions.eventTypes
return (
Expand Down Expand Up @@ -200,7 +226,7 @@ export namespace PanningManager {
| 'mouseWheelDown'
export interface Options {
enabled?: boolean
modifiers?: string | ModifierKey[] | null
modifiers?: string | Array<ModifierKey | 'space'> | null
eventTypes?: EventType[]
}
}
2 changes: 1 addition & 1 deletion sites/x6-sites/docs/api/graph/panning.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ interface Options {
`ModifierKey` 的类型定义如下:

```ts
type ModifierKey = string | ('alt' | 'ctrl' | 'meta' | 'shift')[] | null
type ModifierKey = string | ('alt' | 'ctrl' | 'meta' | 'shift' | 'space')[] | null
```

支持以下几种形式:
Expand Down
Loading