Skip to content

Commit adda9e8

Browse files
committed
feat: hide select-parent option when element doesn't supports it
1 parent c4ec986 commit adda9e8

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,30 @@ function RemoteFunctions(config) {
612612
return true;
613613
}
614614

615+
/**
616+
* this function is to check if an element should show the 'select-parent' option
617+
* because we don't want to show the select parent option when the parent is directly the body/html tag
618+
* or the parent doesn't have the 'data-brackets-id'
619+
* @param {Element} element - DOM element to check
620+
* @returns {boolean} - true if we should show the select parent option otherwise false
621+
*/
622+
function _shouldShowSelectParentOption(element) {
623+
if (!element || !element.parentElement) {
624+
return false;
625+
}
626+
627+
const parentElement = element.parentElement;
628+
629+
if (parentElement.tagName === "HTML" || parentElement.tagName === "BODY") {
630+
return false;
631+
}
632+
if (!parentElement.hasAttribute("data-brackets-id")) {
633+
return false;
634+
}
635+
636+
return true;
637+
}
638+
615639
/**
616640
* This is for the advanced DOM options that appears when a DOM element is clicked
617641
* advanced options like: 'select parent', 'duplicate', 'delete'
@@ -654,11 +678,29 @@ function RemoteFunctions(config) {
654678
// the element that was clicked
655679
let elemBounds = this.element.getBoundingClientRect();
656680

657-
// check if edit text option should be shown to determine box width
681+
// check which options should be shown to determine box width
658682
const showEditTextOption = _shouldShowEditTextOption(this.element);
683+
const showSelectParentOption = _shouldShowSelectParentOption(this.element);
659684

660-
// the box width and the positions where it should be placed
661-
const boxWidth = showEditTextOption ? 106 : 82;
685+
// calculate box width based on visible options
686+
// NOTE: duplicate and delete buttons are always shown
687+
let optionCount = 2;
688+
if (showSelectParentOption) {
689+
optionCount++;
690+
}
691+
if (showEditTextOption) {
692+
optionCount++;
693+
}
694+
695+
// box width we need to decide based on the no. of options
696+
let boxWidth;
697+
if (optionCount === 2) {
698+
boxWidth = 52;
699+
} else if (optionCount === 3) {
700+
boxWidth = 82;
701+
} else {
702+
boxWidth = 106;
703+
}
662704
const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
663705
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
664706

@@ -719,17 +761,23 @@ function RemoteFunctions(config) {
719761
`
720762
};
721763

722-
let content = `<div class="node-options">
723-
<span data-action="select-parent" title="Select Parent">
764+
let content = `<div class="node-options">`;
765+
766+
// Only include select parent option if element supports it
767+
if (showSelectParentOption) {
768+
content += `<span data-action="select-parent" title="Select Parent">
724769
${ICONS.arrowUp}
725770
</span>`;
771+
}
726772

727-
if (showEditTextOption) { // to check if the element is editable
773+
// Only include edit text option if element supports it
774+
if (showEditTextOption) {
728775
content += `<span data-action="edit-text" title="Edit Text">
729776
${ICONS.edit}
730777
</span>`;
731778
}
732779

780+
// Always include duplicate and delete options
733781
content += `<span data-action="duplicate" title="Duplicate">
734782
${ICONS.copy}
735783
</span>

0 commit comments

Comments
 (0)