Skip to content

Commit 718317c

Browse files
devvaannshabose
authored andcommitted
feat: add support for editing js rendered elements
1 parent fe6824c commit 718317c

4 files changed

Lines changed: 168 additions & 5 deletions

File tree

‎src/LiveDevelopment/BrowserScripts/RemoteFunctions.js‎

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function RemoteFunctions(config = {}) {
1212
// to distinguish between phoenix internal vs user created elements
1313
PHCODE_INTERNAL_ATTR: "data-phcode-internal-c15r5a9",
1414
DATA_BRACKETS_ID_ATTR: "data-brackets-id", // data attribute used to track elements for live preview operations
15+
LP_REF_ATTR: "data-phcode-lp-ref", // identity of a script-added element, stamped only when it is selected
1516
HIGHLIGHT_CLASSNAME: "__brackets-ld-highlight" // CSS class name used for highlighting elements in live preview
1617
};
1718

@@ -41,6 +42,12 @@ function RemoteFunctions(config = {}) {
4142
// this will store the element that was clicked previously (before the new click)
4243
// we need this so that we can remove click styling from the previous element when a new element is clicked
4344
let previouslySelectedElement = null;
45+
let _sourcelessObserver = null;
46+
let _sourcelessCheckTimer = null;
47+
let _sourcelessPath = null;
48+
let _sourcelessTag = null;
49+
let _sourcelessClass = null;
50+
const SOURCELESS_RECOVER_DELAY_MS = 60;
4451
let _selectedFromEditor = false;
4552
// the selected element the `phcode-no-lp-edit` opt-out is lifted for, see _isEditOptedOut
4653
let _editOptOutOverride = null;
@@ -176,6 +183,40 @@ function RemoteFunctions(config = {}) {
176183
return isElementInspectable(element, onlyHighlight) && element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
177184
}
178185

186+
// no data-brackets-id means a script added the element, so there is no HTML source for it
187+
function isSourceless(element) {
188+
return !!element && !element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
189+
}
190+
191+
let _lpRefCounter = 0;
192+
const LP_REF_PREFIX = "j";
193+
const RE_NUMERIC_ID = /^\d+$/;
194+
195+
function getElementRef(element) {
196+
if (!element || element.nodeType !== Node.ELEMENT_NODE) {
197+
return null;
198+
}
199+
const tagId = element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
200+
if (tagId) {
201+
return tagId;
202+
}
203+
let ref = element.getAttribute(GLOBALS.LP_REF_ATTR);
204+
if (!ref) {
205+
ref = LP_REF_PREFIX + (++_lpRefCounter);
206+
element.setAttribute(GLOBALS.LP_REF_ATTR, ref);
207+
}
208+
return ref;
209+
}
210+
211+
function getElementByRef(ref) {
212+
if (ref === null || ref === undefined || ref === "") {
213+
return null;
214+
}
215+
const text = String(ref);
216+
const attr = RE_NUMERIC_ID.test(text) ? GLOBALS.DATA_BRACKETS_ID_ATTR : GLOBALS.LP_REF_ATTR;
217+
return window.document.querySelector("[" + attr + '="' + text + '"]');
218+
}
219+
179220
/**
180221
* this function calc the screen offset of an element
181222
*
@@ -205,6 +246,9 @@ function RemoteFunctions(config = {}) {
205246
getAllToolHandlers: getAllToolHandlers,
206247
isElementEditable: isElementEditable,
207248
isElementInspectable: isElementInspectable,
249+
isSourceless: isSourceless,
250+
getElementRef: getElementRef,
251+
getElementByRef: getElementByRef,
208252
isElementVisible: isElementVisible,
209253
screenOffset: screenOffset,
210254
selectElement: selectElement,
@@ -754,6 +798,78 @@ function RemoteFunctions(config = {}) {
754798
previouslySelectedElement = element;
755799
_selectedFromEditor = fromEditor || false;
756800
window.__current_ph_lp_selected = element;
801+
if (isSourceless(element)) {
802+
_watchSourcelessSelection(element);
803+
}
804+
}
805+
806+
function _elementIndexPath(element) {
807+
const path = [];
808+
let el = element;
809+
while (el && el !== window.document.body) {
810+
const parent = el.parentElement;
811+
if (!parent) {
812+
return null;
813+
}
814+
path.unshift(Array.prototype.indexOf.call(parent.children, el));
815+
el = parent;
816+
}
817+
return el === window.document.body ? path : null;
818+
}
819+
820+
function _elementAtIndexPath(path) {
821+
let el = window.document.body;
822+
for (let i = 0; i < path.length && el; i++) {
823+
el = el.children[path[i]];
824+
}
825+
return el || null;
826+
}
827+
828+
// a re-render replaces a script-added node, so re-select the same tag in the same place or dismiss
829+
function _watchSourcelessSelection(element) {
830+
_unwatchSourcelessSelection();
831+
_sourcelessPath = _elementIndexPath(element);
832+
if (!_sourcelessPath) {
833+
return;
834+
}
835+
_sourcelessTag = element.tagName;
836+
_sourcelessClass = typeof element.className === "string" ? element.className : "";
837+
_sourcelessObserver = new MutationObserver(function () {
838+
if (_sourcelessCheckTimer || !previouslySelectedElement || previouslySelectedElement.isConnected) {
839+
return;
840+
}
841+
_sourcelessCheckTimer = setTimeout(_recoverSourcelessSelection, SOURCELESS_RECOVER_DELAY_MS);
842+
});
843+
_sourcelessObserver.observe(window.document.body, { childList: true, subtree: true });
844+
}
845+
846+
function _unwatchSourcelessSelection() {
847+
if (_sourcelessObserver) {
848+
_sourcelessObserver.disconnect();
849+
_sourcelessObserver = null;
850+
}
851+
if (_sourcelessCheckTimer) {
852+
clearTimeout(_sourcelessCheckTimer);
853+
_sourcelessCheckTimer = null;
854+
}
855+
_sourcelessPath = null;
856+
}
857+
858+
function _recoverSourcelessSelection() {
859+
_sourcelessCheckTimer = null;
860+
const old = previouslySelectedElement;
861+
if (!old || old.isConnected || !_sourcelessPath) {
862+
return;
863+
}
864+
const fresh = _elementAtIndexPath(_sourcelessPath);
865+
const className = fresh && typeof fresh.className === "string" ? fresh.className : "";
866+
if (fresh && fresh.tagName === _sourcelessTag && className === _sourcelessClass &&
867+
isSourceless(fresh) && isElementInspectable(fresh, true) && isElementVisible(fresh)) {
868+
const fromEditor = _selectedFromEditor;
869+
selectElement(fresh, fromEditor);
870+
} else {
871+
dismissUIAndCleanupState();
872+
}
757873
}
758874

759875
function disableHoverListeners() {
@@ -849,12 +965,14 @@ function RemoteFunctions(config = {}) {
849965
* @param {HTMLElement} element
850966
*/
851967
function sendSelectionToEditor(element) {
852-
if (!element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR) ||
853-
config.syncSourceAndPreview === false) {
968+
if (config.syncSourceAndPreview === false) {
854969
return;
855970
}
971+
// sent without a tagId too, so a css file in the editor can still jump to the rule
972+
const tagId = element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
856973
MessageBroker.send({
857-
"tagId": element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR),
974+
"tagId": tagId || null,
975+
"sourceless": !tagId,
858976
"nodeID": element.id,
859977
"nodeClassList": element.classList,
860978
"nodeName": element.nodeName,
@@ -1542,6 +1660,7 @@ function RemoteFunctions(config = {}) {
15421660
previouslySelectedElement = null;
15431661
window.__current_ph_lp_selected = null;
15441662
}
1663+
_unwatchSourcelessSelection();
15451664
_editOptOutOverride = null;
15461665

15471666
// Reset hover tracking so the same-element skip doesn't suppress

‎src/LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ define(function (require, exports, module) {
292292
}
293293
return;
294294
}
295+
// a script-added element has no place in the html, only a css file can show it
296+
if (!tagId) {
297+
if (activeEditor && (liveDoc.isRelated(activeEditorPath) || _isLessOrSCSS(activeEditor))) {
298+
_searchAndCursorIfCSS(activeEditor, allSelectors, nodeName);
299+
}
300+
return;
301+
}
295302
const allOpenFileCount = MainViewManager.getWorkingSetSize(MainViewManager.ALL_PANES);
296303
function selectInHTMLEditor(fullHtmlEditor) {
297304
const positionResult = HTMLInstrumentation.getPositionFromTagId(fullHtmlEditor, parseInt(tagId, 10));
@@ -385,7 +392,7 @@ define(function (require, exports, module) {
385392
}
386393
} else if (msg.keyForward) {
387394
_forwardKeyboardShortcutFromIframe(msg);
388-
} else if (msg.clicked && msg.tagId) {
395+
} else if (msg.clicked && (msg.tagId || msg.sourceless)) {
389396
// While previewing an html file, and if css related file is active in the editor, then clicking on the
390397
// live preview, here we set the cursor position in the css file. but this will also trigger a css
391398
// highlight as the cursor changes which jumps the live preview selection.
@@ -402,7 +409,8 @@ define(function (require, exports, module) {
402409
}
403410
_keepFocusUntil = 0;
404411
editMode && liveDoc && liveDoc.disableHighlightOnCursorActivity(false);
405-
liveDoc && liveDoc.updateHighlight();
412+
// the caret did not move for a script-added element, re-highlighting would drop its selection
413+
liveDoc && !msg.sourceless && liveDoc.updateHighlight();
406414
} else {
407415
// enrich received message with clientId
408416
msg.clientId = clientId;

‎src/nls/root/strings.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ define({
187187
"LIVE_DEV_SETTINGS_SHOW_STARTER_BAR_PREFERENCE": "Show the starter bar when the live preview page is empty. Defaults to 'true'",
188188
"LIVE_DEV_SETTINGS_STYLES_BAR_POSITION_PREFERENCE": "Which edge the live preview styles bar docks to, 'top' or 'bottom'. Defaults to 'bottom'",
189189
"LIVE_DEV_TOOLBOX_SELECT_PARENT": "Select Parent",
190+
"LIVE_DEV_TOOLBOX_SELECT_SOURCE_PARENT": "Select Parent in Source",
191+
"LIVE_DEV_TOOLBOX_FIND_IN_CODE": "Find in Code",
192+
"LIVE_DEV_SCRIPT_GENERATED_BADGE": "Script generated",
193+
"LIVE_DEV_SCRIPT_GENERATED_TOOLTIP": "Added by a script, so it is not in the HTML source. Its CSS rules can still be edited.",
194+
"LIVE_DEV_STYLER_SCRIPT_ELEMENT_NOTICE": "Script-generated element: edits are saved to its CSS rules",
190195
"LIVE_DEV_TOOLBOX_EDIT_TEXT": "Edit Text",
191196
"LIVE_DEV_TOOLBOX_DOUBLE_CLICK_HINT": "Double-click",
192197
"LIVE_DEV_TOOLBOX_EDIT_ELEMENT_PROPS": "Edit Element Properties",
@@ -841,6 +846,8 @@ define({
841846
"LIVE_PREVIEW_LAYERS_OPEN_SOURCE": "Open in editor",
842847
"LIVE_PREVIEW_LAYERS_RULE_INACTIVE": "Not active in the current state",
843848
"LIVE_PREVIEW_LAYERS_EDIT_STYLE": "Edit in Styles Bar",
849+
"LIVE_PREVIEW_LAYERS_SHOW_SCRIPT_NODES": "Show Script-Generated Elements",
850+
"LIVE_PREVIEW_LAYERS_SCRIPT_GENERATED": "Generated by script, not in the source file",
844851

845852
"LIVE_DEV_DETACHED_REPLACED_WITH_DEVTOOLS": "Live Preview was canceled because the browser's developer tools were opened",
846853
"LIVE_DEV_DETACHED_TARGET_CLOSED": "Live Preview was canceled because the page was closed in the browser",

‎src/styles/Extn-LayersPanel.less‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,35 @@
500500
margin-left: -2px;
501501
}
502502

503+
.layers-node.layers-script-node .layers-node-icon,
504+
.layers-node.layers-script-node .layers-node-name,
505+
.layers-node.layers-script-node .layers-node-tag {
506+
opacity: 0.72;
507+
}
508+
509+
.layers-node-badge {
510+
display: inline-flex;
511+
align-items: center;
512+
flex: none;
513+
margin-left: 5px;
514+
color: @project-panel-text-2;
515+
opacity: 0.8;
516+
517+
svg {
518+
width: 11px;
519+
height: 11px;
520+
display: block;
521+
}
522+
}
523+
524+
.layers-props-note {
525+
padding: 4px 5px 6px;
526+
color: @project-panel-text-2;
527+
font-style: italic;
528+
line-height: 1.4;
529+
white-space: normal;
530+
}
531+
503532
.layers-node.layers-root-node .layers-node-tag {
504533
font-weight: 500;
505534
color: @project-panel-text-2;

0 commit comments

Comments
 (0)