From 492a744995b5dba338bd9f3d415de007db26498f Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Wed, 10 Apr 2024 15:29:29 +0200 Subject: [PATCH] Add clipboard feature to web app --- src/js/Clipboard.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/js/Clipboard.js b/src/js/Clipboard.js index 16b5a6671f9..6c36da742df 100644 --- a/src/js/Clipboard.js +++ b/src/js/Clipboard.js @@ -1,4 +1,5 @@ import GUI from './gui.js'; +import { isWeb } from "./utils/isWeb"; /** * Encapsulates the Clipboard logic, depending on web or nw @@ -75,6 +76,45 @@ Clipboard._configureClipboardAsCordova = function() { }; +Clipboard._configureClipboardAsWeb = function() { + + console.log('Web Clipboard available'); + + this.available = true; + this.readAvailable = true; + this.writeAvailable = true; + + this.writeText = function(text, onSuccess, onError) { + + navigator.clipboard.writeText(text).then( + function() { + if (onSuccess) { + onSuccess(text); + } + }, function(err) { + if (onError) { + onError(err); + } + }, + ); + }; + + this.readText = function(onSuccess, onError) { + + navigator.clipboard.readText().then( + function(text) { + if (onSuccess) { + onSuccess(text); + } + }, function(err) { + if (onError) { + onError(err); + } + }, + ); + }; +}; + Clipboard._configureClipboardAsOther = function() { console.warn('NO Clipboard available'); @@ -96,6 +136,8 @@ if (GUI.isNWJS()){ Clipboard._configureClipboardAsNwJs(GUI.nwGui); } else if (GUI.isCordova()) { Clipboard._configureClipboardAsCordova(); +} else if (isWeb()) { + Clipboard._configureClipboardAsWeb(); } else { Clipboard._configureClipboardAsOther(); }