Genesis
This commit is contained in:
86
MarkdownViewer/ContentView.swift
Normal file
86
MarkdownViewer/ContentView.swift
Normal file
@@ -0,0 +1,86 @@
|
||||
import SwiftUI
|
||||
import UniformTypeIdentifiers
|
||||
import AppKit
|
||||
|
||||
struct ContentView: View {
|
||||
@State private var selectedFileURL: URL?
|
||||
@State private var markdownContent: String = ""
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
VStack {
|
||||
Button("Open Markdown File") {
|
||||
openFile()
|
||||
}
|
||||
.padding()
|
||||
|
||||
Button("Test with Sample Content") {
|
||||
markdownContent = "# Test\n\nThis is **test** content."
|
||||
print("Set test content: \(markdownContent)")
|
||||
}
|
||||
.padding()
|
||||
|
||||
if let url = selectedFileURL {
|
||||
Text("File: \(url.lastPathComponent)")
|
||||
.foregroundColor(.secondary)
|
||||
.font(.caption)
|
||||
.truncationMode(.middle)
|
||||
.padding(.horizontal)
|
||||
}
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.frame(minWidth: 200)
|
||||
.navigationSplitViewColumnWidth(min: 200, ideal: 250)
|
||||
} detail: {
|
||||
if !markdownContent.isEmpty {
|
||||
MarkdownRenderer(content: markdownContent)
|
||||
.onAppear {
|
||||
print("MarkdownRenderer appeared with content length: \(markdownContent.count)")
|
||||
}
|
||||
} else {
|
||||
VStack {
|
||||
Image(systemName: "doc.text")
|
||||
.font(.system(size: 60))
|
||||
.foregroundColor(.secondary)
|
||||
Text("Select a markdown file to preview")
|
||||
.foregroundColor(.secondary)
|
||||
.padding()
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
}
|
||||
.navigationTitle("Markdown Viewer")
|
||||
}
|
||||
|
||||
private func openFile() {
|
||||
let panel = NSOpenPanel()
|
||||
panel.allowsMultipleSelection = false
|
||||
panel.canChooseDirectories = false
|
||||
panel.canChooseFiles = true
|
||||
panel.allowedContentTypes = [UTType.plainText, UTType(filenameExtension: "md")!]
|
||||
|
||||
if panel.runModal() == .OK {
|
||||
if let url = panel.url {
|
||||
selectedFileURL = url
|
||||
loadMarkdownFile(from: url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func loadMarkdownFile(from url: URL) {
|
||||
do {
|
||||
let content = try String(contentsOf: url, encoding: .utf8)
|
||||
print("File loaded successfully, content length: \(content.count)")
|
||||
print("First 100 characters: \(String(content.prefix(100)))")
|
||||
markdownContent = content
|
||||
} catch {
|
||||
print("Error reading file: \(error)")
|
||||
markdownContent = "Error loading file: \(error.localizedDescription)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ContentView()
|
||||
}
|
||||
Reference in New Issue
Block a user