Skip to content

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

4.7 KB
Raw Blame
<script setup lang="ts">
import { computed } from 'vue'

import { resolveEditorProfile } from '@open-pencil/core'
import { useEditorCommands } from '@open-pencil/vue/editor/commands'
import { useViewportKind } from '@open-pencil/vue/editor/viewport-kind'
import { useI18n } from '@open-pencil/vue/i18n'
import { ToolbarRoot, useToolbarState } from '@open-pencil/vue/primitives/Toolbar'

import { useEditorStore } from '@/app/editor/active-store'
import { TOOLBAR_ACTIONS, toolbarItems, toolbarLayoutFor } from '@/app/editor/toolbar/layout'
import { toolShortcutLabels } from '@/app/editor/toolbar/shortcuts'
import { appPreferences } from '@/app/settings/preferences/store'
import { useActionToast } from '@/app/shell/toast/action'
import InsertIconButton from '@/components/icon-picker/InsertIconButton.vue'
import { toolIcons } from '@/components/icons'
import { useToolbarActions } from '@/components/toolbar/actions'
import DesktopToolbar from '@/components/toolbar/DesktopToolbar.vue'
import { useToolLabels } from '@/components/toolbar/labels'
import MobileToolbar from '@/components/toolbar/MobileToolbar.vue'
import ToolbarContextMenu from '@/components/toolbar/ToolbarContextMenu.vue'
import type { ToolbarActionItem } from '@/components/toolbar/types'
import { useMenuUI } from '@/components/ui/menu/menu'

const store = useEditorStore()
const { isMobile } = useViewportKind()
const { getCommand } = useEditorCommands()
const { showActionToast } = useActionToast()
const { menu } = useI18n()

const toolLabels = useToolLabels()

const profile = computed(() => resolveEditorProfile(store.state))
const toolShortcuts = computed(() => toolShortcutLabels(profile.value.toolKeys))
const items = computed(() => {
  const layout = appPreferences.value.toolbar
  return toolbarItems(
    toolbarLayoutFor(layout, profile.value.tools),
    toolLabels.value,
    toolShortcuts.value
  )
})
const tools = computed(() =>
  items.value.flatMap((item) => (item.kind === 'tool' ? [item.tool] : []))
)
const actionItems = computed(() =>
  items.value.flatMap((item) => (item.kind === 'action' ? [item.action] : []))
)
/**
 * Commands hidden from the toolbar keep their pickers, which the menu and the palette still open;
 * a read-only document leaves them out, as it does the buttons.
 */
const hiddenActions = computed(() =>
  !profile.value.edits
    ? []
    : TOOLBAR_ACTIONS.filter((action) => !actionItems.value.includes(action))
)

const flyoutMenuCls = useMenuUI({ content: 'min-w-32' })
const toolbarUI = { flyoutContent: flyoutMenuCls.content }
const { editActions, arrangeActions } = useToolbarActions({ store, getCommand, menu })

const { mobileCategory, slideDirection, hasPrev, hasNext, goPrev, goNext } = useToolbarState()

function onActionTap(item: ToolbarActionItem) {
  item.action()
  showActionToast(item.label)
}
</script>

<template>
  <ToolbarRoot v-slot="{ activeTool, flyoutSelections, actions }" :tools="tools">
    <!-- A profile that fixes the tools, as a board's does, has nothing to customize, as in FigJam. -->
    <ToolbarContextMenu :disabled="profile.tools !== null">
      <DesktopToolbar
        v-if="!isMobile"
        :items="items"
        :active-tool="activeTool"
        :flyout-selections="flyoutSelections"
        :tool-icons="toolIcons"
        :tool-labels="toolLabels"
        :tool-shortcuts="toolShortcuts"
        :ui="toolbarUI"
        @set-tool="actions.setTool"
      >
        <template #action="{ action }">
          <InsertIconButton v-if="action === 'insert-icon'" :ui="toolbarUI" />
        </template>
        <template #hidden>
          <template v-for="action in hiddenActions" :key="action">
            <InsertIconButton v-if="action === 'insert-icon'" anchored />
          </template>
        </template>
      </DesktopToolbar>

      <MobileToolbar
        v-else
        :tools="tools"
        :active-tool="activeTool"
        :flyout-selections="flyoutSelections"
        :tool-icons="toolIcons"
        :tool-labels="toolLabels"
        :tool-shortcuts="toolShortcuts"
        :ui="toolbarUI"
        :mobile-category="mobileCategory"
        :slide-direction="slideDirection"
        :has-prev="hasPrev"
        :has-next="hasNext"
        :edit-actions="editActions"
        :arrange-actions="arrangeActions"
        @set-tool="actions.setTool"
        @prev="goPrev"
        @next="goNext"
        @action="onActionTap"
      >
        <template #end>
          <template v-for="action in actionItems" :key="action">
            <InsertIconButton v-if="action === 'insert-icon'" mobile :ui="toolbarUI" />
          </template>
          <template v-for="action in hiddenActions" :key="action">
            <InsertIconButton v-if="action === 'insert-icon'" anchored />
          </template>
        </template>
      </MobileToolbar>
    </ToolbarContextMenu>
  </ToolbarRoot>
</template>