Allow text file editing
This commit is contained in:
parent
4131b483e6
commit
c1a3d22002
@ -17,7 +17,6 @@ import SFSafeSymbols
|
|||||||
- Posts: Generate separate pages for posts to link to
|
- Posts: Generate separate pages for posts to link to
|
||||||
- Settings: Introduce `Authors` (`name`, `image`, `description`)
|
- Settings: Introduce `Authors` (`name`, `image`, `description`)
|
||||||
- Page: Property `author`
|
- Page: Property `author`
|
||||||
- Text file editing
|
|
||||||
- Blocks: Convert more commands to blocks
|
- Blocks: Convert more commands to blocks
|
||||||
- Graphs, Map, GPX for hikes
|
- Graphs, Map, GPX for hikes
|
||||||
|
|
||||||
|
@ -98,6 +98,10 @@ final class FileResource: Item, LocalizedItem {
|
|||||||
content.storage.fileContent(for: id) ?? ""
|
content.storage.fileContent(for: id) ?? ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func save(textContent: String) -> Bool {
|
||||||
|
content.storage.save(fileContent: textContent, for: id)
|
||||||
|
}
|
||||||
|
|
||||||
func dataContent() -> Foundation.Data? {
|
func dataContent() -> Foundation.Data? {
|
||||||
content.storage.fileData(for: id)
|
content.storage.fileData(for: id)
|
||||||
}
|
}
|
||||||
|
@ -71,12 +71,15 @@ struct SecurityBookmark {
|
|||||||
return write(data, to: relativePath)
|
return write(data, to: relativePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func write(_ string: String,
|
/**
|
||||||
|
Write text to a file at the relative path.
|
||||||
|
*/
|
||||||
|
func write(_ content: String,
|
||||||
to relativePath: String,
|
to relativePath: String,
|
||||||
createParentFolder: Bool = true,
|
createParentFolder: Bool = true,
|
||||||
ifFileExists overwrite: OverwriteBehaviour = .writeIfChanged) -> Bool {
|
ifFileExists overwrite: OverwriteBehaviour = .writeIfChanged) -> Bool {
|
||||||
guard let data = string.data(using: .utf8) else {
|
guard let data = content.data(using: .utf8) else {
|
||||||
delegate?.securityBookmark(error: "Failed to encode string to write to \(relativePath)")
|
delegate?.securityBookmark(error: "Failed to encode content to write to \(relativePath)")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return write(data, to: relativePath, createParentFolder: createParentFolder, ifFileExists: overwrite)
|
return write(data, to: relativePath, createParentFolder: createParentFolder, ifFileExists: overwrite)
|
||||||
|
@ -360,6 +360,12 @@ final class Storage: ObservableObject {
|
|||||||
return contentScope.readData(at: path)
|
return contentScope.readData(at: path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func save(fileContent: String, for fileId: String) -> Bool {
|
||||||
|
guard let contentScope else { return false }
|
||||||
|
let path = filePath(file: fileId)
|
||||||
|
return contentScope.write(fileContent, to: path)
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: Settings
|
// MARK: Settings
|
||||||
|
|
||||||
func loadSettings() -> Settings.Data? {
|
func loadSettings() -> Settings.Data? {
|
||||||
|
@ -8,31 +8,65 @@ struct TextFileContentView: View {
|
|||||||
@State
|
@State
|
||||||
private var fileContent: String = ""
|
private var fileContent: String = ""
|
||||||
|
|
||||||
|
@State
|
||||||
|
private var loadedFile: String?
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
if fileContent != "" {
|
VStack {
|
||||||
TextEditor(text: $fileContent)
|
if fileContent != "" {
|
||||||
.font(.body.monospaced())
|
HStack {
|
||||||
.textEditorStyle(.plain)
|
Button("Reload", action: reload)
|
||||||
//.background(.clear)
|
Button("Save", action: save)
|
||||||
} else {
|
Spacer()
|
||||||
VStack {
|
}
|
||||||
|
TextEditor(text: $fileContent)
|
||||||
|
.font(.body.monospaced())
|
||||||
|
.textEditorStyle(.plain)
|
||||||
|
.foregroundStyle(.primary)
|
||||||
|
} else {
|
||||||
Image(systemSymbol: .docText)
|
Image(systemSymbol: .docText)
|
||||||
.resizable()
|
.resizable()
|
||||||
.aspectRatio(contentMode: .fit)
|
.aspectRatio(contentMode: .fit)
|
||||||
.frame(width: 150)
|
.frame(width: 150)
|
||||||
Text("No preview available")
|
Text("No preview available or empty file")
|
||||||
.font(.title)
|
.font(.title)
|
||||||
}
|
}
|
||||||
.foregroundStyle(.secondary)
|
|
||||||
.onAppear(perform: loadFileContent)
|
|
||||||
}
|
}
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.onAppear(perform: loadFileContent)
|
||||||
|
.onDisappear(perform: save)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func loadFileContent() {
|
private func loadFileContent() {
|
||||||
guard fileContent == "" else {
|
guard fileContent == "" else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
reload()
|
||||||
|
}
|
||||||
|
|
||||||
|
private func reload() {
|
||||||
fileContent = file.textContent()
|
fileContent = file.textContent()
|
||||||
|
loadedFile = file.id
|
||||||
print("Loaded content of file \(file.id)")
|
print("Loaded content of file \(file.id)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func save() {
|
||||||
|
guard let loadedFile else {
|
||||||
|
print("[ERROR] Text File View: No file loaded to save")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard loadedFile == file.id else {
|
||||||
|
print("[ERROR] Text File View: Not saving since file changed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard fileContent != "" else {
|
||||||
|
print("Text File View: Not saving empty file \(file.id)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard file.save(textContent: fileContent) else {
|
||||||
|
print("[ERROR] Text File View: Failed to save file \(file.id)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
print("Text File View: Saved file \(file.id)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user