From 21ed97dd4a933cf35d420fb972199bd2de3ab6ec Mon Sep 17 00:00:00 2001 From: Alexander Gorbunov Date: Thu, 28 Mar 2024 19:06:39 +0900 Subject: [PATCH] Add support for color mode defined by color name prefix (not only suffix) #218 --- Sources/FigmaExport/Input/Params.swift | 5 +++ .../FigmaExport/Loaders/ColorsLoader.swift | 38 ++++++++++++------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Sources/FigmaExport/Input/Params.swift b/Sources/FigmaExport/Input/Params.swift index 9865e0b4..0550b343 100644 --- a/Sources/FigmaExport/Input/Params.swift +++ b/Sources/FigmaExport/Input/Params.swift @@ -18,7 +18,12 @@ struct Params: Decodable { let nameValidateRegexp: String? let nameReplaceRegexp: String? let useSingleFile: Bool? + let lightModePrefix: String? + let darkModePrefix: String? + let lightModeSuffix: String? let darkModeSuffix: String? + let lightHCModePrefix: String? + let darkHCModePrefix: String? let lightHCModeSuffix: String? let darkHCModeSuffix: String? } diff --git a/Sources/FigmaExport/Loaders/ColorsLoader.swift b/Sources/FigmaExport/Loaders/ColorsLoader.swift index 6ff18627..c2f53323 100644 --- a/Sources/FigmaExport/Loaders/ColorsLoader.swift +++ b/Sources/FigmaExport/Loaders/ColorsLoader.swift @@ -37,29 +37,39 @@ final class ColorsLoader { lightHC: [Color]?, darkHC: [Color]?) { let colors = try loadColors(fileId: figmaParams.lightFileId, filter: filter) - - let darkSuffix = colorParams?.darkModeSuffix ?? "_dark" + + let lightPrefix = colorParams?.lightModePrefix + let darkPrefix = colorParams?.darkModePrefix + let lightSuffix = colorParams?.lightModeSuffix + let darkSuffix = colorParams?.darkModeSuffix + let lightHCPrefix = colorParams?.lightHCModePrefix ?? "lightHC_" + let darkHCPrefix = colorParams?.darkHCModePrefix ?? "darkHC_" let lightHCSuffix = colorParams?.lightHCModeSuffix ?? "_lightHC" let darkHCSuffix = colorParams?.darkHCModeSuffix ?? "_darkHC" - let lightColors = colors - .filter { - !$0.name.hasSuffix(darkSuffix) && - !$0.name.hasSuffix(lightHCSuffix) && - !$0.name.hasSuffix(darkHCSuffix) - } - let darkColors = filteredColors(colors, suffix: darkSuffix) - let lightHCColors = filteredColors(colors, suffix: lightHCSuffix) - let darkHCColors = filteredColors(colors, suffix: darkHCSuffix) + let lightColors = filteredColors(colors, prefix: lightPrefix, suffix: lightSuffix) + let darkColors = filteredColors(colors, prefix: darkPrefix, suffix: darkSuffix) + let lightHCColors = filteredColors(colors, prefix: lightHCPrefix, suffix: lightHCSuffix) + let darkHCColors = filteredColors(colors, prefix: darkHCPrefix, suffix: darkHCSuffix) return (lightColors, darkColors, lightHCColors, darkHCColors) } - private func filteredColors(_ colors: [Color], suffix: String) -> [Color] { + private func filteredColors(_ colors: [Color], prefix: String?, suffix: String?) -> [Color] { let filteredColors = colors - .filter { $0.name.hasSuffix(suffix) } + .filter { + if let prefix, let suffix { + return $0.name.hasPrefix(prefix) && $0.name.hasSuffix(suffix) + } else if let prefix { + return $0.name.hasPrefix(prefix) + } else if let suffix { + return $0.name.hasSuffix(suffix) + } else { + return true + } + } .map { color -> Color in var newColor = color - newColor.name = String(color.name.dropLast(suffix.count)) + newColor.name = String(color.name.dropFirst(prefix?.count ?? 0).dropLast(suffix?.count ?? 0)) return newColor } return filteredColors