Skip to content

Commit 916c330

Browse files
committed
Allow async loading. Refactor.
1 parent 82ff9a2 commit 916c330

12 files changed

+743
-199
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let package = Package(
1515
targets: ["LaTeXSwiftUI"]),
1616
],
1717
dependencies: [
18-
.package(url: "https://github.com/colinc86/MathJaxSwift", from: "3.2.2"),
18+
.package(url: "https://github.com/colinc86/MathJaxSwift", from: "3.3.0"),
1919
.package(url: "https://github.com/exyte/SVGView", from: "1.0.4"),
2020
.package(url: "https://github.com/kean/Nuke", from: "12.1.0"),
2121
.package(url: "https://github.com/Kitura/swift-html-entities", from: "4.0.1")

Sources/LaTeXSwiftUI/Extensions/EnvironmentValues+Extensions.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ private struct EquationNumberOffsetKey: EnvironmentKey {
6262
static let defaultValue: CGFloat = 0.0
6363
}
6464

65+
private struct RenderingStyleKey: EnvironmentKey {
66+
static let defaultValue: LaTeX.RenderingStyle = .wait
67+
}
68+
69+
private struct AnimatedKey: EnvironmentKey {
70+
static let defaultValue: Bool = false
71+
}
72+
6573
extension EnvironmentValues {
6674

6775
/// The image rendering mode of this environment.
@@ -118,4 +126,16 @@ extension EnvironmentValues {
118126
set { self[EquationNumberOffsetKey.self] = newValue }
119127
}
120128

129+
/// The rendering style of this environment.
130+
var renderingStyle: LaTeX.RenderingStyle {
131+
get { self[RenderingStyleKey.self] }
132+
set { self[RenderingStyleKey.self] = newValue }
133+
}
134+
135+
/// Whether or not rendering should be animated.
136+
var animated: Bool {
137+
get { self[AnimatedKey.self] }
138+
set { self[AnimatedKey.self] = newValue }
139+
}
140+
121141
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// TeXInputProcessorOptions+Extensions.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 Foundation
27+
import MathJaxSwift
28+
29+
extension TeXInputProcessorOptions {
30+
31+
/// Initializes a set of options with the correct properties set for rendering
32+
/// a `LaTeX` view.
33+
///
34+
/// - Parameters:
35+
/// - processEscapes: Whether or not escapes should be processed.
36+
/// - errorMode: The view's error mode.
37+
convenience init(processEscapes: Bool, errorMode: LaTeX.ErrorMode) {
38+
self.init()
39+
self.processEscapes = processEscapes
40+
41+
var packages = TeXInputProcessorOptions.Packages.all
42+
if errorMode != .rendered {
43+
if let noErrorsIndex = packages.firstIndex(of: TeXInputProcessorOptions.Packages.noerrors) {
44+
packages.remove(at: noErrorsIndex)
45+
}
46+
if let noUndefinedIndex = packages.firstIndex(of: TeXInputProcessorOptions.Packages.noundefined) {
47+
packages.remove(at: noUndefinedIndex)
48+
}
49+
}
50+
loadPackages = packages
51+
}
52+
53+
}

Sources/LaTeXSwiftUI/Extensions/View+Extensions.swift

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,6 @@ import SwiftUI
2828

2929
public extension View {
3030

31-
/// Preloads a `LaTeX` view's SVG and image data.
32-
///
33-
/// This method should be called last in the view's modifier chain (if at
34-
/// all).
35-
///
36-
/// - Example:
37-
///
38-
/// ```
39-
/// LaTeX("Hello, $\\LaTeX$!")
40-
/// .font(.title)
41-
/// .processEscapes()
42-
/// .preload()
43-
/// ```
44-
///
45-
/// - Note: If the receiver isn't a `LaTeX` view, then this method does
46-
/// nothing.
47-
///
48-
/// - Returns: A preloaded view.
49-
@MainActor func preload() -> some View {
50-
if let latex = self as? LaTeX {
51-
latex.preload()
52-
}
53-
return self
54-
}
55-
5631
/// Sets the image rendering mode for images rendered by MathJax.
5732
///
5833
/// - Parameter mode: The template rendering mode.
@@ -137,4 +112,20 @@ public extension View {
137112
environment(\.equationNumberOffset, offset)
138113
}
139114

115+
/// Sets the view rendering style.
116+
///
117+
/// - Parameter style: The rendering style to use.
118+
/// - Returns: A view that renders its content.
119+
func renderingStyle(_ style: LaTeX.RenderingStyle) -> some View {
120+
environment(\.renderingStyle, style)
121+
}
122+
123+
/// Sets whether or not the view will animate its render state changes.
124+
///
125+
/// - Parameter animate: Whether animations are enabled.
126+
/// - Returns: A view that animates its rendering.
127+
func animated(_ animate: Bool = true) -> some View {
128+
environment(\.animated, animate)
129+
}
130+
140131
}

0 commit comments

Comments
 (0)