From 4f77c85d84e41fc4adb88eb7265337d26ce76662 Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Wed, 10 Apr 2024 15:29:29 +0200 Subject: [PATCH 1/3] 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 16b5a6671f..6c36da742d 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(); } From 878655340c891abef6081ec93d3f53e72e0aebc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=ADguel=20=C3=81ngel=20Mulero=20Mart=C3=ADnez?= Date: Thu, 11 Apr 2024 19:59:12 +0200 Subject: [PATCH 2/3] Make the code simpler using modern js Co-authored-by: Tomas Chmelevskij --- src/js/Clipboard.js | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/js/Clipboard.js b/src/js/Clipboard.js index 6c36da742d..5a2c551000 100644 --- a/src/js/Clipboard.js +++ b/src/js/Clipboard.js @@ -87,30 +87,16 @@ Clipboard._configureClipboardAsWeb = function() { this.writeText = function(text, onSuccess, onError) { navigator.clipboard.writeText(text).then( - function() { - if (onSuccess) { - onSuccess(text); - } - }, function(err) { - if (onError) { - onError(err); - } - }, + () => onSuccess?.(text), + onError, ); }; this.readText = function(onSuccess, onError) { navigator.clipboard.readText().then( - function(text) { - if (onSuccess) { - onSuccess(text); - } - }, function(err) { - if (onError) { - onError(err); - } - }, + () => onSuccess?.(text), + onError, ); }; }; From d6e28fbb10fc0ef1090a5ea26469e0716d5f0094 Mon Sep 17 00:00:00 2001 From: Miguel Angel Mulero Martinez Date: Thu, 11 Apr 2024 20:16:04 +0200 Subject: [PATCH 3/3] Fix VTX table read from clipboard --- .../jsonschema/vtxconfig_schema-1.0.json | 4 +-- src/js/Clipboard.js | 2 +- src/js/tabs/vtx.js | 33 ++++++++++--------- 3 files changed, 20 insertions(+), 19 deletions(-) rename {src/resources => resources}/jsonschema/vtxconfig_schema-1.0.json (97%) diff --git a/src/resources/jsonschema/vtxconfig_schema-1.0.json b/resources/jsonschema/vtxconfig_schema-1.0.json similarity index 97% rename from src/resources/jsonschema/vtxconfig_schema-1.0.json rename to resources/jsonschema/vtxconfig_schema-1.0.json index 35c03e61e0..c39cbeab25 100644 --- a/src/resources/jsonschema/vtxconfig_schema-1.0.json +++ b/resources/jsonschema/vtxconfig_schema-1.0.json @@ -1,9 +1,9 @@ { "definitions": {}, "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "http://example.com/root.json", + "$id": "http://betaflight.com/vtxconfig_schema-1.0.json", "type": "object", - "title": "The Root Schema", + "title": "Betaflight VTX Table JSON Schema", "required": [ "description", "version", diff --git a/src/js/Clipboard.js b/src/js/Clipboard.js index 5a2c551000..7e6723151b 100644 --- a/src/js/Clipboard.js +++ b/src/js/Clipboard.js @@ -95,7 +95,7 @@ Clipboard._configureClipboardAsWeb = function() { this.readText = function(onSuccess, onError) { navigator.clipboard.readText().then( - () => onSuccess?.(text), + (text) => onSuccess?.(text), onError, ); }; diff --git a/src/js/tabs/vtx.js b/src/js/tabs/vtx.js index 585de96ffe..02a046897f 100644 --- a/src/js/tabs/vtx.js +++ b/src/js/tabs/vtx.js @@ -14,6 +14,7 @@ import { API_VERSION_1_42, API_VERSION_1_44 } from '../data_storage'; import UI_PHONES from "../phones_ui"; import { gui_log } from "../gui_log"; import { checkChromeRuntimeError } from "../utils/common"; +import { isWeb } from "../utils/isWeb"; import $ from 'jquery'; const vtx = { @@ -171,30 +172,30 @@ vtx.initialize = function (callback) { callback_error(); } - // Load schema - const urlVtxSchema = chrome.runtime.getURL(`resources/jsonschema/vtxconfig_schema-${vtxConfig.version}.json`); + function validateAgainstSchema(schemaJson, vtxConfig) { + let valid = false; + if (schemaJson !== undefined) { + // Validate + valid = (TABS.vtx.env.validate(schemaJson, vtxConfig) === undefined); + } + + console.log("Validation against schema result:", valid); + valid ? callback_valid() : callback_error(); + } + const vtxJsonSchemaUrl = `../../resources/jsonschema/vtxconfig_schema-${vtxConfig.version}.json`; + + // Load schema depending on the system if (GUI.isCordova()) { // FIXME On android : Fetch API cannot load : URL scheme "file" is not supported callback_valid(); } else { - fetch(urlVtxSchema) + let vtxJsonSchemaUrl2Fetch = isWeb() ? vtxJsonSchemaUrl : chrome.runtime.getURL(vtxJsonSchemaUrl); + fetch(vtxJsonSchemaUrl2Fetch) .then(response => response.json()) .catch(error => console.error('Error fetching VTX Schema:', error)) - .then(schemaJson => { - - let valid = false; - if (schemaJson !== undefined) { - // Validate - valid = (TABS.vtx.env.validate(schemaJson, vtxConfig) === undefined); - } - - console.log("Validation against schema result:", valid); - valid ? callback_valid() : callback_error(); - }, - ); + .then(schemaJson => validateAgainstSchema(schemaJson, vtxConfig)); } - } // Emulates the MSP read from a vtxConfig object (JSON)