Skip to content

Commit 79360bb

Browse files
committed
fix: in popped out live preview cut/copy/paste doenst work
1 parent fc287dc commit 79360bb

File tree

3 files changed

+43
-50
lines changed

3 files changed

+43
-50
lines changed

src/LiveDevelopment/BrowserScripts/LivePreviewTransportRemote.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@
440440

441441
let alertQueue = [], confirmCalled = false, promptCalled = false;
442442
let addToQueue = true;
443+
window.__PHOENIX_APP_INFO = {isTauri, platform};
443444
if(!isExternalBrowser){
444445
// this is an embedded iframe we always take hold of the alert api for better ux within the live preivew frame.
445446
window.__PHOENIX_EMBED_INFO = {isTauri, platform};

src/extensionsIntegrated/phoenix-pro/LivePreviewEdit.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ define(function (require, exports, module) {
584584
});
585585
}
586586

587+
let clipboardTextCopiedFallback = null;
588+
587589
/**
588590
* this saves the element to clipboard and deletes its source code
589591
* @param {Number} tagId
@@ -601,9 +603,7 @@ define(function (require, exports, module) {
601603

602604
const { startPos, endPos } = range;
603605
const text = editor.getTextBetween(startPos, endPos);
604-
605-
Phoenix.app.copyToClipboard(text);
606-
_showCopyToastIfNeeded();
606+
clipboardTextCopiedFallback = text;
607607

608608
// delete the elements source code
609609
editor.document.batchOperation(function () {
@@ -616,6 +616,10 @@ define(function (require, exports, module) {
616616
editor.replaceRange("", {line: startPos.line - 1, ch: chPrevLine}, startPos);
617617
}
618618
});
619+
_showCopyToastIfNeeded();
620+
Phoenix.app.copyToClipboard(text)
621+
.catch(console.error); // silently fail as in browser popped out live preview case, this will fail
622+
// and since its expected failure with browser security there is litttle we can do.
619623
}
620624

621625
function _copyElementToClipboard(tagId) {
@@ -631,9 +635,12 @@ define(function (require, exports, module) {
631635

632636
const { startPos, endPos } = range;
633637
const text = editor.getTextBetween(startPos, endPos);
638+
clipboardTextCopiedFallback = text;
634639

635-
Phoenix.app.copyToClipboard(text);
636640
_showCopyToastIfNeeded();
641+
Phoenix.app.copyToClipboard(text)
642+
.catch(console.error); // silently fail as in browser popped out live preview case, this will fail
643+
// and since its expected failure with browser security there is litttle we can do.
637644
}
638645

639646
/**
@@ -652,7 +659,7 @@ define(function (require, exports, module) {
652659

653660
const { startPos, endPos } = range;
654661

655-
Phoenix.app.clipboardReadText().then(text => {
662+
function replaceText(text) {
656663
if (!text) {
657664
return;
658665
}
@@ -668,9 +675,17 @@ define(function (require, exports, module) {
668675
// and we will exit live preview context. So when a user repeatedly presses ctrl+v, we will exit the context
669676
// if we dont set it to true here.
670677
_isInLivePreviewSelectionContext = true;
671-
}).catch(err => {
672-
console.error("Failed to read from clipboard:", err);
673-
});
678+
}
679+
680+
Phoenix.app.clipboardReadText()
681+
.then(replaceText)
682+
.catch(err => {
683+
console.error("Failed to read from clipboard:", err);
684+
// this can fail if the paste is initiated in popped out live preview in browser app and then
685+
// if we try to access clipboard in Phoenix which isn't focused, the browser wont allow that.
686+
// in which case we will use the fallback text passed in from the live preview.
687+
replaceText(clipboardTextCopiedFallback);
688+
});
674689
}
675690

676691
/**

src/phoenix/shell.js

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ let cliArgs, cliCWD, singleInstanceCLIHandler, quitTimeAppUpdateHandler;
4848
const PHOENIX_WINDOW_PREFIX = 'phcode-';
4949
const PHOENIX_EXTENSION_WINDOW_PREFIX = 'extn-';
5050

51-
// this shared a copy of the text that was most recently copied to clipboard. This is used as a fallback for
52-
// clipboardReadText when clipboardReadText fails in the browser(browser disables clipboard
53-
// apis when page is not in focus)
54-
let copiedClipboardText = "";
55-
5651
async function _getTauriWindowLabel(prefix) {
5752
// cannot use tauri sync api here as it returns stale window list window.__TAURI__.window.getAll();
5853
const tauriWindowLabels = await window.__TAURI__.invoke('_get_window_labels');
@@ -275,20 +270,13 @@ Phoenix.app = {
275270
});
276271
}
277272
},
278-
clipboardReadText: async function () {
279-
try{
280-
let textRead;
281-
if(Phoenix.isNativeApp){
282-
textRead = await window.__TAURI__.clipboard.readText();
283-
} else if(window.navigator && window.navigator.clipboard){
284-
textRead = await window.navigator.clipboard.readText();
285-
}
286-
return textRead;
287-
} catch (e) {
288-
// we silently bail out with fallback text. see `copyToClipboard` on why we do this.
289-
console.error("Error while reading clipboard: ", e);
273+
clipboardReadText: function () {
274+
if(Phoenix.isNativeApp){
275+
return window.__TAURI__.clipboard.readText();
276+
} else if(window.navigator && window.navigator.clipboard){
277+
return window.navigator.clipboard.readText();
290278
}
291-
return copiedClipboardText;
279+
return Promise.reject(new Error("clipboardReadText: Not supported."));
292280
},
293281
clipboardReadFiles: function () {
294282
return new Promise((resolve, reject)=>{
@@ -310,30 +298,19 @@ Phoenix.app = {
310298
}
311299
});
312300
},
313-
copyToClipboard: async function (textToCopy) {
314-
try{
315-
copiedClipboardText = textToCopy;
316-
if(Phoenix.isNativeApp){
317-
await window.__TAURI__.clipboard.writeText(textToCopy);
318-
return;
319-
} else if(window.navigator && window.navigator.clipboard){
320-
await window.navigator.clipboard.writeText(textToCopy);
321-
return;
322-
}
323-
const textArea = document.createElement("textarea");
324-
textArea.value = textToCopy;
325-
document.body.appendChild(textArea);
326-
textArea.select();
327-
document.execCommand("copy");
328-
document.body.removeChild(textArea);
329-
} catch (e) {
330-
// we silently bail out as we will fallback to copiedClipboardText local variable.
331-
// in browsers, when tab is not in focus, the clipboard apis can fail as its expected browser behavior.
332-
// in popped out live previews, it may edit the clipboard for copy-paste operations which is normal
333-
// behavior which should work with fallback text. Since this is normal behavior, we dont want to
334-
// report the errors upstream but just log for now.
335-
console.error("Error while copying to clipboard: ", e);
336-
}
301+
copyToClipboard: function (textToCopy) {
302+
if(Phoenix.isNativeApp){
303+
return window.__TAURI__.clipboard.writeText(textToCopy);
304+
} else if(window.navigator && window.navigator.clipboard){
305+
return window.navigator.clipboard.writeText(textToCopy);
306+
}
307+
const textArea = document.createElement("textarea");
308+
textArea.value = textToCopy;
309+
document.body.appendChild(textArea);
310+
textArea.select();
311+
document.execCommand("copy");
312+
document.body.removeChild(textArea);
313+
return Promise.resolve();
337314
},
338315
isFullscreen: function () {
339316
if(!Phoenix.isNativeApp) {

0 commit comments

Comments
 (0)