| 22 | * No side effects. |
| 23 | */ |
| 24 | export class DtcgEmitterHandler implements DtcgEmitterSpec { |
| 25 | execute(state: DesignSystemState): DtcgEmitterResult { |
| 26 | const file: DtcgTokenFile = { |
| 27 | $schema: DTCG_SCHEMA_URL, |
| 28 | }; |
| 29 | |
| 30 | if (state.name || state.description) { |
| 31 | file.$description = state.description || state.name; |
| 32 | } |
| 33 | |
| 34 | const colorGroup = this.mapColors(state); |
| 35 | if (colorGroup) file['color'] = colorGroup; |
| 36 | |
| 37 | const spacingGroup = this.mapDimensionGroup(state.spacing); |
| 38 | if (spacingGroup) file['spacing'] = spacingGroup; |
| 39 | |
| 40 | const roundedGroup = this.mapDimensionGroup(state.rounded); |
| 41 | if (roundedGroup) file['rounded'] = roundedGroup; |
| 42 | |
| 43 | const typographyGroup = this.mapTypography(state); |
| 44 | if (typographyGroup) file['typography'] = typographyGroup; |
| 45 | |
| 46 | return { success: true, data: file as Record<string, unknown> }; |
| 47 | } |
| 48 | |
| 49 | private mapColors(state: DesignSystemState): DtcgGroup | null { |
| 50 | if (state.colors.size === 0) return null; |
| 51 | const group: DtcgGroup = { $type: 'color' }; |
| 52 | for (const [name, color] of state.colors) { |
| 53 | group[name] = { |
| 54 | $value: this.colorToValue(color), |
| 55 | } as DtcgToken; |
| 56 | } |
| 57 | return group; |
| 58 | } |
| 59 | |
| 60 | private colorToValue(color: ResolvedColor): DtcgColorValue { |
| 61 | return { |
| 62 | colorSpace: 'srgb', |
| 63 | components: [ |
| 64 | this.round(color.r / 255), |
| 65 | this.round(color.g / 255), |
| 66 | this.round(color.b / 255), |
| 67 | ], |
| 68 | hex: color.hex.toLowerCase(), |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | private mapDimensionGroup(dims: Map<string, ResolvedDimension>): DtcgGroup | null { |
| 73 | if (dims.size === 0) return null; |
| 74 | const group: DtcgGroup = { $type: 'dimension' }; |
| 75 | for (const [name, dim] of dims) { |
| 76 | group[name] = { |
| 77 | $value: this.dimToValue(dim), |
| 78 | } as DtcgToken; |
| 79 | } |
| 80 | return group; |
| 81 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…