Skip to content

Commit cb2e9ef

Browse files
committed
fix: it should be possible to select text in live preview and copy it
1 parent 1ebd185 commit cb2e9ef

File tree

4 files changed

+31
-53
lines changed

4 files changed

+31
-53
lines changed

src/LiveDevelopment/BrowserScripts/LiveDevProtocolRemote.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,16 @@
367367
* the HTML tag corresponding to the clicked element.
368368
*/
369369
function onDocumentClick(event) {
370+
// Get the user's current selection
371+
const selection = window.getSelection();
372+
373+
// Check if there is a selection
374+
if (selection.toString().length > 0) {
375+
// if there is any selection like text or others, we don't see it as a live selection event
376+
// Eg: user may selects ome text in live preview to copy, in which case we should nt treat it
377+
// as a live select.
378+
return;
379+
}
370380
var element = event.target;
371381
if (element && element.hasAttribute('data-brackets-id')) {
372382
MessageBroker.send({

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* modules should define a single function that returns an object of all
2929
* exported functions.
3030
*/
31-
function RemoteFunctions(config, remoteWSPort) {
31+
function RemoteFunctions(config) {
3232

3333

3434
var experimental;
@@ -997,42 +997,7 @@ function RemoteFunctions(config, remoteWSPort) {
997997
if (experimental) {
998998
window.document.addEventListener("keydown", onKeyDown);
999999
}
1000-
1001-
var _ws = null;
1002-
1003-
function onDocumentClick(event) {
1004-
var element = event.target,
1005-
currentDataId,
1006-
newDataId;
1007-
1008-
if (_ws && element && element.hasAttribute('data-brackets-id')) {
1009-
_ws.send(JSON.stringify({
1010-
type: "message",
1011-
message: element.getAttribute('data-brackets-id')
1012-
}));
1013-
}
1014-
}
1015-
1016-
1017-
function createWebSocket() {
1018-
_ws = new WebSocket("ws://localhost:" + remoteWSPort);
1019-
_ws.onopen = function () {
1020-
window.document.addEventListener("click", onDocumentClick);
1021-
};
1022-
1023-
_ws.onmessage = function (evt) {
1024-
};
1025-
1026-
_ws.onclose = function () {
1027-
// websocket is closed
1028-
window.document.removeEventListener("click", onDocumentClick);
1029-
};
1030-
}
1031-
1032-
if (remoteWSPort) {
1033-
createWebSocket();
1034-
}
1035-
1000+
10361001
return {
10371002
"DOMEditHandler" : DOMEditHandler,
10381003
"showGoto" : showGoto,

src/extensionsIntegrated/Phoenix-live-preview/main.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ define(function (require, exports, module) {
6767
const StaticServer = Phoenix.browser.isTauri? NodeStaticServer : BrowserStaticServer;
6868

6969
const EVENT_EMBEDDED_IFRAME_WHO_AM_I = 'whoAmIframePhoenix';
70+
const EVENT_EMBEDDED_IFRAME_FOCUS_EDITOR = 'embeddedIframeFocusEditor';
7071

7172
const PREVIEW_TRUSTED_PROJECT_KEY = "preview_trusted";
7273
const PREVIEW_PROJECT_README_KEY = "preview_readme";
@@ -98,27 +99,15 @@ define(function (require, exports, module) {
9899
}, "*"); // this is not sensitive info, and is only dispatched if requested by the iframe
99100
}
100101
});
102+
StaticServer.on(EVENT_EMBEDDED_IFRAME_FOCUS_EDITOR, function () {
103+
const editor = EditorManager.getActiveEditor();
104+
editor.focus();
105+
});
101106

102107
function _isLiveHighlightEnabled() {
103108
return CommandManager.get(Commands.FILE_LIVE_HIGHLIGHT).getChecked();
104109
}
105110

106-
window.addEventListener('blur', function() {
107-
setTimeout(function() {
108-
const editor = EditorManager.getActiveEditor();
109-
if(!_isLiveHighlightEnabled() || !editor){
110-
return;
111-
}
112-
if (document.activeElement === document.getElementById(LIVE_PREVIEW_IFRAME_ID)
113-
&& !utils.isHTMLFile(editor.document.file.fullPath)) {
114-
// Editor focus is never lost to live preview if live highlights is enabled.
115-
// For html files, they have special handling to set focus so that live preview can take inputs in
116-
// text fields and text area for user to be able to type in live preview html text areas.
117-
editor.focus();
118-
}
119-
}, 100);
120-
});
121-
122111
function _getTrustProjectPage() {
123112
const trustProjectMessage = StringUtils.format(Strings.TRUST_PROJECT,
124113
path.basename(ProjectManager.getProjectRoot().fullPath));

src/extensionsIntegrated/Phoenix-live-preview/markdown.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@
7070
return absoluteUrl.href;
7171
}
7272
document.addEventListener('click', function(event) {
73+
// Get the user's current selection
74+
const selection = window.getSelection();
75+
76+
// Check if there is a selection
77+
if (selection.toString().length === 0) {
78+
// there may be selection like text or others Eg: user may select some text
79+
// in live preview to copy, in which case we should not treat it as a live select.
80+
// if there is no selection, then we have set the focus to the editor
81+
window.parent.postMessage({
82+
handlerName: "ph-liveServer",
83+
eventName: 'embeddedIframeFocusEditor',
84+
href: location.href
85+
}, "{{{PARENT_ORIGIN}}}");
86+
}
7387
// in desktop phoenix builds, tauri will not open anchor tags in browser if it is in
7488
// an iframe(except for the intel mac bug)
7589
// in normal browsers, we dont need to do this.

0 commit comments

Comments
 (0)