Skip to content

Commit 1ea06f2

Browse files
committed
Revert "fix: highlighting animation not working as expected"
This reverts commit 77e4825.
1 parent 5d8852f commit 1ea06f2

File tree

1 file changed

+105
-87
lines changed

1 file changed

+105
-87
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 105 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ 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+
}
2950

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

6081
const _toolHandlers = new Map();
61-
6282
function registerToolHandler(handlerName, handler) {
6383
if(_toolHandlers.get(handlerName)) {
6484
console.error(`lp: Tool handler '${handlerName}' already registered. Ignoring new registration`);
@@ -77,49 +97,13 @@ function RemoteFunctions(config = {}) {
7797
}
7898
_toolHandlers.set(handlerName, handler);
7999
}
80-
81100
function getToolHandler(handlerName) {
82101
return _toolHandlers.get(handlerName);
83102
}
84-
85103
function getAllToolHandlers() {
86104
return Array.from(_toolHandlers.values());
87105
}
88106

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-
123107
/**
124108
* check if an element is inspectable.
125109
* inspectable elements are those which doesn't have GLOBALS.DATA_BRACKETS_ID_ATTR ('data-brackets-id'),
@@ -177,6 +161,56 @@ function RemoteFunctions(config = {}) {
177161
return { left: offsetLeft, top: offsetTop };
178162
}
179163

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+
180214
// Checks if the element is in Viewport in the client browser
181215
function isInViewport(element) {
182216
var rect = element.getBoundingClientRect();
@@ -222,42 +256,6 @@ function RemoteFunctions(config = {}) {
222256
return element.offsetTop + (element.offsetParent ? getDocumentOffsetTop(element.offsetParent) : 0);
223257
}
224258

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-
261259
function Highlight(color, trigger) {
262260
this.color = color;
263261
this.trigger = !!trigger;
@@ -488,7 +486,7 @@ function RemoteFunctions(config = {}) {
488486

489487
var transitionValues = {
490488
"transition-property": "opacity, background-color, transform",
491-
"transition-duration": "300ms, 2.6s"
489+
"transition-duration": "300ms, 2.3s"
492490
};
493491

494492
function _setStyleValues(styleValues, obj) {
@@ -603,16 +601,21 @@ function RemoteFunctions(config = {}) {
603601
return false;
604602
}
605603

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

612613
// Store original background color to restore on hover out
613614
element._originalBackgroundColor = element.style.backgroundColor;
614615
element.style.backgroundColor = "rgba(0, 162, 255, 0.2)";
615616

617+
_hoverHighlight.add(element, false);
618+
616619
// create the info box for the hovered element
617620
const infoBoxHandler = LivePreviewView.getToolHandler("InfoBox");
618621
if (infoBoxHandler) {
@@ -661,7 +664,6 @@ function RemoteFunctions(config = {}) {
661664
* @param {Element} element - The DOM element to select
662665
*/
663666
function selectElement(element) {
664-
hideHighlight();
665667
dismissUIAndCleanupState();
666668
// this should also be there when users are in highlight mode
667669
scrollElementToViewPort(element);
@@ -693,7 +695,18 @@ function RemoteFunctions(config = {}) {
693695
const outlineColor = element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR) ? "#4285F4" : "#3C3F41";
694696
element.style.outline = `1px solid ${outlineColor}`;
695697

696-
_clickHighlight = new Highlight("#cfc");
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();
697710
_clickHighlight.add(element, true);
698711

699712
previouslyClickedElement = element;
@@ -725,7 +738,7 @@ function RemoteFunctions(config = {}) {
725738
_hoverLockTimer = setTimeout(() => {
726739
enableHoverListeners();
727740
_hoverLockTimer = null;
728-
}, 600);
741+
}, 800);
729742
}
730743

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

866879
window.addEventListener("resize", redrawEverything);
867880

868-
/**
869-
* @type {DOMEditHandler}
870-
*/
871-
var _editHandler;
872-
873881
/**
874882
* Constructor
875883
* @param {Document} htmlDocument
@@ -1193,7 +1201,16 @@ function RemoteFunctions(config = {}) {
11931201
* when config is changed we clear all the highlighting and stuff
11941202
*/
11951203
function _handleConfigurationChange() {
1196-
hideHighlight();
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+
}
11971214
dismissUIAndCleanupState();
11981215
}
11991216

@@ -1209,6 +1226,7 @@ function RemoteFunctions(config = {}) {
12091226
}
12101227
delete previouslyClickedElement._originalOutline;
12111228

1229+
clearElementBackground(previouslyClickedElement);
12121230
hideHighlight();
12131231

12141232
// Notify handlers about cleanup

0 commit comments

Comments
 (0)