Skip to content

Commit 61f74ca

Browse files
howwohmmclaude
andcommitted
refactor: deduplicate copied-state handling in clipboard fallback chain
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 85e31f3 commit 61f74ca

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

app/pages/package/[[org]]/[name].vue

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ function prefetchReadmeMarkdown() {
109109
// synchronous while the fetch resolves asynchronously inside the item.
110110
// See: https://wolfgangrittner.dev/how-to-use-clipboard-api-in-safari/
111111
async function copyReadmeHandler() {
112+
let copied = false
113+
112114
try {
113115
const item = new ClipboardItem({
114116
'text/plain': (async () => {
@@ -119,39 +121,35 @@ async function copyReadmeHandler() {
119121
})(),
120122
})
121123
await navigator.clipboard.write([item])
122-
copiedReadme.value = true
123-
setTimeout(() => {
124-
copiedReadme.value = false
125-
}, 2000)
124+
copied = true
126125
} catch {
127126
// Fallback for browsers without ClipboardItem Promise support
128127
await fetchReadmeMarkdown()
129128
const markdown = readmeMarkdownData.value?.markdown
130-
if (!markdown) return
131-
try {
132-
await navigator.clipboard.writeText(markdown)
133-
copiedReadme.value = true
134-
setTimeout(() => {
135-
copiedReadme.value = false
136-
}, 2000)
137-
} catch {
138-
// last resort: execCommand
139-
const textarea = document.createElement('textarea')
140-
textarea.value = markdown
141-
textarea.style.position = 'fixed'
142-
textarea.style.opacity = '0'
143-
document.body.appendChild(textarea)
144-
textarea.select()
145-
const ok = document.execCommand('copy')
146-
document.body.removeChild(textarea)
147-
if (ok) {
148-
copiedReadme.value = true
149-
setTimeout(() => {
150-
copiedReadme.value = false
151-
}, 2000)
129+
if (markdown) {
130+
try {
131+
await navigator.clipboard.writeText(markdown)
132+
copied = true
133+
} catch {
134+
// last resort: execCommand
135+
const textarea = document.createElement('textarea')
136+
textarea.value = markdown
137+
textarea.style.position = 'fixed'
138+
textarea.style.opacity = '0'
139+
document.body.appendChild(textarea)
140+
textarea.select()
141+
copied = document.execCommand('copy')
142+
document.body.removeChild(textarea)
152143
}
153144
}
154145
}
146+
147+
if (copied) {
148+
copiedReadme.value = true
149+
setTimeout(() => {
150+
copiedReadme.value = false
151+
}, 2000)
152+
}
155153
}
156154
157155
// Track active TOC item based on scroll position

0 commit comments

Comments
 (0)