@@ -3,8 +3,22 @@ import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';
3
3
import { CanvasModuleBase } from 'features/controlLayers/konva/CanvasModuleBase' ;
4
4
import type { CanvasToolModule } from 'features/controlLayers/konva/CanvasTool/CanvasToolModule' ;
5
5
import { getPrefixedId } from 'features/controlLayers/konva/util' ;
6
+ import type { Coordinate } from 'features/controlLayers/store/types' ;
6
7
import type { Logger } from 'roarr' ;
7
8
9
+ type CanvasMoveToolModuleConfig = {
10
+ /**
11
+ * The number of pixels to nudge the entity by when moving with the arrow keys.
12
+ */
13
+ NUDGE_PX : number ;
14
+ } ;
15
+
16
+ const DEFAULT_CONFIG : CanvasMoveToolModuleConfig = {
17
+ NUDGE_PX : 1 ,
18
+ } ;
19
+
20
+ type NudgeKey = 'ArrowLeft' | 'ArrowRight' | 'ArrowUp' | 'ArrowDown' ;
21
+
8
22
export class CanvasMoveToolModule extends CanvasModuleBase {
9
23
readonly type = 'move_tool' ;
10
24
readonly id : string ;
@@ -13,6 +27,9 @@ export class CanvasMoveToolModule extends CanvasModuleBase {
13
27
readonly manager : CanvasManager ;
14
28
readonly log : Logger ;
15
29
30
+ config : CanvasMoveToolModuleConfig = DEFAULT_CONFIG ;
31
+ nudgeOffsets : Record < NudgeKey , Coordinate > ;
32
+
16
33
constructor ( parent : CanvasToolModule ) {
17
34
super ( ) ;
18
35
this . id = getPrefixedId ( this . type ) ;
@@ -21,6 +38,17 @@ export class CanvasMoveToolModule extends CanvasModuleBase {
21
38
this . path = this . manager . buildPath ( this ) ;
22
39
this . log = this . manager . buildLogger ( this ) ;
23
40
this . log . debug ( 'Creating module' ) ;
41
+
42
+ this . nudgeOffsets = {
43
+ ArrowLeft : { x : - this . config . NUDGE_PX , y : 0 } ,
44
+ ArrowRight : { x : this . config . NUDGE_PX , y : 0 } ,
45
+ ArrowUp : { x : 0 , y : - this . config . NUDGE_PX } ,
46
+ ArrowDown : { x : 0 , y : this . config . NUDGE_PX } ,
47
+ } ;
48
+ }
49
+
50
+ isNudgeKey ( key : string ) : key is NudgeKey {
51
+ return this . nudgeOffsets [ key as NudgeKey ] !== undefined ;
24
52
}
25
53
26
54
syncCursorStyle = ( ) => {
@@ -33,26 +61,44 @@ export class CanvasMoveToolModule extends CanvasModuleBase {
33
61
}
34
62
} ;
35
63
36
- onKeyDown = ( e : KeyboardEvent ) => {
37
- // Support moving via arrow keys
38
- const OFFSET = 1 ; // How much to move, in px
39
- const offsets : Record < string , { x : number ; y : number } > = {
40
- ArrowLeft : { x : - OFFSET , y : 0 } ,
41
- ArrowRight : { x : OFFSET , y : 0 } ,
42
- ArrowUp : { x : 0 , y : - OFFSET } ,
43
- ArrowDown : { x : 0 , y : OFFSET } ,
44
- } ;
45
- const { key } = e ;
64
+ nudge = ( nudgeKey : NudgeKey ) => {
65
+ if ( $focusedRegion . get ( ) !== 'canvas' ) {
66
+ return ;
67
+ }
68
+
46
69
const selectedEntity = this . manager . stateApi . getSelectedEntityAdapter ( ) ;
47
- const { x : offsetX = 0 , y : offsetY = 0 } = offsets [ key ] || { } ;
70
+
71
+ if ( ! selectedEntity ) {
72
+ return ;
73
+ }
48
74
49
75
if (
50
- ! ( selectedEntity && selectedEntity . $isInteractable . get ( ) && $focusedRegion . get ( ) === 'canvas' ) ||
51
- ( offsetX === 0 && offsetY === 0 )
76
+ selectedEntity . $isDisabled . get ( ) ||
77
+ selectedEntity . $isEmpty . get ( ) ||
78
+ selectedEntity . $isLocked . get ( ) ||
79
+ selectedEntity . $isEntityTypeHidden . get ( )
52
80
) {
53
- return ; // Early return if no entity is selected or it is disabled or canvas is not focused
81
+ return ;
82
+ }
83
+
84
+ const isBusy = this . manager . $isBusy . get ( ) ;
85
+ const isMoveToolSelected = this . parent . $tool . get ( ) === 'move' ;
86
+ const isThisEntityTransforming = this . manager . stateApi . $transformingAdapter . get ( ) === selectedEntity ;
87
+
88
+ if ( isBusy ) {
89
+ // When the canvas is busy, we shouldn't allow nudging - except when the canvas is busy transforming the selected
90
+ // entity. Nudging is allowed during transformation, regardless of the selected tool.
91
+ if ( ! isThisEntityTransforming ) {
92
+ return ;
93
+ }
94
+ } else {
95
+ // Otherwise, the canvas is not busy, and we should only allow nudging when the move tool is selected.
96
+ if ( ! isMoveToolSelected ) {
97
+ return ;
98
+ }
54
99
}
55
100
56
- selectedEntity . transformer . nudgePosition ( offsetX , offsetY ) ;
101
+ const offset = this . nudgeOffsets [ nudgeKey ] ;
102
+ selectedEntity . transformer . nudgeBy ( offset ) ;
57
103
} ;
58
104
}
0 commit comments