Skip to content

Commit 40673e3

Browse files
committed
feat: enable deletion of elements in source code from live preview
1 parent a0cd589 commit 40673e3

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

src/LiveDevelopment/BrowserScripts/LiveDevProtocolRemote.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@
128128
}
129129
};
130130

131+
global._Brackets_MessageBroker = MessageBroker;
132+
131133
/**
132134
* Runtime Domain. Implements remote commands for "Runtime.*"
133135
*/

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,25 +229,39 @@ function RemoteFunctions(config) {
229229
console.log("handle duplicate option button was clicked");
230230
}
231231

232-
// TODO: need to implement
233-
function _handleDeleteOptionClick(e) {
234-
console.log("handle delete option button was clicked");
232+
/**
233+
* This function gets called when the delete button is clicked
234+
* it sends a message to the editor using postMessage to delete the element from the source code
235+
* @param {Event} e
236+
* @param {DOMElement} element - the HTML DOM element that was clicked. it is to get the data-brackets-id attribute
237+
*/
238+
function _handleDeleteOptionClick(e, element) {
239+
const tagId = element.getAttribute("data-brackets-id");
240+
if (tagId) {
241+
window._Brackets_MessageBroker.send({
242+
livePreviewEditEnabled: true,
243+
element: element,
244+
event: e,
245+
tagId: tagId,
246+
delete: true
247+
});
248+
}
235249
}
236250

237-
238251
/**
239252
* This function will get triggered when from the multiple advance DOM buttons, one is clicked
240253
* this function just checks which exact button was clicked and call the required function
241254
* @param {Event} e
242255
* @param {String} action - the data-action attribute to differentiate between buttons
256+
* @param {DOMElement} element - the selected DOM element
243257
*/
244-
function handleOptionClick(e, action) {
258+
function handleOptionClick(e, action, element) {
245259
if (action === "select-parent") {
246-
_handleSelectParentOptionClick(e);
260+
_handleSelectParentOptionClick(e, element);
247261
} else if (action === "duplicate") {
248-
_handleDuplicateOptionClick(e);
262+
_handleDuplicateOptionClick(e, element);
249263
} else if (action === "delete") {
250-
_handleDeleteOptionClick(e);
264+
_handleDeleteOptionClick(e, element);
251265
}
252266
}
253267
/**
@@ -288,7 +302,7 @@ function RemoteFunctions(config) {
288302

289303
const boxWidth = 82;
290304

291-
this.body.style.setProperty("left", (elemBounds.left + (elemBounds.width - boxWidth)) + "px");
305+
this.body.style.setProperty("left", (elemBounds.right - boxWidth) + "px");
292306
this.body.style.setProperty(
293307
"top",
294308
// if there's not enough space to show the box above the element,
@@ -343,7 +357,7 @@ function RemoteFunctions(config) {
343357
event.stopPropagation();
344358
event.preventDefault();
345359
const action = event.currentTarget.getAttribute('data-action');
346-
handleOptionClick(event, action);
360+
handleOptionClick(event, action, this.element);
347361
});
348362
});
349363
},

src/LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ define(function (require, exports, module) {
5252
HTMLInstrumentation = require("LiveDevelopment/MultiBrowserImpl/language/HTMLInstrumentation"),
5353
StringUtils = require("utils/StringUtils"),
5454
FileViewController = require("project/FileViewController"),
55-
MainViewManager = require("view/MainViewManager");
55+
MainViewManager = require("view/MainViewManager"),
56+
LivePreviewEdit = require("LiveDevelopment/livePreviewEdit");
5657

5758
const LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME = `phoenix_live_preview_scripts_instrumented_${StringUtils.randomString(8)}.js`;
5859
const LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME = `pageLoaderWorker_${StringUtils.randomString(8)}.js`;
@@ -207,7 +208,9 @@ define(function (require, exports, module) {
207208
var msg = JSON.parse(msgStr),
208209
event = msg.method || "event",
209210
deferred;
210-
if (msg.id) {
211+
if (msg.livePreviewEditEnabled) {
212+
LivePreviewEdit.deleteElementInSourceByTagId(msg);
213+
} else if (msg.id) {
211214
deferred = _responseDeferreds[msg.id];
212215
if (deferred) {
213216
delete _responseDeferreds[msg.id];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
define(function (require, exports, module) {
2+
const EditorManager = require("editor/EditorManager");
3+
4+
function deleteElementInSourceByTagId(tagId) {
5+
const editor = EditorManager.getActiveEditor();
6+
if (!editor || !tagId) {
7+
return;
8+
}
9+
10+
// this will give us the text marker object
11+
const mark = editor.getAllMarks().find((m) => m.tagID === Number(tagId));
12+
if (!mark) {
13+
return;
14+
}
15+
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) {
20+
return;
21+
}
22+
23+
editor.replaceRange("", range.from, range.to);
24+
}
25+
26+
exports.deleteElementInSourceByTagId = deleteElementInSourceByTagId;
27+
});

0 commit comments

Comments
 (0)