-
-
Notifications
You must be signed in to change notification settings - Fork 643
/
keyboard.ts
212 lines (189 loc) · 5.14 KB
/
keyboard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {Handler} from '../handler_manager';
import type {Map} from '../map';
import {TransformProvider} from './transform-provider';
const defaultOptions = {
panStep: 100,
bearingStep: 15,
pitchStep: 10
};
/**
* The `KeyboardHandler` allows the user to zoom, rotate, and pan the map using
* the following keyboard shortcuts:
*
* - `=` / `+`: Increase the zoom level by 1.
* - `Shift-=` / `Shift-+`: Increase the zoom level by 2.
* - `-`: Decrease the zoom level by 1.
* - `Shift--`: Decrease the zoom level by 2.
* - Arrow keys: Pan by 100 pixels.
* - `Shift+⇢`: Increase the rotation by 15 degrees.
* - `Shift+⇠`: Decrease the rotation by 15 degrees.
* - `Shift+⇡`: Increase the pitch by 10 degrees.
* - `Shift+⇣`: Decrease the pitch by 10 degrees.
*
* @group Handlers
*/
export class KeyboardHandler implements Handler {
_tr: TransformProvider;
_enabled: boolean;
_active: boolean;
_panStep: number;
_bearingStep: number;
_pitchStep: number;
_rotationDisabled: boolean;
/** @internal */
constructor(map: Map) {
this._tr = new TransformProvider(map);
const stepOptions = defaultOptions;
this._panStep = stepOptions.panStep;
this._bearingStep = stepOptions.bearingStep;
this._pitchStep = stepOptions.pitchStep;
this._rotationDisabled = false;
}
reset() {
this._active = false;
}
keydown(e: KeyboardEvent) {
if (e.altKey || e.ctrlKey || e.metaKey) return;
let zoomDir = 0;
let bearingDir = 0;
let pitchDir = 0;
let xDir = 0;
let yDir = 0;
switch (e.keyCode) {
case 61:
case 107:
case 171:
case 187:
zoomDir = 1;
break;
case 189:
case 109:
case 173:
zoomDir = -1;
break;
case 37:
if (e.shiftKey) {
bearingDir = -1;
} else {
e.preventDefault();
xDir = -1;
}
break;
case 39:
if (e.shiftKey) {
bearingDir = 1;
} else {
e.preventDefault();
xDir = 1;
}
break;
case 38:
if (e.shiftKey) {
pitchDir = 1;
} else {
e.preventDefault();
yDir = -1;
}
break;
case 40:
if (e.shiftKey) {
pitchDir = -1;
} else {
e.preventDefault();
yDir = 1;
}
break;
default:
return;
}
if (this._rotationDisabled) {
bearingDir = 0;
pitchDir = 0;
}
return {
cameraAnimation: (map: Map) => {
const tr = this._tr;
map.easeTo({
duration: 300,
easeId: 'keyboardHandler',
easing: easeOut,
zoom: zoomDir ? Math.round(tr.zoom) + zoomDir * (e.shiftKey ? 2 : 1) : tr.zoom,
bearing: tr.bearing + bearingDir * this._bearingStep,
pitch: tr.pitch + pitchDir * this._pitchStep,
offset: [-xDir * this._panStep, -yDir * this._panStep],
center: tr.center
}, {originalEvent: e});
}
};
}
/**
* Enables the "keyboard rotate and zoom" interaction.
*
* @example
* ```ts
* map.keyboard.enable();
* ```
*/
enable() {
this._enabled = true;
}
/**
* Disables the "keyboard rotate and zoom" interaction.
*
* @example
* ```ts
* map.keyboard.disable();
* ```
*/
disable() {
this._enabled = false;
this.reset();
}
/**
* Returns a Boolean indicating whether the "keyboard rotate and zoom"
* interaction is enabled.
*
* @returns `true` if the "keyboard rotate and zoom"
* interaction is enabled.
*/
isEnabled() {
return this._enabled;
}
/**
* Returns true if the handler is enabled and has detected the start of a
* zoom/rotate gesture.
*
* @returns `true` if the handler is enabled and has detected the
* start of a zoom/rotate gesture.
*/
isActive() {
return this._active;
}
/**
* Disables the "keyboard pan/rotate" interaction, leaving the
* "keyboard zoom" interaction enabled.
*
* @example
* ```ts
* map.keyboard.disableRotation();
* ```
*/
disableRotation() {
this._rotationDisabled = true;
}
/**
* Enables the "keyboard pan/rotate" interaction.
*
* @example
* ```ts
* map.keyboard.enable();
* map.keyboard.enableRotation();
* ```
*/
enableRotation() {
this._rotationDisabled = false;
}
}
function easeOut(t: number) {
return t * (2 - t);
}