Skip to content

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

785 B
Raw Blame
import { type ComputedRef, type InjectionKey, type Ref, inject, provide } from 'vue'

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

export interface ToolbarContext {
  editor: Editor
  tools: EditorToolDef[]
  activeTool: ComputedRef<Tool>
  flyoutSelections: ReadonlyMap<Tool, Tool>
  expandedFlyout: Ref<Tool | null>
  setTool: (tool: Tool) => void
  toggleFlyout: (tool: Tool) => void
  closeFlyout: () => void
}

export const TOOLBAR_KEY: InjectionKey<ToolbarContext> = Symbol('toolbar')

export function provideToolbar(ctx: ToolbarContext) {
  provide(TOOLBAR_KEY, ctx)
}

export function useToolbar(): ToolbarContext {
  const ctx = inject(TOOLBAR_KEY)
  if (!ctx) throw new Error('[open-pencil] useToolbar() called outside <ToolbarRoot>')
  return ctx
}