Skip to content

Commit 9cfd89a

Browse files
committed
fix: live preview getting scrolled even when the element is completely in viewport
1 parent a5c832b commit 9cfd89a

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,7 @@ function RemoteFunctions(config = {}) {
188188
var html = window.document.documentElement;
189189
return (
190190
rect.top >= 0 &&
191-
rect.left >= 0 &&
192-
rect.bottom <= (window.innerHeight || html.clientHeight) &&
193-
rect.right <= (window.innerWidth || html.clientWidth)
191+
rect.bottom <= (window.innerHeight || html.clientHeight)
194192
);
195193
}
196194

@@ -3211,25 +3209,15 @@ function RemoteFunctions(config = {}) {
32113209
window.document.body.appendChild(highlight);
32123210
},
32133211

3214-
// shouldAutoScroll is whether to scroll page to element if not in view
3215-
// true when user clicks on the source code of some element, in that case we want to scroll the live preview
3216-
add: function (element, doAnimation, shouldAutoScroll) {
3212+
add: function (element, doAnimation) {
32173213
if (this._elementExists(element) || element === window.document) {
32183214
return;
32193215
}
32203216
if (this.trigger) {
32213217
_trigger(element, "highlight", 1);
32223218
}
32233219

3224-
if (shouldAutoScroll && (!window.event || window.event instanceof MessageEvent) && !isInViewport(element)) {
3225-
var top = getDocumentOffsetTop(element);
3226-
if (top) {
3227-
top -= (window.innerHeight / 2);
3228-
window.scrollTo(0, top);
3229-
}
3230-
}
32313220
this.elements.push(element);
3232-
32333221
this._makeHighlightDiv(element, doAnimation);
32343222
},
32353223

@@ -3263,7 +3251,7 @@ function RemoteFunctions(config = {}) {
32633251

32643252
this.clear();
32653253
for (i = 0; i < highlighted.length; i++) {
3266-
this.add(highlighted[i], false, false); // 3rd arg is for auto-scroll
3254+
this.add(highlighted[i], false);
32673255
}
32683256
}
32693257
};
@@ -3281,7 +3269,7 @@ function RemoteFunctions(config = {}) {
32813269
if (_validEvent(event)) {
32823270
const element = event.target;
32833271
if(isElementEditable(element) && element.nodeType === Node.ELEMENT_NODE ) {
3284-
_localHighlight.add(element, true, false); // false means no-auto scroll
3272+
_localHighlight.add(element, true);
32853273
}
32863274
}
32873275
}
@@ -3353,7 +3341,7 @@ function RemoteFunctions(config = {}) {
33533341
element._originalBackgroundColor = element.style.backgroundColor;
33543342
element.style.backgroundColor = "rgba(0, 162, 255, 0.2)";
33553343

3356-
_hoverHighlight.add(element, false, false); // false means no auto-scroll
3344+
_hoverHighlight.add(element, false);
33573345

33583346
// Create info box for the hovered element
33593347
dismissNodeInfoBox();
@@ -3378,6 +3366,21 @@ function RemoteFunctions(config = {}) {
33783366
}
33793367
}
33803368

3369+
function scrollElementToViewPort(element) {
3370+
if (!element) {
3371+
return;
3372+
}
3373+
3374+
// Check if element is in viewport, if not scroll to it
3375+
if (!isInViewport(element)) {
3376+
let top = getDocumentOffsetTop(element);
3377+
if (top) {
3378+
top -= (window.innerHeight / 2);
3379+
window.scrollTo(0, top);
3380+
}
3381+
}
3382+
}
3383+
33813384
/**
33823385
* this function is responsible to select an element in the live preview
33833386
* @param {Element} element - The DOM element to select
@@ -3386,10 +3389,16 @@ function RemoteFunctions(config = {}) {
33863389
// dismiss all UI boxes and cleanup previous element state when selecting a different element
33873390
dismissUIAndCleanupState();
33883391
dismissImageRibbonGallery();
3392+
3393+
// this should always happen before isElementEditable check because this is not a live preview edit feature
3394+
// this should also be there when users are in highlight mode
3395+
scrollElementToViewPort(element);
3396+
33893397
if(!isElementEditable(element)) {
33903398
return false;
33913399
}
33923400

3401+
33933402
// make sure that the element is actually visible to the user
33943403
if (isElementVisible(element)) {
33953404
_nodeMoreOptionsBox = new NodeMoreOptionsBox(element);
@@ -3416,7 +3425,7 @@ function RemoteFunctions(config = {}) {
34163425

34173426
if (_hoverHighlight) {
34183427
_hoverHighlight.clear();
3419-
_hoverHighlight.add(element, true, false); // false means no auto-scroll
3428+
_hoverHighlight.add(element, true);
34203429
}
34213430

34223431
previouslyClickedElement = element;
@@ -3497,7 +3506,7 @@ function RemoteFunctions(config = {}) {
34973506
_clickHighlight.clear();
34983507
}
34993508
if (isElementEditable(element, true) && element.nodeType === Node.ELEMENT_NODE) {
3500-
_clickHighlight.add(element, true, true); // 3rd arg is for auto-scroll
3509+
_clickHighlight.add(element, true);
35013510
}
35023511
}
35033512

0 commit comments

Comments
 (0)