From b8060a7d6e73226257cbac0d6a3cb0f9be528953 Mon Sep 17 00:00:00 2001 From: Sjmarf <78750526+Sjmarf@users.noreply.github.com> Date: Thu, 4 Jan 2024 21:01:52 +0000 Subject: [PATCH] Fix scroll-up feed lag (#842) --- Mlem/Views/Shared/Markdown View.swift | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Mlem/Views/Shared/Markdown View.swift b/Mlem/Views/Shared/Markdown View.swift index e860dde80..61cad42a2 100644 --- a/Mlem/Views/Shared/Markdown View.swift +++ b/Mlem/Views/Shared/Markdown View.swift @@ -220,8 +220,8 @@ struct MarkdownBlock: Identifiable { } struct MarkdownView: View { - @State private var text: String - @State private var blocks: [MarkdownBlock] = [] + private let text: String + private let blocks: [MarkdownBlock] private let isNsfw: Bool private let replaceImagesWithEmoji: Bool @@ -235,13 +235,12 @@ struct MarkdownView: View { isInline: Bool = false, alignment: TextAlignment = .leading ) { - _text = isInline - ? .init(wrappedValue: MarkdownView.prepareInlineMarkdown(text: text)) - : .init(wrappedValue: text) + self.text = isInline ? MarkdownView.prepareInlineMarkdown(text: text) : text self.isNsfw = isNsfw self.replaceImagesWithEmoji = replaceImagesWithEmoji self.isInline = isInline self.alignment = alignment + self.blocks = MarkdownView.parseMarkdownForImages(text: text) } var body: some View { @@ -259,9 +258,6 @@ struct MarkdownView: View { } } } - .task { - blocks = parseMarkdownForImages(text: text) - } } private var theme: Theme { @@ -275,7 +271,7 @@ struct MarkdownView: View { .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) } - func parseMarkdownForImages(text: String) -> [MarkdownBlock] { + static func parseMarkdownForImages(text: String) -> [MarkdownBlock] { // this regex will capture the '![label](url "title") pattern so we can handle it separately // piece by piece: // !\[(?'label'[^\]]*)\] matches '![label]' and captures 'label' as label