openpencil/src/components/CodePanel.vue
Danila Poyarkov 0fbaf1c24e Adopt Reka UI Tooltip/ToggleGroup, VueUse useClipboard
Reka UI:
- Tip.vue — reusable tooltip wrapping Reka UI Tooltip primitives
- TooltipProvider in EditorView.vue with 400ms delay
- PositionSection: 9 title= attrs → <Tip> wrappers
- TypographySection: alignment → ToggleGroup (single),
  formatting (bold/italic/underline/strikethrough) → ToggleGroup
  (multiple). Keyboard nav + ARIA for free.

VueUse:
- useClipboard replaces navigator.clipboard.writeText in
  CodePanel, CollabPanel, MobileHud, CanvasMenu
2026-03-17 11:30:25 +03:00

130 lines
3.7 KiB
Vue

<script setup lang="ts">
import Prism from 'prismjs'
import 'prismjs/components/prism-jsx'
import { ScrollAreaRoot, ScrollAreaScrollbar, ScrollAreaThumb, ScrollAreaViewport } from 'reka-ui'
import { useClipboard } from '@vueuse/core'
import { computed, ref, watch } from 'vue'
import { selectionToJSX } from '@open-pencil/core'
import { useEditor } from '@open-pencil/vue'
import type { JSXFormat } from '@open-pencil/core'
const store = useEditor()
const { copy } = useClipboard()
const copied = ref(false)
const jsxFormat = ref<JSXFormat>('openpencil')
function toggleFormat() {
jsxFormat.value = jsxFormat.value === 'openpencil' ? 'tailwind' : 'openpencil'
}
const jsxCode = computed(() => {
void store.state.sceneVersion
const ids = [...store.state.selectedIds]
if (ids.length === 0) return ''
return selectionToJSX(ids, store.graph, jsxFormat.value)
})
const highlightedLines = computed(() => {
if (!jsxCode.value) return []
const grammar = Prism.languages.jsx ?? Prism.languages.javascript
return jsxCode.value.split('\n').map((line) => Prism.highlight(line, grammar, 'jsx'))
})
let copyTimeout: ReturnType<typeof setTimeout> | undefined
function copyCode() {
copy(jsxCode.value)
copied.value = true
clearTimeout(copyTimeout)
copyTimeout = setTimeout(() => (copied.value = false), 2000)
}
watch(jsxCode, () => {
copied.value = false
})
</script>
<template>
<div
v-if="!jsxCode"
data-test-id="code-panel-empty"
class="flex flex-1 items-center justify-center px-4 text-center"
>
<span class="text-xs text-muted">Select a layer to see its JSX code</span>
</div>
<div v-else data-test-id="code-panel" class="flex min-h-0 flex-1 flex-col">
<div
data-test-id="code-panel-header"
class="flex shrink-0 items-center justify-between border-b border-border px-3 py-1.5"
>
<div class="flex items-center gap-1.5">
<span class="text-[11px] text-muted">JSX</span>
<button
data-test-id="code-panel-format-toggle"
class="rounded px-1.5 py-0.5 text-[11px] text-muted hover:bg-hover hover:text-surface"
@click="toggleFormat"
>
{{ jsxFormat === 'openpencil' ? 'OpenPencil' : 'Tailwind' }}
</button>
</div>
<button
data-test-id="code-panel-copy"
class="flex items-center gap-1 rounded px-1.5 py-0.5 text-[11px] text-muted hover:bg-hover hover:text-surface"
@click="copyCode"
>
<icon-lucide-check v-if="copied" class="size-3 text-green-400" />
<icon-lucide-copy v-else class="size-3" />
{{ copied ? 'Copied' : 'Copy' }}
</button>
</div>
<ScrollAreaRoot class="min-h-0 flex-1">
<ScrollAreaViewport class="size-full">
<div class="p-3">
<div v-for="(html, i) in highlightedLines" :key="i" class="flex text-xs leading-5">
<span
class="mr-3 shrink-0 text-right text-muted/40 select-none"
style="min-width: 1.5em"
>{{ i + 1 }}</span
>
<pre
class="m-0 min-w-0 flex-1 break-words whitespace-pre-wrap"
><code v-html="html" /></pre>
</div>
</div>
</ScrollAreaViewport>
<ScrollAreaScrollbar orientation="vertical" class="flex w-1.5 touch-none p-px select-none">
<ScrollAreaThumb class="relative flex-1 rounded-full bg-white/10" />
</ScrollAreaScrollbar>
</ScrollAreaRoot>
</div>
</template>
<style scoped>
.token.tag {
color: #7dd3fc;
}
.token.attr-name {
color: #c4b5fd;
}
.token.attr-value,
.token.string {
color: #86efac;
}
.token.number {
color: #fca5a5;
}
.token.punctuation {
color: #888;
}
.token.boolean {
color: #fca5a5;
}
.token.keyword {
color: #c4b5fd;
}
</style>