-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathTextStylesLoader.swift
More file actions
66 lines (53 loc) · 2.25 KB
/
TextStylesLoader.swift
File metadata and controls
66 lines (53 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import FigmaAPI
import FigmaExportCore
/// Loads text styles from Figma
final class TextStylesLoader {
private let client: Client
private let params: Params.Figma
init(client: Client, params: Params.Figma) {
self.client = client
self.params = params
}
func load() throws -> [TextStyle] {
return try loadTextStyles(fileId: params.lightFileId)
}
private func loadTextStyles(fileId: String) throws -> [TextStyle] {
let styles = try loadStyles(fileId: fileId)
guard !styles.isEmpty else {
throw FigmaExportError.stylesNotFound
}
let nodes = try loadNodes(fileId: fileId, nodeIds: styles.map { $0.nodeId } )
return styles.compactMap { style -> TextStyle? in
guard let node = nodes[style.nodeId] else { return nil}
guard let textStyle = node.document.style else { return nil }
let lineHeight: Double? = textStyle.lineHeightUnit == .intrinsic ? nil : textStyle.lineHeightPx
let textCase: TextStyle.TextCase
switch textStyle.textCase {
case .lower:
textCase = .lowercased
case .upper:
textCase = .uppercased
default:
textCase = .original
}
return TextStyle(
name: style.name,
fontName: textStyle.fontPostScriptName ?? textStyle.fontFamily ?? "",
fontSize: textStyle.fontSize,
fontStyle: DynamicTypeStyle(rawValue: style.description),
lineHeight: lineHeight,
letterSpacing: textStyle.letterSpacing,
textCase: textCase
)
}
}
private func loadStyles(fileId: String) throws -> [Style] {
let endpoint = StylesEndpoint(fileId: fileId)
let styles = try client.requestWithRetry(endpoint, configuration: .default)
return styles.filter { $0.styleType == .text }
}
private func loadNodes(fileId: String, nodeIds: [String]) throws -> [NodeId: Node] {
let endpoint = NodesEndpoint(fileId: fileId, nodeIds: nodeIds)
return try client.requestWithRetry(endpoint, configuration: .default)
}
}