Skip to content

Commit 207da0b

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

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
@@ -19,6 +19,27 @@ function RemoteFunctions(config = {}) {
1919
__description: "Use this to keep shared state for Live Preview Edit instead of window.*"
2020
};
2121

22+
let _hoverHighlight;
23+
let _clickHighlight;
24+
let _hoverLockTimer = null;
25+
26+
// this will store the element that was clicked previously (before the new click)
27+
// we need this so that we can remove click styling from the previous element when a new element is clicked
28+
let previouslyClickedElement = null;
29+
30+
var req, timeout;
31+
function animateHighlight(time) {
32+
if(req) {
33+
window.cancelAnimationFrame(req);
34+
window.clearTimeout(timeout);
35+
}
36+
req = window.requestAnimationFrame(redrawHighlights);
37+
38+
timeout = setTimeout(function () {
39+
window.cancelAnimationFrame(req);
40+
req = null;
41+
}, time * 1000);
42+
}
2243

2344
// the following fucntions can be in the handler and live preview will call those functions when the below
2445
// events happen
@@ -51,7 +72,6 @@ function RemoteFunctions(config = {}) {
5172
];
5273

5374
const _toolHandlers = new Map();
54-
5575
function registerToolHandler(handlerName, handler) {
5676
if(_toolHandlers.get(handlerName)) {
5777
console.error(`lp: Tool handler '${handlerName}' already registered. Ignoring new registration`);
@@ -70,49 +90,13 @@ function RemoteFunctions(config = {}) {
7090
}
7191
_toolHandlers.set(handlerName, handler);
7292
}
73-
7493
function getToolHandler(handlerName) {
7594
return _toolHandlers.get(handlerName);
7695
}
77-
7896
function getAllToolHandlers() {
7997
return Array.from(_toolHandlers.values());
8098
}
8199

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

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

218-
// the below code comment is replaced by added scripts for extensibility
219-
// DONT_STRIP_MINIFY:REPLACE_WITH_ADDED_REMOTE_CONSTANT_SCRIPTS
220-
221-
222-
223-
let _hoverHighlight;
224-
let _clickHighlight;
225-
let _hoverLockTimer = null;
226-
227-
// set an event on a element
228-
function _trigger(element, name, value, autoRemove) {
229-
var key = "data-ld-" + name;
230-
if (value !== undefined && value !== null) {
231-
element.setAttribute(key, value);
232-
if (autoRemove) {
233-
window.setTimeout(element.removeAttribute.bind(element, key));
234-
}
235-
} else {
236-
element.removeAttribute(key);
237-
}
238-
}
239-
240-
var req, timeout;
241-
function animateHighlight(time) {
242-
if(req) {
243-
window.cancelAnimationFrame(req);
244-
window.clearTimeout(timeout);
245-
}
246-
req = window.requestAnimationFrame(redrawHighlights);
247-
248-
timeout = setTimeout(function () {
249-
window.cancelAnimationFrame(req);
250-
req = null;
251-
}, time * 1000);
252-
}
253-
254252
function Highlight(color, trigger) {
255253
this.color = color;
256254
this.trigger = !!trigger;
@@ -481,7 +479,7 @@ function RemoteFunctions(config = {}) {
481479

482480
var transitionValues = {
483481
"transition-property": "opacity, background-color, transform",
484-
"transition-duration": "300ms, 2.6s"
482+
"transition-duration": "300ms, 2.3s"
485483
};
486484

487485
function _setStyleValues(styleValues, obj) {
@@ -596,16 +594,21 @@ function RemoteFunctions(config = {}) {
596594
return false;
597595
}
598596

599-
// this is to check the user's settings, if they want to show the elements highlights on hover or click
600-
if (shouldShowHighlightOnHover()) {
601-
hideHighlight();
597+
// if _hoverHighlight is uninitialized, initialize it
598+
if (!_hoverHighlight && shouldShowHighlightOnHover()) {
602599
_hoverHighlight = new Highlight("#c8f9c5", true);
603-
_hoverHighlight.add(element, false);
600+
}
601+
602+
// this is to check the user's settings, if they want to show the elements highlights on hover or click
603+
if (_hoverHighlight && shouldShowHighlightOnHover()) {
604+
_hoverHighlight.clear();
604605

605606
// Store original background color to restore on hover out
606607
element._originalBackgroundColor = element.style.backgroundColor;
607608
element.style.backgroundColor = "rgba(0, 162, 255, 0.2)";
608609

610+
_hoverHighlight.add(element, false);
611+
609612
// create the info box for the hovered element
610613
const infoBoxHandler = LivePreviewView.getToolHandler("InfoBox");
611614
if (infoBoxHandler) {
@@ -654,7 +657,6 @@ function RemoteFunctions(config = {}) {
654657
* @param {Element} element - The DOM element to select
655658
*/
656659
function selectElement(element) {
657-
hideHighlight();
658660
dismissUIAndCleanupState();
659661
// this should also be there when users are in highlight mode
660662
scrollElementToViewPort(element);
@@ -686,7 +688,18 @@ function RemoteFunctions(config = {}) {
686688
const outlineColor = element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR) ? "#4285F4" : "#3C3F41";
687689
element.style.outline = `1px solid ${outlineColor}`;
688690

689-
_clickHighlight = new Highlight("#cfc");
691+
// Only apply background tint for editable elements (not for dynamic/read-only)
692+
if (element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR)) {
693+
if (element._originalBackgroundColor === undefined) {
694+
element._originalBackgroundColor = element.style.backgroundColor;
695+
}
696+
element.style.backgroundColor = "rgba(0, 162, 255, 0.2)";
697+
}
698+
699+
if (!_clickHighlight) {
700+
_clickHighlight = new Highlight("#cfc");
701+
}
702+
_clickHighlight.clear();
690703
_clickHighlight.add(element, true);
691704

692705
previouslyClickedElement = element;
@@ -718,7 +731,7 @@ function RemoteFunctions(config = {}) {
718731
_hoverLockTimer = setTimeout(() => {
719732
enableHoverListeners();
720733
_hoverLockTimer = null;
721-
}, 600);
734+
}, 800);
722735
}
723736

724737
/**
@@ -858,11 +871,6 @@ function RemoteFunctions(config = {}) {
858871

859872
window.addEventListener("resize", redrawEverything);
860873

861-
/**
862-
* @type {DOMEditHandler}
863-
*/
864-
var _editHandler;
865-
866874
/**
867875
* Constructor
868876
* @param {Document} htmlDocument
@@ -1186,7 +1194,16 @@ function RemoteFunctions(config = {}) {
11861194
* when config is changed we clear all the highlighting and stuff
11871195
*/
11881196
function _handleConfigurationChange() {
1189-
hideHighlight();
1197+
if (_hoverHighlight) {
1198+
_hoverHighlight.clear();
1199+
}
1200+
cleanupPreviousElementState();
1201+
const allElements = window.document.querySelectorAll(`[${GLOBALS.DATA_BRACKETS_ID_ATTR}]`);
1202+
for (let i = 0; i < allElements.length; i++) {
1203+
if (allElements[i]._originalBackgroundColor !== undefined) {
1204+
clearElementBackground(allElements[i]);
1205+
}
1206+
}
11901207
dismissUIAndCleanupState();
11911208
}
11921209

@@ -1202,6 +1219,7 @@ function RemoteFunctions(config = {}) {
12021219
}
12031220
delete previouslyClickedElement._originalOutline;
12041221

1222+
clearElementBackground(previouslyClickedElement);
12051223
hideHighlight();
12061224

12071225
// Notify handlers about cleanup

0 commit comments

Comments
 (0)