Unintuive behaviour with editor_did_unfocus_field hook

  1. Open note browser with at least two notes
  2. In editor for note A, focus a field input that will trigger the editor_did_unfocus_field for some addon
  3. Without clicking anywhere else click on note B
  4. In editor for note B, click somewhere else
  5. → Focus is lost in the field input that was previously focused, triggering the hook. The hook receives note B as the note arg.

Example with addon triggering a slow API call

What I’d expect to happen
Nothing. After switching to note B, the field doesn’t appear focused so the hook triggering seems weird. Typing will not edit the field but clicking somewhere still triggers the hook, so the input appears to still be invisibly focused in some way.

I’d expect the editor_did_unfocus_field hook to be triggered only when there is clearly an input in focus, then it gets unfocused. In this case, the event is leaving one note editor and opening another which doesn’t look or feel like leaving one field input which is what the editor_did_unfocus_field feels like it should do.

This isn’t so bad when the the triggered addon code takes a fraction of a second to complete. In my addon, the hook triggers an AI API call that takes 3-5 seconds. It feels annoying because I was not intending to trigger it when switching notes and then I’m stucking waiting for it to finish.

The reason why this happens might be related to the other editor focusing bug, some focused flag getting left over when switching the note in the editor? Note editor jumps when previous focus was in HTML editor

1 Like

This appears to fix it, but it needs more testing:

diff --git a/ts/editor/NoteEditor.svelte b/ts/editor/NoteEditor.svelte
index 17ced575b..1c3407246 100644
--- a/ts/editor/NoteEditor.svelte
+++ b/ts/editor/NoteEditor.svelte
@@ -679,7 +679,7 @@ the AddCards dialog) should be implemented in the user of this component.

     {#if !$ioMaskEditorVisible}
         <Fields>
-            {#each fieldsData as field, index}
+            {#each fieldsData as field, index ([noteId, index])}
                 {@const content = fieldStores[index]}

                 <EditorField

I might look into this more for the new editor (#4029), but the hook is broken there at the moment anyway.

2 Likes