fix: retry editor focus after panel open (shortcut focus race)

This commit is contained in:
Fabio Montefuscolo 2026-08-03 22:20:07 +02:00
parent fe12a15a43
commit 5affe87f21
2 changed files with 22 additions and 1 deletions

View file

@ -17,7 +17,7 @@
<button id="open-list" title="View all notes" aria-label="View all notes">☰</button> <button id="open-list" title="View all notes" aria-label="View all notes">☰</button>
</div> </div>
<textarea id="editor" placeholder="Write your note…" spellcheck="true"></textarea> <textarea id="editor" placeholder="Write your note…" spellcheck="true" autofocus></textarea>
<div id="find-bar" hidden> <div id="find-bar" hidden>
<input type="search" id="find-input" placeholder="Find in note…"> <input type="search" id="find-input" placeholder="Find in note…">

View file

@ -497,10 +497,31 @@ document.getElementById('open-list').addEventListener('click', () => {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Init // Init
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Chrome does not always grant focus to the panel document the moment it
// opens (notably via the keyboard shortcut), so the focus() call in
// loadNoteForTab can silently fail. Retry briefly after init, stopping as
// soon as the editor has focus or the user interacts with anything else.
function retryEditorFocus() {
let attempts = 0;
const timer = setInterval(() => {
attempts++;
if (document.activeElement === editor || attempts > 10) {
clearInterval(timer);
return;
}
editor.focus();
}, 100);
const stop = () => clearInterval(timer);
window.addEventListener('pointerdown', stop, { capture: true, once: true });
window.addEventListener('keydown', stop, { capture: true, once: true });
}
(async function init() { (async function init() {
const tabId = await resolveHostTab(); const tabId = await resolveHostTab();
if (tabId != null) { if (tabId != null) {
await loadNoteForTab(tabId); await loadNoteForTab(tabId);
retryEditorFocus();
} else { } else {
pageTitleEl.textContent = 'Side Notes'; pageTitleEl.textContent = 'Side Notes';
updateStatus('Could not detect active tab'); updateStatus('Could not detect active tab');