Skip to content

Commit

Permalink
Add support for color mode defined by color name prefix (not only suf…
Browse files Browse the repository at this point in the history
  • Loading branch information
fedulvtubudul committed Mar 28, 2024
1 parent 90ef6b7 commit 21ed97d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
5 changes: 5 additions & 0 deletions Sources/FigmaExport/Input/Params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
Expand Down
38 changes: 24 additions & 14 deletions Sources/FigmaExport/Loaders/ColorsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21ed97d

Please sign in to comment.