Skip to content

Commit

Permalink
fix copy to clipboard failure on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
llcc authored and tiensonqin committed Apr 17, 2022
1 parent 8df65e7 commit 5a53163
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -66,6 +66,7 @@
"@capacitor/android": "3.2.2",
"@capacitor/app": "1.0.6",
"@capacitor/camera": "1.2.1",
"@capacitor/clipboard": "^1.0.8",
"@capacitor/core": "3.2.2",
"@capacitor/filesystem": "1.0.6",
"@capacitor/ios": "3.2.2",
Expand Down
58 changes: 32 additions & 26 deletions src/main/frontend/utils.js
@@ -1,5 +1,6 @@
import path from 'path/path.js'
import { StatusBar, Style } from '@capacitor/status-bar'
import { Clipboard } from '@capacitor/clipboard';

if (typeof window === 'undefined') {
global.window = {}
Expand Down Expand Up @@ -242,32 +243,37 @@ export const getClipText = (cb, errorHandler) => {
}

export const writeClipboard = (text, isHtml) => {
let blob = new Blob([text], {
type: ["text/plain"]
});
let data = [new ClipboardItem({
["text/plain"]: blob
})];
if (isHtml) {
blob = new Blob([text], {
type: ["text/plain", "text/html"]
})
data = [new ClipboardItem({
["text/plain"]: blob,
["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")
})
}
})
if (typeof navigator.permissions !== "undefined") {
let blob = new Blob([text], {
type: ["text/plain"]
});
let data = [new ClipboardItem({
["text/plain"]: blob
})];
if (isHtml) {
blob = new Blob([text], {
type: ["text/plain", "text/html"]
})
data = [new ClipboardItem({
["text/plain"]: blob,
["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
});
}
}

export const toPosixPath = (input) => {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -535,6 +535,11 @@
tslib "^2.1.0"
xml2js "^0.4.23"

"@capacitor/clipboard@^1.0.8":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@capacitor/clipboard/-/clipboard-1.0.8.tgz#5799cad4518618d45501509348a5c20d3ed32647"
integrity sha512-q8Fb5imJHQtYA+1sGqqigCrXmf0R1ZGXf/XMRtTznQqm0jhiayUusQN63Rv1YtKIPTJeeVJqy/i6rv72d4GH0Q==

"@capacitor/core@3.2.2":
version "3.2.2"
resolved "https://registry.yarnpkg.com/@capacitor/core/-/core-3.2.2.tgz#5926788920ba9117aa735d2941f5b2bdc4a6889a"
Expand Down

0 comments on commit 5a53163

Please sign in to comment.