Skip to content

Commit abb929c

Browse files
committed
Refactor component blocks.
1 parent 0a18295 commit abb929c

File tree

4 files changed

+96
-81
lines changed

4 files changed

+96
-81
lines changed

Sources/LaTeXSwiftUI/Models/Component.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ internal struct ComponentBlock: Hashable, Identifiable {
4444
components.count == 1 && !components[0].type.inline
4545
}
4646

47+
/// Creates the image view and its size for the given block.
48+
///
49+
/// If the block isn't an equation block, then this method returns `nil`.
50+
///
51+
/// - Parameter block: The block.
52+
/// - Returns: The image, its size, and any associated error text.
53+
@MainActor func image(
54+
font: Font,
55+
displayScale: CGFloat,
56+
renderingMode: Image.TemplateRenderingMode
57+
) -> (Image, CGSize, String?)? {
58+
guard isEquationBlock, let component = components.first else {
59+
return nil
60+
}
61+
return component.convertToImage(
62+
font: font,
63+
displayScale: displayScale,
64+
renderingMode: renderingMode)
65+
}
66+
4767
}
4868

4969
/// A LaTeX component.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// ComponentBlockText.swift
3+
// LaTeXSwiftUI
4+
//
5+
// Copyright (c) 2023 Colin Campbell
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to
9+
// deal in the Software without restriction, including without limitation the
10+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11+
// sell copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23+
// IN THE SOFTWARE.
24+
//
25+
26+
import SwiftUI
27+
28+
/// Displays a component block as a text view.
29+
internal struct ComponentBlockText: View {
30+
31+
/// The component blocks to display in the view.
32+
let block: ComponentBlock
33+
34+
// MARK: Private properties
35+
36+
/// The rendering mode to use with the rendered MathJax images.
37+
@Environment(\.imageRenderingMode) private var imageRenderingMode
38+
39+
/// What to do in the case of an error.
40+
@Environment(\.errorMode) private var errorMode
41+
42+
/// The view's font.
43+
@Environment(\.font) private var font
44+
45+
/// The view's current display scale.
46+
@Environment(\.displayScale) private var displayScale
47+
48+
/// The view's block rendering mode.
49+
@Environment(\.blockMode) private var blockMode
50+
51+
// MARK: View body
52+
53+
var body: some View {
54+
block.components.enumerated().map { i, component in
55+
return component.convertToText(
56+
font: font ?? .body,
57+
displayScale: displayScale,
58+
renderingMode: imageRenderingMode,
59+
errorMode: errorMode,
60+
blockRenderingMode: blockMode,
61+
isInEquationBlock: block.isEquationBlock)
62+
}.reduce(Text(""), +)
63+
}
64+
65+
}
66+
67+
struct ComponentBlockTextPreviews: PreviewProvider {
68+
static var previews: some View {
69+
ComponentBlockText(block: ComponentBlock(components: [
70+
Component(text: "Hello, World!", type: .text)
71+
]))
72+
}
73+
}

Sources/LaTeXSwiftUI/Views/ComponentBlocksText.swift

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,11 @@ internal struct ComponentBlocksText: View {
3434
/// Whether inline mode should be forced.
3535
var forceInline: Bool = false
3636

37-
// MARK: Private properties
38-
39-
/// The rendering mode to use with the rendered MathJax images.
40-
@Environment(\.imageRenderingMode) private var imageRenderingMode
41-
42-
/// What to do in the case of an error.
43-
@Environment(\.errorMode) private var errorMode
44-
45-
/// The view's font.
46-
@Environment(\.font) private var font
47-
48-
/// The view's current display scale.
49-
@Environment(\.displayScale) private var displayScale
50-
51-
/// The view's block rendering mode.
52-
@Environment(\.blockMode) private var blockMode
53-
5437
// MARK: View body
5538

5639
var body: some View {
5740
blocks.map { block in
58-
let text = text(for: block)
41+
let text = ComponentBlockText(block: block).body as! Text
5942
return block.isEquationBlock && !forceInline ?
6043
Text("\n") + text + Text("\n") :
6144
text
@@ -64,28 +47,6 @@ internal struct ComponentBlocksText: View {
6447

6548
}
6649

67-
// MARK: Private methods
68-
69-
extension ComponentBlocksText {
70-
71-
/// Creates the text view for the given block.
72-
///
73-
/// - Parameter block: The block.
74-
/// - Returns: The text view.
75-
@MainActor private func text(for block: ComponentBlock) -> Text {
76-
block.components.enumerated().map { i, component in
77-
return component.convertToText(
78-
font: font ?? .body,
79-
displayScale: displayScale,
80-
renderingMode: imageRenderingMode,
81-
errorMode: errorMode,
82-
blockRenderingMode: blockMode,
83-
isInEquationBlock: block.isEquationBlock)
84-
}.reduce(Text(""), +)
85-
}
86-
87-
}
88-
8950
struct ComponentBlocksTextPreviews: PreviewProvider {
9051
static var previews: some View {
9152
ComponentBlocksText(blocks: [ComponentBlock(components: [

Sources/LaTeXSwiftUI/Views/ComponentBlocksViews.swift

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ internal struct ComponentBlocksViews: View {
5757
VStack(alignment: .leading, spacing: lineSpacing + 4) {
5858
ForEach(blocks, id: \.self) { block in
5959
if block.isEquationBlock,
60-
let (image, size, errorText) = image(for: block) {
60+
let (image, size, errorText) = block.image(font: font ?? .body, displayScale: displayScale, renderingMode: imageRenderingMode) {
6161
HStack(spacing: 0) {
6262
EquationNumber(blockIndex: blocks.filter({ $0.isEquationBlock }).firstIndex(of: block) ?? 0, side: .left)
6363

@@ -81,53 +81,14 @@ internal struct ComponentBlocksViews: View {
8181
}
8282
}
8383
else {
84-
text(for: block)
84+
ComponentBlockText(block: block)
8585
}
8686
}
8787
}
8888
}
8989

9090
}
9191

92-
// MARK: Private methods
93-
94-
extension ComponentBlocksViews {
95-
96-
/// Creates the text view for the given block.
97-
///
98-
/// - Parameter block: The block.
99-
/// - Returns: The text view.
100-
@MainActor private func text(for block: ComponentBlock) -> Text {
101-
block.components.enumerated().map { i, component in
102-
return component.convertToText(
103-
font: font ?? .body,
104-
displayScale: displayScale,
105-
renderingMode: imageRenderingMode,
106-
errorMode: errorMode,
107-
blockRenderingMode: blockMode,
108-
isInEquationBlock: block.isEquationBlock)
109-
}.reduce(Text(""), +)
110-
}
111-
112-
/// Creates the image view and its size for the given block.
113-
///
114-
/// If the block isn't an equation block, then this method returns `nil`.
115-
///
116-
/// - Parameter block: The block.
117-
/// - Returns: The image, its size, and any associated error text.
118-
@MainActor private func image(for block: ComponentBlock) -> (Image, CGSize, String?)? {
119-
guard block.isEquationBlock,
120-
let component = block.components.first else {
121-
return nil
122-
}
123-
return component.convertToImage(
124-
font: font ?? .body,
125-
displayScale: displayScale,
126-
renderingMode: imageRenderingMode)
127-
}
128-
129-
}
130-
13192
struct ComponentBlocksViewsPreviews: PreviewProvider {
13293
static var previews: some View {
13394
ComponentBlocksViews(blocks: [ComponentBlock(components: [

0 commit comments

Comments
 (0)