Skip to content

AI-native design editor. Open-source Figma alternative.

Integrate SDK composables into all app components

Danila Poyarkov29b63842fafe
diff --git a/src/components/CanvasMenu.vue b/src/components/CanvasMenu.vue
index e7086c62b..66977e685 100644
--- a/src/components/CanvasMenu.vue
+++ b/src/components/CanvasMenu.vue
@@ -42,18 +42,6 @@ function ids() {
return [...selectedIds.value]
}
-function copyAsText() {
- return editor.copySelectionAsText(ids())
-}
-
-function copyAsSVG() {
- return editor.copySelectionAsSVG(ids())
-}
-
-function copyAsJSX() {
- return editor.copySelectionAsJSX(ids())
-}
-
function execCommand(cmd: string) {
window.document.execCommand(cmd)
}
@@ -65,7 +53,7 @@ async function clipboardWrite(text: string | null, label: string) {
}
async function copyAsPNG() {
- const data = await store.renderExportImage([...store.state.selectedIds], 2, 'PNG')
+ const data = await store.renderExportImage([...selectedIds.value], 2, 'PNG')
if (!data) return
const blob = new Blob([data], { type: 'image/png' })
await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })])
@@ -197,16 +185,22 @@ const cls = {
</ContextMenuSubTrigger>
<ContextMenuPortal>
<ContextMenuSubContent :class="cls.menu">
- <ContextMenuItem :class="cls.item" @select="clipboardWrite(copyAsText(), 'text')"
+ <ContextMenuItem
+ :class="cls.item"
+ @select="clipboardWrite(editor.copySelectionAsText(ids()), 'text')"
>Copy as text</ContextMenuItem
>
- <ContextMenuItem :class="cls.item" @select="clipboardWrite(copyAsSVG(), 'SVG')"
+ <ContextMenuItem
+ :class="cls.item"
+ @select="clipboardWrite(editor.copySelectionAsSVG(ids()), 'SVG')"
>Copy as SVG</ContextMenuItem
>
<ContextMenuItem :class="cls.item" @select="copyAsPNG">
<span>Copy as PNG</span><span class="text-[11px] text-muted">⇧⌘C</span>
</ContextMenuItem>
- <ContextMenuItem :class="cls.item" @select="clipboardWrite(copyAsJSX(), 'JSX')"
+ <ContextMenuItem
+ :class="cls.item"
+ @select="clipboardWrite(editor.copySelectionAsJSX(ids()), 'JSX')"
>Copy as JSX</ContextMenuItem
>
</ContextMenuSubContent>
diff --git a/src/components/FillPicker.vue b/src/components/FillPicker.vue
index 8e23efca6..5d99e90c2 100644
--- a/src/components/FillPicker.vue
+++ b/src/components/FillPicker.vue
@@ -6,22 +6,11 @@ import { twMerge } from 'tailwind-merge'
import GradientEditor from './GradientEditor.vue'
import HsvColorArea from './HsvColorArea.vue'
import ImageFillPicker from './ImageFillPicker.vue'
-import { colorToCSS } from '@open-pencil/core'
+import { useFillPicker } from '@open-pencil/vue'
import Tip from './Tip.vue'
-import type { Fill, GradientStop } from '@open-pencil/core'
-
-type FillCategory = 'SOLID' | 'GRADIENT' | 'IMAGE'
-
-const FILL_CATEGORY: Record<string, FillCategory> = {
- SOLID: 'SOLID',
- GRADIENT_LINEAR: 'GRADIENT',
- GRADIENT_RADIAL: 'GRADIENT',
- GRADIENT_ANGULAR: 'GRADIENT',
- GRADIENT_DIAMOND: 'GRADIENT',
- IMAGE: 'IMAGE'
-}
+import type { Fill } from '@open-pencil/core'
const TAB_BASE =
'flex size-6 cursor-pointer items-center justify-center rounded border-none p-0 transition-colors'
@@ -36,44 +25,10 @@ function tabClass(active: boolean) {
const { fill } = defineProps<{ fill: Fill }>()
const emit = defineEmits<{ update: [fill: Fill] }>()
-const category = computed(() => FILL_CATEGORY[fill.type] ?? 'SOLID')
-
-function toSolid() {
- if (category.value === 'SOLID') return
- const color = fill.gradientStops?.[0]?.color ?? fill.color
- emit('update', { ...fill, type: 'SOLID', color: { ...color } })
-}
-
-function toGradient() {
- if (category.value === 'GRADIENT') return
- const stops: GradientStop[] = fill.gradientStops?.length
- ? fill.gradientStops
- : [
- { color: { ...fill.color }, position: 0 },
- { color: { r: 1, g: 1, b: 1, a: 1 }, position: 1 }
- ]
- emit('update', {
- ...fill,
- type: 'GRADIENT_LINEAR',
- gradientStops: stops,
- gradientTransform: { m00: 1, m01: 0, m02: 0, m10: 0, m11: 0, m12: 0.5 }
- })
-}
-
-function toImage() {
- if (category.value === 'IMAGE') return
- emit('update', { ...fill, type: 'IMAGE' })
-}
-
-function gradientCSS(stops: GradientStop[]): string {
- return stops.map((s) => `${colorToCSS(s.color)} ${s.position * 100}%`).join(', ')
-}
-
-const swatchBg = computed(() => {
- if (category.value === 'GRADIENT' && fill.gradientStops?.length)
- return `linear-gradient(to right, ${gradientCSS(fill.gradientStops)})`
- return colorToCSS(fill.color)
-})
+const { category, swatchBg, toSolid, toGradient, toImage } = useFillPicker(
+ computed(() => fill),
+ (updated) => emit('update', updated)
+)
</script>
<template>
diff --git a/src/components/PagesPanel.vue b/src/components/PagesPanel.vue
index f60e952ef..c751f5a19 100644
--- a/src/components/PagesPanel.vue
+++ b/src/components/PagesPanel.vue
@@ -2,20 +2,19 @@
import { ref } from 'vue'
import Tip from '@/components/Tip.vue'
-import { useInlineRename, useEditor, useSceneComputed } from '@open-pencil/vue'
+import { useInlineRename, usePageList } from '@open-pencil/vue'
-const store = useEditor()
+const { pages, currentPageId, switchPage, addPage, renamePage } = usePageList()
const DIVIDER_RE = /^[-–—*\s]+$/
-const pageInputRefs = new Map<string, HTMLInputElement>()
function isDivider(page: { name: string; childIds: string[] }) {
return page.childIds.length === 0 && DIVIDER_RE.test(page.name)
}
-const pages = useSceneComputed(() => store.getPages())
+const pageInputRefs = new Map<string, HTMLInputElement>()
-const rename = useInlineRename((id, name) => store.renamePage(id, name))
+const rename = useInlineRename((id, name) => renamePage(id, name))
const activeRenameId = ref<string | null>(null)
function setPageInputRef(pageId: string, el: HTMLInputElement | null) {
@@ -44,7 +43,7 @@ function startRename(pg: { id: string; name: string }) {
<button
data-test-id="pages-add"
class="cursor-pointer rounded border-none bg-transparent px-1 text-base leading-none text-muted hover:bg-hover hover:text-surface"
- @click="store.addPage()"
+ @click="addPage()"
>
+
</button>
@@ -78,11 +77,11 @@ function startRename(pg: { id: string; name: string }) {
data-test-id="pages-item"
class="flex w-full cursor-pointer items-center gap-1.5 rounded border-none px-2 py-1 text-left text-xs"
:class="
- pg.id === store.state.currentPageId
+ pg.id === currentPageId
? 'bg-hover text-surface'
: 'bg-transparent text-muted hover:bg-hover hover:text-surface'
"
- @click="store.switchPage(pg.id)"
+ @click="switchPage(pg.id)"
@dblclick="startRename(pg)"
>
<icon-lucide-file class="size-3 shrink-0" />
diff --git a/src/components/Toolbar.vue b/src/components/Toolbar.vue
index efc8e96e0..90a9befb9 100644
--- a/src/components/Toolbar.vue
+++ b/src/components/Toolbar.vue
@@ -29,11 +29,9 @@ import Tip from '@/components/Tip.vue'
import { ACTION_TOAST_DURATION } from '@/constants'
import { useEditorStore } from '@/stores/editor'
import { toolIcons } from '@/utils/tools'
+import { ToolbarRoot, ToolbarItem } from '@open-pencil/vue'
import type { Component } from 'vue'
import type { EditorToolDef, Tool } from '@open-pencil/vue'
-import { EDITOR_TOOLS } from '@open-pencil/vue'
-
-const TOOLS = EDITOR_TOOLS
const store = useEditorStore()
const breakpoints = useBreakpoints({ mobile: 768 })
@@ -67,13 +65,13 @@ const toolShortcuts: Record<Tool, string> = {
HAND: 'H'
}
-function isActive(tool: EditorToolDef): boolean {
- if (tool.key === store.state.activeTool) return true
- return tool.flyout?.includes(store.state.activeTool) ?? false
+function isActive(tool: EditorToolDef, activeTool: Tool): boolean {
+ if (tool.key === activeTool) return true
+ return tool.flyout?.includes(activeTool) ?? false
}
-function activeKeyForTool(tool: EditorToolDef): Tool {
- if (tool.flyout?.includes(store.state.activeTool)) return store.state.activeTool
+function activeKeyForTool(tool: EditorToolDef, activeTool: Tool): Tool {
+ if (tool.flyout?.includes(activeTool)) return activeTool
return tool.key
}
@@ -137,256 +135,272 @@ function goNext() {
</script>
<template>
- <div v-if="!isMobile" class="absolute bottom-4 left-1/2 z-10 flex -translate-x-1/2 items-center">
+ <ToolbarRoot v-slot="{ tools, activeTool, setTool }">
<div
- data-test-id="toolbar"
- class="flex gap-0.5 rounded-xl border border-border bg-panel p-1 shadow-lg"
+ v-if="!isMobile"
+ class="absolute bottom-4 left-1/2 z-10 flex -translate-x-1/2 items-center"
>
- <template v-for="tool in TOOLS" :key="tool.key">
- <div v-if="tool.flyout && tool.flyout.length > 1" class="flex items-center">
- <Tip :label="`${toolLabels[activeKeyForTool(tool)]} (${tool.shortcut})`">
- <button
- :data-test-id="`toolbar-tool-${activeKeyForTool(tool).toLowerCase()}`"
- class="flex size-8 cursor-pointer items-center justify-center rounded-lg border-none transition-colors"
- :class="
- isActive(tool)
- ? 'bg-accent text-white'
- : 'bg-transparent text-muted hover:bg-hover hover:text-surface'
- "
- @click="store.setTool(activeKeyForTool(tool))"
- >
- <component :is="toolIcons[activeKeyForTool(tool)]" class="size-4" />
- </button>
- </Tip>
-
- <DropdownMenuRoot>
- <DropdownMenuTrigger as-child>
+ <div
+ data-test-id="toolbar"
+ class="flex gap-0.5 rounded-xl border border-border bg-panel p-1 shadow-lg"
+ >
+ <template v-for="tool in tools" :key="tool.key">
+ <div v-if="tool.flyout && tool.flyout.length > 1" class="flex items-center">
+ <Tip :label="`${toolLabels[activeKeyForTool(tool, activeTool)]} (${tool.shortcut})`">
<button
- :data-test-id="`toolbar-flyout-${tool.key.toLowerCase()}`"
- class="flex h-8 w-3 cursor-pointer items-center justify-center rounded-lg border-none transition-colors"
+ :data-test-id="`toolbar-tool-${activeKeyForTool(tool, activeTool).toLowerCase()}`"
+ class="flex size-8 cursor-pointer items-center justify-center rounded-lg border-none transition-colors"
:class="
- isActive(tool)
+ isActive(tool, activeTool)
? 'bg-accent text-white'
: 'bg-transparent text-muted hover:bg-hover hover:text-surface'
"
+ @click="setTool(activeKeyForTool(tool, activeTool))"
>
- <IconChevronDown class="size-2.5" />
+ <component :is="toolIcons[activeKeyForTool(tool, activeTool)]" class="size-4" />
</button>
- </DropdownMenuTrigger>
+ </Tip>
- <DropdownMenuPortal>
- <DropdownMenuContent
- side="top"
- :side-offset="8"
- align="start"
- :class="menuContent({ class: 'min-w-32' })"
- >
- <DropdownMenuItem
- v-for="sub in tool.flyout"
- :key="sub"
- :data-test-id="`toolbar-flyout-item-${sub.toLowerCase()}`"
+ <DropdownMenuRoot>
+ <DropdownMenuTrigger as-child>
+ <button
+ :data-test-id="`toolbar-flyout-${tool.key.toLowerCase()}`"
+ class="flex h-8 w-3 cursor-pointer items-center justify-center rounded-lg border-none transition-colors"
:class="
- menuItem({
- class: store.state.activeTool === sub ? 'bg-accent text-white' : undefined
- })
+ isActive(tool, activeTool)
+ ? 'bg-accent text-white'
+ : 'bg-transparent text-muted hover:bg-hover hover:text-surface'
"
- @select="store.setTool(sub)"
>
- <component :is="toolIcons[sub]" class="size-3.5" />
- <span class="flex-1">{{ toolLabels[sub] }}</span>
- <span v-if="toolShortcuts[sub]" class="text-[11px] text-muted">{{
- toolShortcuts[sub]
- }}</span>
- </DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenuPortal>
- </DropdownMenuRoot>
- </div>
-
- <Tip v-else :label="`${toolLabels[tool.key]} (${tool.shortcut})`">
- <button
- :data-test-id="`toolbar-tool-${tool.key.toLowerCase()}`"
- class="flex size-8 cursor-pointer items-center justify-center rounded-lg border-none transition-colors"
- :class="
- isActive(tool)
- ? 'bg-accent text-white'
- : 'bg-transparent text-muted hover:bg-hover hover:text-surface'
- "
- @click="store.setTool(tool.key)"
- >
- <component :is="toolIcons[tool.key]" class="size-4" />
- </button>
- </Tip>
- </template>
- </div>
- </div>
+ <IconChevronDown class="size-2.5" />
+ </button>
+ </DropdownMenuTrigger>
- <!-- Mobile toolbar -->
- <div
- v-else
- data-test-id="mobile-toolbar"
- class="fixed left-1/2 z-20 flex -translate-x-1/2 items-center gap-1.5"
- :style="{
- maxWidth: 'calc(100vw - 2rem)',
- bottom: `calc(56px + env(safe-area-inset-bottom) + 0.75rem)`
- }"
- >
- <motion.button
- data-test-id="mobile-toolbar-prev"
- class="flex size-7 shrink-0 cursor-pointer items-center justify-center rounded-full border border-border bg-panel shadow-sm select-none"
- :class="hasPrev ? 'text-muted' : 'pointer-events-none'"
- :animate="{ opacity: hasPrev ? 1 : 0 }"
- :transition="{ duration: 0.15 }"
- @click="goPrev"
- >
- <IconChevronLeft class="size-3.5" />
- </motion.button>
+ <DropdownMenuPortal>
+ <DropdownMenuContent
+ side="top"
+ :side-offset="8"
+ align="start"
+ :class="menuContent({ class: 'min-w-32' })"
+ >
+ <ToolbarItem
+ v-for="sub in tool.flyout"
+ :key="sub"
+ v-slot="{ active: subActive, select: selectSub }"
+ :tool="sub"
+ >
+ <DropdownMenuItem
+ :data-test-id="`toolbar-flyout-item-${sub.toLowerCase()}`"
+ :class="
+ menuItem({
+ class: subActive ? 'bg-accent text-white' : undefined
+ })
+ "
+ @select="selectSub"
+ >
+ <component :is="toolIcons[sub]" class="size-3.5" />
+ <span class="flex-1">{{ toolLabels[sub] }}</span>
+ <span v-if="toolShortcuts[sub]" class="text-[11px] text-muted">{{
+ toolShortcuts[sub]
+ }}</span>
+ </DropdownMenuItem>
+ </ToolbarItem>
+ </DropdownMenuContent>
+ </DropdownMenuPortal>
+ </DropdownMenuRoot>
+ </div>
- <motion.div
- layout
- data-test-id="mobile-toolbar-container"
- class="relative flex h-11 items-center overflow-hidden rounded-[8px] border border-border bg-panel px-2 shadow-lg"
- :transition="{ layout: { type: 'spring', damping: 30, stiffness: 500 } }"
- >
- <AnimatePresence mode="popLayout" :custom="slideDirection">
- <motion.div
- v-if="mobileCategory === 0"
- key="tools"
- data-test-id="mobile-toolbar-tools"
- class="flex gap-0.5"
- :variants="slideVariants"
- initial="initial"
- animate="animate"
- exit="exit"
- :transition="{ duration: 0.15 }"
- >
- <template v-for="tool in TOOLS" :key="tool.key">
- <div v-if="tool.flyout && tool.flyout.length > 1" class="flex items-center">
+ <ToolbarItem v-else v-slot="{ active, select: selectTool }" :tool="tool.key">
+ <Tip :label="`${toolLabels[tool.key]} (${tool.shortcut})`">
<button
- :data-test-id="`mobile-toolbar-tool-${activeKeyForTool(tool).toLowerCase()}`"
- class="flex size-8 cursor-pointer items-center justify-center rounded-[6px] border-none transition-colors select-none"
+ :data-test-id="`toolbar-tool-${tool.key.toLowerCase()}`"
+ class="flex size-8 cursor-pointer items-center justify-center rounded-lg border-none transition-colors"
:class="
- isActive(tool)
+ active
? 'bg-accent text-white'
- : 'bg-transparent text-muted active:bg-hover'
+ : 'bg-transparent text-muted hover:bg-hover hover:text-surface'
"
- @click="store.setTool(activeKeyForTool(tool))"
+ @click="selectTool"
>
- <component :is="toolIcons[activeKeyForTool(tool)]" class="size-4" />
+ <component :is="toolIcons[tool.key]" class="size-4" />
</button>
+ </Tip>
+ </ToolbarItem>
+ </template>
+ </div>
+ </div>
- <DropdownMenuRoot>
- <DropdownMenuTrigger as-child>
- <button
- :data-test-id="`mobile-toolbar-flyout-${tool.key.toLowerCase()}`"
- class="flex h-8 w-3 cursor-pointer items-center justify-center rounded-[6px] border-none transition-colors select-none"
- :class="
- isActive(tool)
- ? 'bg-accent text-white'
- : 'bg-transparent text-muted active:bg-hover'
- "
- >
- <IconChevronDown class="size-2.5" />
- </button>
- </DropdownMenuTrigger>
+ <!-- Mobile toolbar -->
+ <div
+ v-else
+ data-test-id="mobile-toolbar"
+ class="fixed left-1/2 z-20 flex -translate-x-1/2 items-center gap-1.5"
+ :style="{
+ maxWidth: 'calc(100vw - 2rem)',
+ bottom: `calc(56px + env(safe-area-inset-bottom) + 0.75rem)`
+ }"
+ >
+ <motion.button
+ data-test-id="mobile-toolbar-prev"
+ class="flex size-7 shrink-0 cursor-pointer items-center justify-center rounded-full border border-border bg-panel shadow-sm select-none"
+ :class="hasPrev ? 'text-muted' : 'pointer-events-none'"
+ :animate="{ opacity: hasPrev ? 1 : 0 }"
+ :transition="{ duration: 0.15 }"
+ @click="goPrev"
+ >
+ <IconChevronLeft class="size-3.5" />
+ </motion.button>
- <DropdownMenuPortal>
- <DropdownMenuContent
- side="top"
- :side-offset="8"
- align="start"
- :class="menuContent({ class: 'min-w-32' })"
- >
- <DropdownMenuItem
- v-for="sub in tool.flyout"
- :key="sub"
- :data-test-id="`mobile-toolbar-flyout-item-${sub.toLowerCase()}`"
+ <motion.div
+ layout
+ data-test-id="mobile-toolbar-container"
+ class="relative flex h-11 items-center overflow-hidden rounded-[8px] border border-border bg-panel px-2 shadow-lg"
+ :transition="{ layout: { type: 'spring', damping: 30, stiffness: 500 } }"
+ >
+ <AnimatePresence mode="popLayout" :custom="slideDirection">
+ <motion.div
+ v-if="mobileCategory === 0"
+ key="tools"
+ data-test-id="mobile-toolbar-tools"
+ class="flex gap-0.5"
+ :variants="slideVariants"
+ initial="initial"
+ animate="animate"
+ exit="exit"
+ :transition="{ duration: 0.15 }"
+ >
+ <template v-for="tool in tools" :key="tool.key">
+ <div v-if="tool.flyout && tool.flyout.length > 1" class="flex items-center">
+ <button
+ :data-test-id="`mobile-toolbar-tool-${activeKeyForTool(tool, activeTool).toLowerCase()}`"
+ class="flex size-8 cursor-pointer items-center justify-center rounded-[6px] border-none transition-colors select-none"
+ :class="
+ isActive(tool, activeTool)
+ ? 'bg-accent text-white'
+ : 'bg-transparent text-muted active:bg-hover'
+ "
+ @click="setTool(activeKeyForTool(tool, activeTool))"
+ >
+ <component :is="toolIcons[activeKeyForTool(tool, activeTool)]" class="size-4" />
+ </button>
+
+ <DropdownMenuRoot>
+ <DropdownMenuTrigger as-child>
+ <button
+ :data-test-id="`mobile-toolbar-flyout-${tool.key.toLowerCase()}`"
+ class="flex h-8 w-3 cursor-pointer items-center justify-center rounded-[6px] border-none transition-colors select-none"
:class="
- menuItem({
- class: store.state.activeTool === sub ? 'bg-accent text-white' : undefined
- })
+ isActive(tool, activeTool)
+ ? 'bg-accent text-white'
+ : 'bg-transparent text-muted active:bg-hover'
"
- @select="store.setTool(sub)"
>
- <component :is="toolIcons[sub]" class="size-3.5" />
- <span class="flex-1">{{ toolLabels[sub] }}</span>
- </DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenuPortal>
- </DropdownMenuRoot>
- </div>
+ <IconChevronDown class="size-2.5" />
+ </button>
+ </DropdownMenuTrigger>
+
+ <DropdownMenuPortal>
+ <DropdownMenuContent
+ side="top"
+ :side-offset="8"
+ align="start"
+ :class="menuContent({ class: 'min-w-32' })"
+ >
+ <ToolbarItem
+ v-for="sub in tool.flyout"
+ :key="sub"
+ v-slot="{ active: subActive, select: selectSub }"
+ :tool="sub"
+ >
+ <DropdownMenuItem
+ :data-test-id="`mobile-toolbar-flyout-item-${sub.toLowerCase()}`"
+ :class="
+ menuItem({
+ class: subActive ? 'bg-accent text-white' : undefined
+ })
+ "
+ @select="selectSub"
+ >
+ <component :is="toolIcons[sub]" class="size-3.5" />
+ <span class="flex-1">{{ toolLabels[sub] }}</span>
+ </DropdownMenuItem>
+ </ToolbarItem>
+ </DropdownMenuContent>
+ </DropdownMenuPortal>
+ </DropdownMenuRoot>
+ </div>
+ <ToolbarItem v-else v-slot="{ active, select: selectTool }" :tool="tool.key">
+ <button
+ :data-test-id="`mobile-toolbar-tool-${tool.key.toLowerCase()}`"
+ class="flex size-8 cursor-pointer items-center justify-center rounded-[6px] border-none transition-colors select-none"
+ :class="
+ active ? 'bg-accent text-white' : 'bg-transparent text-muted active:bg-hover'
+ "
+ @click="selectTool"
+ >
+ <component :is="toolIcons[tool.key]" class="size-4" />
+ </button>
+ </ToolbarItem>
+ </template>
+ </motion.div>
+
+ <motion.div
+ v-else-if="mobileCategory === 1"
+ key="edit"
+ data-test-id="mobile-toolbar-edit"
+ class="flex gap-0.5"
+ :variants="slideVariants"
+ initial="initial"
+ animate="animate"
+ exit="exit"
+ :transition="{ duration: 0.15 }"
+ >
<button
- v-else
- :data-test-id="`mobile-toolbar-tool-${tool.key.toLowerCase()}`"
- class="flex size-8 cursor-pointer items-center justify-center rounded-[6px] border-none transition-colors select-none"
- :class="
- isActive(tool)
- ? 'bg-accent text-white'
- : 'bg-transparent text-muted active:bg-hover'
- "
- @click="store.setTool(tool.key)"
+ v-for="item in editActions"
+ :key="item.label"
+ :data-test-id="`mobile-toolbar-${item.label.toLowerCase()}`"
+ class="flex size-8 cursor-pointer items-center justify-center rounded-[6px] border-none bg-transparent text-muted transition-colors select-none active:bg-hover active:text-surface"
+ @click="onActionTap(item)"
>
- <component :is="toolIcons[tool.key]" class="size-4" />
+ <component :is="item.icon" class="size-4" />
</button>
- </template>
- </motion.div>
+ </motion.div>
- <motion.div
- v-else-if="mobileCategory === 1"
- key="edit"
- data-test-id="mobile-toolbar-edit"
- class="flex gap-0.5"
- :variants="slideVariants"
- initial="initial"
- animate="animate"
- exit="exit"
- :transition="{ duration: 0.15 }"
- >
- <button
- v-for="item in editActions"
- :key="item.label"
- :data-test-id="`mobile-toolbar-${item.label.toLowerCase()}`"
- class="flex size-8 cursor-pointer items-center justify-center rounded-[6px] border-none bg-transparent text-muted transition-colors select-none active:bg-hover active:text-surface"
- @click="onActionTap(item)"
+ <motion.div
+ v-else
+ key="arrange"
+ data-test-id="mobile-toolbar-arrange"
+ class="flex gap-0.5"
+ :variants="slideVariants"
+ initial="initial"
+ animate="animate"
+ exit="exit"
+ :transition="{ duration: 0.15 }"
>
- <component :is="item.icon" class="size-4" />
- </button>
- </motion.div>
-
- <motion.div
- v-else
- key="arrange"
- data-test-id="mobile-toolbar-arrange"
- class="flex gap-0.5"
- :variants="slideVariants"
- initial="initial"
- animate="animate"
- exit="exit"
- :transition="{ duration: 0.15 }"
- >
- <button
- v-for="item in arrangeActions"
- :key="item.label"
- :data-test-id="`mobile-toolbar-${item.label.toLowerCase()}`"
- class="flex size-8 cursor-pointer items-center justify-center rounded-[6px] border-none bg-transparent text-muted transition-colors select-none active:bg-hover active:text-surface"
- @click="onActionTap(item)"
- >
- <component :is="item.icon" class="size-4" />
- </button>
- </motion.div>
- </AnimatePresence>
- </motion.div>
+ <button
+ v-for="item in arrangeActions"
+ :key="item.label"
+ :data-test-id="`mobile-toolbar-${item.label.toLowerCase()}`"
+ class="flex size-8 cursor-pointer items-center justify-center rounded-[6px] border-none bg-transparent text-muted transition-colors select-none active:bg-hover active:text-surface"
+ @click="onActionTap(item)"
+ >
+ <component :is="item.icon" class="size-4" />
+ </button>
+ </motion.div>
+ </AnimatePresence>
+ </motion.div>
- <motion.button
- data-test-id="mobile-toolbar-next"
- class="flex size-7 shrink-0 cursor-pointer items-center justify-center rounded-full border border-border bg-panel shadow-sm select-none"
- :class="hasNext ? 'text-muted' : 'pointer-events-none'"
- :animate="{ opacity: hasNext ? 1 : 0 }"
- :transition="{ duration: 0.15 }"
- @click="goNext"
- >
- <IconChevronRight class="size-3.5" />
- </motion.button>
- </div>
+ <motion.button
+ data-test-id="mobile-toolbar-next"
+ class="flex size-7 shrink-0 cursor-pointer items-center justify-center rounded-full border border-border bg-panel shadow-sm select-none"
+ :class="hasNext ? 'text-muted' : 'pointer-events-none'"
+ :animate="{ opacity: hasNext ? 1 : 0 }"
+ :transition="{ duration: 0.15 }"
+ @click="goNext"
+ >
+ <IconChevronRight class="size-3.5" />
+ </motion.button>
+ </div>
+ </ToolbarRoot>
</template>
diff --git a/src/components/VariablesDialog.vue b/src/components/VariablesDialog.vue
index e490c0a66..5bbb9ae77 100644
--- a/src/components/VariablesDialog.vue
+++ b/src/components/VariablesDialog.vue
@@ -1,5 +1,5 @@
<script setup lang="ts">
-import { ref, computed, watch, nextTick, h, type Component } from 'vue'
+import { ref, computed, nextTick, h, type Component } from 'vue'
import {
DialogRoot,
DialogPortal,
@@ -25,20 +25,27 @@ import IconToggleLeft from '~icons/lucide/toggle-left'
import IconX from '~icons/lucide/x'
import Tip from './Tip.vue'
import ColorInput from './ColorInput.vue'
-import { colorToHexRaw, parseColor, randomHex } from '@open-pencil/core'
-import { useEditor, useSceneComputed } from '@open-pencil/vue'
-import type { Variable, VariableCollection, VariableValue, Color } from '@open-pencil/core'
+import { useVariables } from '@open-pencil/vue'
+import type { Variable, Color } from '@open-pencil/core'
const open = defineModel<boolean>('open', { default: false })
-const store = useEditor()
-const searchTerm = ref('')
-const collections = useSceneComputed(() => store.getCollections())
-
-const activeTab = ref(collections.value[0]?.id ?? '')
-watch(collections, (cols) => {
- if (!activeTab.value && cols[0]) activeTab.value = cols[0].id
-})
+const {
+ collections,
+ variables,
+ activeCollectionId,
+ activeModes,
+ searchTerm,
+ addCollection,
+ renameCollection,
+ addVariable,
+ removeVariable,
+ renameVariable,
+ updateVariableValue,
+ formatModeValue,
+ parseVariableValue,
+ shortName
+} = useVariables()
const editingCollectionId = ref<string | null>(null)
const collectionInputRefs = new Map<string, HTMLInputElement>()
@@ -65,105 +72,24 @@ function startRenameCollection(id: string) {
function commitRenameCollection(id: string, input: HTMLInputElement) {
if (editingCollectionId.value !== id) return
const value = input.value.trim()
- const collection = store.getCollection(id)
- if (collection && value && value !== collection.name) {
- store.renameCollection(id, value)
+ const col = collections.value.find((c) => c.id === id)
+ if (col && value && value !== col.name) {
+ renameCollection(id, value)
}
editingCollectionId.value = null
}
-const activeModes = computed(() => {
- const col = store.getCollection(activeTab.value)
- return col?.modes ?? []
-})
-
-const variables = useSceneComputed(() => {
- if (!activeTab.value) return [] as Variable[]
- const all = store.getVariablesForCollection(activeTab.value)
- if (!searchTerm.value) return all
- const q = searchTerm.value.toLowerCase()
- return all.filter((v) => v.name.toLowerCase().includes(q))
-})
-
-function formatModeValue(variable: Variable, modeId: string): string {
- const value = variable.valuesByMode[modeId]
- if (value === undefined) return '—'
- if (typeof value === 'object' && 'r' in value) return colorToHexRaw(value as Color)
- if (typeof value === 'object' && 'aliasId' in value) {
- const aliased = store.getVariable(value.aliasId)
- return aliased ? `→ ${aliased.name}` : '→ ?'
- }
- return String(value)
-}
-
-function shortName(variable: Variable): string {
- const parts = variable.name.split('/')
- return parts[parts.length - 1] ?? variable.name
-}
-
function commitNameEdit(variable: Variable, newName: string) {
if (newName && newName !== variable.name) {
- store.renameVariable(variable.id, newName)
+ renameVariable(variable.id, newName)
}
}
-function updateColorValue(variable: Variable, modeId: string, color: Color) {
- store.updateVariableValue(variable.id, modeId, color)
-}
-
function commitValueEdit(variable: Variable, modeId: string, newValue: string) {
- let parsed: VariableValue
- if (variable.type === 'COLOR') {
- parsed = parseColor(newValue.startsWith('#') ? newValue : `#${newValue}`)
- } else if (variable.type === 'FLOAT') {
- const num = parseFloat(newValue)
- if (isNaN(num)) return
- parsed = num
- } else if (variable.type === 'BOOLEAN') {
- parsed = newValue === 'true'
- } else {
- parsed = newValue
- }
- store.updateVariableValue(variable.id, modeId, parsed)
-}
-
-function addVariable() {
- const col = store.getCollection(activeTab.value)
- if (!col) return
-
- const id = `var:${randomHex(8)}`
- const valuesByMode: Record<string, VariableValue> = {}
- for (const mode of col.modes) {
- valuesByMode[mode.modeId] = { r: 0, g: 0, b: 0, a: 1 }
- }
-
- const variable: Variable = {
- id,
- name: 'New variable',
- type: 'COLOR',
- collectionId: col.id,
- valuesByMode,
- description: '',
- hiddenFromPublishing: false
- }
- store.addVariable(variable)
-}
-
-function addCollection() {
- const id = `col:${randomHex(8)}`
- const collection: VariableCollection = {
- id,
- name: 'New collection',
- modes: [{ modeId: 'default', name: 'Mode 1' }],
- defaultModeId: 'default',
- variableIds: []
+ const parsed = parseVariableValue(variable, newValue)
+ if (parsed !== undefined) {
+ updateVariableValue(variable.id, modeId, parsed)
}
- store.addCollection(collection)
- activeTab.value = id
-}
-
-function removeVariable(id: string) {
- store.removeVariable(id)
}
const VARIABLE_TYPE_ICONS: Record<string, Component> = {
@@ -223,7 +149,7 @@ const columns = computed<ColumnDef<Variable>[]>(() => {
if (v.type === 'COLOR' && value && typeof value === 'object' && 'r' in value) {
return h(ColorInput, {
color: value as Color,
- onUpdate: (c: Color) => updateColorValue(v, mode.modeId, c)
+ onUpdate: (c: Color) => updateVariableValue(v.id, mode.modeId, c)
})
}
@@ -319,7 +245,7 @@ const table = useVueTable({
</div>
<template v-else>
- <TabsRoot v-model="activeTab" class="flex flex-1 flex-col overflow-hidden">
+ <TabsRoot v-model="activeCollectionId" class="flex flex-1 flex-col overflow-hidden">
<!-- Top bar -->
<div class="flex shrink-0 items-center border-b border-border">
<TabsList class="flex flex-1 gap-0.5 overflow-x-auto px-3 py-1">