fix: retry editor focus after panel open (shortcut focus race)
This commit is contained in:
parent
fe12a15a43
commit
5affe87f21
2 changed files with 22 additions and 1 deletions
|
|
@ -17,7 +17,7 @@
|
|||
<button id="open-list" title="View all notes" aria-label="View all notes">☰</button>
|
||||
</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>
|
||||
<input type="search" id="find-input" placeholder="Find in note…">
|
||||
|
|
|
|||
|
|
@ -497,10 +497,31 @@ document.getElementById('open-list').addEventListener('click', () => {
|
|||
// ---------------------------------------------------------------------------
|
||||
// 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() {
|
||||
const tabId = await resolveHostTab();
|
||||
if (tabId != null) {
|
||||
await loadNoteForTab(tabId);
|
||||
retryEditorFocus();
|
||||
} else {
|
||||
pageTitleEl.textContent = 'Side Notes';
|
||||
updateStatus('Could not detect active tab');
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue