Skip to content

Commit bc33277

Browse files
committed
feat: allow auto scrolling when on viewport edges
1 parent 430d6b3 commit bc33277

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,55 @@ function RemoteFunctions(config) {
5555

5656
var HIGHLIGHT_CLASSNAME = "__brackets-ld-highlight";
5757

58+
// auto-scroll variables to auto scroll the live preview when an element is dragged to the top/bottom
59+
let _autoScrollTimer = null;
60+
let _isAutoScrolling = false; // to disable highlights when auto scrolling
61+
const AUTO_SCROLL_SPEED = 12; // pixels per scroll
62+
const AUTO_SCROLL_EDGE_SIZE = 0.05; // 5% of viewport height (either top/bottom)
63+
64+
/**
65+
* this function is responsible to auto scroll the live preview when
66+
* dragging an element to the viewport edges
67+
* @param {number} clientY - curr mouse Y position
68+
*/
69+
function _handleAutoScroll(clientY) {
70+
const viewportHeight = window.innerHeight;
71+
const scrollEdgeSize = viewportHeight * AUTO_SCROLL_EDGE_SIZE;
72+
73+
// Clear existing timer
74+
if (_autoScrollTimer) {
75+
clearInterval(_autoScrollTimer);
76+
_autoScrollTimer = null;
77+
}
78+
79+
let scrollDirection = 0;
80+
81+
// check if near top edge (scroll up)
82+
if (clientY <= scrollEdgeSize) {
83+
scrollDirection = -AUTO_SCROLL_SPEED;
84+
} else if (clientY >= viewportHeight - scrollEdgeSize) {
85+
// check if near bottom edge (scroll down)
86+
scrollDirection = AUTO_SCROLL_SPEED;
87+
}
88+
89+
// Start scrolling if needed
90+
if (scrollDirection !== 0) {
91+
_isAutoScrolling = true;
92+
_autoScrollTimer = setInterval(() => {
93+
window.scrollBy(0, scrollDirection);
94+
}, 16); // 16 is ~60fps
95+
}
96+
}
97+
98+
// stop autoscrolling
99+
function _stopAutoScroll() {
100+
if (_autoScrollTimer) {
101+
clearInterval(_autoScrollTimer);
102+
_autoScrollTimer = null;
103+
}
104+
_isAutoScrolling = false;
105+
}
106+
58107
// determine whether an event should be processed for Live Development
59108
function _validEvent(event) {
60109
if (window.navigator.platform.substr(0, 3) === "Mac") {
@@ -838,6 +887,7 @@ function RemoteFunctions(config) {
838887
// before creating a drop marker, make sure that we clear all the drop markers
839888
_clearDropMarkers();
840889
_createDropMarker(target, dropZone, indicatorType);
890+
_handleAutoScroll(event.clientY);
841891
}
842892

843893
/**
@@ -847,6 +897,7 @@ function RemoteFunctions(config) {
847897
function onDragLeave(event) {
848898
if (!event.relatedTarget) {
849899
_clearDropMarkers();
900+
_stopAutoScroll();
850901
}
851902
}
852903

@@ -874,6 +925,7 @@ function RemoteFunctions(config) {
874925
// skip if no valid target found or if it's the dragged element
875926
if (!target || target === window._currentDraggedElement) {
876927
_clearDropMarkers();
928+
_stopAutoScroll();
877929
_dragEndChores(window._currentDraggedElement);
878930
dismissMoreOptionsBox();
879931
delete window._currentDraggedElement;
@@ -883,6 +935,7 @@ function RemoteFunctions(config) {
883935
// Skip BODY, HTML tags and elements inside HEAD
884936
if (target.tagName === "BODY" || target.tagName === "HTML" || _isInsideHeadTag(target)) {
885937
_clearDropMarkers();
938+
_stopAutoScroll();
886939
_dragEndChores(window._currentDraggedElement);
887940
dismissMoreOptionsBox();
888941
delete window._currentDraggedElement;
@@ -922,6 +975,7 @@ function RemoteFunctions(config) {
922975
window._Brackets_MessageBroker.send(messageData);
923976

924977
_clearDropMarkers();
978+
_stopAutoScroll();
925979
_dragEndChores(window._currentDraggedElement);
926980
dismissMoreOptionsBox();
927981
delete window._currentDraggedElement;
@@ -1033,6 +1087,7 @@ function RemoteFunctions(config) {
10331087
event.stopPropagation();
10341088
_dragEndChores(this.element);
10351089
_clearDropMarkers();
1090+
_stopAutoScroll();
10361091
delete window._currentDraggedElement;
10371092
});
10381093
},
@@ -1792,6 +1847,11 @@ function RemoteFunctions(config) {
17921847
}
17931848

17941849
function onElementHover(event) {
1850+
// don't want highlighting and stuff when auto scrolling
1851+
if (_isAutoScrolling) {
1852+
return;
1853+
}
1854+
17951855
// this is to check the user's settings, if they want to show the elements highlights on hover or click
17961856
if (_hoverHighlight && config.isLPEditFeaturesActive && shouldShowHighlightOnHover()) {
17971857
_hoverHighlight.clear();
@@ -1823,6 +1883,11 @@ function RemoteFunctions(config) {
18231883
}
18241884

18251885
function onElementHoverOut(event) {
1886+
// don't want highlighting and stuff when auto scrolling
1887+
if (_isAutoScrolling) {
1888+
return;
1889+
}
1890+
18261891
// this is to check the user's settings, if they want to show the elements highlights on hover or click
18271892
if (_hoverHighlight && config.isLPEditFeaturesActive && shouldShowHighlightOnHover()) {
18281893
_hoverHighlight.clear();

0 commit comments

Comments
 (0)