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

Add Highlightr for the Markdown.CodeBlockView #41

Open
erikbasargin opened this issue Jan 24, 2021 · 0 comments · May be fixed by #77
Open

Add Highlightr for the Markdown.CodeBlockView #41

erikbasargin opened this issue Jan 24, 2021 · 0 comments · May be fixed by #77
Assignees
Labels
feature New feature or request Low Low priority
Milestone

Comments

@erikbasargin
Copy link
Collaborator

Describe the solution you'd like

An example of how we could add the Highlightr

import Highlightr

Add to Markdown.CodeBlockView the next code:

var body: some View {
    switch unit.type {
    case let .codeBlock(_, code):
        if let lang = codeType {
            content(codeType: codeType, code: code)
        } else {
            content(code: code)
        }
    default:
        EmptyView()
    }
}

private func content(codeType: String?, code: String) -> some View {
    ScrollView(.horizontal, showsIndicators: true) {
        CodeView(codeType: codeType, code: code)
            .scaledToFit()
    }
    .padding([.leading, .top, .trailing], 12)
    .background(Color.background)
    .cornerRadius(6)
    .padding(.bottom, 3)
}

Example of the CodeView:

fileprivate struct CodeView: UIViewRepresentable {
    let codeType: String?
    let code: String

    init(codeType: String?, code: String) {
        self.codeType = codeType
        self.code = code
    }

    private var textStorage = CodeAttributedString()

    func makeUIView(context: Context) -> UITextView {
        textStorage.language = codeType?.lowercased()
        let layoutManager = NSLayoutManager()
        textStorage.addLayoutManager(layoutManager)

        let textContainer = NSTextContainer(size: .zero)
        layoutManager.addTextContainer(textContainer)

        let textView = UITextView(frame: .zero, textContainer: textContainer)
        textView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        textView.autocorrectionType = UITextAutocorrectionType.no
        textView.autocapitalizationType = UITextAutocapitalizationType.none
        textView.font = UIFont(name: "Menlo-Regular", size: 13)
        textView.textColor = UIColor(named: "codeBlockForeground")
        textView.isSelectable = true
        textView.isUserInteractionEnabled = true
        textView.isEditable = false
        textView.isScrollEnabled = false
        textView.dataDetectorTypes = .all
        textView.backgroundColor = .clear
        textView.textContainer.lineFragmentPadding = .zero
        textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        textView.text = code

        return textView
    }

    func updateUIView(_ uiView: UITextView, context: Context) {}
}
@erikbasargin erikbasargin added the feature New feature or request label Jan 24, 2021
@erikbasargin erikbasargin added this to the Home Flow milestone Jan 24, 2021
@erikbasargin erikbasargin self-assigned this Jan 24, 2021
@erikbasargin erikbasargin added this to To do in StackOv 2.0 MVP via automation Jan 24, 2021
@erikbasargin erikbasargin removed their assignment Jan 24, 2021
@erikbasargin erikbasargin added the Low Low priority label Jan 24, 2021
@Stampoo Stampoo linked a pull request Mar 26, 2021 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request Low Low priority
Projects
Development

Successfully merging a pull request may close this issue.

2 participants