improvements on the ui
This commit is contained in:
@@ -59,6 +59,27 @@ public struct ImageStore: Sendable {
|
||||
return relativePath(target)
|
||||
}
|
||||
|
||||
/// Deletes the image files in the image folder whose `images/<name>` path is not in `used`.
|
||||
///
|
||||
/// Only regular files directly in the folder go; a symbolic link, a folder or a hidden file stays, and a file
|
||||
/// that cannot be deleted is skipped.
|
||||
/// - Returns: the paths of the deleted files, sorted.
|
||||
@discardableResult
|
||||
public func removeUnused(keeping used: Set<String>) -> [String] {
|
||||
let manager = FileManager.default
|
||||
let keys: Set<URLResourceKey> = [.isRegularFileKey, .isSymbolicLinkKey]
|
||||
guard (try? manager.attributesOfItem(atPath: folder.path)[.type] as? FileAttributeType) == .typeDirectory,
|
||||
let urls = try? manager.contentsOfDirectory(at: folder, includingPropertiesForKeys: Array(keys), options: .skipsHiddenFiles)
|
||||
else { return [] }
|
||||
var removed: [String] = []
|
||||
for url in urls where Self.isImageFile(url) && !used.contains(relativePath(url)) {
|
||||
guard let values = try? url.resourceValues(forKeys: keys), values.isRegularFile == true, values.isSymbolicLink != true,
|
||||
(try? manager.removeItem(at: url)) != nil else { continue }
|
||||
removed.append(relativePath(url))
|
||||
}
|
||||
return removed.sorted()
|
||||
}
|
||||
|
||||
/// The name for an image made now: `2026-10-16-085512-a3f0`.
|
||||
public func name(ext: String) -> String {
|
||||
var calendar = Calendar(identifier: .gregorian)
|
||||
|
||||
@@ -20,7 +20,7 @@ public final class PageStore {
|
||||
.appendingPathComponent("page.md")
|
||||
}
|
||||
|
||||
/// The page file as read at launch; the first page when there was no file.
|
||||
/// The page file as read at launch, after the pruning; the first page when there was no file.
|
||||
public let loaded: PageFile
|
||||
|
||||
/// Makes the page file text when a save runs, so a keystroke costs no conversion; nil writes nothing.
|
||||
@@ -36,12 +36,15 @@ public final class PageStore {
|
||||
@ObservationIgnored private var isDirty = false
|
||||
|
||||
/// Reads the page file; a missing file gives `firstPage`, which the next flush writes unless it is empty.
|
||||
///
|
||||
/// With `pruneAt`, the old and undated blocks are left out and the page file is written at once when one was.
|
||||
/// - Throws: `PageStoreError.unreadable` when the file exists but cannot be read.
|
||||
public init(
|
||||
url: URL = PageStore.defaultURL,
|
||||
debounce: Duration = .milliseconds(500),
|
||||
write: @escaping @Sendable (Data, URL) throws -> Void = PageStore.writeAtomically,
|
||||
firstPage: @autoclosure () -> PageFile = WelcomeText.page(createdAt: Date())
|
||||
firstPage: @autoclosure () -> PageFile = WelcomeText.page(createdAt: Date()),
|
||||
pruneAt now: Date? = nil
|
||||
) throws {
|
||||
self.url = url
|
||||
self.debounce = debounce
|
||||
@@ -50,7 +53,16 @@ public final class PageStore {
|
||||
guard let data = try? Data(contentsOf: url), let text = String(data: data, encoding: .utf8) else {
|
||||
throw PageStoreError.unreadable(path: url.path)
|
||||
}
|
||||
loaded = PageFile(text: text)
|
||||
let read = PageFile(text: text)
|
||||
loaded = now.map { read.pruned(now: $0) } ?? read
|
||||
if loaded != read {
|
||||
do {
|
||||
try write(Data(loaded.text().utf8), url)
|
||||
} catch {
|
||||
saveError = error.localizedDescription
|
||||
isDirty = true
|
||||
}
|
||||
}
|
||||
} else {
|
||||
loaded = firstPage()
|
||||
isDirty = !loaded.lines.isEmpty
|
||||
|
||||
Reference in New Issue
Block a user