-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
getVariables.ts
112 lines (109 loc) · 3.52 KB
/
getVariables.ts
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
import config from '@config/config'
import { tokenTypes } from '@config/tokenTypes'
import { tokenCategoryType } from '@typings/tokenCategory'
import { tokenExportKeyType } from '@typings/tokenExportKey'
import { PropertyType } from '@typings/valueTypes'
import { roundRgba } from './convertColor'
import roundWithDecimals from './roundWithDecimals'
import handleVariableAlias from './handleVariableAlias'
import processAliasModes from './processAliasModes'
import { Settings } from '@typings/settings'
const extractVariable = (variable, value, mode) => {
let category: tokenCategoryType = 'color'
let values = {}
if (value.type === 'VARIABLE_ALIAS') {
return handleVariableAlias(variable, value, mode)
}
switch (variable.resolvedType) {
case 'COLOR':
category = 'color'
values = {
fill: {
value: roundRgba(value),
type: 'color' as PropertyType,
blendMode: 'normal'
}
}
break
case 'FLOAT':
category = 'dimension'
values = roundWithDecimals(value, 2)
break
case 'STRING':
category = 'string'
values = value
break
case 'BOOLEAN':
category = 'boolean'
values = value
break
}
return {
name: variable.name,
description: variable.description || undefined,
exportKey: tokenTypes.variables.key as tokenExportKeyType,
category,
values
}
}
export const getVariables = (figma: PluginAPI, settings: Settings) => {
const excludedCollectionIds = figma.variables
.getLocalVariableCollections()
.filter(
(collection) =>
!['.', '_', ...settings.exclusionPrefix.split(',')].includes(
collection.name.charAt(0)
)
)
.map((collection) => collection.id)
// get collections
const collections = Object.fromEntries(
figma.variables
.getLocalVariableCollections()
.map((collection) => [collection.id, collection])
)
// get variables
const variables = figma.variables
.getLocalVariables()
.filter((variable) =>
excludedCollectionIds.includes(variable.variableCollectionId)
)
.map((variable) => {
// get collection name and modes
const { variableCollectionId } = variable
const { name: collection, modes } = collections[variableCollectionId]
// return each mode value as a separate variable
return Object.entries(variable.valuesByMode).map(([id, value]) => {
// Only add mode if there's more than one
const addMode = settings.modeReference && modes.length > 1
return {
...extractVariable(
variable,
value,
modes.find(({ modeId }) => modeId === id)
),
// name is contstructed from collection, mode and variable name
name: addMode
? `${collection}/${
modes.find(({ modeId }) => modeId === id).name
}/${variable.name}`
: `${collection}/${variable.name}`,
// add mnetadata to extensions
extensions: {
[config.key.extensionPluginData]: {
mode: settings.modeReference
? modes.find(({ modeId }) => modeId === id).name
: undefined,
collection: collection,
scopes: variable.scopes,
[config.key.extensionVariableStyleId]: variable.id,
exportKey: tokenTypes.variables.key as tokenExportKeyType
}
}
}
})
})
return settings.modeReference
? processAliasModes(variables.flat())
: variables.flat()
}