|
1 | 1 | import { uploadFile } from '../../utils/upload-file' |
2 | | -import type { Devup, DevupTypography } from './types' |
| 2 | +import { applyTypography } from './apply-typography' |
| 3 | +import { buildTargetStyleNames } from './build-target-style-names' |
| 4 | +import type { Devup } from './types' |
3 | 5 | import { getDevupColorCollection } from './utils/get-devup-color-collection' |
4 | 6 | import { uploadDevupXlsx } from './utils/upload-devup-xlsx' |
5 | 7 |
|
6 | | -type TargetTypography = [target: string, typography: DevupTypography] |
7 | | -const TYPO_PREFIX = ['mobile', '1', 'tablet', '3', 'desktop', '5'] as const |
8 | | - |
9 | 8 | export async function importDevup(input: 'json' | 'excel') { |
10 | 9 | const devup = await loadDevup(input) |
11 | 10 | await importColors(devup) |
@@ -71,77 +70,3 @@ async function importTypography(devup: Devup) { |
71 | 70 | } |
72 | 71 | } |
73 | 72 | } |
74 | | - |
75 | | -function buildTargetStyleNames( |
76 | | - style: string, |
77 | | - value: DevupTypography | (DevupTypography | null)[], |
78 | | -): TargetTypography[] { |
79 | | - const targets: TargetTypography[] = [] |
80 | | - if (Array.isArray(value)) { |
81 | | - value.forEach((typo, idx) => { |
82 | | - if (!typo) return |
83 | | - const prefix = TYPO_PREFIX[idx] ?? `${idx}` |
84 | | - targets.push([`${prefix}/${style}`, typo]) |
85 | | - }) |
86 | | - return targets |
87 | | - } |
88 | | - targets.push([`mobile/${style}`, value]) |
89 | | - return targets |
90 | | -} |
91 | | - |
92 | | -async function applyTypography( |
93 | | - target: string, |
94 | | - typography: DevupTypography, |
95 | | - styles: TextStyle[], |
96 | | -) { |
97 | | - const st = styles.find((s) => s.name === target) ?? figma.createTextStyle() |
98 | | - st.name = target |
99 | | - const fontFamily = { |
100 | | - family: typography.fontFamily ?? 'Inter', |
101 | | - style: typography.fontStyle === 'italic' ? 'Italic' : 'Regular', |
102 | | - } |
103 | | - |
104 | | - try { |
105 | | - await figma.loadFontAsync(fontFamily) |
106 | | - st.fontName = fontFamily |
107 | | - if (typography.fontSize) st.fontSize = parseInt(typography.fontSize, 10) |
108 | | - if (typography.letterSpacing) { |
109 | | - st.letterSpacing = typography.letterSpacing.endsWith('em') |
110 | | - ? { |
111 | | - unit: 'PERCENT', |
112 | | - value: parseFloat(typography.letterSpacing), |
113 | | - } |
114 | | - : { |
115 | | - unit: 'PIXELS', |
116 | | - value: parseFloat(typography.letterSpacing) * 100, |
117 | | - } |
118 | | - } |
119 | | - if (typography.lineHeight) { |
120 | | - st.lineHeight = |
121 | | - typography.lineHeight === 'normal' |
122 | | - ? { unit: 'AUTO' } |
123 | | - : typeof typography.lineHeight === 'string' |
124 | | - ? { |
125 | | - unit: 'PIXELS', |
126 | | - value: parseInt(typography.lineHeight, 10), |
127 | | - } |
128 | | - : { |
129 | | - unit: 'PERCENT', |
130 | | - value: Math.round(typography.lineHeight / 10) / 10, |
131 | | - } |
132 | | - } |
133 | | - if (typography.textTransform) { |
134 | | - st.textCase = typography.textTransform.toUpperCase() as TextCase |
135 | | - } |
136 | | - if (typography.textDecoration) { |
137 | | - st.textDecoration = |
138 | | - typography.textDecoration.toUpperCase() as TextDecoration |
139 | | - } |
140 | | - } catch (error) { |
141 | | - console.error('Failed to create text style', error) |
142 | | - figma.notify( |
143 | | - `Failed to create text style (${target}, ${fontFamily.family} - ${fontFamily.style})`, |
144 | | - { error: true }, |
145 | | - ) |
146 | | - } |
147 | | -} |
0 commit comments