Files
wekan/client/lib/autofocus.js
T
Lauri Ojansivu 9c0f408a00 Autofocus no longer takes the keyboard from a field being typed in, nor replaces its text.
client/lib/autofocus.js focused an [autofocus] element whenever a re-render
added one: the LAST [autofocus] anywhere in the document, selecting its
text, 50 ms later - and decided before those 50 ms whether it was already
focused. On the import page, typing the attachment token after pasting a
Jira export moved the focus to the pasted JSON, all of it selected, and the
token replaced it: the import then said "Your text is not valid JSON"
(import-attachment-auth.e2e.js in Chromium). A focus() then select() on the
textarea, recorded in the browser and found at autofocus.js in the running
bundle, showed it.

Now only an [autofocus] element that was itself just added is a candidate,
and whether to focus it is decided when the focus happens: never while
the user is typing in another field (an input, textarea, select or
contenteditable). Opening a popup or a form still focuses its field -
focus is then on the button that opened it, or on a composer field the
re-render removed. client/lib/autofocusDecision.js is the decision;
tests/autofocusDecision.test.cjs drives it with stand-in elements.

Thanks to xet7 !
2026-10-28 00:27:24 +02:00

54 lines
1.8 KiB
JavaScript

// Native replacement for mquandalle:autofocus package
// Handles autofocus attribute in dynamically rendered Blaze templates
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
const { autofocusTarget } = require('./autofocusDecision');
// Use MutationObserver to watch for elements with autofocus attribute
Meteor.startup(() => {
// Focus the newest of the [autofocus] elements just added - decided when
// the focus happens, 50 ms later, not before: in between the user may have
// gone on typing in another field, and focusing and selecting then replaced
// what they had pasted (client/lib/autofocusDecision.js).
const focusAdded = added => {
setTimeout(() => {
const el = autofocusTarget(added, document.activeElement, document.body);
if (!el) return;
el.focus();
// For textareas and text inputs, also select the content if it exists
if ((el.tagName === 'TEXTAREA' || (el.tagName === 'INPUT' && el.type === 'text')) && el.value) {
el.select();
}
}, 50);
};
// Watch for DOM changes
const observer = new MutationObserver((mutations) => {
const added = [];
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === 1) { // Element node
if (node.hasAttribute && node.hasAttribute('autofocus')) {
added.push(node);
} else if (node.querySelectorAll) {
added.push(...node.querySelectorAll('[autofocus]'));
}
}
});
});
if (added.length) {
Tracker.afterFlush(() => focusAdded(added));
}
});
// Start observing
observer.observe(document.body, {
childList: true,
subtree: true,
});
// Also handle initial autofocus elements
Tracker.afterFlush(() => focusAdded([...document.querySelectorAll('[autofocus]')]));
});