Skip to content

Commit 5d8852f

Browse files
committed
fix: highlighting animation not working as expected
1 parent 24ce29c commit 5d8852f

File tree

1 file changed

+87
-105
lines changed

1 file changed

+87
-105
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 87 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,6 @@ function RemoteFunctions(config = {}) {
2626
__description: "Use this to keep shared state for Live Preview Edit instead of window.*"
2727
};
2828

29-
let _hoverHighlight;
30-
let _clickHighlight;
31-
let _hoverLockTimer = null;
32-
33-
// this will store the element that was clicked previously (before the new click)
34-
// we need this so that we can remove click styling from the previous element when a new element is clicked
35-
let previouslyClickedElement = null;
36-
37-
var req, timeout;
38-
function animateHighlight(time) {
39-
if(req) {
40-
window.cancelAnimationFrame(req);
41-
window.clearTimeout(timeout);
42-
}
43-
req = window.requestAnimationFrame(redrawHighlights);
44-
45-
timeout = setTimeout(function () {
46-
window.cancelAnimationFrame(req);
47-
req = null;
48-
}, time * 1000);
49-
}
5029

5130
// the following fucntions can be in the handler and live preview will call those functions when the below
5231
// events happen
@@ -79,6 +58,7 @@ function RemoteFunctions(config = {}) {
7958
];
8059

8160
const _toolHandlers = new Map();
61+
8262
function registerToolHandler(handlerName, handler) {
8363
if(_toolHandlers.get(handlerName)) {
8464
console.error(`lp: Tool handler '${handlerName}' already registered. Ignoring new registration`);
@@ -97,13 +77,49 @@ function RemoteFunctions(config = {}) {
9777
}
9878
_toolHandlers.set(handlerName, handler);
9979
}
80+
10081
function getToolHandler(handlerName) {
10182
return _toolHandlers.get(handlerName);
10283
}
84+
10385
function getAllToolHandlers() {
10486
return Array.from(_toolHandlers.values());
10587
}
10688

89+
const LivePreviewView = {
90+
registerToolHandler: registerToolHandler,
91+
getToolHandler: getToolHandler,
92+
getAllToolHandlers: getAllToolHandlers,
93+
isElementEditable: isElementEditable,
94+
isElementInspectable: isElementInspectable,
95+
isElementVisible: isElementVisible,
96+
screenOffset: screenOffset,
97+
selectElement: selectElement,
98+
brieflyDisableHoverListeners: brieflyDisableHoverListeners,
99+
handleElementClick: handleElementClick,
100+
cleanupPreviousElementState: cleanupPreviousElementState
101+
};
102+
103+
104+
// this will store the element that was clicked previously (before the new click)
105+
// we need this so that we can remove click styling from the previous element when a new element is clicked
106+
let previouslyClickedElement = null;
107+
108+
// helper function to check if an element is inside the HEAD tag
109+
// we need this because we don't wanna trigger the element highlights on head tag and its children,
110+
// except for <style> tags which should be allowed
111+
function _isInsideHeadTag(element) {
112+
let parent = element;
113+
while (parent && parent !== window.document) {
114+
if (parent.tagName.toLowerCase() === "head") {
115+
// allow <style> tags inside <head>
116+
return element.tagName.toLowerCase() !== "style";
117+
}
118+
parent = parent.parentElement;
119+
}
120+
return false;
121+
}
122+
107123
/**
108124
* check if an element is inspectable.
109125
* inspectable elements are those which doesn't have GLOBALS.DATA_BRACKETS_ID_ATTR ('data-brackets-id'),
@@ -161,56 +177,6 @@ function RemoteFunctions(config = {}) {
161177
return { left: offsetLeft, top: offsetTop };
162178
}
163179

164-
const LivePreviewView = {
165-
registerToolHandler: registerToolHandler,
166-
getToolHandler: getToolHandler,
167-
getAllToolHandlers: getAllToolHandlers,
168-
isElementEditable: isElementEditable,
169-
isElementInspectable: isElementInspectable,
170-
isElementVisible: isElementVisible,
171-
screenOffset: screenOffset,
172-
selectElement: selectElement,
173-
brieflyDisableHoverListeners: brieflyDisableHoverListeners,
174-
handleElementClick: handleElementClick,
175-
cleanupPreviousElementState: cleanupPreviousElementState
176-
};
177-
178-
/**
179-
* @type {DOMEditHandler}
180-
*/
181-
var _editHandler;
182-
183-
// the below code comment is replaced by added scripts for extensibility
184-
// DONT_STRIP_MINIFY:REPLACE_WITH_ADDED_REMOTE_CONSTANT_SCRIPTS
185-
186-
// helper function to check if an element is inside the HEAD tag
187-
// we need this because we don't wanna trigger the element highlights on head tag and its children,
188-
// except for <style> tags which should be allowed
189-
function _isInsideHeadTag(element) {
190-
let parent = element;
191-
while (parent && parent !== window.document) {
192-
if (parent.tagName.toLowerCase() === "head") {
193-
// allow <style> tags inside <head>
194-
return element.tagName.toLowerCase() !== "style";
195-
}
196-
parent = parent.parentElement;
197-
}
198-
return false;
199-
}
200-
201-
// set an event on a element
202-
function _trigger(element, name, value, autoRemove) {
203-
var key = "data-ld-" + name;
204-
if (value !== undefined && value !== null) {
205-
element.setAttribute(key, value);
206-
if (autoRemove) {
207-
window.setTimeout(element.removeAttribute.bind(element, key));
208-
}
209-
} else {
210-
element.removeAttribute(key);
211-
}
212-
}
213-
214180
// Checks if the element is in Viewport in the client browser
215181
function isInViewport(element) {
216182
var rect = element.getBoundingClientRect();
@@ -256,6 +222,42 @@ function RemoteFunctions(config = {}) {
256222
return element.offsetTop + (element.offsetParent ? getDocumentOffsetTop(element.offsetParent) : 0);
257223
}
258224

225+
// the below code comment is replaced by added scripts for extensibility
226+
// DONT_STRIP_MINIFY:REPLACE_WITH_ADDED_REMOTE_CONSTANT_SCRIPTS
227+
228+
229+
230+
let _hoverHighlight;
231+
let _clickHighlight;
232+
let _hoverLockTimer = null;
233+
234+
// set an event on a element
235+
function _trigger(element, name, value, autoRemove) {
236+
var key = "data-ld-" + name;
237+
if (value !== undefined && value !== null) {
238+
element.setAttribute(key, value);
239+
if (autoRemove) {
240+
window.setTimeout(element.removeAttribute.bind(element, key));
241+
}
242+
} else {
243+
element.removeAttribute(key);
244+
}
245+
}
246+
247+
var req, timeout;
248+
function animateHighlight(time) {
249+
if(req) {
250+
window.cancelAnimationFrame(req);
251+
window.clearTimeout(timeout);
252+
}
253+
req = window.requestAnimationFrame(redrawHighlights);
254+
255+
timeout = setTimeout(function () {
256+
window.cancelAnimationFrame(req);
257+
req = null;
258+
}, time * 1000);
259+
}
260+
259261
function Highlight(color, trigger) {
260262
this.color = color;
261263
this.trigger = !!trigger;
@@ -486,7 +488,7 @@ function RemoteFunctions(config = {}) {
486488

487489
var transitionValues = {
488490
"transition-property": "opacity, background-color, transform",
489-
"transition-duration": "300ms, 2.3s"
491+
"transition-duration": "300ms, 2.6s"
490492
};
491493

492494
function _setStyleValues(styleValues, obj) {
@@ -601,21 +603,16 @@ function RemoteFunctions(config = {}) {
601603
return false;
602604
}
603605

604-
// if _hoverHighlight is uninitialized, initialize it
605-
if (!_hoverHighlight && shouldShowHighlightOnHover()) {
606-
_hoverHighlight = new Highlight("#c8f9c5", true);
607-
}
608-
609606
// this is to check the user's settings, if they want to show the elements highlights on hover or click
610-
if (_hoverHighlight && shouldShowHighlightOnHover()) {
611-
_hoverHighlight.clear();
607+
if (shouldShowHighlightOnHover()) {
608+
hideHighlight();
609+
_hoverHighlight = new Highlight("#c8f9c5", true);
610+
_hoverHighlight.add(element, false);
612611

613612
// Store original background color to restore on hover out
614613
element._originalBackgroundColor = element.style.backgroundColor;
615614
element.style.backgroundColor = "rgba(0, 162, 255, 0.2)";
616615

617-
_hoverHighlight.add(element, false);
618-
619616
// create the info box for the hovered element
620617
const infoBoxHandler = LivePreviewView.getToolHandler("InfoBox");
621618
if (infoBoxHandler) {
@@ -664,6 +661,7 @@ function RemoteFunctions(config = {}) {
664661
* @param {Element} element - The DOM element to select
665662
*/
666663
function selectElement(element) {
664+
hideHighlight();
667665
dismissUIAndCleanupState();
668666
// this should also be there when users are in highlight mode
669667
scrollElementToViewPort(element);
@@ -695,18 +693,7 @@ function RemoteFunctions(config = {}) {
695693
const outlineColor = element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR) ? "#4285F4" : "#3C3F41";
696694
element.style.outline = `1px solid ${outlineColor}`;
697695

698-
// Only apply background tint for editable elements (not for dynamic/read-only)
699-
if (element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR)) {
700-
if (element._originalBackgroundColor === undefined) {
701-
element._originalBackgroundColor = element.style.backgroundColor;
702-
}
703-
element.style.backgroundColor = "rgba(0, 162, 255, 0.2)";
704-
}
705-
706-
if (!_clickHighlight) {
707-
_clickHighlight = new Highlight("#cfc");
708-
}
709-
_clickHighlight.clear();
696+
_clickHighlight = new Highlight("#cfc");
710697
_clickHighlight.add(element, true);
711698

712699
previouslyClickedElement = element;
@@ -738,7 +725,7 @@ function RemoteFunctions(config = {}) {
738725
_hoverLockTimer = setTimeout(() => {
739726
enableHoverListeners();
740727
_hoverLockTimer = null;
741-
}, 800);
728+
}, 600);
742729
}
743730

744731
/**
@@ -878,6 +865,11 @@ function RemoteFunctions(config = {}) {
878865

879866
window.addEventListener("resize", redrawEverything);
880867

868+
/**
869+
* @type {DOMEditHandler}
870+
*/
871+
var _editHandler;
872+
881873
/**
882874
* Constructor
883875
* @param {Document} htmlDocument
@@ -1201,16 +1193,7 @@ function RemoteFunctions(config = {}) {
12011193
* when config is changed we clear all the highlighting and stuff
12021194
*/
12031195
function _handleConfigurationChange() {
1204-
if (_hoverHighlight) {
1205-
_hoverHighlight.clear();
1206-
}
1207-
cleanupPreviousElementState();
1208-
const allElements = window.document.querySelectorAll(`[${GLOBALS.DATA_BRACKETS_ID_ATTR}]`);
1209-
for (let i = 0; i < allElements.length; i++) {
1210-
if (allElements[i]._originalBackgroundColor !== undefined) {
1211-
clearElementBackground(allElements[i]);
1212-
}
1213-
}
1196+
hideHighlight();
12141197
dismissUIAndCleanupState();
12151198
}
12161199

@@ -1226,7 +1209,6 @@ function RemoteFunctions(config = {}) {
12261209
}
12271210
delete previouslyClickedElement._originalOutline;
12281211

1229-
clearElementBackground(previouslyClickedElement);
12301212
hideHighlight();
12311213

12321214
// Notify handlers about cleanup

0 commit comments

Comments
 (0)