Skip to content

Commit

Permalink
Update after review
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey1312 committed Apr 8, 2024
1 parent f823e55 commit 07c0e7d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 43 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,9 @@ For `figma-export colors`

**Important, the [API](https://www.figma.com/developers/api#variables) for working with color variables in Figma is still in `Beta` stage, so something may break at any time.**

**Important, in [CONFIG.md](CONFIG.md) use either colors or variablesColors.**
**Important, in [figma-export.yaml](CONFIG.md) use either `colors` or `variablesColors`.**

With the introduction of color variables in Figma, you can use color variables instead of color styles. Color variables can be used in figma-export, for this in [CONFIG.md](CONFIG.md) you need to use the `variablesColors` variable instead of `colors`.
With the introduction of color variables in Figma, you can use it instead of color styles. Color variables can be used in figma-export, for this in [figma-export.yaml](CONFIG.md) you need to use the `variablesColors` option instead of `colors`.

The value of variables can be either the final color value or another variable. For example, the `Primary` variable can contain the value `#FFFFFF`, and the `Secondary` variable can contain the value `Pand/90`. Figma-export can work with any depth of variable nesting. You can specify the `primitivesModeName` parameter to indicate the mode for the final table with your primitives, if the parameter is not specified, the default value will be used.

Expand All @@ -522,10 +522,10 @@ Example:

<img src="images/figma_colors_primitives.png" width="352" />

1. primitivesModeName - the name of the variable column, if the value in [CONFIG.md](CONFIG.md) is not specified, the default value will be used
1. primitivesModeName - the name of the variable column, if the value in [figma-export.yaml](CONFIG.md) is not specified, the default value will be used
2. A variable that has a local value

See [CONFIG.md](CONFIG.md) for more information in the variablesColors section.
See [CONFIG.md](CONFIG.md) for more information in the `variablesColors` section.

### For icons

Expand Down
27 changes: 7 additions & 20 deletions Sources/FigmaAPI/Model/Variables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@ public struct Mode: Decodable {
public var name: String
}

public struct VariableCollectionId: Decodable {
public struct VariableCollectionValue: Decodable {
public var defaultModeId: String
public var id: String
public var name: String
public var remote: Bool
public var modes: [Mode]
public var key: String
public var hiddenFromPublishing: Bool
public var variableIds: [String]
}

public enum ResolvedType: String, Decodable {
case boolean = "BOOLEAN"
case float = "FLOAT"
case string = "STRING"
case color = "COLOR"
}

public struct VariableAlias: Codable {
public var id: String
public var type: String
Expand Down Expand Up @@ -58,25 +48,22 @@ public enum ValuesByMode: Decodable {
}
}

public struct VariableID: Decodable {
public struct VariableValue: Decodable {
public var id: String
public var name: String
public var key: String
public var variableCollectionId: String
public var resolvedType: ResolvedType
public var valuesByMode: [String: ValuesByMode]
public var remote: Bool
public var description: String
public var hiddenFromPublishing: Bool
}

public typealias VariableId = String
public typealias VariableCollectionId = String

public struct VariablesMeta: Decodable {
public var variableCollections: [String: VariableCollectionId]
public var variables: [String: VariableID]
public var variableCollections: [VariableCollectionId: VariableCollectionValue]
public var variables: [VariableId: VariableValue]
}

public struct VariablesResponse: Decodable {
public let error: Bool
public let status: Int
public let meta: VariablesMeta
}
4 changes: 3 additions & 1 deletion Sources/FigmaExport/Loaders/Colors/ColorsLoader.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import FigmaAPI
import FigmaExportCore

typealias ColorsLoaderOutput = (light: [Color], dark: [Color]?, lightHC: [Color]?, darkHC: [Color]?)

/// Loads colors from Figma
final class ColorsLoader: ColorsLoaderProtocol {
final class ColorsLoader {

private let client: Client
private let figmaParams: Params.Figma
Expand Down
8 changes: 0 additions & 8 deletions Sources/FigmaExport/Loaders/Colors/ColorsLoaderProtocol.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import FigmaAPI
import FigmaExportCore

/// Loads color variables from Figma
final class ColorsVariablesLoader: ColorsLoaderProtocol {
final class ColorsVariablesLoader {
private let client: Client
private let variableParams: Params.Common.VariablesColors?
private let filter: String?
Expand Down Expand Up @@ -42,7 +42,7 @@ final class ColorsVariablesLoader: ColorsLoaderProtocol {
return try client.request(endpoint)
}

private func extractModeIds(from collections: Dictionary<String, VariableCollectionId>.Values.Element) -> ModeIds {
private func extractModeIds(from collections: Dictionary<String, VariableCollectionValue>.Values.Element) -> ModeIds {
var modeIds = ModeIds()
collections.modes.forEach { mode in
switch mode.name {
Expand All @@ -61,7 +61,7 @@ final class ColorsVariablesLoader: ColorsLoaderProtocol {
return modeIds
}

private func mapVariableMetaToVariable(variableMeta: VariableID, modeIds: ModeIds) -> Variable {
private func mapVariableMetaToVariable(variableMeta: VariableValue, modeIds: ModeIds) -> Variable {
let values = Values(
light: variableMeta.valuesByMode[modeIds.lightModeId],
dark: variableMeta.valuesByMode[modeIds.darkModeId],
Expand Down
13 changes: 6 additions & 7 deletions Sources/FigmaExport/Subcommands/ExportColors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,31 @@ extension FigmaExportCommand {

let figmaParams = options.params.figma
var colors: ColorsLoaderOutput?
var loader: ColorsLoaderProtocol
var nameValidateRegexp: String?
var nameReplaceRegexp: String?

if let colorParams = commonParams?.colors {
loader = ColorsLoader(
let loader = ColorsLoader(
client: client,
figmaParams: figmaParams,
colorParams: colorParams,
filter: filter
)
colors = try loader.load()

nameValidateRegexp = options.params.common?.colors?.nameValidateRegexp
nameReplaceRegexp = options.params.common?.colors?.nameReplaceRegexp
nameValidateRegexp = colorParams.nameValidateRegexp
nameReplaceRegexp = colorParams.nameReplaceRegexp
} else if let variableParams = commonParams?.variablesColors {
loader = ColorsVariablesLoader(
let loader = ColorsVariablesLoader(
client: client,
figmaParams: figmaParams,
variableParams: variableParams,
filter: filter
)
colors = try loader.load()

nameValidateRegexp = options.params.common?.variablesColors?.nameValidateRegexp
nameReplaceRegexp = options.params.common?.variablesColors?.nameReplaceRegexp
nameValidateRegexp = variableParams.nameValidateRegexp
nameReplaceRegexp = variableParams.nameReplaceRegexp
}

guard let colors else {
Expand Down

0 comments on commit 07c0e7d

Please sign in to comment.