Skip to content

Commit 34cea17

Browse files
committed
Split import logic
1 parent 5d05d81 commit 34cea17

File tree

3 files changed

+82
-78
lines changed

3 files changed

+82
-78
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import type { DevupTypography } from './types'
2+
3+
export async function applyTypography(
4+
target: string,
5+
typography: DevupTypography,
6+
styles: TextStyle[],
7+
) {
8+
const st = styles.find((s) => s.name === target) ?? figma.createTextStyle()
9+
st.name = target
10+
const fontFamily = {
11+
family: typography.fontFamily ?? 'Inter',
12+
style: typography.fontStyle === 'italic' ? 'Italic' : 'Regular',
13+
}
14+
15+
try {
16+
await figma.loadFontAsync(fontFamily)
17+
st.fontName = fontFamily
18+
if (typography.fontSize) st.fontSize = parseInt(typography.fontSize, 10)
19+
if (typography.letterSpacing) {
20+
st.letterSpacing = typography.letterSpacing.endsWith('em')
21+
? {
22+
unit: 'PERCENT',
23+
value: parseFloat(typography.letterSpacing),
24+
}
25+
: {
26+
unit: 'PIXELS',
27+
value: parseFloat(typography.letterSpacing) * 100,
28+
}
29+
}
30+
if (typography.lineHeight) {
31+
st.lineHeight =
32+
typography.lineHeight === 'normal'
33+
? { unit: 'AUTO' }
34+
: typeof typography.lineHeight === 'string'
35+
? {
36+
unit: 'PIXELS',
37+
value: parseInt(typography.lineHeight, 10),
38+
}
39+
: {
40+
unit: 'PERCENT',
41+
value: Math.round(typography.lineHeight / 10) / 10,
42+
}
43+
}
44+
if (typography.textTransform) {
45+
st.textCase = typography.textTransform.toUpperCase() as TextCase
46+
}
47+
if (typography.textDecoration) {
48+
st.textDecoration =
49+
typography.textDecoration.toUpperCase() as TextDecoration
50+
}
51+
} catch (error) {
52+
console.error('Failed to create text style', error)
53+
figma.notify(
54+
`Failed to create text style (${target}, ${fontFamily.family} - ${fontFamily.style})`,
55+
{ error: true },
56+
)
57+
}
58+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { DevupTypography } from './types'
2+
3+
type TargetTypography = [target: string, typography: DevupTypography]
4+
const TYPO_PREFIX = ['mobile', '1', 'tablet', '3', 'desktop', '5'] as const
5+
6+
export function buildTargetStyleNames(
7+
style: string,
8+
value: DevupTypography | (DevupTypography | null)[],
9+
): TargetTypography[] {
10+
const targets: TargetTypography[] = []
11+
if (Array.isArray(value)) {
12+
value.forEach((typo, idx) => {
13+
if (!typo) return
14+
const prefix = TYPO_PREFIX[idx] ?? `${idx}`
15+
targets.push([`${prefix}/${style}`, typo])
16+
})
17+
return targets
18+
}
19+
targets.push([`mobile/${style}`, value])
20+
return targets
21+
}

src/commands/devup/import-devup.ts

Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
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'
35
import { getDevupColorCollection } from './utils/get-devup-color-collection'
46
import { uploadDevupXlsx } from './utils/upload-devup-xlsx'
57

6-
type TargetTypography = [target: string, typography: DevupTypography]
7-
const TYPO_PREFIX = ['mobile', '1', 'tablet', '3', 'desktop', '5'] as const
8-
98
export async function importDevup(input: 'json' | 'excel') {
109
const devup = await loadDevup(input)
1110
await importColors(devup)
@@ -71,77 +70,3 @@ async function importTypography(devup: Devup) {
7170
}
7271
}
7372
}
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

Comments
 (0)