@@ -4,29 +4,16 @@ import { tokenCategoryType } from '@typings/tokenCategory'
4
4
import { tokenExportKeyType } from '@typings/tokenExportKey'
5
5
import { PropertyType } from '@typings/valueTypes'
6
6
import { roundRgba } from './convertColor'
7
- import { changeNotation } from './changeNotation'
8
- import { getVariableTypeByValue } from './getVariableTypeByValue'
9
7
import roundWithDecimals from './roundWithDecimals'
8
+ import handleVariableAlias from './handleVariableAlias'
9
+ import processAliasModes from './processAliasModes'
10
10
import { Settings } from '@typings/settings'
11
11
12
- const extractVariable = ( variable , value ) => {
12
+ const extractVariable = ( variable , value , mode ) => {
13
13
let category : tokenCategoryType = 'color'
14
14
let values = { }
15
15
if ( value . type === 'VARIABLE_ALIAS' ) {
16
- const resolvedAlias = figma . variables . getVariableById ( value . id )
17
- const collection = figma . variables . getVariableCollectionById ( resolvedAlias . variableCollectionId )
18
- return {
19
- name : variable . name ,
20
- description : variable . description || undefined ,
21
- exportKey : tokenTypes . variables . key as tokenExportKeyType ,
22
- category : getVariableTypeByValue ( Object . values ( resolvedAlias . valuesByMode ) [ 0 ] ) ,
23
- values : `{${ collection . name . toLowerCase ( ) } .${ changeNotation ( resolvedAlias . name , '/' , '.' ) } }` ,
24
-
25
- // this is being stored so we can properly update the design tokens later to account for all
26
- // modes when using aliases
27
- aliasCollectionName : collection . name . toLowerCase ( ) ,
28
- aliasModes : collection . modes
29
- }
16
+ return handleVariableAlias ( variable , value , mode )
30
17
}
31
18
switch ( variable . resolvedType ) {
32
19
case 'COLOR' :
@@ -61,62 +48,65 @@ const extractVariable = (variable, value) => {
61
48
}
62
49
}
63
50
64
- const processAliasModes = ( variables ) => {
65
- return variables . reduce ( ( collector , variable ) => {
66
- // nothing needs to be done to variables that have no alias modes, or only one mode
67
- if ( ! variable . aliasModes || variable . aliasModes . length < 2 ) {
68
- collector . push ( variable )
69
-
70
- return collector
71
- }
72
-
73
- const { aliasModes, aliasCollectionName } = variable
74
-
75
- // this was only added for this function to process that data so before we return the variables, we can remove it
76
- delete variable . aliasModes
77
- delete variable . aliasCollectionName
78
-
79
- for ( const aliasMode of aliasModes ) {
80
- const modeBasedVariable = { ...variable }
81
- modeBasedVariable . values = modeBasedVariable . values . replace ( new RegExp ( `({${ aliasCollectionName } .)` , "i" ) , `{${ aliasCollectionName } .${ aliasMode . name } .` )
82
-
83
- collector . push ( modeBasedVariable )
84
- }
85
-
86
- return collector
87
- } , [ ] )
88
- }
89
-
90
51
export const getVariables = ( figma : PluginAPI , settings : Settings ) => {
91
- const excludedCollectionIds = figma . variables . getLocalVariableCollections ( ) . filter ( collection => ! [ '.' , '_' , ...settings . exclusionPrefix . split ( ',' ) ] . includes ( collection . name . charAt ( 0 ) ) ) . map ( collection => collection . id ) ;
52
+ const excludedCollectionIds = figma . variables
53
+ . getLocalVariableCollections ( )
54
+ . filter (
55
+ ( collection ) =>
56
+ ! [ '.' , '_' , ...settings . exclusionPrefix . split ( ',' ) ] . includes (
57
+ collection . name . charAt ( 0 )
58
+ )
59
+ )
60
+ . map ( ( collection ) => collection . id )
92
61
// get collections
93
- const collections = Object . fromEntries ( figma . variables . getLocalVariableCollections ( ) . map ( ( collection ) => [ collection . id , collection ] ) )
62
+ const collections = Object . fromEntries (
63
+ figma . variables
64
+ . getLocalVariableCollections ( )
65
+ . map ( ( collection ) => [ collection . id , collection ] )
66
+ )
94
67
// get variables
95
- const variables = figma . variables . getLocalVariables ( ) . filter ( variable => excludedCollectionIds . includes ( variable . variableCollectionId ) ) . map ( ( variable ) => {
96
- // get collection name and modes
97
- const { variableCollectionId } = variable
98
- const { name : collection , modes } = collections [ variableCollectionId ]
99
- // return each mode value as a separate variable
100
- return Object . entries ( variable . valuesByMode ) . map ( ( [ id , value ] ) => {
101
- // Only add mode if there's more than one
102
- let addMode = settings . modeReference && modes . length > 1
103
- return {
104
- ...extractVariable ( variable , value ) ,
105
- // name is contstructed from collection, mode and variable name
68
+ const variables = figma . variables
69
+ . getLocalVariables ( )
70
+ . filter ( ( variable ) =>
71
+ excludedCollectionIds . includes ( variable . variableCollectionId )
72
+ )
73
+ . map ( ( variable ) => {
74
+ // get collection name and modes
75
+ const { variableCollectionId } = variable
76
+ const { name : collection , modes } = collections [ variableCollectionId ]
77
+ // return each mode value as a separate variable
78
+ return Object . entries ( variable . valuesByMode ) . map ( ( [ id , value ] ) => {
79
+ // Only add mode if there's more than one
80
+ const addMode = settings . modeReference && modes . length > 1
81
+ return {
82
+ ...extractVariable (
83
+ variable ,
84
+ value ,
85
+ modes . find ( ( { modeId } ) => modeId === id )
86
+ ) ,
87
+ // name is contstructed from collection, mode and variable name
106
88
107
- name : addMode ? `${ collection } /${ modes . find ( ( { modeId } ) => modeId === id ) . name } /${ variable . name } ` : `${ collection } /${ variable . name } ` ,
108
- // add mnetadata to extensions
109
- extensions : {
110
- [ config . key . extensionPluginData ] : {
111
- mode : settings . modeReference ? modes . find ( ( { modeId } ) => modeId === id ) . name : undefined ,
112
- collection : collection ,
113
- scopes : variable . scopes ,
114
- [ config . key . extensionVariableStyleId ] : variable . id ,
115
- exportKey : tokenTypes . variables . key as tokenExportKeyType
89
+ name : addMode
90
+ ? `${ collection } /${
91
+ modes . find ( ( { modeId } ) => modeId === id ) . name
92
+ } /${ variable . name } `
93
+ : `${ collection } /${ variable . name } ` ,
94
+ // add mnetadata to extensions
95
+ extensions : {
96
+ [ config . key . extensionPluginData ] : {
97
+ mode : settings . modeReference
98
+ ? modes . find ( ( { modeId } ) => modeId === id ) . name
99
+ : undefined ,
100
+ collection : collection ,
101
+ scopes : variable . scopes ,
102
+ [ config . key . extensionVariableStyleId ] : variable . id ,
103
+ exportKey : tokenTypes . variables . key as tokenExportKeyType
104
+ }
116
105
}
117
106
}
118
- }
107
+ } )
119
108
} )
120
- } )
121
- return settings . modeReference ? processAliasModes ( variables . flat ( ) ) : variables . flat ( ) ;
122
- }
109
+ return settings . modeReference
110
+ ? processAliasModes ( variables . flat ( ) )
111
+ : variables . flat ( )
112
+ }
0 commit comments