Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .lighthouserc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
ci: {
collect: {
startServerCommand: 'pnpm preview',
startServerReadyPattern: 'Listening',
startServerReadyPattern: '(Listening|Local|:3000)',
url: [
'http://localhost:3000/',
'http://localhost:3000/search?q=nuxt',
Expand Down
37 changes: 37 additions & 0 deletions app/composables/useClipboardAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { ShallowRef } from 'vue'

type UseClipboardAsyncReturn = {
copy: () => void
copied: ShallowRef<boolean>
}

type UseClipboardAsyncOptions = {
copiedDuring: number
}

export function useClipboardAsync(
fn: () => Promise<string>,
options?: UseClipboardAsyncOptions,
): UseClipboardAsyncReturn {
const copied = shallowRef(false)
const timeout = useTimeoutFn(() => (copied.value = false), options?.copiedDuring ?? 0, {
immediate: false,
})

function copy() {
const asyncClipboard = new ClipboardItem({
'text/plain': fn().then(text => {
return new Blob([text], { type: 'text/plain' })
}),
})

copied.value = true
navigator.clipboard.write([asyncClipboard])
timeout.start()
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

return {
copy,
copied,
}
}
24 changes: 10 additions & 14 deletions app/pages/package/[[org]]/[name].vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,22 @@ const {
)

//copy README file as Markdown
const { copied: copiedReadme, copy: copyReadme } = useClipboard({
source: () => '',
copiedDuring: 2000,
})
const { copied: copiedReadme, copy: copyReadme } = useClipboardAsync(
async () => {
await fetchReadmeMarkdown()
return readmeMarkdownData.value?.markdown ?? ''
},
{
copiedDuring: 2000,
},
)

function prefetchReadmeMarkdown() {
if (readmeMarkdownStatus.value === 'idle') {
fetchReadmeMarkdown()
}
}

async function copyReadmeHandler() {
await fetchReadmeMarkdown()

const markdown = readmeMarkdownData.value?.markdown
if (!markdown) return

await copyReadme(markdown)
}

// Track active TOC item based on scroll position
const tocItems = computed(() => readmeData.value?.toc ?? [])
const { activeId: activeTocId } = useActiveTocItem(tocItems)
Expand Down Expand Up @@ -1019,7 +1015,7 @@ const showSkeleton = shallowRef(false)
<ButtonBase
@mouseenter="prefetchReadmeMarkdown"
@focus="prefetchReadmeMarkdown"
@click="copyReadmeHandler()"
@click="copyReadme"
:aria-pressed="copiedReadme"
:aria-label="
copiedReadme ? $t('common.copied') : $t('package.readme.copy_as_markdown')
Expand Down
Loading