Skip to content

Commit ece11d0

Browse files
committed
feat: show drop marker label with tag class id names
1 parent e0b1150 commit ece11d0

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function RemoteFunctions(config = {}) {
4848
DROP_MARKER_VERTICAL_CLASSNAME: "__brackets-drop-marker-vertical",
4949
DROP_MARKER_INSIDE_CLASSNAME: "__brackets-drop-marker-inside",
5050
DROP_MARKER_ARROW_CLASSNAME: "__brackets-drop-marker-arrow",
51+
DROP_MARKER_LABEL_CLASSNAME: "__brackets-drop-marker-label",
5152

5253
// image ribbon gallery cache, to store the last query and its results
5354
CACHE_EXPIRY_TIME: 168 * 60 * 60 * 1000, // 7 days
@@ -927,6 +928,90 @@ function RemoteFunctions(config = {}) {
927928
return clientY < rect.top + rect.height / 2 ? "before" : "after";
928929
}
929930

931+
/**
932+
* this function creates the HTML content for the drop target label
933+
*
934+
* @param {DOMElement} element - The target element
935+
* @returns {String} - HTML string with tag, ID, and classes
936+
*/
937+
function _createLabelContent(element) {
938+
const tagName = element.tagName.toLowerCase();
939+
const id = element.id.trim();
940+
const classList = Array.from(element.classList || []);
941+
942+
let content = `<strong>${tagName}</strong>`;
943+
944+
if (id) {
945+
// for ids we have a max char limit of 15
946+
if (id.length >= 15) {
947+
content += `#${id.substring(0, 15)}...`;
948+
} else {
949+
content += `#${id}`;
950+
}
951+
}
952+
953+
if (classList.length > 0) {
954+
// max 3 classes
955+
let classStr = classList.slice(0, 3).join('.');
956+
957+
// max char limit (30 chars for all 3 classes)
958+
if (classStr.length > 30) {
959+
classStr = classStr.substring(0, 27) + '...';
960+
}
961+
962+
content += `.<em>${classStr}</em>`;
963+
}
964+
965+
return content;
966+
}
967+
968+
/**
969+
* create the drop target label box
970+
* this box will be shown at the side of the arrow marker
971+
* it will show the tagname, class name and id name of the dropped target
972+
*
973+
* @param {DOMElement} element - The target element
974+
* @param {DOMElement} arrow - The arrow marker element
975+
* @returns {DOMElement} - The label element
976+
*/
977+
function _createDropLabel(element, arrow) {
978+
const label = window.document.createElement('div');
979+
label.className = GLOBALS.DROP_MARKER_LABEL_CLASSNAME;
980+
label.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, 'true');
981+
label.innerHTML = _createLabelContent(element);
982+
983+
label.style.position = 'fixed';
984+
label.style.zIndex = '2147483647';
985+
label.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
986+
label.style.color = 'white';
987+
label.style.padding = '4px 8px';
988+
label.style.borderRadius = '3px';
989+
label.style.fontSize = '11px';
990+
label.style.fontFamily = 'monospace';
991+
label.style.whiteSpace = 'nowrap';
992+
label.style.boxShadow = '0 2px 6px rgba(0, 0, 0, 0.3)';
993+
label.style.pointerEvents = 'none';
994+
995+
// append temporarily for dimensions calculations
996+
window.document.body.appendChild(label);
997+
const labelRect = label.getBoundingClientRect();
998+
const arrowRect = arrow.getBoundingClientRect();
999+
1000+
// default position: 10px to the right of arrow, vertically centered
1001+
let labelLeft = arrowRect.right + 10;
1002+
const labelTop = arrowRect.top + (arrowRect.height / 2) - (labelRect.height / 2);
1003+
1004+
// if right side overflows, we switch to the left side
1005+
if (labelLeft + labelRect.width > window.innerWidth) {
1006+
labelLeft = arrowRect.left - labelRect.width - 10;
1007+
}
1008+
1009+
label.style.left = labelLeft + 'px';
1010+
label.style.top = labelTop + 'px';
1011+
1012+
return label;
1013+
}
1014+
9301015
/**
9311016
* this is to create a marker to indicate a valid drop position
9321017
*
@@ -1036,6 +1121,10 @@ function RemoteFunctions(config = {}) {
10361121
element._dropArrow = arrow; // store arrow reference too
10371122
window.document.body.appendChild(marker);
10381123
window.document.body.appendChild(arrow);
1124+
1125+
// create and append the drop target label
1126+
const label = _createDropLabel(element, arrow);
1127+
element._dropLabel = label;
10391128
}
10401129

10411130
/**
@@ -1051,6 +1140,10 @@ function RemoteFunctions(config = {}) {
10511140
element._dropArrow.parentNode.removeChild(element._dropArrow);
10521141
delete element._dropArrow;
10531142
}
1143+
if (element._dropLabel && element._dropLabel.parentNode) {
1144+
element._dropLabel.parentNode.removeChild(element._dropLabel);
1145+
delete element._dropLabel;
1146+
}
10541147
}
10551148

10561149
/**
@@ -1062,6 +1155,7 @@ function RemoteFunctions(config = {}) {
10621155
let verticalMarkers = window.document.querySelectorAll("." + GLOBALS.DROP_MARKER_VERTICAL_CLASSNAME);
10631156
let insideMarkers = window.document.querySelectorAll("." + GLOBALS.DROP_MARKER_INSIDE_CLASSNAME);
10641157
let arrows = window.document.querySelectorAll("." + GLOBALS.DROP_MARKER_ARROW_CLASSNAME);
1158+
let labels = window.document.querySelectorAll("." + GLOBALS.DROP_MARKER_LABEL_CLASSNAME);
10651159

10661160
for (let i = 0; i < horizontalMarkers.length; i++) {
10671161
if (horizontalMarkers[i].parentNode) {
@@ -1087,11 +1181,18 @@ function RemoteFunctions(config = {}) {
10871181
}
10881182
}
10891183

1184+
for (let i = 0; i < labels.length; i++) {
1185+
if (labels[i].parentNode) {
1186+
labels[i].parentNode.removeChild(labels[i]);
1187+
}
1188+
}
1189+
10901190
// Also clear any element references
10911191
let elements = window.document.querySelectorAll(`[${GLOBALS.DATA_BRACKETS_ID_ATTR}]`);
10921192
for (let j = 0; j < elements.length; j++) {
10931193
delete elements[j]._dropMarker;
10941194
delete elements[j]._dropArrow;
1195+
delete elements[j]._dropLabel;
10951196
// only restore the styles that were modified by drag operations
10961197
if (elements[j]._originalDragBackgroundColor !== undefined) {
10971198
elements[j].style.backgroundColor = elements[j]._originalDragBackgroundColor;

0 commit comments

Comments
 (0)