Skip to content

Commit

Permalink
Merge pull request #33 from colinc86/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
colinc86 authored Mar 26, 2024
2 parents 1e5181a + 72aea7c commit 766be13
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 84 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ It won't
Add the dependency to your package manifest file.

```swift
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.3.1")
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.3.2")
```

## ⌨️ Usage
Expand Down
6 changes: 4 additions & 2 deletions Sources/LaTeXSwiftUI/LaTeX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ public struct LaTeX: View {
switch renderingStyle {
case .empty, .original, .progress:
// Render the components asynchronously
loadingView().task(renderAsync)
loadingView().task {
await renderAsync()
}
case .wait:
// Render the components synchronously
bodyWithBlocks(renderSync())
Expand Down Expand Up @@ -248,7 +250,7 @@ extension LaTeX {
}

/// Renders the view's components.
@Sendable private func renderAsync() async {
private func renderAsync() async {
await renderer.render(
latex: latex,
unencodeHTML: unencodeHTML,
Expand Down
30 changes: 30 additions & 0 deletions Sources/LaTeXSwiftUI/Models/Component.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ internal struct ComponentBlock: Hashable, Identifiable {
components.count == 1 && !components[0].type.inline
}

/// Converts a component block to a `Text` view.
///
/// - Parameters:
/// - renderer: The renderer to use.
/// - font: The font to use.
/// - displayScale: The display scale.
/// - renderingMode: The rendering mode.
/// - errorMode: The error mode.
/// - blockRenderingMode: The block rendering mode.
/// - Returns: A `Text` view.
@MainActor func toText(
using renderer: Renderer,
font: Font?,
displayScale: CGFloat,
renderingMode: Image.TemplateRenderingMode,
errorMode: LaTeX.ErrorMode,
blockRenderingMode: LaTeX.BlockMode
) -> Text {
components.enumerated().map { i, component in
return renderer.convertToText(
component: component,
font: font ?? .body,
displayScale: displayScale,
renderingMode: renderingMode,
errorMode: errorMode,
blockRenderingMode: blockRenderingMode,
isInEquationBlock: isEquationBlock)
}.reduce(Text(""), +)
}

}

/// A LaTeX component.
Expand Down
77 changes: 0 additions & 77 deletions Sources/LaTeXSwiftUI/Views/ComponentBlockText.swift

This file was deleted.

40 changes: 37 additions & 3 deletions Sources/LaTeXSwiftUI/Views/ComponentBlocksText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,53 @@ internal struct ComponentBlocksText: View {
/// The view's renderer.
@EnvironmentObject private var renderer: Renderer

/// The rendering mode to use with the rendered MathJax images.
@Environment(\.imageRenderingMode) private var imageRenderingMode

/// What to do in the case of an error.
@Environment(\.errorMode) private var errorMode

/// The view's font.
@Environment(\.font) private var font

/// The view's current display scale.
@Environment(\.displayScale) private var displayScale

/// The view's block rendering mode.
@Environment(\.blockMode) private var blockMode

// MARK: View body

var body: some View {
blocks.map { block in
let text = ComponentBlockText(block: block, renderer: renderer).body
return block.isEquationBlock && !forceInline ?
Text("\n") + text + Text("\n") :
text
Text("\n") + text(for: block) + Text("\n") :
text(for: block)
}.reduce(Text(""), +)
}

}

// MARK: Private methods

extension ComponentBlocksText {

/// Gets the `Text` view for the given component block.
///
/// - Parameter block: The component block.
/// - Returns: A `Text` view.
private func text(for block: ComponentBlock) -> Text {
block.toText(
using: renderer,
font: font,
displayScale: displayScale,
renderingMode: imageRenderingMode,
errorMode: errorMode,
blockRenderingMode: blockMode)
}

}

struct ComponentBlocksTextPreviews: PreviewProvider {
static var previews: some View {
ComponentBlocksText(blocks: [ComponentBlock(components: [
Expand Down
8 changes: 7 additions & 1 deletion Sources/LaTeXSwiftUI/Views/ComponentBlocksViews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ internal struct ComponentBlocksViews: View {
}
}
else {
ComponentBlockText(block: block, renderer: renderer)
block.toText(
using: renderer,
font: font,
displayScale: displayScale,
renderingMode: imageRenderingMode,
errorMode: errorMode,
blockRenderingMode: blockMode)
}
}
}
Expand Down

0 comments on commit 766be13

Please sign in to comment.