f2e9c73b31
* Prefer key over keyCode * Do not close TS pages on Esc when floating elements are open * Close pop-up when Escape is pressed regardless of keepOnKeyup * Close help modals when Escape is pressed * Avoid duplicate handling of Esc in WithFloating * Formatting * Handle closing of preset management modals * Reset text input modal to initial value
35 lines
1021 B
TypeScript
35 lines
1021 B
TypeScript
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
import { on } from "@tslib/events";
|
|
|
|
interface ModalClosingHandler {
|
|
set: (value: boolean) => void;
|
|
remove: () => void;
|
|
}
|
|
|
|
/**
|
|
* Register a keydown handler on the document that can optionally stop propagation to other handlers if Escape is pressed and the associated flag is set.
|
|
* Intended to override the general handler in webview.py when a modal is open.
|
|
*/
|
|
function registerModalClosingHandler(callback?: () => void): ModalClosingHandler {
|
|
let modalIsOpen = false;
|
|
|
|
function set(value: boolean) {
|
|
modalIsOpen = value;
|
|
}
|
|
|
|
const remove = on(document, "keydown", (event) => {
|
|
if (event.key === "Escape" && modalIsOpen) {
|
|
event.stopImmediatePropagation();
|
|
if (callback) {
|
|
callback();
|
|
}
|
|
}
|
|
}, { capture: true });
|
|
|
|
return { set, remove };
|
|
}
|
|
|
|
export { registerModalClosingHandler };
|