@@ -48,6 +48,11 @@ let cliArgs, cliCWD, singleInstanceCLIHandler, quitTimeAppUpdateHandler;
4848const PHOENIX_WINDOW_PREFIX = 'phcode-' ;
4949const 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+
5156async function _getTauriWindowLabel ( prefix ) {
5257 // cannot use tauri sync api here as it returns stale window list window.__TAURI__.window.getAll();
5358 const tauriWindowLabels = await window . __TAURI__ . invoke ( '_get_window_labels' ) ;
@@ -177,14 +182,6 @@ Phoenix.app = {
177182 window . __TAURI__ . window . getCurrent ( ) . setFocus ( ) ;
178183 window . __TAURI__ . window . getCurrent ( ) . setAlwaysOnTop ( false ) ;
179184 } ,
180- clipboardReadText : function ( ) {
181- if ( Phoenix . isNativeApp ) {
182- return window . __TAURI__ . clipboard . readText ( ) ;
183- } else if ( window . navigator && window . navigator . clipboard ) {
184- return window . navigator . clipboard . readText ( ) ;
185- }
186- return Promise . reject ( new Error ( "clipboardReadText: Not supported." ) ) ;
187- } ,
188185 /**
189186 * Gets the commandline argument in desktop builds and null in browser builds.
190187 * Will always return CLI of the current process only.
@@ -278,6 +275,21 @@ Phoenix.app = {
278275 } ) ;
279276 }
280277 } ,
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 ) ;
290+ }
291+ return copiedClipboardText ;
292+ } ,
281293 clipboardReadFiles : function ( ) {
282294 return new Promise ( ( resolve , reject ) => {
283295 if ( Phoenix . isNativeApp ) {
@@ -298,19 +310,30 @@ Phoenix.app = {
298310 }
299311 } ) ;
300312 } ,
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 ( ) ;
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+ }
314337 } ,
315338 isFullscreen : function ( ) {
316339 if ( ! Phoenix . isNativeApp ) {
0 commit comments