Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 01acd57

Browse filesBrowse files
authored
feat(useClipboard): should fall back to legacy clipboard when read/write fails (#4512)
1 parent 0ebefbe commit 01acd57
Copy full SHA for 01acd57

File tree

Expand file treeCollapse file tree

3 files changed

+60
-8
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+60
-8
lines changed
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useClipboard, usePermission } from '@vueuse/core'
2+
import { describe, expect, it } from 'vitest'
3+
4+
describe('useClipboard', () => {
5+
it('should be be supported', () => {
6+
const { isSupported } = useClipboard()
7+
expect(isSupported.value).toBe(true)
8+
})
9+
10+
describe('without permissions', () => {
11+
it('should write to legacy clipboard', async () => {
12+
const writePermission = usePermission('clipboard-write')
13+
expect(writePermission.value).toBe(undefined) // currently no permissions testing legacy fallback
14+
const { text, copy, copied } = useClipboard()
15+
expect(text.value).toBe('')
16+
expect(copied.value).toBe(false)
17+
await copy('hello')
18+
expect(text.value).toBe('hello')
19+
expect(copied.value).toBe(true)
20+
})
21+
it.todo('should read from legacy clipboard')
22+
})
23+
24+
describe('with permissions', () => {
25+
// todo: mock navigator permissions
26+
it.todo('should write to clipboard')
27+
it.todo('should read from clipboard')
28+
it.todo('should fall back to legacy clipboard if write fails')
29+
it.todo('should fall back to legacy clipboard if read fails')
30+
})
31+
})
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { useClipboard } from '@vueuse/core'
2+
import { describe, expect, it } from 'vitest'
3+
4+
describe('useClipboard', () => {
5+
it('should be defined', () => {
6+
expect(useClipboard).toBeDefined()
7+
})
8+
})

‎packages/core/useClipboard/index.ts

Copy file name to clipboardExpand all lines: packages/core/useClipboard/index.ts
+21-8Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ export function useClipboard(options: UseClipboardOptions<MaybeRefOrGetter<strin
7171
const timeout = useTimeoutFn(() => copied.value = false, copiedDuring, { immediate: false })
7272

7373
function updateText() {
74-
if (isClipboardApiSupported.value && isAllowed(permissionRead.value)) {
75-
navigator!.clipboard.readText().then((value) => {
76-
text.value = value
77-
})
74+
let useLegacy = !(isClipboardApiSupported.value && isAllowed(permissionRead.value))
75+
if (!useLegacy) {
76+
try {
77+
navigator!.clipboard.readText().then((value) => {
78+
text.value = value
79+
})
80+
}
81+
catch {
82+
useLegacy = true
83+
}
7884
}
79-
else {
85+
if (useLegacy) {
8086
text.value = legacyRead()
8187
}
8288
}
@@ -86,9 +92,16 @@ export function useClipboard(options: UseClipboardOptions<MaybeRefOrGetter<strin
8692

8793
async function copy(value = toValue(source)) {
8894
if (isSupported.value && value != null) {
89-
if (isClipboardApiSupported.value && isAllowed(permissionWrite.value))
90-
await navigator!.clipboard.writeText(value)
91-
else
95+
let useLegacy = !(isClipboardApiSupported.value && isAllowed(permissionWrite.value))
96+
if (!useLegacy) {
97+
try {
98+
await navigator!.clipboard.writeText(value)
99+
}
100+
catch {
101+
useLegacy = true
102+
}
103+
}
104+
if (useLegacy)
92105
legacyCopy(value)
93106

94107
text.value = value

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.