Update the GenericAppMixin so that it persists focus better

This commit is contained in:
Eldritch-Oliver 2025-10-12 18:01:47 -06:00
parent 28989c2d35
commit 4a8ce9b099

View file

@ -37,6 +37,8 @@ export function GenericAppMixin(HandlebarsApp) {
_popoverManagers = new Map();
/** @type {Map<number, string>} */
_hookIDs = new Map();
/** @type {string | null} */
#focus = null;
// #endregion
// #region Lifecycle
@ -53,6 +55,26 @@ export function GenericAppMixin(HandlebarsApp) {
};
};
/**
* @override
* This method overrides Foundry's default behaviour for caching the focused
* element so that it actually works when the application has a root partial
*/
async _preRender(...args) {
if (this.rendered) {
const target = this.element.querySelector(`:focus`);
if (target) {
if (target.id) {
this.#focus = `#${CSS.escape(target.id)}`;
}
else if (target.name) {
this.#focus = `${target.tagName}[name="${target.name}"]`;
};
};
};
return super._preRender(...args);
};
/** @override */
async _onRender(...args) {
await super._onRender(...args);
@ -83,11 +105,25 @@ export function GenericAppMixin(HandlebarsApp) {
});
};
async _prepareContext() {
/**
* @override
* This method overrides Foundry's default behaviour for caching the focused
* element so that it actually works when the application has a root partial
*/
async _postRender(...args) {
if (this.rendered) {
const target = this.element.querySelector(this.#focus);
target?.focus();
};
this.#focus = null;
return super._postRender(...args);
};
async _prepareContext(_options) {
const ctx = {};
ctx.meta ??= {};
ctx.meta.idp = this.document?.uuid ?? this.id;
ctx.meta.idp = this.id;
if (this.document) {
ctx.meta.limited = this.document.limited;
ctx.meta.editable = this.isEditable || game.user.isGM;