vueGenerator.ts 4.36 KB
import type { FigmaNodeDSL, FigmaFill } from './types'

/**
 * 将 Figma RGBA 颜色值转换为 CSS rgba() 字符串
 */
function toRgba(fills: FigmaFill[] | undefined): string | null {
  if (!fills?.length) return null
  const solid = fills.find(f => f.type === 'SOLID' && f.visible !== false)
  if (!solid?.color) return null
  const { r, g, b, a = 1 } = solid.color
  return `rgba(${Math.round(r * 255)},${Math.round(g * 255)},${Math.round(b * 255)},${a.toFixed(2)})`
}

/**
 * 将 DSL 节点属性映射为 Tailwind CSS 类名和内联样式
 */
function buildStyles(node: FigmaNodeDSL): { cls: string; style: string } {
  const cls: string[] = []
  const styleMap: string[] = []

  // Auto Layout → Flexbox
  if (node.layoutMode === 'HORIZONTAL') {
    cls.push('flex', 'flex-row')
    const ca = node.counterAxisAlignItems
    const pa = node.primaryAxisAlignItems
    if (ca === 'CENTER') cls.push('items-center')
    else if (ca === 'MAX') cls.push('items-end')
    if (pa === 'CENTER') cls.push('justify-center')
    else if (pa === 'MAX') cls.push('justify-end')
    else if (pa === 'SPACE_BETWEEN') cls.push('justify-between')
  } else if (node.layoutMode === 'VERTICAL') {
    cls.push('flex', 'flex-col')
    const ca = node.counterAxisAlignItems
    const pa = node.primaryAxisAlignItems
    if (ca === 'CENTER') cls.push('items-center')
    else if (ca === 'MAX') cls.push('items-end')
    if (pa === 'CENTER') cls.push('justify-center')
    else if (pa === 'MAX') cls.push('justify-end')
    else if (pa === 'SPACE_BETWEEN') cls.push('justify-between')
  }

  // 尺寸
  if (node.width) cls.push(`w-[${node.width}px]`)
  if (node.height) cls.push(`h-[${node.height}px]`)

  // 内边距
  if (node.padding) {
    const { top, right, bottom, left } = node.padding
    if (top) cls.push(`pt-[${top}px]`)
    if (right) cls.push(`pr-[${right}px]`)
    if (bottom) cls.push(`pb-[${bottom}px]`)
    if (left) cls.push(`pl-[${left}px]`)
  }

  // 间距
  if (node.itemSpacing) cls.push(`gap-[${node.itemSpacing}px]`)

  // 圆角
  if (node.cornerRadius) {
    if (typeof node.cornerRadius === 'number' && node.cornerRadius > 0) {
      cls.push(`rounded-[${node.cornerRadius}px]`)
    } else if (Array.isArray(node.cornerRadius)) {
      const [tl, tr, br, bl] = node.cornerRadius
      styleMap.push(`border-radius: ${tl}px ${tr}px ${br}px ${bl}px`)
    }
  }

  // 文本样式
  if (node.textStyle) {
    const { fontSize, fontWeight, textAlignHorizontal } = node.textStyle
    if (fontSize) cls.push(`text-[${fontSize}px]`)
    if (fontWeight) {
      if (fontWeight >= 700) cls.push('font-bold')
      else if (fontWeight >= 600) cls.push('font-semibold')
      else if (fontWeight >= 500) cls.push('font-medium')
      else cls.push('font-normal')
    }
    if (textAlignHorizontal === 'CENTER') cls.push('text-center')
    else if (textAlignHorizontal === 'RIGHT') cls.push('text-right')
    else if (textAlignHorizontal === 'LEFT') cls.push('text-left')

    const textColor = toRgba(node.fills)
    if (textColor) styleMap.push(`color: ${textColor}`)
  }

  // 背景色(非文本节点)
  if (node.type !== 'TEXT') {
    const bgColor = toRgba(node.fills)
    if (bgColor) styleMap.push(`background-color: ${bgColor}`)
  }

  return {
    cls: cls.join(' '),
    style: styleMap.join('; '),
  }
}

/**
 * 递归将 DSL 节点渲染为 HTML 字符串
 */
function renderNode(node: FigmaNodeDSL, indent = 4): string {
  const { cls, style } = buildStyles(node)
  const pad = ' '.repeat(indent)
  const clsAttr = cls ? ` class="${cls}"` : ''
  const styleAttr = style ? ` style="${style}"` : ''

  if (node.type === 'TEXT') {
    return `${pad}<span${clsAttr}${styleAttr}>${node.text ?? ''}</span>`
  }

  if (!node.children?.length) {
    return `${pad}<div${clsAttr}${styleAttr}></div>`
  }

  const children = node.children.map(c => renderNode(c, indent + 2)).join('\n')
  return `${pad}<div${clsAttr}${styleAttr}>\n${children}\n${pad}</div>`
}

export interface VueSFCOptions {
  componentName?: string
}

/**
 * 将 DSL 根节点转换为完整的 Vue 3 SFC 文件内容
 */
export function dslToVueSFC(root: FigmaNodeDSL, options: VueSFCOptions = {}): string {
  const { componentName = 'GeneratedComponent' } = options
  const template = renderNode(root, 4)

  return `<template>
  <div>
${template}
  </div>
</template>

<script setup lang="ts">
// ${componentName}
// Generated by Design-to-Code Platform (Rule Engine)
</script>
`
}