-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
76 lines (66 loc) · 1.99 KB
/
Copy pathContentView.swift
File metadata and controls
76 lines (66 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import CodeViewerKit
import CodeViewerKitStandardLanguages
import Foundation
import SwiftUI
struct ContentView: View {
@State private var highlightStore = CodeHighlightStore(
grammars: CodeViewerKitStandardLanguages.all
)
// Replace `.bundledHTML` with `.inlineSwift` to use an inline string.
private static let document = ExampleDocument.bundledHTML
var body: some View {
NavigationStack {
CodeViewer(
documentID: Self.document.id,
sourceCode: Self.document.sourceCode,
highlightStore: highlightStore,
language: Self.document.language,
lineWrapping: .word
)
.navigationTitle(Self.document.title)
}
}
}
private struct ExampleDocument {
let id: String
let title: String
let sourceCode: String
let language: CodeLanguage
static let bundledHTML = Self(
id: "lorem-ipsum-html",
title: "Lorem Ipsum HTML",
sourceCode: bundledSource(named: "lorem-ipsum-html", extension: "txt"),
language: "html"
)
static let inlineSwift = Self(
id: "greeting-view",
title: "Inline Swift",
sourceCode: """
import SwiftUI
struct GreetingView: View {
let name: String
var body: some View {
Label("Hello, \\(name)!", systemImage: "swift")
.font(.title)
.padding()
}
}
""",
language: "swift"
)
private static func bundledSource(
named name: String,
extension pathExtension: String
) -> String {
guard let url = Bundle.main.url(
forResource: name,
withExtension: pathExtension
), let sourceCode = try? String(contentsOf: url, encoding: .utf8) else {
return "<!-- Add \(name).\(pathExtension) to the app target. -->"
}
return sourceCode
}
}
#Preview {
ContentView()
}