Skip to content

Commit e6fbfb4

Browse files
committed
feat: enable undo in live preview to undo live preview edit operations
1 parent 0918e2e commit e6fbfb4

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/LiveDevelopment/BrowserScripts/LiveDevProtocolRemote.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,5 +439,14 @@
439439
}
440440
}
441441
window.document.addEventListener("click", onDocumentClick);
442+
window.document.addEventListener("keydown", function (e) {
443+
// for undo. refer to LivePreviewEdit.js file 'handleLivePreviewEditOperation' function
444+
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "z") {
445+
MessageBroker.send({
446+
livePreviewEditEnabled: true,
447+
undoLivePreviewOperation: true
448+
});
449+
}
450+
});
442451

443452
}(this));

src/LiveDevelopment/LivePreviewEdit.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ define(function (require, exports, module) {
142142

143143
/**
144144
* This is the main function that is exported.
145-
* it will be called by LiveDevProtocol when it receives a message from RemoteFunctions.js using MessageBroker
145+
* it will be called by LiveDevProtocol when it receives a message from RemoteFunctions.js
146+
* or LiveDevProtocolRemote.js (for undo) using MessageBroker
146147
* Refer to: `handleOptionClick` function in the RemoteFunctions.js and `_receive` function in LiveDevProtocol.js
147148
*
148149
* @param {Object} message - this is the object that is passed by RemoteFunctions.js using MessageBroker
@@ -151,11 +152,22 @@ define(function (require, exports, module) {
151152
livePreviewEditEnabled: true,
152153
tagId: tagId,
153154
delete || duplicate || livePreviewTextEdit: true
155+
undoLivePreviewOperation: true (this property is available only for undo operation)
154156
}
155157
* these are the main properties that are passed through the message
156158
*/
157159
function handleLivePreviewEditOperation(message) {
158160
if (!message.element || !message.tagId) {
161+
// check for undo
162+
if(message.undoLivePreviewOperation) {
163+
const currLiveDoc = LiveDevMultiBrowser.getCurrentLiveDoc();
164+
if(!currLiveDoc || !currLiveDoc.editor) {
165+
return;
166+
}
167+
168+
const editor = currLiveDoc.editor;
169+
editor.undo();
170+
}
159171
return;
160172
}
161173

0 commit comments

Comments
 (0)