Skip to content

Commit

Permalink
fix: optimize code style
Browse files Browse the repository at this point in the history
  • Loading branch information
aMoonkin committed Oct 27, 2023
1 parent 63877a6 commit 62e1eec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
14 changes: 9 additions & 5 deletions packages/x6-plugin-selection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,7 @@ export class Selection
}

protected onBlankMouseDown({ e }: EventArgs['blank:mousedown']) {
const eventTypes = this.options.eventTypes
if (
!(eventTypes && eventTypes.includes('leftMouseDown') && e.button === 0) &&
!(eventTypes && eventTypes.includes('mouseWheelDown') && e.button === 1)
) {
if (!this.allowBlankMouseDown(e)) {
return
}

Expand All @@ -339,6 +335,14 @@ export class Selection
}
}

protected allowBlankMouseDown(e: Dom.MouseDownEvent) {
const eventTypes = this.options.eventTypes
return (
(eventTypes?.includes('leftMouseDown') && e.button === 0) ||
(eventTypes?.includes('mouseWheelDown') && e.button === 1)
)
}

protected onBlankClick() {
this.clean()
}
Expand Down
21 changes: 13 additions & 8 deletions packages/x6/src/graph/panning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,10 @@ export class PanningManager extends Base {
}

protected onMouseDown({ e }: { e: Dom.MouseDownEvent }) {
const eventTypes = this.widgetOptions.eventTypes
if (
!(eventTypes && eventTypes.includes('leftMouseDown') && e.button === 0) &&
!(eventTypes && eventTypes.includes('mouseWheelDown') && e.button === 1)
) {
if (!this.allowBlankMouseDown(e)) {
return
}

const selection = this.graph.getPlugin<any>('selection')
const allowRubberband = selection && selection.allowRubberband(e, true)
if (
Expand All @@ -120,24 +117,32 @@ export class PanningManager extends Base {

protected onRightMouseDown(e: Dom.MouseDownEvent) {
const eventTypes = this.widgetOptions.eventTypes
if (!(eventTypes && eventTypes.includes('rightMouseDown'))) {
if (!(eventTypes?.includes('rightMouseDown') && e.button === 2)) {
return
}
if (e.button === 2 && this.allowPanning(e, true)) {
if (this.allowPanning(e, true)) {
this.startPanning(e)
}
}

protected onMouseWheel(e: WheelEvent, deltaX: number, deltaY: number) {
const eventTypes = this.widgetOptions.eventTypes
if (!(eventTypes && eventTypes.includes('mouseWheel'))) {
if (!eventTypes?.includes('mouseWheel')) {
return
}
if (!e.ctrlKey) {
this.graph.translateBy(-deltaX, -deltaY)
}
}

protected allowBlankMouseDown(e: Dom.MouseDownEvent) {
const eventTypes = this.widgetOptions.eventTypes
return (
(eventTypes?.includes('leftMouseDown') && e.button === 0) ||
(eventTypes?.includes('mouseWheelDown') && e.button === 1)
)
}

protected allowMouseWheel(e: WheelEvent) {
return this.pannable && !e.ctrlKey
}
Expand Down

0 comments on commit 62e1eec

Please sign in to comment.