Skip to content

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

1.4 KB
Raw Blame
<script setup lang="ts">
import { computed, reactive, ref, watch } from 'vue'

import { EDITOR_TOOLS, type EditorToolDef, type Tool } from '@open-pencil/core'

import { useEditor } from '#vue/editor/context'
import { provideToolbar } from '#vue/primitives/Toolbar/context'

const { tools = EDITOR_TOOLS } = defineProps<{
  tools?: EditorToolDef[]
}>()

const editor = useEditor()
const activeTool = computed(() => editor.state.activeTool)
const expandedFlyout = ref<Tool | null>(null)
const flyoutSelections = reactive(new Map<Tool, Tool>())

watch(
  activeTool,
  (currentTool) => {
    for (const tool of tools) {
      if (tool.flyout?.includes(currentTool)) {
        flyoutSelections.set(tool.key, currentTool)
      }
    }
  },
  { immediate: true }
)

function setTool(tool: Tool) {
  editor.setTool(tool)
  expandedFlyout.value = null
}

function toggleFlyout(tool: Tool) {
  expandedFlyout.value = expandedFlyout.value === tool ? null : tool
}

function closeFlyout() {
  expandedFlyout.value = null
}

const actions = {
  setTool,
  toggleFlyout,
  closeFlyout
}

provideToolbar({
  editor,
  tools,
  activeTool,
  flyoutSelections,
  expandedFlyout,
  setTool,
  toggleFlyout,
  closeFlyout
})
</script>

<template>
  <slot
    :tools="tools"
    :active-tool="activeTool"
    :flyout-selections="flyoutSelections"
    :expanded-flyout="expandedFlyout"
    :actions="actions"
  />
</template>