maplair
Version:
JavaScript library for mobile-friendly interactive maps
190 lines (189 loc) • 6.96 kB
JavaScript
import { stamp } from "../../core/Util.js";
import { Point } from "../../geometry/Point.js";
import { disableClickPropagation, disableScrollPropagation, off, on, stop } from "../../dom/DomEvent.js";
import { create } from "../../dom/DomUtil.js";
import { I18n } from "../../core/I18n.js";
import { Handler } from "../../core/Handler.js";
// src/map/handler/KeyboardHandler.js
/** @import {MaplairMap} from '../Map.js' */
/** @import {MaplairEvent} from '../../core/Events.js' */
/**
* Map options of `KeyboardHandler`, the keyboard navigation options.
* @typedef {object} KeyboardHandlerMapOptions
* @lends MaplairMapOptions
* @property {boolean} [keyboard=true] Makes the map focusable and allows users to
* navigate the map with keyboard arrows and `+`/`-` keys.
* @property {number} [keyboardPanDelta=80] Amount of pixels to pan when pressing an
* arrow key.
* @property {boolean|string} [ariaKeyShortcuts=false] Sets the `aria-keyshortcuts`
* attribute of the map container. ARIA names keys by what they type on a given layout,
* so `true` uses the names for a US English layout, which can be wrong for pages in
* other languages; pass a string to set the value for your audience's layout instead.
*/
/**
* Handler of `MaplairMap` for keyboard navigation.
* @typedef {object} KeyboardHandlerMapProperties
* @lends MaplairMap
* @property {KeyboardHandler} keyboard Keyboard navigation handler.
*/
/**
* Handles keyboard interaction with the map, enabled by default, see the map option
* `keyboard`. The `?` key shows a list of the keyboard shortcuts.
*
* @fires MaplairMap#focus {MaplairEvent} Fired when the map gains focus.
* @fires MaplairMap#blur {MaplairEvent} Fired when the map loses focus.
*/
var KeyboardHandler = class KeyboardHandler extends Handler {
/**
* The [values](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)
* of the keys for panning and zooming the map, so zoom keys follow the keyboard
* layout and cover the numpad too; `=` and `_` keep the US `+` and `-` keys working
* both with and without Shift.
* @type {Record<'left'|'right'|'down'|'up'|'zoomIn'|'zoomOut', string[]>}
*/
static keys = {
left: ["ArrowLeft"],
right: ["ArrowRight"],
down: ["ArrowDown"],
up: ["ArrowUp"],
zoomIn: ["+", "="],
zoomOut: ["-", "_"]
};
/**
* @param {MaplairMap} map
*/
constructor(map) {
super(map);
this.callInitHooks(KeyboardHandler);
}
/**
* Called when the handler is enabled, adds the event hooks.
*/
addHooks() {
const container = this._map._container;
if (container.tabIndex <= 0) container.tabIndex = 0;
const shortcuts = this._map.options.ariaKeyShortcuts;
if (shortcuts) container.setAttribute("aria-keyshortcuts", shortcuts === true ? "ArrowLeft ArrowRight ArrowUp ArrowDown = Plus -" : shortcuts);
on(container, {
focus: this._onFocus,
blur: this._onBlur,
pointerdown: this._onPointerDown,
keydown: this._onContainerKeyDown
}, this);
}
/**
* Called when the handler is disabled, removes the event hooks.
*/
removeHooks() {
off(document, "keydown", this._onKeyDown, this);
if (this._map.options.ariaKeyShortcuts) this._map._container.removeAttribute("aria-keyshortcuts");
off(this._map._container, {
focus: this._onFocus,
blur: this._onBlur,
pointerdown: this._onPointerDown,
keydown: this._onContainerKeyDown
}, this);
this._help?.remove();
this._help = null;
}
_toggleHelp() {
this._help ??= this._createHelp();
this._help.togglePopover();
if (this._help.matches(":popover-open")) this._help.querySelector(".maplair-keyboard-help-close").focus();
}
_createHelp() {
const help = create("dialog", "maplair-keyboard-help", this._map._container), heading = create("h2", "", help), close = create("button", "maplair-keyboard-help-close", help);
help.popover = "auto";
heading.id = `maplair-keyboard-help-${stamp(this)}`;
heading.textContent = I18n.translate("Keyboard shortcuts");
help.setAttribute("aria-labelledby", heading.id);
close.type = "button";
close.popoverTargetElement = help;
close.popoverTargetAction = "hide";
close.innerHTML = "<span aria-hidden=\"true\">×</span>";
close.title = I18n.translate("Close");
close.setAttribute("aria-label", close.title);
const list = create("dl", "", help);
for (const [keys, description] of [
[[
"←",
"→",
"↑",
"↓"
], I18n.translate("Pan the map")],
[["+"], I18n.translate("Zoom in")],
[["−"], I18n.translate("Zoom out")],
[["Shift"], I18n.translate("Pan or zoom further, together with the keys above")],
[["Esc"], I18n.translate("Close popup")],
[["?"], I18n.translate("Show or hide the keyboard shortcuts")]
]) {
const row = create("div", "", list), dt = create("dt", "", row);
for (const key of keys) create("kbd", "", dt).textContent = key;
create("dd", "", row).textContent = description;
}
disableClickPropagation(help);
disableScrollPropagation(help);
return help;
}
_onPointerDown() {
if (this._focused || this._map._container.contains(document.activeElement)) return;
this._map._container.focus({
preventScroll: true,
focusVisible: false
});
}
_onFocus() {
this._focused = true;
on(document, "keydown", this._onKeyDown, this);
this._map.fire("focus");
}
_onBlur() {
this._focused = false;
off(document, "keydown", this._onKeyDown, this);
this._map.fire("blur");
}
_onContainerKeyDown(e) {
const map = this._map, popup = map._popup;
if (e.key !== "Escape" || e.defaultPrevented || !popup?.isOpen() || !popup.options.closeOnEscapeKey || map._controlContainer?.contains(e.target)) return;
map.closePopup();
e.preventDefault();
}
_onKeyDown(e) {
if (e.altKey || e.ctrlKey || e.metaKey) return;
if (e.key === "?") {
this._toggleHelp();
stop(e);
return;
}
const key = e.key, map = this._map, { left, right, down, up, zoomIn, zoomOut } = KeyboardHandler.keys, x = +right.includes(key) - +left.includes(key), y = +down.includes(key) - +up.includes(key), dz = +zoomIn.includes(key) - +zoomOut.includes(key), k = e.shiftKey ? 3 : 1;
if (x || y) {
if (!map._panAnim?._inProgress) {
let offset = new Point(x, y).multiplyBy(k * map.options.keyboardPanDelta);
if (map.options.maxBounds) offset = map._limitOffset(offset, map.options.maxBounds);
map._setUserEvent(e);
map.panBy(offset);
}
} else if (dz) {
if (!map._zoomInputBlocked()) {
map._setUserEvent(e);
map.setZoom(map.getZoom() + k * dz * map.options.zoomDelta);
}
} else return;
stop(e);
}
/**
* Registers this default feature on the map class, called by `MaplairMap` itself.
* @internal
* @param {typeof MaplairMap} MaplairMap
*/
static register(MaplairMap) {
MaplairMap.mergeOptions({
keyboard: true,
keyboardPanDelta: 80,
ariaKeyShortcuts: false
});
MaplairMap.addInitHook("addHandler", "keyboard", KeyboardHandler);
}
};
export { KeyboardHandler };
//# sourceMappingURL=KeyboardHandler.js.map