Skip to content

Commit 7c48fa9

Browse files
committed
fix: live preview getting scrolled even when the element is completely in viewport
1 parent 3a9e6d2 commit 7c48fa9

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

@@ -3206,25 +3204,15 @@ function RemoteFunctions(config = {}) {
32063204
window.document.body.appendChild(highlight);
32073205
},
32083206

3209-
// shouldAutoScroll is whether to scroll page to element if not in view
3210-
// true when user clicks on the source code of some element, in that case we want to scroll the live preview
3211-
add: function (element, doAnimation, shouldAutoScroll) {
3207+
add: function (element, doAnimation) {
32123208
if (this._elementExists(element) || element === window.document) {
32133209
return;
32143210
}
32153211
if (this.trigger) {
32163212
_trigger(element, "highlight", 1);
32173213
}
32183214

3219-
if (shouldAutoScroll && (!window.event || window.event instanceof MessageEvent) && !isInViewport(element)) {
3220-
var top = getDocumentOffsetTop(element);
3221-
if (top) {
3222-
top -= (window.innerHeight / 2);
3223-
window.scrollTo(0, top);
3224-
}
3225-
}
32263215
this.elements.push(element);
3227-
32283216
this._makeHighlightDiv(element, doAnimation);
32293217
},
32303218

@@ -3258,7 +3246,7 @@ function RemoteFunctions(config = {}) {
32583246

32593247
this.clear();
32603248
for (i = 0; i < highlighted.length; i++) {
3261-
this.add(highlighted[i], false, false); // 3rd arg is for auto-scroll
3249+
this.add(highlighted[i], false);
32623250
}
32633251
}
32643252
};
@@ -3276,7 +3264,7 @@ function RemoteFunctions(config = {}) {
32763264
if (_validEvent(event)) {
32773265
const element = event.target;
32783266
if(isElementEditable(element) && element.nodeType === Node.ELEMENT_NODE ) {
3279-
_localHighlight.add(element, true, false); // false means no-auto scroll
3267+
_localHighlight.add(element, true);
32803268
}
32813269
}
32823270
}
@@ -3348,7 +3336,7 @@ function RemoteFunctions(config = {}) {
33483336
element._originalBackgroundColor = element.style.backgroundColor;
33493337
element.style.backgroundColor = "rgba(0, 162, 255, 0.2)";
33503338

3351-
_hoverHighlight.add(element, false, false); // false means no auto-scroll
3339+
_hoverHighlight.add(element, false);
33523340

33533341
// Create info box for the hovered element
33543342
dismissNodeInfoBox();
@@ -3373,6 +3361,21 @@ function RemoteFunctions(config = {}) {
33733361
}
33743362
}
33753363

3364+
function scrollElementToViewPort(element) {
3365+
if (!element) {
3366+
return;
3367+
}
3368+
3369+
// Check if element is in viewport, if not scroll to it
3370+
if (!isInViewport(element)) {
3371+
let top = getDocumentOffsetTop(element);
3372+
if (top) {
3373+
top -= (window.innerHeight / 2);
3374+
window.scrollTo(0, top);
3375+
}
3376+
}
3377+
}
3378+
33763379
/**
33773380
* this function is responsible to select an element in the live preview
33783381
* @param {Element} element - The DOM element to select
@@ -3381,10 +3384,16 @@ function RemoteFunctions(config = {}) {
33813384
// dismiss all UI boxes and cleanup previous element state when selecting a different element
33823385
dismissUIAndCleanupState();
33833386
dismissImageRibbonGallery();
3387+
3388+
// this should always happen before isElementEditable check because this is not a live preview edit feature
3389+
// this should also be there when users are in highlight mode
3390+
scrollElementToViewPort(element);
3391+
33843392
if(!isElementEditable(element)) {
33853393
return false;
33863394
}
33873395

3396+
33883397
// make sure that the element is actually visible to the user
33893398
if (isElementVisible(element)) {
33903399
_nodeMoreOptionsBox = new NodeMoreOptionsBox(element);
@@ -3411,7 +3420,7 @@ function RemoteFunctions(config = {}) {
34113420

34123421
if (_hoverHighlight) {
34133422
_hoverHighlight.clear();
3414-
_hoverHighlight.add(element, true, false); // false means no auto-scroll
3423+
_hoverHighlight.add(element, true);
34153424
}
34163425

34173426
previouslyClickedElement = element;
@@ -3492,7 +3501,7 @@ function RemoteFunctions(config = {}) {
34923501
_clickHighlight.clear();
34933502
}
34943503
if (isElementEditable(element, true) && element.nodeType === Node.ELEMENT_NODE) {
3495-
_clickHighlight.add(element, true, true); // 3rd arg is for auto-scroll
3504+
_clickHighlight.add(element, true);
34963505
}
34973506
}
34983507

0 commit comments

Comments
 (0)