-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update ExportColors Update params name Update params name Small refactoring Small refactoring Add doc Refactoring Refactoring
- Loading branch information
1 parent
bb5d949
commit 45960ad
Showing
10 changed files
with
267 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
Sources/FigmaExport/Loaders/Colors/ColorsLoaderProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import FigmaAPI | ||
import FigmaExportCore | ||
|
||
typealias ColorsLoaderOutput = (light: [Color], dark: [Color]?, lightHC: [Color]?, darkHC: [Color]?) | ||
|
||
protocol ColorsLoaderProtocol { | ||
func load(filter: String?) throws -> ColorsLoaderOutput | ||
} |
143 changes: 143 additions & 0 deletions
143
Sources/FigmaExport/Loaders/Colors/ColorsVariablesLoader.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import FigmaAPI | ||
import FigmaExportCore | ||
|
||
/// Loads variables colors from Figma | ||
final class ColorsVariablesLoader: ColorsLoaderProtocol { | ||
private let client: Client | ||
private let variableParams: Params.Common.VariablesColors? | ||
|
||
init(client: Client, figmaParams: Params.Figma, variableParams: Params.Common.VariablesColors?) { | ||
self.client = client | ||
self.variableParams = variableParams | ||
} | ||
|
||
func load(filter: String?) throws -> ColorsLoaderOutput { | ||
guard | ||
let tokensFileId = variableParams?.tokensFileId, | ||
let tokensCollectionName = variableParams?.tokensCollectionName | ||
else { throw FigmaExportError.custom(errorString: "tokensFileId or tokensLightCollectionName is nil") } | ||
|
||
return try loadProcess(colorTokensFileId: tokensFileId, tokensCollectionName: tokensCollectionName) | ||
} | ||
|
||
private func loadProcess(colorTokensFileId: String, tokensCollectionName: String) throws -> ColorsLoaderOutput { | ||
// Load variables | ||
let meta = try loadVariables(fileId: colorTokensFileId) | ||
|
||
guard let tokenCollection = meta.variableCollections.filter({ $0.value.name == tokensCollectionName }).first | ||
else { throw FigmaExportError.custom(errorString: "tokensCollectionName is nil") } | ||
|
||
let tokensId = tokenCollection.value.variableIds | ||
let modeIds = extractModeIds(from: tokenCollection.value) | ||
let primitivesModeName = variableParams?.primitivesModeName | ||
|
||
let variables: [Variable] = tokensId.compactMap { tokenId in | ||
guard let variableMeta = meta.variables[tokenId] | ||
else { return nil } | ||
|
||
let values = Values( | ||
light: variableMeta.valuesByMode[modeIds.lightModeId], | ||
dark: variableMeta.valuesByMode[modeIds.darkModeId], | ||
lightHC: variableMeta.valuesByMode[modeIds.lightHCModeId], | ||
darkHC: variableMeta.valuesByMode[modeIds.darkHCModeId] | ||
) | ||
|
||
return Variable( | ||
name: variableMeta.name, | ||
description: variableMeta.description, | ||
valuesByMode: values | ||
) | ||
} | ||
|
||
var colors = Colors() | ||
func handleColorMode(variable: Variable, mode: ValuesByMode?, colorsArray: inout [Color]) { | ||
if case let .variableAlias(variableAlias) = mode { | ||
guard | ||
let variableMeta = meta.variables[variableAlias.id], | ||
let variableCollectionId = meta.variableCollections[variableMeta.variableCollectionId] | ||
else { return } | ||
let modeId = variableCollectionId.modes | ||
.filter { $0.name == primitivesModeName } | ||
.first?.modeId ?? variableCollectionId.defaultModeId | ||
if case let .color(color) = variableMeta.valuesByMode[modeId] { | ||
colorsArray.append(createColor(from: variable, color: color)) | ||
} else { | ||
handleColorMode(variable: variable, mode: mode, colorsArray: &colorsArray) | ||
} | ||
} else if case let .color(color) = mode { | ||
colorsArray.append(createColor(from: variable, color: color)) | ||
} | ||
} | ||
variables.forEach { value in | ||
handleColorMode(variable: value, mode: value.valuesByMode.light, colorsArray: &colors.lightColors) | ||
handleColorMode(variable: value, mode: value.valuesByMode.dark, colorsArray: &colors.darkColors) | ||
handleColorMode(variable: value, mode: value.valuesByMode.lightHC, colorsArray: &colors.lightHCColors) | ||
handleColorMode(variable: value, mode: value.valuesByMode.darkHC, colorsArray: &colors.darkHCColors) | ||
} | ||
return (colors.lightColors, colors.darkColors, colors.lightHCColors, colors.darkHCColors) | ||
} | ||
|
||
private func loadVariables(fileId: String) throws -> VariablesEndpoint.Content { | ||
let endpoint = VariablesEndpoint(fileId: fileId) | ||
return try client.request(endpoint) | ||
} | ||
|
||
private func extractModeIds(from collections: Dictionary<String, VariableCollectionId>.Values.Element) -> ModeIds { | ||
var modeIds = ModeIds() | ||
collections.modes.forEach { | ||
switch $0.name { | ||
case variableParams?.lightModeName: | ||
modeIds.lightModeId = $0.modeId | ||
case variableParams?.darkModeName: | ||
modeIds.darkModeId = $0.modeId | ||
case variableParams?.lightHCModeName: | ||
modeIds.lightHCModeId = $0.modeId | ||
case variableParams?.darkHCModeName: | ||
modeIds.darkHCModeId = $0.modeId | ||
default: | ||
modeIds.lightModeId = $0.modeId | ||
} | ||
} | ||
return modeIds | ||
} | ||
|
||
private func createColor(from variable: Variable, color: PaintColor) -> Color { | ||
return Color( | ||
name: variable.name, | ||
platform: Platform(rawValue: variable.description), | ||
red: color.r, | ||
green: color.g, | ||
blue: color.b, | ||
alpha: color.a | ||
) | ||
} | ||
} | ||
|
||
private extension ColorsVariablesLoader { | ||
struct ModeIds { | ||
var lightModeId = String() | ||
var darkModeId = String() | ||
var lightHCModeId = String() | ||
var darkHCModeId = String() | ||
} | ||
|
||
struct Colors { | ||
var lightColors: [Color] = [] | ||
var darkColors: [Color] = [] | ||
var lightHCColors: [Color] = [] | ||
var darkHCColors: [Color] = [] | ||
} | ||
|
||
struct Values { | ||
let light: ValuesByMode? | ||
let dark: ValuesByMode? | ||
let lightHC: ValuesByMode? | ||
let darkHC: ValuesByMode? | ||
} | ||
|
||
struct Variable { | ||
let name: String | ||
let description: String | ||
let valuesByMode: Values | ||
} | ||
} |
Oops, something went wrong.