test(e2e): emulate copy-to-clipboard when no webAPI permission

This commit is contained in:
Junyi Du
2022-04-25 03:20:05 +08:00
parent 15f9657c06
commit 472f548d50
3 changed files with 103 additions and 36 deletions

View File

@@ -1,6 +1,8 @@
import path from 'path/path.js'
// TODO split the capacitor abilities to a separate file for capacitor APIs
import { StatusBar, Style } from '@capacitor/status-bar'
import { Clipboard } from '@capacitor/clipboard';
import { Clipboard as CapacitorClipboard } from '@capacitor/clipboard'
if (typeof window === 'undefined') {
global.window = {}
@@ -242,8 +244,19 @@ export const getClipText = (cb, errorHandler) => {
})
}
// TODO split the capacitor clipboard to a separate file for capacitor APIs
export const writeClipboard = (text, isHtml) => {
if (typeof navigator.permissions !== "undefined") {
if (typeof navigator.permissions == "undefined") {
CapacitorClipboard.write({ string: text });
return
}
navigator.permissions.query({
name: "clipboard-write"
}).then((result) => {
if (typeof ClipboardItem === 'undefined' || (result.state != "granted" && result.state != "prompt")){
console.debug("Copy without `clipboard-write` permission:", text)
return
}
let blob = new Blob([text], {
type: ["text/plain"]
});
@@ -259,21 +272,12 @@ export const writeClipboard = (text, isHtml) => {
["text/html"]: blob
})];
}
navigator.permissions.query({
name: "clipboard-write"
}).then((result) => {
if (result.state == "granted" || result.state == "prompt") {
navigator.clipboard.write(data).then(() => {
/* success */
}).catch(e => {
console.log(e, "fail")
})
}
})} else {
Clipboard.write({
string: text
});
}
navigator.clipboard.write(data).then(() => {
/* success */
}).catch(e => {
console.log(e, "fail")
})
})
}
export const toPosixPath = (input) => {