Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to reload new content in SwiftUI? #101

Open
wyk111wyk opened this issue Aug 18, 2022 · 2 comments
Open

How to reload new content in SwiftUI? #101

wyk111wyk opened this issue Aug 18, 2022 · 2 comments

Comments

@wyk111wyk
Copy link

No description provided.

@vuvdv3101
Copy link

vuvdv3101 commented Jan 30, 2024

Can try this. Hope resolve your issue

public struct MarkdownUI2: UIViewRepresentable {
  private let markdownView: MarkdownView
  @Binding public var body: String
  
  public init(body: Binding<String>, css: String? = nil, plugins: [String]? = nil, stylesheets: [URL]? = nil, styled: Bool = true) {
    self._body = body
    self.markdownView = MarkdownView(css: css, plugins: [], stylesheets: stylesheets, styled: styled)
    self.markdownView.isScrollEnabled = false
  }
  
  public func onTouchLink(perform action: @escaping ((URLRequest) -> Bool)) -> MarkdownUI2 {
    self.markdownView.onTouchLink = action
    return self
  }
  
  public func onRendered(perform action: @escaping ((CGFloat) -> Void)) -> MarkdownUI2 {
    self.markdownView.onRendered = action
    return self
  }
}

extension MarkdownUI2 {
  
  public func makeUIView(context: Context) -> MarkdownView {
    return markdownView
  }
  
  public func updateUIView(_ uiView: MarkdownView, context: Context) {
      self.markdownView.load(markdown: self.body)
  }
}

@Mamong
Copy link

Mamong commented Sep 17, 2024

import UIKit
import SwiftUI
import MarkdownView

public struct MarkdownUI2: UIViewRepresentable {

    let text: String
    let css: String?
    let plugins: [String]?
    let stylesheets: [URL]?
    let styled:Bool

    var onTouchLink: (URLRequest) -> Bool = { _ in true }
    var onRendered: (CGFloat) -> Void = { _ in }

    // you can omit some parameters
    public init(text: String, css: String? = nil, plugins: [String]? = nil, stylesheets: [URL]? = nil, styled: Bool = true) {
        self.text = text
        self.css = css
        self.plugins = plugins
        self.stylesheets = stylesheets
        self.styled = styled
    }

    public func makeUIView(context: Context) -> MarkdownView {
        let markdownView = MarkdownView(css: css, plugins: [], stylesheets: stylesheets, styled: styled)
        markdownView.isScrollEnabled = false
        return markdownView
    }

    // should delay when first load, because it takes long time before webview load main.js completely.
   // you can optimize it when necessary, such as "(uiView.intrinsicContentSize.height==0 ? 1.0:0.0)"
    public func updateUIView(_ uiView: MarkdownView, context: Context) {
        uiView.onRendered = onRendered
        uiView.onTouchLink = onTouchLink
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            uiView.show(markdown: self.text)
        }
    }
}

extension MarkdownUI2 {

    func onTouchLink(_ action: @escaping (URLRequest) -> Bool) -> Self {
        then {
            $0.onTouchLink = action
        }
    }

    func onRendered(_ action: @escaping (CGFloat) -> Void) -> Self {
        then {
            $0.onRendered = action
        }
    }
}

extension View {
    func then(_ body: (inout Self) -> Void) -> Self {
        var result = self
        body(&result)
        return result
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants