From 812cded291fc961a3fb449d107aa42d7747282f7 Mon Sep 17 00:00:00 2001 From: Dmitry S Date: Mon, 12 Jun 2023 18:39:53 -0400 Subject: [PATCH] (core) Fix flaky FormulaEditor test by adjusting focus behavior of FormulaEditor. Summary: The issue is in the app due to the possibility of subtle differences in order of events. It's hard to trigger a wrong order, but the fix is intended to make it impossible. Test Plan: Running test with a bunch of iterations, to see how reliable it is Reviewers: georgegevoian Reviewed By: georgegevoian Subscribers: georgegevoian Differential Revision: https://phab.getgrist.com/D3918 --- app/client/widgets/FormulaEditor.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/client/widgets/FormulaEditor.ts b/app/client/widgets/FormulaEditor.ts index 6c818488..903b5c14 100644 --- a/app/client/widgets/FormulaEditor.ts +++ b/app/client/widgets/FormulaEditor.ts @@ -375,7 +375,15 @@ export class FormulaEditor extends NewBaseEditor { } // Resize editor in case it is needed. this._formulaEditor.resize(); + + // This focus method will try to focus a textarea immediately and again on setTimeout. But + // other things may happen by the setTimeout time, messing up focus. The reason the immediate + // call doesn't usually help is that this is called on 'mousedown' before its corresponding + // focus/blur occur. We can do a bit better by restoring focus immediately after blur occurs. aceObj.focus(); + const lis = dom.onElem(aceObj.textInput.getElement(), 'blur', e => { lis.dispose(); aceObj.focus(); }); + // If no blur right away, clear the listener, to avoid unexpected interference. + setTimeout(() => lis.dispose(), 0); } }