diff --git a/README.md b/README.md index 2760aa7..afb3d0f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Sources/LaTeXSwiftUI/LaTeX.swift b/Sources/LaTeXSwiftUI/LaTeX.swift index 7ad9404..0f0205b 100644 --- a/Sources/LaTeXSwiftUI/LaTeX.swift +++ b/Sources/LaTeXSwiftUI/LaTeX.swift @@ -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()) @@ -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, diff --git a/Sources/LaTeXSwiftUI/Models/Component.swift b/Sources/LaTeXSwiftUI/Models/Component.swift index 06f62b0..78054ef 100644 --- a/Sources/LaTeXSwiftUI/Models/Component.swift +++ b/Sources/LaTeXSwiftUI/Models/Component.swift @@ -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. diff --git a/Sources/LaTeXSwiftUI/Views/ComponentBlockText.swift b/Sources/LaTeXSwiftUI/Views/ComponentBlockText.swift deleted file mode 100644 index e56e0e9..0000000 --- a/Sources/LaTeXSwiftUI/Views/ComponentBlockText.swift +++ /dev/null @@ -1,77 +0,0 @@ -// -// ComponentBlockText.swift -// LaTeXSwiftUI -// -// Copyright (c) 2023 Colin Campbell -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to -// deal in the Software without restriction, including without limitation the -// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -// sell copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -// IN THE SOFTWARE. -// - -import SwiftUI - -/// Displays a component block as a text view. -internal struct ComponentBlockText: View { - - /// The component blocks to display in the view. - let block: ComponentBlock - - /// The view's renderer. - let renderer: Renderer - - // MARK: Private properties - - /// 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: Text { - block.components.enumerated().map { i, component in - return renderer.convertToText( - component: component, - font: font ?? .body, - displayScale: displayScale, - renderingMode: imageRenderingMode, - errorMode: errorMode, - blockRenderingMode: blockMode, - isInEquationBlock: block.isEquationBlock) - }.reduce(Text(""), +) - } - -} - -struct ComponentBlockTextPreviews: PreviewProvider { - static var previews: some View { - ComponentBlockText(block: ComponentBlock(components: [ - Component(text: "Hello, World!", type: .text) - ]), renderer: Renderer()) - } -} diff --git a/Sources/LaTeXSwiftUI/Views/ComponentBlocksText.swift b/Sources/LaTeXSwiftUI/Views/ComponentBlocksText.swift index 95a091f..50164e4 100644 --- a/Sources/LaTeXSwiftUI/Views/ComponentBlocksText.swift +++ b/Sources/LaTeXSwiftUI/Views/ComponentBlocksText.swift @@ -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: [ diff --git a/Sources/LaTeXSwiftUI/Views/ComponentBlocksViews.swift b/Sources/LaTeXSwiftUI/Views/ComponentBlocksViews.swift index c87db37..0dedcab 100644 --- a/Sources/LaTeXSwiftUI/Views/ComponentBlocksViews.swift +++ b/Sources/LaTeXSwiftUI/Views/ComponentBlocksViews.swift @@ -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) } } }