Skip to content

Commit 3fa2e6e

Browse files
devvaannshabose
authored andcommitted
fix: keep focus where it was when live preview is asked to select
1 parent 18b7c08 commit 3fa2e6e

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

‎src/LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol.js‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,24 @@ define(function (require, exports, module) {
209209
document.body.dispatchEvent(event);
210210
}
211211

212+
// A selection Phoenix asked the live preview to make - the layers panel picking
213+
// an element - never took focus away from the editor side, so there is nothing
214+
// to restore and pulling focus into the editor would take it off whatever asked
215+
// for the selection. Time boxed so a selection that never reports back cannot
216+
// leave the next real click in the preview without its focus.
217+
const KEEP_FOCUS_WINDOW_MS = 1500;
218+
let _keepFocusUntil = 0;
219+
220+
function keepFocusOnNextSelect() {
221+
_keepFocusUntil = Date.now() + KEEP_FOCUS_WINDOW_MS;
222+
}
223+
224+
function _shouldKeepFocus() {
225+
return Date.now() < _keepFocusUntil;
226+
}
227+
212228
function _focusEditorIfNeeded(editor, tagName, contentEditable) {
213-
if (WorkspaceManager.isInDesignMode()) {
229+
if (WorkspaceManager.isInDesignMode() || _shouldKeepFocus()) {
214230
return;
215231
}
216232
const focusShouldBeInLivePreview = ['INPUT', 'TEXTAREA'].includes(tagName) || contentEditable;
@@ -271,7 +287,7 @@ define(function (require, exports, module) {
271287
activeEditorPath = activeEditor ? activeEditor.document.file.fullPath : null,
272288
activeFullEditorPath = activeFullEditor ? activeFullEditor.document.file.fullPath : null;
273289
if(!liveDocPath){
274-
if (activeEditor && !WorkspaceManager.isInDesignMode()) {
290+
if (activeEditor && !WorkspaceManager.isInDesignMode() && !_shouldKeepFocus()) {
275291
activeEditor.focus(); // restore focus from live preview
276292
}
277293
return;
@@ -384,6 +400,7 @@ define(function (require, exports, module) {
384400
} catch (e) {
385401
console.error("error in tag selection", e);
386402
}
403+
_keepFocusUntil = 0;
387404
editMode && liveDoc && liveDoc.disableHighlightOnCursorActivity(false);
388405
liveDoc && liveDoc.updateHighlight();
389406
} else {
@@ -767,6 +784,7 @@ define(function (require, exports, module) {
767784
exports.setLivePreviewMessageHandler = setLivePreviewMessageHandler;
768785
exports.setCustomRemoteFunctionProvider = setCustomRemoteFunctionProvider;
769786
// lp communication functions
787+
exports.keepFocusOnNextSelect = keepFocusOnNextSelect;
770788
exports.registerPhoenixFn = registerPhoenixFn;
771789
exports.triggerLPFn = triggerLPFn;
772790
exports.LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME = LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME;

0 commit comments

Comments
 (0)