vueGenerator.ts
4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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>
`
}