feat: add copy button
This commit is contained in:
parent
47700869cf
commit
e57d25e8db
38
apps/www/.vitepress/theme/components/BlockCopyButton.vue
Normal file
38
apps/www/.vitepress/theme/components/BlockCopyButton.vue
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { useClipboard } from '@vueuse/core'
|
||||||
|
import { toRefs } from 'vue'
|
||||||
|
import { CheckIcon, ClipboardIcon } from '@radix-icons/vue'
|
||||||
|
import { Button } from '@/lib/registry/new-york/ui/button'
|
||||||
|
import {
|
||||||
|
Tooltip,
|
||||||
|
TooltipContent,
|
||||||
|
TooltipTrigger,
|
||||||
|
} from '@/lib/registry/new-york/ui/tooltip'
|
||||||
|
|
||||||
|
const props = withDefaults(defineProps<{
|
||||||
|
code?: string
|
||||||
|
}>(), {
|
||||||
|
code: '',
|
||||||
|
})
|
||||||
|
const { code } = toRefs(props)
|
||||||
|
|
||||||
|
const { copy, copied } = useClipboard({ source: code })
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<Tooltip :delay-duration="100">
|
||||||
|
<TooltipTrigger as-child>
|
||||||
|
<Button
|
||||||
|
size="icon"
|
||||||
|
variant="outline"
|
||||||
|
class="h-7 w-7 [&_svg]:size-3.5"
|
||||||
|
@click="copy()"
|
||||||
|
>
|
||||||
|
<span class="sr-only">Copy</span>
|
||||||
|
<CheckIcon v-if="copied" />
|
||||||
|
<ClipboardIcon v-else />
|
||||||
|
</Button>
|
||||||
|
</TooltipTrigger>
|
||||||
|
<TooltipContent>Copy code</TooltipContent>
|
||||||
|
</Tooltip>
|
||||||
|
</template>
|
||||||
|
|
@ -7,10 +7,9 @@ import MagicString from 'magic-string'
|
||||||
import { cssVariables } from '../config/shiki'
|
import { cssVariables } from '../config/shiki'
|
||||||
import StyleSwitcher from './StyleSwitcher.vue'
|
import StyleSwitcher from './StyleSwitcher.vue'
|
||||||
import Spinner from './Spinner.vue'
|
import Spinner from './Spinner.vue'
|
||||||
|
import BlockCopyButton from './BlockCopyButton.vue'
|
||||||
import { useConfigStore } from '@/stores/config'
|
import { useConfigStore } from '@/stores/config'
|
||||||
|
|
||||||
// import { BlockCopyCodeButton } from '@/components/block-copy-code-button'
|
|
||||||
// import { Icons } from '@/components/icons'
|
|
||||||
// import { V0Button } from '@/components/v0-button'
|
// import { V0Button } from '@/components/v0-button'
|
||||||
import { Badge } from '@/lib/registry/new-york/ui/badge'
|
import { Badge } from '@/lib/registry/new-york/ui/badge'
|
||||||
import { Popover, PopoverContent, PopoverTrigger } from '@/lib/registry/new-york/ui/popover'
|
import { Popover, PopoverContent, PopoverTrigger } from '@/lib/registry/new-york/ui/popover'
|
||||||
|
|
@ -59,9 +58,11 @@ function transformImportPath(code: string) {
|
||||||
|
|
||||||
watch([style, codeConfig], async () => {
|
watch([style, codeConfig], async () => {
|
||||||
try {
|
try {
|
||||||
rawString.value = await import(`../../../src/lib/registry/${style.value}/block/${props.name}.vue?raw`).then(res => res.default.trim())
|
const baseRawString = await import(`../../../src/lib/registry/${style.value}/block/${props.name}.vue?raw`).then(res => res.default.trim())
|
||||||
|
rawString.value = transformImportPath(removeScript(baseRawString))
|
||||||
|
|
||||||
if (!metadata.description) {
|
if (!metadata.description) {
|
||||||
const { descriptor } = parse(rawString.value)
|
const { descriptor } = parse(baseRawString)
|
||||||
const ast = compileScript(descriptor, { id: '' })
|
const ast = compileScript(descriptor, { id: '' })
|
||||||
walk(ast.scriptAst, {
|
walk(ast.scriptAst, {
|
||||||
enter(node: any) {
|
enter(node: any) {
|
||||||
|
|
@ -78,7 +79,7 @@ watch([style, codeConfig], async () => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
codeHtml.value = await codeToHtml(transformImportPath(removeScript(rawString.value)), {
|
codeHtml.value = await codeToHtml(rawString.value, {
|
||||||
lang: 'vue',
|
lang: 'vue',
|
||||||
theme: cssVariables,
|
theme: cssVariables,
|
||||||
})
|
})
|
||||||
|
|
@ -196,9 +197,9 @@ watch([style, codeConfig], async () => {
|
||||||
</p>
|
</p>
|
||||||
</PopoverContent>
|
</PopoverContent>
|
||||||
</Popover>
|
</Popover>
|
||||||
<!-- <Separator orientation="vertical" class="mx-2 h-4" />
|
<Separator orientation="vertical" class="mx-2 h-4" />
|
||||||
<BlockCopyCodeButton name="{block.name}" code="{block.code}" />
|
<BlockCopyButton :code="rawString" />
|
||||||
<V0Button
|
<!-- <V0Button
|
||||||
name="{block.name}"
|
name="{block.name}"
|
||||||
description="{block.description" || "Edit in v0"}
|
description="{block.description" || "Edit in v0"}
|
||||||
code="{block.code}"
|
code="{block.code}"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user