Skip to content

Commit 4e21326

Browse files
committed
feat: delete and backspace key support to delete live preview elements
1 parent 79360bb commit 4e21326

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

src/LiveDevelopment/BrowserScripts/LiveDevProtocolRemote.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,13 @@
497497
}
498498
}
499499

500+
if (e.key.toLowerCase() === 'delete' || e.key.toLowerCase() === 'backspace') {
501+
if (!isEditingText && isInEditMode && window._LD.handleDeleteElement) {
502+
e.preventDefault();
503+
window._LD.handleDeleteElement();
504+
}
505+
}
506+
500507
// for save
501508
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "s") {
502509
e.preventDefault();

src/editor/EditorHelper/ChangeHelper.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ define(function (require, exports, module) {
2828
let _cutInterceptor = null;
2929
let _copyInterceptor = null;
3030
let _pasteInterceptor = null;
31+
let _keyEventInterceptor = null;
3132

3233
const CodeMirror = require("thirdparty/CodeMirror/lib/codemirror"),
3334
Menus = require("command/Menus");
@@ -174,6 +175,10 @@ define(function (require, exports, module) {
174175

175176
// Redispatch these CodeMirror key events as Editor events
176177
function _onKeyEvent(instance, event) {
178+
if(_keyEventInterceptor && _keyEventInterceptor(self, self._codeMirror, event)){
179+
// the interceptor processed it, so don't pass it along to CodeMirror'
180+
return;
181+
}
177182
self.trigger("keyEvent", self, event); // deprecated
178183
self.trigger(event.type, self, event);
179184
return event.defaultPrevented; // false tells CodeMirror we didn't eat the event
@@ -310,7 +315,7 @@ define(function (require, exports, module) {
310315
}
311316

312317
/**
313-
* Sets the cut interceptor function
318+
* Sets the cut interceptor function in codemirror
314319
* @param {Function} interceptor - Function(editor, cm, event) that returns true to
315320
preventDefault
316321
*/
@@ -319,7 +324,7 @@ define(function (require, exports, module) {
319324
}
320325

321326
/**
322-
* Sets the copy interceptor function
327+
* Sets the copy interceptor function in codemirror
323328
* @param {Function} interceptor - Function(editor, cm, event) that returns true to
324329
preventDefault
325330
*/
@@ -328,16 +333,26 @@ define(function (require, exports, module) {
328333
}
329334

330335
/**
331-
* Sets the paste interceptor function
336+
* Sets the paste interceptor function in codemirror
332337
* @param {Function} interceptor - Function(editor, cm, event) that returns true to
333338
preventDefault
334339
*/
335340
function setPasteInterceptor(interceptor) {
336341
_pasteInterceptor = interceptor;
337342
}
338343

344+
/**
345+
* Sets the key down/up/press interceptor function in codemirror
346+
* @param {Function} interceptor - Function(editor, cm, event) that returns true to
347+
preventDefault
348+
*/
349+
function setKeyEventInterceptor(interceptor) {
350+
_keyEventInterceptor = interceptor;
351+
}
352+
339353
exports.addHelpers =addHelpers;
340354
exports.setCutInterceptor = setCutInterceptor;
341355
exports.setCopyInterceptor = setCopyInterceptor;
342356
exports.setPasteInterceptor = setPasteInterceptor;
357+
exports.setKeyEventInterceptor = setKeyEventInterceptor;
343358
});

src/extensionsIntegrated/phoenix-pro/LivePreviewEdit.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ define(function (require, exports, module) {
716716
editor.replaceRange("", {line: startPos.line - 1, ch: chPrevLine}, startPos);
717717
}
718718
});
719+
_isInLivePreviewSelectionContext = true;
719720
}
720721

721722
/**
@@ -1964,6 +1965,21 @@ define(function (require, exports, module) {
19641965
}
19651966
});
19661967

1968+
ChangeHelper.setKeyEventInterceptor(function(editor, cm, event) {
1969+
if(event.type !== "keydown" || (event.key.toLowerCase() !== "delete"
1970+
&& event.key.toLowerCase() !== "backspace")) {
1971+
return;
1972+
}
1973+
if (_isInLivePreviewSelectionContext && LiveDevelopment.getCurrentMode() === CONSTANTS.LIVE_EDIT_MODE) {
1974+
const currLiveDoc = LiveDevMultiBrowser.getCurrentLiveDoc();
1975+
if (currLiveDoc && currLiveDoc.protocol && currLiveDoc.protocol.evaluate) {
1976+
event.preventDefault();
1977+
currLiveDoc.protocol.evaluate(`_LD.handleDeleteElement()`);
1978+
return true;
1979+
}
1980+
}
1981+
});
1982+
19671983
function _cursorActivity() {
19681984
if(_isInLivePreviewSelectionContext) {
19691985
window.logger.livePreview.log("unlocked cut/copy/paste from live preview");

src/extensionsIntegrated/phoenix-pro/browser-context/generic-tools.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,29 @@ LivePreviewView.registerNodeMoreOptionsHandler("duplicate", {
4848
});
4949

5050
// delete option
51-
/**
52-
* This function gets called when the delete button is clicked
53-
* it sends a message to the editor using postMessage to delete the element from the source code
54-
* @param {Event} event
55-
* @param {DOMElement} element - the HTML DOM element that was clicked.
56-
*/
57-
function _handleDeleteOptionClick(event, element) {
51+
function _handleDelete(element) {
5852
if (LivePreviewView.isElementEditable(element)) {
5953
const tagId = element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR);
6054

6155
window._Brackets_MessageBroker.send({
6256
livePreviewEditEnabled: true,
6357
element: element,
64-
event: event,
6558
tagId: Number(tagId),
6659
delete: true
6760
});
6861
} else {
6962
console.error("The TagID might be unavailable or the element tag is directly body or html");
7063
}
7164
}
65+
/**
66+
* This function gets called when the delete button is clicked
67+
* it sends a message to the editor using postMessage to delete the element from the source code
68+
* @param {Event} event
69+
* @param {DOMElement} element - the HTML DOM element that was clicked.
70+
*/
71+
function _handleDeleteOptionClick(event, element) {
72+
_handleDelete(element);
73+
}
7274

7375
function _renderDeleteIcon(element, shadow) {
7476
if (element) {
@@ -310,3 +312,8 @@ customReturns.handlePasteElement = ()=> {
310312
_handlePaste(selectedElement);
311313
}
312314
};
315+
customReturns.handleDeleteElement = ()=> {
316+
if(selectedElement){
317+
_handleDelete(selectedElement);
318+
}
319+
};

0 commit comments

Comments
 (0)