Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Components/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ let package = Package(
.package(path: "../Icons"),
.package(path: "../Common"),
.package(path: "../Markdown"),
.package(path: "../AppScript")
.package(path: "../AppScript"),
.package(url: "https://github.com/raspu/Highlightr.git", .revision("93199b9e434f04bda956a613af8f571933f9f037"))
],
targets: [
.target(name: "Components", dependencies: ["Palette", "Markdown", "AppScript", "Icons", "Common"])
.target(name: "Components", dependencies: ["Palette", "Markdown", "AppScript", "Icons", "Common", "Highlightr"])
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ extension Markdown {
// MARK: - View

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

// MARK: - View methods

func content(code: String) -> some View {
func content(codeType: String?, code: String) -> some View {
ScrollView(.horizontal, showsIndicators: true) {
VStack(alignment: .center, spacing: .zero) {
Text(code)
.font(.custom("Menlo-Regular", size: 13))
.foregroundColor(.foreground)
.fixedSize(horizontal: false, vertical: true)
CodeView(codeType: codeType, code: code)
}
}
.padding([.leading, .top, .trailing], 12)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//
// CodeView.swift
// StackOv (Components module)
//
// Created by Илья Князьков
// Copyright © 2021 Erik Basargin. All rights reserved.
//

import SwiftUI
import Highlightr

struct CodeView: UIViewRepresentable {

// MARK: - Nested types

private enum Constants {
static let codeFont = UIFont.init(name: "Menlo-Regular", size: 13)
}

typealias UIViewType = UITextView

// MARK: - Private properties

private let codeType: String?
private let code: String
private var textStorage = CodeAttributedString()

// MARK: - Initialization

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

// MARK: - View

func makeUIView(context: Context) -> UITextView {
let (textContainer, layoutManager) = getTextContainerAndLayoutManager()

textStorage.language = codeType?.lowercased()
textStorage.addLayoutManager(layoutManager)
layoutManager.addTextContainer(textContainer)

return getTextView(with: code, container: textContainer)
}

func updateUIView(_ uiView: UITextView, context: Context) { }

// MARK: - Private properties

private func getTextView(with text: String, container: NSTextContainer) -> UITextView {
let textView = UITextView(frame: .zero, textContainer: container)
textView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
textView.autocorrectionType = .no
textView.autocapitalizationType = .none
textView.font = Constants.codeFont
textView.isEditable = false
textView.isScrollEnabled = false
textView.dataDetectorTypes = .all
textView.backgroundColor = .clear
textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
textView.text = text
return textView
}

private func getTextContainerAndLayoutManager() -> (container: NSTextContainer, manager: NSLayoutManager) {
let textContainer = NSTextContainer(size: .zero)
textContainer.lineFragmentPadding = .zero

let layoutManager = NSLayoutManager()


return (textContainer, layoutManager)
}
}

// MARK: - Previews

struct CodeView_Previews: PreviewProvider {

static let code = ("""
func convertHtml() -> NSAttributedString {
guard let data = data(using: .utf8) else { return NSAttributedString() }
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
return attributedString
} else {
return NSAttributedString()
}
}
""")
static let codeType = "swift"

static var previews: some View {
CodeView(codeType: codeType, code: code)
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.