File tree Expand file tree Collapse file tree 2 files changed +51
-14
lines changed
Expand file tree Collapse file tree 2 files changed +51
-14
lines changed Original file line number Diff line number Diff line change 1+ import type { ShallowRef } from 'vue'
2+
3+ type UseClipboardAsyncReturn = {
4+ copy : ( ) => void
5+ copied : ShallowRef < boolean >
6+ }
7+
8+ type UseClipboardAsyncOptions = {
9+ copiedDuring : number
10+ }
11+
12+ export function useClipboardAsync (
13+ fn : ( ) => Promise < string > ,
14+ options ?: UseClipboardAsyncOptions ,
15+ ) : UseClipboardAsyncReturn {
16+ const copied = shallowRef ( false )
17+ const timeout = useTimeoutFn ( ( ) => ( copied . value = false ) , options ?. copiedDuring ?? 0 , {
18+ immediate : false ,
19+ } )
20+
21+ async function copy ( ) {
22+ const asyncClipboard = new ClipboardItem ( {
23+ 'text/plain' : fn ( ) . then ( text => {
24+ return new Blob ( [ text ] , { type : 'text/plain' } )
25+ } ) ,
26+ } )
27+
28+ try {
29+ await navigator . clipboard . write ( [ asyncClipboard ] )
30+ copied . value = true
31+ timeout . start ( )
32+ } catch {
33+ copied . value = false
34+ }
35+ }
36+
37+ return {
38+ copy,
39+ copied,
40+ }
41+ }
Original file line number Diff line number Diff line change @@ -96,26 +96,22 @@ const {
9696)
9797
9898// copy README file as Markdown
99- const { copied : copiedReadme, copy : copyReadme } = useClipboard ({
100- source : () => ' ' ,
101- copiedDuring: 2000 ,
102- })
99+ const { copied : copiedReadme, copy : copyReadme } = useClipboardAsync (
100+ async () => {
101+ await fetchReadmeMarkdown ()
102+ return readmeMarkdownData .value ?.markdown ?? ' '
103+ },
104+ {
105+ copiedDuring: 2000 ,
106+ },
107+ )
103108
104109function prefetchReadmeMarkdown() {
105110 if (readmeMarkdownStatus .value === ' idle' ) {
106111 fetchReadmeMarkdown ()
107112 }
108113}
109114
110- async function copyReadmeHandler() {
111- await fetchReadmeMarkdown ()
112-
113- const markdown = readmeMarkdownData .value ?.markdown
114- if (! markdown ) return
115-
116- await copyReadme (markdown )
117- }
118-
119115// Track active TOC item based on scroll position
120116const tocItems = computed (() => readmeData .value ?.toc ?? [])
121117const { activeId : activeTocId } = useActiveTocItem (tocItems )
@@ -1019,7 +1015,7 @@ const showSkeleton = shallowRef(false)
10191015 <ButtonBase
10201016 @mouseenter =" prefetchReadmeMarkdown"
10211017 @focus =" prefetchReadmeMarkdown"
1022- @click =" copyReadmeHandler() "
1018+ @click =" copyReadme "
10231019 :aria-pressed =" copiedReadme"
10241020 :aria-label ="
10251021 copiedReadme ? $t('common.copied') : $t('package.readme.copy_as_markdown')
You can’t perform that action at this time.
0 commit comments