Skip to content

Commit 1cf48a2

Browse files
committed
fix: use existing APIs to reference the source code of the element given its tagId
1 parent 40673e3 commit 1cf48a2

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,16 @@ function RemoteFunctions(config) {
232232
/**
233233
* This function gets called when the delete button is clicked
234234
* it sends a message to the editor using postMessage to delete the element from the source code
235-
* @param {Event} e
235+
* @param {Event} event
236236
* @param {DOMElement} element - the HTML DOM element that was clicked. it is to get the data-brackets-id attribute
237237
*/
238-
function _handleDeleteOptionClick(e, element) {
238+
function _handleDeleteOptionClick(event, element) {
239239
const tagId = element.getAttribute("data-brackets-id");
240240
if (tagId) {
241241
window._Brackets_MessageBroker.send({
242242
livePreviewEditEnabled: true,
243243
element: element,
244-
event: e,
244+
event: event,
245245
tagId: tagId,
246246
delete: true
247247
});

src/LiveDevelopment/MultiBrowserImpl/language/HTMLInstrumentation.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,10 +851,12 @@ define(function (require, exports, module) {
851851
return (mark.tagID === tagId);
852852
});
853853
if (markFound) {
854-
return markFound.find().from;
854+
return {
855+
from: markFound.find().from,
856+
to: markFound.find().to
857+
};
855858
}
856859
return null;
857-
858860
}
859861

860862
// private methods

src/LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ define(function (require, exports, module) {
166166
}
167167
const allOpenFileCount = MainViewManager.getWorkingSetSize(MainViewManager.ALL_PANES);
168168
function selectInHTMLEditor(fullHtmlEditor) {
169-
const position = HTMLInstrumentation.getPositionFromTagId(fullHtmlEditor, parseInt(tagId, 10));
169+
const position = HTMLInstrumentation.getPositionFromTagId(fullHtmlEditor, parseInt(tagId, 10)).from;
170170
if(position && fullHtmlEditor) {
171171
const masterEditor = fullHtmlEditor.document._masterEditor || fullHtmlEditor;
172172
masterEditor.setCursorPos(position.line, position.ch, true);
@@ -209,8 +209,10 @@ define(function (require, exports, module) {
209209
event = msg.method || "event",
210210
deferred;
211211
if (msg.livePreviewEditEnabled) {
212-
LivePreviewEdit.deleteElementInSourceByTagId(msg);
213-
} else if (msg.id) {
212+
LivePreviewEdit.handleLivePreviewEditOperation(msg);
213+
}
214+
215+
if (msg.id) {
214216
deferred = _responseDeferreds[msg.id];
215217
if (deferred) {
216218
delete _responseDeferreds[msg.id];
Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,55 @@
11
define(function (require, exports, module) {
22
const EditorManager = require("editor/EditorManager");
3+
const HTMLInstrumentation = require("LiveDevelopment/MultiBrowserImpl/language/HTMLInstrumentation");
34

4-
function deleteElementInSourceByTagId(tagId) {
5+
6+
/**
7+
* This function is responsible to delete an element from the source code
8+
* @param {Number} tagId - the data-brackets-id of the DOM element
9+
*/
10+
function _deleteElementInSourceByTagId(tagId) {
511
const editor = EditorManager.getActiveEditor();
612
if (!editor || !tagId) {
713
return;
814
}
915

10-
// this will give us the text marker object
11-
const mark = editor.getAllMarks().find((m) => m.tagID === Number(tagId));
12-
if (!mark) {
16+
// this will give us the start pos and end pos of the DOM element in the source code
17+
// can be referenced using range.from and range.to
18+
const range = HTMLInstrumentation.getPositionFromTagId(editor, tagId);
19+
if(!range) {
1320
return;
1421
}
1522

16-
// this give us the start position to the end position of the code.
17-
// we just need to delete it
18-
const range = mark.find();
19-
if (!range) {
23+
editor.replaceRange("", range.from, range.to);
24+
}
25+
26+
/**
27+
* This is the main function that is exported.
28+
* it will be called by LiveDevProtocol when it receives a message from RemoteFunctions.js using MessageBroker
29+
* Refer to: `handleOptionClick` function in the RemoteFunctions.js and `_receive` function in LiveDevProtocol.js
30+
*
31+
* @param {Object} message - this is the object that is passed by RemoteFunctions.js using MessageBroker
32+
* this object will be in the format
33+
* {
34+
livePreviewEditEnabled: true,
35+
element: element,
36+
event: event,
37+
tagId: tagId,
38+
delete: true
39+
}
40+
* here element is the actual DOM element that is clicked, and tagId is the data-brackets-id
41+
* and 'delete: true is just an example, it might be 'duplicate' or 'select-parent' also
42+
*/
43+
function handleLivePreviewEditOperation(message) {
44+
if (!message.element || !message.tagId) {
2045
return;
2146
}
2247

23-
editor.replaceRange("", range.from, range.to);
48+
// just call the required functions
49+
if (message.delete) {
50+
_deleteElementInSourceByTagId(Number(message.tagId));
51+
}
2452
}
2553

26-
exports.deleteElementInSourceByTagId = deleteElementInSourceByTagId;
54+
exports.handleLivePreviewEditOperation = handleLivePreviewEditOperation;
2755
});

0 commit comments

Comments
 (0)