Skip to content

Commit

Permalink
Merge pull request #357 from trifle-labs/no-share-just-copy
Browse files Browse the repository at this point in the history
just copy to clipboard if possible
  • Loading branch information
okwme authored Sep 26, 2024
2 parents 382ec15 + 961c6ff commit f4ed576
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dist/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/module.js.map

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions src/visuals.js
Original file line number Diff line number Diff line change
Expand Up @@ -2670,27 +2670,15 @@ export const Visuals = {
async shareCanvas(showPopup = true) {
const file = this.shareCanvasBlob
if (!file) throw new Error('Nothing available to share!')

if (navigator.share) {
console.log('sharing canvas...')
try {
await navigator.share({ files: [file] })
return undefined
} catch (e) {
// ignore user aborting
if (e?.name === 'AbortError') return undefined
console.error('Couldnt share via navigator', e)
// don't throw error, try clipboard
}
}

let copySuccess = false
if (navigator.clipboard && navigator.clipboard.write) {
try {
console.log('trying to copy canvas to clipboard...')
await navigator.clipboard.write([
new ClipboardItem({ 'image/png': file })
])
const msg = 'Copied results to your clipboard.'
copySuccess = true
if (showPopup) {
this.popup = {
header: 'Go Share!',
Expand All @@ -2713,6 +2701,18 @@ export const Visuals = {
throw new Error("Couldn't copy to clipboard. Blocked by browser?")
}
}
if (navigator.share && !copySuccess) {
console.log('sharing canvas...')
try {
await navigator.share({ files: [file] })
return undefined
} catch (e) {
// ignore user aborting
if (e?.name === 'AbortError') return undefined
console.error('Couldnt share via navigator', e)
// don't throw error, try clipboard
}
}

if (!showPopup) return
this.popup = {
Expand Down

0 comments on commit f4ed576

Please sign in to comment.