Skip to content

Commit

Permalink
js: Added global Esc key event handler to close top-most dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
cederberg committed Sep 7, 2024
1 parent dbcce68 commit 3e2a58a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/plugin/system/files/js/RapidContext_Widget_Dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ RapidContext.Widget.Dialog = function (attrs/*, ... */) {
RapidContext.Widget._widgetMixin(o, RapidContext.Widget.Dialog);
o.classList.add("widgetDialog");
o._setHidden(true);
o.setAttrs(Object.assign({ modal: false, system: false, center: true }, attrs));
let def = { modal: false, system: false, center: true, resizeable: true, closeable: true };
o.setAttrs(Object.assign(def, attrs));
o.addAll(Array.from(arguments).slice(1));
o.on("click", "[data-dialog]", o._handleClick);
o.on("mousedown", "[data-dialog]", o._handleMouseDown);
Expand Down Expand Up @@ -371,3 +372,15 @@ RapidContext.Widget.Dialog.prototype.resetScroll = function () {
}
Array.from(this.querySelectorAll("*")).forEach(scrollReset);
};

// Add global keydown handler
window.addEventListener("DOMContentLoaded", () => {
document.body.addEventListener("keydown", (evt) => {
if (evt.key == "Escape") {
let isVisible = (el) => el.offsetHeight > 0;
let dlgs = Array.from(document.body.querySelectorAll(".widgetDialog")).filter(isVisible);
let last = dlgs[dlgs.length - 1];
last && last.closeable && last.hide();
}
});
});

0 comments on commit 3e2a58a

Please sign in to comment.