Colors, pages, post links

This commit is contained in:
Christoph Hagen
2024-11-20 13:53:44 +01:00
parent 943d8d962b
commit 8ae2a237cc
19 changed files with 466 additions and 46 deletions

View File

@ -1,5 +1,6 @@
import Foundation
import SwiftUI
import Combine
final class Content: ObservableObject {
@ -19,7 +20,64 @@ final class Content: ObservableObject {
var files: [FileResources] = []
@AppStorage("contentPath")
var contentPath: String = ""
private var storedContentPath: String = ""
@Published
var contentPath: String = "" {
didSet {
storedContentPath = contentPath
}
}
let storage: Storage
private var cancellables = Set<AnyCancellable>()
init(posts: [Post] = [],
pages: [Page] = [],
tags: [Tag] = [],
images: [ImageResource] = [],
files: [FileResources] = [],
storedContentPath: String) {
self.posts = posts
self.pages = pages
self.tags = tags
self.images = images
self.files = files
self.storedContentPath = storedContentPath
self.contentPath = storedContentPath
self.storage = Storage(baseFolder: URL(filePath: storedContentPath))
do {
try storage.createFolderStructure()
} catch {
print(error)
return
}
observeContentPath()
}
init() {
self.storage = Storage(baseFolder: URL(filePath: ""))
contentPath = storedContentPath
do {
try storage.createFolderStructure()
} catch {
print(error)
return
}
try? storage.update(baseFolder: URL(filePath: contentPath), moveContent: false)
observeContentPath()
}
private func observeContentPath() {
$contentPath.sink { newValue in
let url = URL(filePath: newValue)
try? self.storage.update(baseFolder: url, moveContent: true)
}
.store(in: &cancellables)
}
func generateFeed(for language: ContentLanguage, bookmarkKey: String) {
let posts = posts.map { $0.feedEntry(for: language) }
@ -60,13 +118,6 @@ final class Content: ObservableObject {
}
func importOldContent() {
let storage = Storage(baseFolder: URL(filePath: "/Users/ch/Downloads/Content"))
do {
try storage.createFolderStructure()
} catch {
print(error)
return
}
let importer = Importer()
do {

View File

@ -1,4 +1,5 @@
import Foundation
import SwiftUI
/**
A localized page contains the page content of a single language,
@ -69,4 +70,16 @@ final class LocalizedPage: ObservableObject {
self.externalFiles = externalFiles
self.requiredFiles = requiredFiles
}
@MainActor
func editableTitle() -> Binding<String> {
Binding(
get: {
self.title
},
set: { newValue in
self.title = newValue
}
)
}
}

View File

@ -41,6 +41,7 @@ final class LocalizedPost: ObservableObject {
)
}
@MainActor
func editableContent() -> Binding<String> {
Binding(
get: {

View File

@ -59,7 +59,7 @@ final class Page: ObservableObject {
self.tags = tags
}
func metadata(for language: ContentLanguage) -> LocalizedPage? {
func localized(in language: ContentLanguage) -> LocalizedPage {
switch language {
case .german: return german
case .english: return english

View File

@ -24,6 +24,13 @@ final class Tag: ObservableObject {
var url: String {
"/tags/\(linkName).html"
}
func localized(in language: ContentLanguage) -> LocalizedTag {
switch language {
case .english: return english
case .german: return german
}
}
}
extension Tag {