Skip to content

Commit 396cc88

Browse files
committed
Refactor keyboard event handling for map zoom and toggle list functionality. Keyboard shortcuts now also work when focused off the map
1 parent 528a5ca commit 396cc88

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

pcd-website/src/components/MapView.vue

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -206,25 +206,25 @@ function handleKeydown(e: KeyboardEvent) {
206206
}
207207
}
208208
} else if (!isTextInput && /^[0-9]$/.test(e.key)) {
209-
const mapEl = document.getElementById('map');
210-
if (mapEl && (mapEl.contains(document.activeElement) || document.activeElement === mapEl)) {
211-
e.preventDefault();
212-
e.stopPropagation();
213-
if (e.key === '0') {
214-
mapInstance?.setView([20, 10], 3);
215-
} else {
216-
mapInstance?.setZoom(parseInt(e.key));
217-
}
218-
}
219-
} else if (e.key === 'M' || e.key === 'm') {
220-
if (!isTextInput) {
221-
const mapEl = document.getElementById('map');
222-
if (mapEl && (mapEl.contains(document.activeElement) || document.activeElement === mapEl)) {
223-
e.preventDefault();
224-
listOpen.value = !listOpen.value;
225-
if (listOpen.value) selectedNode.value = null;
226-
}
209+
e.preventDefault();
210+
e.stopPropagation();
211+
if (e.key === '0') {
212+
mapInstance?.setView([20, 10], 3);
213+
} else {
214+
mapInstance?.setZoom(parseInt(e.key));
227215
}
216+
} else if (!isTextInput && (e.key === '+' || e.key === '=' || e.key === 'Add')) {
217+
e.preventDefault();
218+
e.stopPropagation();
219+
mapInstance?.zoomIn();
220+
} else if (!isTextInput && (e.key === '-' || e.key === 'Subtract')) {
221+
e.preventDefault();
222+
e.stopPropagation();
223+
mapInstance?.zoomOut();
224+
} else if (!isTextInput && (e.key === 'M' || e.key === 'm')) {
225+
e.preventDefault();
226+
listOpen.value = !listOpen.value;
227+
if (listOpen.value) selectedNode.value = null;
228228
}
229229
}
230230
@@ -243,11 +243,13 @@ onMounted(async () => {
243243
mapInstance = map;
244244
leafletRef = L;
245245
246-
// Remove digit keyCodes (48–57) from Leaflet's built-in zoom handler so our
247-
// number-key zoom shortcuts don't conflict (e.g. keyCode 54 = '6' is zoom-out).
246+
// Remove digit (48–57) and +/- keyCodes from Leaflet's built-in zoom handler
247+
// so our global shortcuts don't double-fire when the map is focused.
248248
const kb = (map as any).keyboard;
249249
if (kb?._zoomKeys) {
250250
for (let code = 48; code <= 57; code++) delete kb._zoomKeys[code];
251+
// +/= (187, 61) and -/_ (189, 173) — Leaflet's default zoom-in/out keys
252+
for (const code of [61, 173, 187, 189]) delete kb._zoomKeys[code];
251253
}
252254
253255
L.control.zoom({ position: 'topleft' }).addTo(map);

0 commit comments

Comments
 (0)