Skip to content

Commit 3ab5b5c

Browse files
committed
feat: keyboard shortcuts for cut/copy/paste should work only in edit mode
1 parent 59dcddb commit 3ab5b5c

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/extensionsIntegrated/phoenix-pro/LivePreviewEdit.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ define(function (require, exports, module) {
3333
const ChangeHelper = require("editor/EditorHelper/ChangeHelper");
3434
const AppInit = require("utils/AppInit");
3535
const Editor = require("editor/Editor").Editor;
36+
const CONSTANTS = require("LiveDevelopment/LivePreviewConstants");
3637

3738
// state manager key, to save the download location of the image
3839
const IMAGE_DOWNLOAD_FOLDER_KEY = "imageGallery.downloadFolder";
@@ -1890,9 +1891,34 @@ define(function (require, exports, module) {
18901891
});
18911892

18921893
let _isInLivePreviewSelectionContext = false;
1893-
// Cut interceptor
1894+
/**
1895+
* Cut (Ctrl+X) interceptor.
1896+
*
1897+
* This handler intercepts CodeMirror’s internal "cut" event (not a DOM event
1898+
* from the Live Preview iframe).
1899+
*
1900+
* Important context:
1901+
* - The Live Preview is embedded, but keyboard focus is always on the editor.
1902+
* - Clicking an element in Live Preview shifts *selection context* to Live Preview,
1903+
* but not keyboard focus.
1904+
* - Therefore, when the user presses Ctrl+X while an element is selected in
1905+
* Live Preview, CodeMirror still receives the cut event.
1906+
*
1907+
* Expected user intent:
1908+
* - Ctrl+X while selecting a Live Preview element → cut the selected DOM node
1909+
* - Ctrl+X while editing text in the editor → cut editor text
1910+
*
1911+
* Behavior:
1912+
* - If we are in Live Preview selection context AND Live Edit mode is active,
1913+
* we prevent CodeMirror’s default cut behavior.
1914+
* - Instead, we forward the cut operation to the Live Preview via protocol,
1915+
* so the selected DOM element is cut correctly.
1916+
*
1917+
* Note:
1918+
* - The Live Preview selection context is reset as soon as any editor event occurs.
1919+
*/
18941920
ChangeHelper.setCutInterceptor(function(editor, cm, event) {
1895-
if (_isInLivePreviewSelectionContext) {
1921+
if (_isInLivePreviewSelectionContext && LiveDevelopment.getCurrentMode() === CONSTANTS.LIVE_EDIT_MODE) {
18961922
const currLiveDoc = LiveDevMultiBrowser.getCurrentLiveDoc();
18971923
if (currLiveDoc && currLiveDoc.protocol && currLiveDoc.protocol.evaluate) {
18981924
event.preventDefault();
@@ -1901,9 +1927,9 @@ define(function (require, exports, module) {
19011927
}
19021928
});
19031929

1904-
// Copy interceptor
1930+
// Copy interceptor, see cut interceptor docs above to understand impl details
19051931
ChangeHelper.setCopyInterceptor(function(editor, cm, event) {
1906-
if (_isInLivePreviewSelectionContext) {
1932+
if (_isInLivePreviewSelectionContext && LiveDevelopment.getCurrentMode() === CONSTANTS.LIVE_EDIT_MODE) {
19071933
const currLiveDoc = LiveDevMultiBrowser.getCurrentLiveDoc();
19081934
if (currLiveDoc && currLiveDoc.protocol && currLiveDoc.protocol.evaluate) {
19091935
event.preventDefault();
@@ -1912,9 +1938,9 @@ define(function (require, exports, module) {
19121938
}
19131939
});
19141940

1915-
// Paste interceptor
1941+
// Paste interceptor, see cut interceptor docs above to understand impl details
19161942
ChangeHelper.setPasteInterceptor(function(editor, cm, event) {
1917-
if (_isInLivePreviewSelectionContext) {
1943+
if (_isInLivePreviewSelectionContext && LiveDevelopment.getCurrentMode() === CONSTANTS.LIVE_EDIT_MODE) {
19181944
const currLiveDoc = LiveDevMultiBrowser.getCurrentLiveDoc();
19191945
if (currLiveDoc && currLiveDoc.protocol && currLiveDoc.protocol.evaluate) {
19201946
event.preventDefault();

0 commit comments

Comments
 (0)