From 07c0e7d56b659fa3a8d6e15d45778ca90529f537 Mon Sep 17 00:00:00 2001 From: alexey1312 Date: Mon, 8 Apr 2024 08:35:37 +0500 Subject: [PATCH] Update after review --- README.md | 8 +++--- Sources/FigmaAPI/Model/Variables.swift | 27 +++++-------------- .../Loaders/Colors/ColorsLoader.swift | 4 ++- .../Loaders/Colors/ColorsLoaderProtocol.swift | 8 ------ .../Colors/ColorsVariablesLoader.swift | 6 ++--- .../Subcommands/ExportColors.swift | 13 +++++---- 6 files changed, 23 insertions(+), 43 deletions(-) delete mode 100644 Sources/FigmaExport/Loaders/Colors/ColorsLoaderProtocol.swift diff --git a/README.md b/README.md index 0216b21..326f90a 100644 --- a/README.md +++ b/README.md @@ -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. @@ -522,10 +522,10 @@ Example: -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 diff --git a/Sources/FigmaAPI/Model/Variables.swift b/Sources/FigmaAPI/Model/Variables.swift index 83fcc2b..5f00859 100644 --- a/Sources/FigmaAPI/Model/Variables.swift +++ b/Sources/FigmaAPI/Model/Variables.swift @@ -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 @@ -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 } diff --git a/Sources/FigmaExport/Loaders/Colors/ColorsLoader.swift b/Sources/FigmaExport/Loaders/Colors/ColorsLoader.swift index eaf1ad8..153fcd2 100644 --- a/Sources/FigmaExport/Loaders/Colors/ColorsLoader.swift +++ b/Sources/FigmaExport/Loaders/Colors/ColorsLoader.swift @@ -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 diff --git a/Sources/FigmaExport/Loaders/Colors/ColorsLoaderProtocol.swift b/Sources/FigmaExport/Loaders/Colors/ColorsLoaderProtocol.swift deleted file mode 100644 index 74cb1a1..0000000 --- a/Sources/FigmaExport/Loaders/Colors/ColorsLoaderProtocol.swift +++ /dev/null @@ -1,8 +0,0 @@ -import FigmaAPI -import FigmaExportCore - -typealias ColorsLoaderOutput = (light: [Color], dark: [Color]?, lightHC: [Color]?, darkHC: [Color]?) - -protocol ColorsLoaderProtocol { - func load() throws -> ColorsLoaderOutput -} diff --git a/Sources/FigmaExport/Loaders/Colors/ColorsVariablesLoader.swift b/Sources/FigmaExport/Loaders/Colors/ColorsVariablesLoader.swift index 4db6ee9..7c5212d 100644 --- a/Sources/FigmaExport/Loaders/Colors/ColorsVariablesLoader.swift +++ b/Sources/FigmaExport/Loaders/Colors/ColorsVariablesLoader.swift @@ -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? @@ -42,7 +42,7 @@ final class ColorsVariablesLoader: ColorsLoaderProtocol { return try client.request(endpoint) } - private func extractModeIds(from collections: Dictionary.Values.Element) -> ModeIds { + private func extractModeIds(from collections: Dictionary.Values.Element) -> ModeIds { var modeIds = ModeIds() collections.modes.forEach { mode in switch mode.name { @@ -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], diff --git a/Sources/FigmaExport/Subcommands/ExportColors.swift b/Sources/FigmaExport/Subcommands/ExportColors.swift index c73b2ab..1f6f782 100644 --- a/Sources/FigmaExport/Subcommands/ExportColors.swift +++ b/Sources/FigmaExport/Subcommands/ExportColors.swift @@ -37,12 +37,11 @@ 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, @@ -50,10 +49,10 @@ extension FigmaExportCommand { ) 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, @@ -61,8 +60,8 @@ extension FigmaExportCommand { ) 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 {