forked from jxnblk/figma-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (152 loc) · 3.86 KB
/
index.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// transform figma document into theme object
const { get } = require('dot-prop')
const chroma = require('chroma-js')
const { struct } = require('superstruct')
const FILL = 'FILL'
const TEXT = 'TEXT'
const flatten = (arr = []) => arr.reduce((a, b) => {
return [
...a,
b,
...flatten(b.children)
]
}, [])
// schema
const Theme = struct({
colors: 'object?',
textStyles: struct.dict([
'string?',
struct({
fontFamily: 'string',
fontSize: 'number',
fontWeight: 'number',
lineHeight: 'number',
})
]),
fonts: 'array?',
fontSizes: 'array?',
fontWeights: 'array?',
lineHeights: 'array?',
metadata: 'object?'
})
const Data = struct.partial({
name: 'string',
lastModified: 'string',
thumbnailUrl: 'string',
document: struct.partial({
children: struct.list([ 'object' ]),
}),
styles: struct.dict([
'string',
struct.partial({
name: 'string',
styleType: 'string',
})
])
})
const unique = arr => [...new Set(arr)]
module.exports = (data, opts = {}) => {
Data(data)
const {
styles = {},
document: {
children: tree = []
}
} = data
const children = flatten(tree)
const styleKeys = Object.keys(styles)
const stylesArray = styleKeys.map(key => Object.assign({
id: key
}, styles[key]))
// colors
const colorStyles = stylesArray.filter(style => style.styleType === FILL)
const colorArray = colorStyles.map(style => {
const child = children
.find(child => get(child, 'styles.fill') === style.id)
if (!child) return
const [ fill = {} ] = child.fills || []
const { r, g, b, a } = fill.color
const alpha = (opts.opacityAsAlpha && fill.opacity !== undefined) ? fill.opacity : a
const rgba = [ r, g, b ].map(n => n * 255).concat(alpha)
const color = chroma.rgb(rgba)
return {
id: style.id,
name: style.name,
value: (opts.rgb || (opts.rgba && alpha < 1)) ? color.css() : color.hex()
}
})
.filter(Boolean)
const colors = colorArray.reduce((a, color) => Object.assign({}, a, {
[color.name]: color.value
}), {})
// textStyles
const textStyles = stylesArray.filter(style => style.styleType === TEXT)
const textArray = textStyles.map(style => {
const child = children
.find(child => get(child, 'styles.text') === style.id)
if (!child) return
return {
id: style.id,
name: style.name,
value: child.style
}
})
.filter(Boolean)
.map(style => {
const {
fontFamily,
fontWeight,
fontSize,
letterSpacing
} = style.value
const lineHeight = style.value.lineHeightPercent / 100
return Object.assign({}, style, {
value: {
fontFamily,
fontWeight,
fontSize,
lineHeight
}
})
})
const textStylesObject = textArray.reduce((a, style) => Object.assign({}, a, {
[style.name]: style.value
}), {})
let fontSizes = unique(textArray.map(style => style.value.fontSize))
let fontWeights = unique(textArray.map(style => style.value.fontWeight))
let fonts = unique(textArray.map(style => style.value.fontFamily))
let lineHeights = unique(textArray.map(style => style.value.lineHeight))
if (opts.sort) {
fontSizes = fontSizes.sort();
fontWeights = fontWeights.sort();
fonts = fonts.sort();
lineHeights = lineHeights.sort();
}
let theme = {
colors,
textStyles: textStylesObject,
fonts,
fontSizes,
fontWeights,
lineHeights
}
if (opts.filter.length > 0) {
theme = opts.filter.reduce((obj, key) => ({ ...obj, [key]: theme[key] }), {})
}
if (opts.metadata) {
theme.metadata = {
name: data.name,
lastModified: data.lastModified,
thumbnailUrl: data.thumbnailUrl,
children,
styles: stylesArray
}
}
// validate
Theme(theme)
return theme
}
module.exports.schemas = {
Data,
Theme
}