Add button to remove a tag

This commit is contained in:
Christoph Hagen
2025-05-04 11:55:54 +02:00
parent a8920a4cd2
commit a4710d525b
4 changed files with 40 additions and 6 deletions

View File

@ -29,6 +29,12 @@ final class SelectedContent: ObservableObject {
} }
} }
func remove(_ tag: Tag) {
if self.tag == tag {
self.tag = nil
}
}
func remove(_ file: FileResource) { func remove(_ file: FileResource) {
if self.file == file { if self.file == file {
self.file = nil self.file = nil

View File

@ -145,6 +145,17 @@ final class Content: ObservableObject {
// TODO: Check for page links and other references in content // TODO: Check for page links and other references in content
} }
func remove(_ tag: Tag) {
tags.remove(tag)
for post in posts {
post.tags.remove(tag)
}
for page in pages {
page.tags.remove(tag)
}
// TODO: Check for tag links and other references is content
}
// MARK: Loading // MARK: Loading
func file(withOutputPath: String) -> FileResource? { func file(withOutputPath: String) -> FileResource? {

View File

@ -151,9 +151,7 @@ final class Storage: ObservableObject {
Completely delete a post file from the content folder Completely delete a post file from the content folder
*/ */
func delete(page pageId: String) -> Bool { func delete(page pageId: String) -> Bool {
guard let contentScope else { guard let contentScope else { return false }
return false
}
guard contentScope.deleteFile(at: pageMetadataPath(page: pageId)) else { guard contentScope.deleteFile(at: pageMetadataPath(page: pageId)) else {
return false return false
} }
@ -213,9 +211,7 @@ final class Storage: ObservableObject {
Completely delete a post file from the content folder Completely delete a post file from the content folder
*/ */
func delete(post postId: String) -> Bool { func delete(post postId: String) -> Bool {
guard let contentScope else { guard let contentScope else { return false }
return false
}
return contentScope.deleteFile(at: postFilePath(post: postId)) return contentScope.deleteFile(at: postFilePath(post: postId))
} }
@ -258,6 +254,11 @@ final class Storage: ObservableObject {
return contentScope.move(tagFilePath(tag: tagId), to: tagFilePath(tag: newId)) return contentScope.move(tagFilePath(tag: tagId), to: tagFilePath(tag: newId))
} }
func delete(tag tagId: String) -> Bool {
guard let contentScope else { return false }
return contentScope.deleteFile(at: tagFilePath(tag: tagId))
}
// MARK: Files // MARK: Files
func size(of file: String) -> Int? { func size(of file: String) -> Int? {

View File

@ -6,6 +6,12 @@ struct TagDetailView: View {
@Environment(\.language) @Environment(\.language)
private var language private var language
@EnvironmentObject
private var content: Content
@EnvironmentObject
private var selection: SelectedContent
@ObservedObject @ObservedObject
var tag: Tag var tag: Tag
@ -37,10 +43,20 @@ struct TagDetailView: View {
tag: tag.localized(in: language), tag: tag.localized(in: language),
transferImage: transferImage) transferImage: transferImage)
.id(tag.id + language.rawValue) .id(tag.id + language.rawValue)
DeleteButton(action: deleteTag)
} }
.padding() .padding()
} }
} }
private func deleteTag() {
guard content.storage.delete(tag: tag.id) else {
print("Tag '\(tag.id)': Failed to delete file in content folder")
return
}
content.remove(tag)
selection.remove(tag)
}
} }
extension TagDetailView: MainContentView { extension TagDetailView: MainContentView {