Skip to content

Commit 468f8a7

Browse files
committed
fix: cursor jump to that start of element when an element in clicked in the source code
1 parent 6f90b03 commit 468f8a7

File tree

1 file changed

+64
-45
lines changed

1 file changed

+64
-45
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,67 @@ function RemoteFunctions(config) {
13201320
}
13211321
}
13221322

1323+
/**
1324+
* this function is responsible to select an element in the live preview
1325+
* @param {Element} element - The DOM element to select
1326+
*/
1327+
function _selectElement(element) {
1328+
// make sure that the feature is enabled and also the element has the attribute 'data-brackets-id'
1329+
if (
1330+
!config.isLPEditFeaturesActive ||
1331+
!element.hasAttribute("data-brackets-id") ||
1332+
element.tagName === "BODY" ||
1333+
element.tagName === "HTML" ||
1334+
_isInsideHeadTag(element)
1335+
) {
1336+
return;
1337+
}
1338+
1339+
if (_nodeMoreOptionsBox) {
1340+
_nodeMoreOptionsBox.remove();
1341+
_nodeMoreOptionsBox = null;
1342+
}
1343+
1344+
// to remove the outline styling from the previously clicked element
1345+
if (previouslyClickedElement) {
1346+
if (previouslyClickedElement._originalOutline !== undefined) {
1347+
previouslyClickedElement.style.outline = previouslyClickedElement._originalOutline;
1348+
} else {
1349+
previouslyClickedElement.style.outline = "";
1350+
}
1351+
delete previouslyClickedElement._originalOutline;
1352+
1353+
// Remove highlighting from previously clicked element
1354+
if (getHighlightMode() === "click") {
1355+
clearElementBackground(previouslyClickedElement);
1356+
}
1357+
}
1358+
1359+
_nodeMoreOptionsBox = new NodeMoreOptionsBox(element);
1360+
1361+
// show the info box when a DOM element is selected
1362+
if (_nodeInfoBox) {
1363+
_nodeInfoBox.remove();
1364+
}
1365+
_nodeInfoBox = new NodeInfoBox(element, true); // true means that the element was selected
1366+
1367+
element._originalOutline = element.style.outline;
1368+
element.style.outline = "1px solid #4285F4";
1369+
1370+
// Add highlight for click mode
1371+
if (getHighlightMode() === "click") {
1372+
element._originalBackgroundColor = element.style.backgroundColor;
1373+
element.style.backgroundColor = "rgba(0, 162, 255, 0.2)";
1374+
1375+
if (_hoverHighlight) {
1376+
_hoverHighlight.clear();
1377+
_hoverHighlight.add(element, true); // true for animation
1378+
}
1379+
}
1380+
1381+
previouslyClickedElement = element;
1382+
}
1383+
13231384
/**
13241385
* This function handles the click event on the live preview DOM element
13251386
* it is to show the advanced DOM manipulation options in the live preview
@@ -1338,49 +1399,7 @@ function RemoteFunctions(config) {
13381399
event.stopPropagation();
13391400
event.stopImmediatePropagation();
13401401

1341-
if (_nodeMoreOptionsBox) {
1342-
_nodeMoreOptionsBox.remove();
1343-
_nodeMoreOptionsBox = null;
1344-
}
1345-
1346-
// to remove the outline styling from the previously clicked element
1347-
if (previouslyClickedElement) {
1348-
if (previouslyClickedElement._originalOutline !== undefined) {
1349-
previouslyClickedElement.style.outline = previouslyClickedElement._originalOutline;
1350-
} else {
1351-
previouslyClickedElement.style.outline = "";
1352-
}
1353-
delete previouslyClickedElement._originalOutline;
1354-
1355-
// Remove highlighting from previously clicked element
1356-
if (getHighlightMode() === "click") {
1357-
clearElementBackground(previouslyClickedElement);
1358-
}
1359-
}
1360-
1361-
_nodeMoreOptionsBox = new NodeMoreOptionsBox(event.target);
1362-
1363-
// show the info box when a DOM element is clicked
1364-
if (_nodeInfoBox) {
1365-
_nodeInfoBox.remove();
1366-
}
1367-
_nodeInfoBox = new NodeInfoBox(event.target, true); // true means that the element was clicked
1368-
1369-
event.target._originalOutline = event.target.style.outline;
1370-
event.target.style.outline = "1px solid #4285F4";
1371-
1372-
// Add highlight for click mode
1373-
if (getHighlightMode() === "click") {
1374-
event.target._originalBackgroundColor = event.target.style.backgroundColor;
1375-
event.target.style.backgroundColor = "rgba(0, 162, 255, 0.2)";
1376-
1377-
if (_hoverHighlight) {
1378-
_hoverHighlight.clear();
1379-
_hoverHighlight.add(event.target, true); // true for animation
1380-
}
1381-
}
1382-
1383-
previouslyClickedElement = event.target;
1402+
_selectElement(event.target);
13841403
} else if ( // when user clicks on the HTML, BODY tags or elements inside HEAD, we want to remove the boxes
13851404
_nodeMoreOptionsBox &&
13861405
(event.target.tagName === "HTML" || event.target.tagName === "BODY" || _isInsideHeadTag(event.target))
@@ -1476,7 +1495,7 @@ function RemoteFunctions(config) {
14761495
}
14771496
_clickHighlight.selector = rule;
14781497

1479-
// trigger click on the first valid highlighted element
1498+
// select the first valid highlighted element
14801499
var foundValidElement = false;
14811500
for (i = 0; i < nodes.length; i++) {
14821501
if (nodes[i].hasAttribute("data-brackets-id") &&
@@ -1485,7 +1504,7 @@ function RemoteFunctions(config) {
14851504
!_isInsideHeadTag(nodes[i]) &&
14861505
nodes[i].tagName !== "BR"
14871506
) {
1488-
nodes[i].click();
1507+
_selectElement(nodes[i]);
14891508
foundValidElement = true;
14901509
break;
14911510
}

0 commit comments

Comments
 (0)