Skip to content

Commit 47f8d0b

Browse files
committed
refactor: move data brackets id and brackets highlight to globals
1 parent da85a5b commit 47f8d0b

File tree

1 file changed

+47
-47
lines changed

1 file changed

+47
-47
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ function RemoteFunctions(config = {}) {
3333
const GLOBALS = {
3434
// given to internal elements like info box, options box, image gallery and all other phcode internal elements
3535
// to distinguish between phoenix internal vs user created elements
36-
PHCODE_INTERNAL_ATTR: "data-phcode-internal-c15r5a9"
36+
PHCODE_INTERNAL_ATTR: "data-phcode-internal-c15r5a9",
37+
DATA_BRACKETS_ID_ATTR: "data-brackets-id", // data attribute used to track elements for live preview operations
38+
HIGHLIGHT_CLASSNAME: "__brackets-ld-highlight" // CSS class name used for highlighting elements in live preview
3739
};
3840

3941
// this will store the element that was clicked previously (before the new click)
@@ -62,8 +64,6 @@ function RemoteFunctions(config = {}) {
6264
*/
6365
var _editHandler;
6466

65-
var HIGHLIGHT_CLASSNAME = "__brackets-ld-highlight";
66-
6767
// auto-scroll variables to auto scroll the live preview when an element is dragged to the top/bottom
6868
let _autoScrollTimer = null;
6969
let _isAutoScrolling = false; // to disable highlights when auto scrolling
@@ -128,7 +128,7 @@ function RemoteFunctions(config = {}) {
128128

129129
/**
130130
* check if an element is inspectable.
131-
* inspectable elements are those which doesn't have data-brackets-id,
131+
* inspectable elements are those which doesn't have GLOBALS.DATA_BRACKETS_ID_ATTR ('data-brackets-id'),
132132
* this normally happens when content is DOM content is inserted by some scripting language
133133
*/
134134
function isElementInspectable(element, onlyHighlight = false) {
@@ -157,7 +157,7 @@ function RemoteFunctions(config = {}) {
157157
function isElementEditable(element, onlyHighlight = false) {
158158
// for an element to be editable it should satisfy all inspectable checks and should also have data-brackets-id
159159
return isElementInspectable(element, onlyHighlight) &&
160-
element.hasAttribute("data-brackets-id");
160+
element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
161161
}
162162

163163
// helper function to check if an element is inside the HEAD tag
@@ -336,11 +336,11 @@ function RemoteFunctions(config = {}) {
336336
* This function gets called when the delete button is clicked
337337
* it sends a message to the editor using postMessage to delete the element from the source code
338338
* @param {Event} event
339-
* @param {DOMElement} element - the HTML DOM element that was clicked. it is to get the data-brackets-id attribute
339+
* @param {DOMElement} element - the HTML DOM element that was clicked.
340340
*/
341341
function _handleDeleteOptionClick(event, element) {
342342
if (isElementEditable(element)) {
343-
const tagId = element.getAttribute("data-brackets-id");
343+
const tagId = element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
344344

345345
window._Brackets_MessageBroker.send({
346346
livePreviewEditEnabled: true,
@@ -357,11 +357,11 @@ function RemoteFunctions(config = {}) {
357357
/**
358358
* this is for duplicate button. Read '_handleDeleteOptionClick' jsdoc to understand more on how this works
359359
* @param {Event} event
360-
* @param {DOMElement} element - the HTML DOM element that was clicked. it is to get the data-brackets-id attribute
360+
* @param {DOMElement} element - the HTML DOM element that was clicked.
361361
*/
362362
function _handleDuplicateOptionClick(event, element) {
363363
if (isElementEditable(element)) {
364-
const tagId = element.getAttribute("data-brackets-id");
364+
const tagId = element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
365365

366366
window._Brackets_MessageBroker.send({
367367
livePreviewEditEnabled: true,
@@ -383,7 +383,7 @@ function RemoteFunctions(config = {}) {
383383
*/
384384
function _handleCutOptionClick(event, element) {
385385
if (isElementEditable(element)) {
386-
const tagId = element.getAttribute("data-brackets-id");
386+
const tagId = element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
387387

388388
window._Brackets_MessageBroker.send({
389389
livePreviewEditEnabled: true,
@@ -404,7 +404,7 @@ function RemoteFunctions(config = {}) {
404404
*/
405405
function _handleCopyOptionClick(event, element) {
406406
if (isElementEditable(element)) {
407-
const tagId = element.getAttribute("data-brackets-id");
407+
const tagId = element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
408408

409409
window._Brackets_MessageBroker.send({
410410
livePreviewEditEnabled: true,
@@ -425,7 +425,7 @@ function RemoteFunctions(config = {}) {
425425
*/
426426
function _handlePasteOptionClick(event, targetElement) {
427427
if (isElementEditable(targetElement)) {
428-
const targetTagId = targetElement.getAttribute("data-brackets-id");
428+
const targetTagId = targetElement.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
429429

430430
window._Brackets_MessageBroker.send({
431431
livePreviewEditEnabled: true,
@@ -443,7 +443,7 @@ function RemoteFunctions(config = {}) {
443443
* this is for select-parent button
444444
* When user clicks on this option for a particular element, we get its parent element and trigger a click on it
445445
* @param {Event} event
446-
* @param {DOMElement} element - the HTML DOM element that was clicked. it is to get the data-brackets-id attribute
446+
* @param {DOMElement} element - the HTML DOM element that was clicked
447447
*/
448448
function _handleSelectParentOptionClick(event, element) {
449449
if (!isElementEditable(element)) {
@@ -1081,7 +1081,7 @@ function RemoteFunctions(config = {}) {
10811081
}
10821082

10831083
// Also clear any element references
1084-
let elements = window.document.querySelectorAll("[data-brackets-id]");
1084+
let elements = window.document.querySelectorAll(`[${GLOBALS.DATA_BRACKETS_ID_ATTR}]`);
10851085
for (let j = 0; j < elements.length; j++) {
10861086
delete elements[j]._dropMarker;
10871087
delete elements[j]._dropArrow;
@@ -1119,7 +1119,7 @@ function RemoteFunctions(config = {}) {
11191119
let parent = currentElement.parentElement;
11201120

11211121
while (parent) {
1122-
if (parent.hasAttribute("data-brackets-id") && isElementEditable(parent)) {
1122+
if (parent.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR) && isElementEditable(parent)) {
11231123
const currentRect = currentElement.getBoundingClientRect();
11241124
const parentRect = parent.getBoundingClientRect();
11251125

@@ -1175,8 +1175,8 @@ function RemoteFunctions(config = {}) {
11751175
continue;
11761176
}
11771177

1178-
// Find closest element with data-brackets-id
1179-
while (target && !target.hasAttribute("data-brackets-id")) {
1178+
// Find closest editable element
1179+
while (target && !target.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR)) {
11801180
target = target.parentElement;
11811181
}
11821182

@@ -1208,8 +1208,8 @@ function RemoteFunctions(config = {}) {
12081208
return;
12091209
}
12101210

1211-
// get the closest element with a data-brackets-id
1212-
while (target && !target.hasAttribute("data-brackets-id")) {
1211+
// get the closest editable element
1212+
while (target && !target.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR)) {
12131213
target = target.parentElement;
12141214
}
12151215

@@ -1278,8 +1278,8 @@ function RemoteFunctions(config = {}) {
12781278
// get the element under the cursor
12791279
let target = document.elementFromPoint(event.clientX, event.clientY);
12801280

1281-
// get the closest element with a data-brackets-id
1282-
while (target && !target.hasAttribute("data-brackets-id")) {
1281+
// get the closest editable element
1282+
while (target && !target.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR)) {
12831283
target = target.parentElement;
12841284
}
12851285

@@ -1311,8 +1311,8 @@ function RemoteFunctions(config = {}) {
13111311
);
13121312

13131313
// IDs of the source and target elements
1314-
const sourceId = window._currentDraggedElement.getAttribute("data-brackets-id");
1315-
const targetId = target.getAttribute("data-brackets-id");
1314+
const sourceId = window._currentDraggedElement.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
1315+
const targetId = target.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
13161316

13171317
// Handle different drop zones
13181318
let messageData = {
@@ -1542,13 +1542,13 @@ function RemoteFunctions(config = {}) {
15421542
NodeMoreOptionsBox.prototype = {
15431543
_registerDragDrop: function() {
15441544
// disable dragging on all elements and then enable it on the current element
1545-
const allElements = document.querySelectorAll('[data-brackets-id]');
1545+
const allElements = document.querySelectorAll(`[${GLOBALS.DATA_BRACKETS_ID_ATTR}]`);
15461546
allElements.forEach(el => el.setAttribute("draggable", "false"));
15471547
this.element.setAttribute("draggable", "true");
15481548

15491549
this.element.addEventListener("dragstart", (event) => {
15501550
event.stopPropagation();
1551-
event.dataTransfer.setData("text/plain", this.element.getAttribute("data-brackets-id"));
1551+
event.dataTransfer.setData("text/plain", this.element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR));
15521552
_dragStartChores(this.element);
15531553
_clearDropMarkers();
15541554
window._currentDraggedElement = this.element;
@@ -1918,7 +1918,7 @@ function RemoteFunctions(config = {}) {
19181918
if (newHref !== oldHref) {
19191919
this.element.setAttribute('href', newHref);
19201920

1921-
const tagId = this.element.getAttribute('data-brackets-id');
1921+
const tagId = this.element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
19221922
window._Brackets_MessageBroker.send({
19231923
livePreviewEditEnabled: true,
19241924
livePreviewHyperlinkEdit: true,
@@ -2397,7 +2397,7 @@ function RemoteFunctions(config = {}) {
23972397
const leftPos = offset.left;
23982398

23992399
// if element is non-editable we use gray bg color in info box, otherwise normal blue color
2400-
const bgColor = this.element.hasAttribute('data-brackets-id') ? '#4285F4' : '#3C3F41';
2400+
const bgColor = this.element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR) ? '#4285F4' : '#3C3F41';
24012401

24022402
const styles = `
24032403
:host {
@@ -2789,7 +2789,7 @@ function RemoteFunctions(config = {}) {
27892789
if(!isElementEditable(element)) {
27902790
return;
27912791
}
2792-
const tagId = element.getAttribute("data-brackets-id");
2792+
const tagId = element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
27932793

27942794
window._Brackets_MessageBroker.send({
27952795
livePreviewEditEnabled: true,
@@ -3697,7 +3697,7 @@ function RemoteFunctions(config = {}) {
36973697
folderSettingsButton.addEventListener('click', (e) => {
36983698
e.stopPropagation();
36993699
// send message to LivePreviewEdit to show folder selection dialog
3700-
const tagId = this.element.getAttribute("data-brackets-id");
3700+
const tagId = this.element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
37013701
window._Brackets_MessageBroker.send({
37023702
livePreviewEditEnabled: true,
37033703
resetImageFolderSelection: true,
@@ -3867,7 +3867,7 @@ function RemoteFunctions(config = {}) {
38673867
},
38683868

38693869
_useImage: function(imageUrl, filename, extnName, isLocalFile, thumbDiv, downloadLocation) {
3870-
const tagId = this.element.getAttribute("data-brackets-id");
3870+
const tagId = this.element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
38713871
const downloadId = Date.now() + Math.random();
38723872

38733873
const messageData = {
@@ -4167,7 +4167,7 @@ function RemoteFunctions(config = {}) {
41674167
config.remoteHighlight.paddingStyling
41684168
);
41694169

4170-
highlight.className = HIGHLIGHT_CLASSNAME;
4170+
highlight.className = GLOBALS.HIGHLIGHT_CLASSNAME;
41714171

41724172
var offset = _screenOffset(element);
41734173

@@ -4247,7 +4247,7 @@ function RemoteFunctions(config = {}) {
42474247
},
42484248

42494249
clear: function () {
4250-
var i, highlights = window.document.querySelectorAll("." + HIGHLIGHT_CLASSNAME),
4250+
var i, highlights = window.document.querySelectorAll("." + GLOBALS.HIGHLIGHT_CLASSNAME),
42514251
body = window.document.body;
42524252

42534253
for (i = 0; i < highlights.length; i++) {
@@ -4288,7 +4288,7 @@ function RemoteFunctions(config = {}) {
42884288
function RulerLines(element) {
42894289
this.element = element;
42904290

4291-
const editable = element.hasAttribute("data-brackets-id");
4291+
const editable = element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
42924292
this.color = editable
42934293
? "rgba(66, 133, 244, 0.4)"
42944294
: "rgba(60, 63, 65, 0.8)";
@@ -4687,13 +4687,13 @@ function RemoteFunctions(config = {}) {
46874687

46884688
// if element is not editable and user clicks on it, then we show a toast notification saying
46894689
// that this element is not editable
4690-
if (!element.hasAttribute("data-brackets-id")) {
4690+
if (!element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR)) {
46914691
showToastMessage("notEditable");
46924692
}
46934693

46944694
// make sure that the element is actually visible to the user
46954695
if (isElementVisible(element)) {
4696-
// Only show more options box for editable elements (have data-brackets-id)
4696+
// Only show more options box for editable elements
46974697
if (isElementEditable(element)) {
46984698
_nodeMoreOptionsBox = new NodeMoreOptionsBox(element);
46994699
}
@@ -4705,11 +4705,11 @@ function RemoteFunctions(config = {}) {
47054705
}
47064706

47074707
element._originalOutline = element.style.outline;
4708-
const outlineColor = element.hasAttribute("data-brackets-id") ? "#4285F4" : "#3C3F41";
4708+
const outlineColor = element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR) ? "#4285F4" : "#3C3F41";
47094709
element.style.outline = `1px solid ${outlineColor}`;
47104710

47114711
// Only apply background tint for editable elements (not for dynamic/read-only)
4712-
if (element.hasAttribute("data-brackets-id")) {
4712+
if (element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR)) {
47134713
if (element._originalBackgroundColor === undefined) {
47144714
element._originalBackgroundColor = element.style.backgroundColor;
47154715
}
@@ -4779,9 +4779,9 @@ function RemoteFunctions(config = {}) {
47794779
}
47804780

47814781
// send cursor movement message to editor so cursor jumps to clicked element
4782-
if (element.hasAttribute("data-brackets-id")) {
4782+
if (element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR)) {
47834783
window._Brackets_MessageBroker.send({
4784-
"tagId": element.getAttribute("data-brackets-id"),
4784+
"tagId": element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR),
47854785
"nodeID": element.id,
47864786
"nodeClassList": element.classList,
47874787
"nodeName": element.nodeName,
@@ -5119,7 +5119,7 @@ function RemoteFunctions(config = {}) {
51195119
return this.rememberedNodes[id];
51205120
}
51215121

5122-
var results = this.htmlDocument.querySelectorAll("[data-brackets-id='" + id + "']");
5122+
var results = this.htmlDocument.querySelectorAll(`[${GLOBALS.DATA_BRACKETS_ID_ATTR}='${id}']`);
51235123
return results && results[0];
51245124
};
51255125

@@ -5184,18 +5184,18 @@ function RemoteFunctions(config = {}) {
51845184
function prevIgnoringHighlights(node) {
51855185
do {
51865186
node = node.previousSibling;
5187-
} while (node && node.className === HIGHLIGHT_CLASSNAME);
5187+
} while (node && node.className === GLOBALS.HIGHLIGHT_CLASSNAME);
51885188
return node;
51895189
}
51905190
function nextIgnoringHighlights(node) {
51915191
do {
51925192
node = node.nextSibling;
5193-
} while (node && node.className === HIGHLIGHT_CLASSNAME);
5193+
} while (node && node.className === GLOBALS.HIGHLIGHT_CLASSNAME);
51945194
return node;
51955195
}
51965196
function lastChildIgnoringHighlights(node) {
51975197
node = (node.childNodes.length ? node.childNodes.item(node.childNodes.length - 1) : null);
5198-
if (node && node.className === HIGHLIGHT_CLASSNAME) {
5198+
if (node && node.className === GLOBALS.HIGHLIGHT_CLASSNAME) {
51995199
node = prevIgnoringHighlights(node);
52005200
}
52015201
return node;
@@ -5321,7 +5321,7 @@ function RemoteFunctions(config = {}) {
53215321
Object.keys(edit.attributes).forEach(function (attr) {
53225322
childElement.setAttribute(attr, self._parseEntities(edit.attributes[attr]));
53235323
});
5324-
childElement.setAttribute("data-brackets-id", edit.tagID);
5324+
childElement.setAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR, edit.tagID);
53255325

53265326
if (!editIsSpecialTag) {
53275327
self._insertChildNode(targetElement, childElement, edit);
@@ -5410,7 +5410,7 @@ function RemoteFunctions(config = {}) {
54105410
_hoverHighlight.clear();
54115411
}
54125412
cleanupPreviousElementState();
5413-
const allElements = window.document.querySelectorAll("[data-brackets-id]");
5413+
const allElements = window.document.querySelectorAll(`[${GLOBALS.DATA_BRACKETS_ID_ATTR}]`);
54145414
for (let i = 0; i < allElements.length; i++) {
54155415
if (allElements[i]._originalBackgroundColor !== undefined) {
54165416
clearElementBackground(allElements[i]);
@@ -5658,7 +5658,7 @@ function RemoteFunctions(config = {}) {
56585658

56595659
dismissUIAndCleanupState();
56605660

5661-
const allElements = window.document.querySelectorAll("[data-brackets-id]");
5661+
const allElements = window.document.querySelectorAll(`[${GLOBALS.DATA_BRACKETS_ID_ATTR}]`);
56625662
for (let i = 0; i < allElements.length; i++) {
56635663
if (allElements[i]._originalBackgroundColor !== undefined) {
56645664
clearElementBackground(allElements[i]);
@@ -5779,7 +5779,7 @@ function RemoteFunctions(config = {}) {
57795779
function finishEditing(element, isEditSuccessful = true) {
57805780
finishEditingCleanup(element);
57815781

5782-
const tagId = element.getAttribute("data-brackets-id");
5782+
const tagId = element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
57835783
window._Brackets_MessageBroker.send({
57845784
livePreviewEditEnabled: true,
57855785
livePreviewTextEdit: true,

0 commit comments

Comments
 (0)