Skip to content

Commit 532816e

Browse files
committed
feat: use filenames as editable note titles
1 parent 6f5be2e commit 532816e

13 files changed

Lines changed: 76 additions & 19 deletions

File tree

‎EdgeMark/Core/Storage/FileStorage.swift‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,22 @@ enum FileStorage {
372372
return result.isEmpty ? "Untitled" : result
373373
}
374374

375+
/// Converts an editable title into one literal Markdown filename. Unlike the
376+
/// legacy sanitizer, this preserves spaces and punctuation and rejects unsafe
377+
/// path components instead of silently changing them.
378+
static func filename(forTitle title: String) -> String? {
379+
let trimmed = title.trimmingCharacters(in: .whitespacesAndNewlines)
380+
guard !trimmed.isEmpty,
381+
trimmed != ".",
382+
trimmed != "..",
383+
!trimmed.contains("/"),
384+
!trimmed.contains(":"),
385+
!trimmed.unicodeScalars.contains(where: CharacterSet.controlCharacters.contains),
386+
Data("\(trimmed).md".utf8).count <= 255
387+
else { return nil }
388+
return "\(trimmed).md"
389+
}
390+
375391
// MARK: - Note I/O
376392

377393
static func loadAllNotes() throws -> [Note] {

‎EdgeMark/Core/Storage/NoteStore.swift‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,32 @@ final class NoteStore {
483483

484484
// MARK: - Duplicate Detection
485485

486+
func isValidNoteTitle(_ title: String) -> Bool {
487+
FileStorage.filename(forTitle: title) != nil
488+
}
489+
486490
/// Whether a note title already exists in the given folder (case-insensitive filename match).
487491
func noteTitleExists(_ title: String, in folder: String, excluding noteID: UUID? = nil) -> Bool {
488-
let filename = "\(title).md"
489-
return notes.contains { note in
492+
guard let filename = FileStorage.filename(forTitle: title) else { return false }
493+
let collidesInMemory = notes.contains { note in
490494
note.id != noteID
491495
&& note.folder == folder
492496
&& note.filename.caseInsensitiveCompare(filename) == .orderedSame
493497
}
498+
if collidesInMemory {
499+
return true
500+
}
501+
if let noteID,
502+
let current = notes.first(where: { $0.id == noteID }),
503+
current.folder == folder,
504+
current.filename.caseInsensitiveCompare(filename) == .orderedSame
505+
{
506+
return false
507+
}
508+
let destination = folder.isEmpty
509+
? FileStorage.rootURL.appendingPathComponent(filename)
510+
: FileStorage.rootURL.appendingPathComponent(folder).appendingPathComponent(filename)
511+
return FileManager.default.fileExists(atPath: destination.path)
494512
}
495513

496514
/// Checks loaded notes and the destination directory for a filename collision.
@@ -557,8 +575,8 @@ final class NoteStore {
557575
let now = Date()
558576
var note = Note(
559577
id: UUID(),
560-
filename: "\(FileStorage.sanitizeForFilename(title)).md",
561-
content: "# \(title)\n\n",
578+
filename: "\(title).md",
579+
content: "",
562580
createdAt: now,
563581
modifiedAt: now,
564582
folder: folder,
@@ -596,13 +614,13 @@ final class NoteStore {
596614
}
597615
}
598616

599-
func renameNote(_ note: Note, to newTitle: String) {
600-
guard let index = notes.firstIndex(where: { $0.id == note.id }) else { return }
617+
@discardableResult
618+
func renameNote(_ note: Note, to newTitle: String) -> Bool {
619+
guard let index = notes.firstIndex(where: { $0.id == note.id }) else { return false }
601620
let trimmed = newTitle.trimmingCharacters(in: .whitespacesAndNewlines)
602-
guard !trimmed.isEmpty else { return }
603-
guard !noteTitleExists(trimmed, in: note.folder, excluding: note.id) else { return }
621+
guard let newFilename = FileStorage.filename(forTitle: trimmed) else { return false }
622+
guard !noteTitleExists(trimmed, in: note.folder, excluding: note.id) else { return false }
604623

605-
let newFilename = "\(trimmed).md"
606624
var renamedNote = notes[index]
607625
renamedNote.modifiedAt = Date()
608626
do {
@@ -622,8 +640,10 @@ final class NoteStore {
622640
selectedNote = notes[index]
623641
}
624642
Log.storage.info("[NoteMetadata] mutation op=rename noteID=\(note.id.uuidString, privacy: .public) committed=true")
643+
return true
625644
} catch {
626645
Log.storage.error("[NoteMetadata] mutation op=rename noteID=\(note.id.uuidString, privacy: .public) committed=false error=\(error)")
646+
return false
627647
}
628648
}
629649

@@ -976,11 +996,11 @@ final class NoteStore {
976996
let baseTitle = notes[index].title
977997
var counter = 2
978998
var newTitle = "\(baseTitle) \(counter)"
979-
var newFilename = "\(FileStorage.sanitizeForFilename(newTitle)).md"
999+
var newFilename = FileStorage.filename(forTitle: newTitle) ?? "Untitled 2.md"
9801000
while noteFilenameWouldCollide(newFilename, in: folder, excluding: conflict.noteID) {
9811001
counter += 1
9821002
newTitle = "\(baseTitle) \(counter)"
983-
newFilename = "\(FileStorage.sanitizeForFilename(newTitle)).md"
1003+
newFilename = FileStorage.filename(forTitle: newTitle) ?? "Untitled \(counter).md"
9841004
}
9851005
notes[index].filename = newFilename
9861006
notes[index].modifiedAt = Date()

‎EdgeMark/Resources/Locales/de.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"tags.clearFilter": "Tag-Filter löschen",
3939
"common.moveHere": "Hierher verschieben",
4040
"common.nameTaken": "Name vergeben",
41+
"common.invalidName": "Ungültiger Name",
4142
"common.copyPlainText": "Als Klartext kopieren",
4243
"common.copyMarkdown": "Als Markdown kopieren",
4344
"common.copyRTF": "Als Rich Text kopieren",

‎EdgeMark/Resources/Locales/en.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"tags.clearFilter": "Clear tag filter",
3939
"common.moveHere": "Move here",
4040
"common.nameTaken": "Name taken",
41+
"common.invalidName": "Invalid name",
4142
"common.copyPlainText": "Copy as Plain Text",
4243
"common.copyMarkdown": "Copy as Markdown",
4344
"common.copyRTF": "Copy as Rich Text",

‎EdgeMark/Resources/Locales/es.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"tags.clearFilter": "Limpiar filtro de etiquetas",
3939
"common.moveHere": "Mover aquí",
4040
"common.nameTaken": "Nombre ya en uso",
41+
"common.invalidName": "Nombre no válido",
4142
"common.copyPlainText": "Copiar como texto plano",
4243
"common.copyMarkdown": "Copiar como Markdown",
4344
"common.copyRTF": "Copiar como texto enriquecido",

‎EdgeMark/Resources/Locales/hi.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"tags.clearFilter": "टैग फ़िल्टर हटाएं",
3939
"common.moveHere": "यहां ले जाएं",
4040
"common.nameTaken": "नाम पहले से मौजूद है",
41+
"common.invalidName": "अमान्य नाम",
4142
"common.copyPlainText": "सादा टेक्स्ट के रूप में कॉपी करें",
4243
"common.copyMarkdown": "Markdown के रूप में कॉपी करें",
4344
"common.copyRTF": "रिच टेक्स्ट के रूप में कॉपी करें",

‎EdgeMark/Resources/Locales/zh-Hans.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"tags.clearFilter": "清除标签筛选",
3939
"common.moveHere": "移动到此处",
4040
"common.nameTaken": "名称已存在",
41+
"common.invalidName": "名称无效",
4142
"common.copyPlainText": "复制为纯文本",
4243
"common.copyMarkdown": "复制为 Markdown",
4344
"common.copyRTF": "复制为富文本",

‎EdgeMark/UI/Components/InlineRenameEditor.swift‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct InlineRenameEditor: View {
99
@Binding var text: String
1010
var isFocused: FocusState<Bool>.Binding
1111
let isConflicting: Bool
12+
var validationMessage: String?
1213
let iconWidth: CGFloat
1314
let onCommit: () -> Void
1415
let onCancel: () -> Void
@@ -27,7 +28,7 @@ struct InlineRenameEditor: View {
2728
.focused(isFocused)
2829
.onSubmit(onCommit)
2930
.overlay(alignment: .trailing) {
30-
Text(L10n.shared["common.nameTaken"])
31+
Text(validationMessage ?? L10n.shared["common.nameTaken"])
3132
.font(.caption2)
3233
.foregroundStyle(.red)
3334
.padding(.horizontal, 6)

‎EdgeMark/UI/Components/NoteHeaderTitle.swift‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct NoteHeaderTitle: View {
1313
var isFocused: FocusState<Bool>.Binding
1414
let isEditing: Bool
1515
let isConflicting: Bool
16+
var validationMessage: String?
1617
let onBeginEditing: () -> Void
1718
let onCommit: () -> Void
1819
let onCancel: () -> Void
@@ -37,7 +38,7 @@ struct NoteHeaderTitle: View {
3738
}
3839
}
3940
.overlay(alignment: .trailing) {
40-
Text(L10n.shared["common.nameTaken"])
41+
Text(validationMessage ?? L10n.shared["common.nameTaken"])
4142
.font(.caption2)
4243
.foregroundStyle(.red)
4344
.padding(.horizontal, 6)

‎EdgeMark/UI/Components/NoteRenameCoordinator.swift‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,22 @@ final class NoteRenameCoordinator {
3131
return noteStore.noteTitleExists(trimmed, in: folder, excluding: noteID)
3232
}
3333

34+
func validationMessage(in noteStore: NoteStore) -> String? {
35+
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
36+
guard !trimmed.isEmpty else { return nil }
37+
guard noteStore.isValidNoteTitle(trimmed) else {
38+
return L10n.shared["common.invalidName"]
39+
}
40+
return isConflicting(in: noteStore) ? L10n.shared["common.nameTaken"] : nil
41+
}
42+
3443
// MARK: - Commit / Cancel
3544

3645
func commit(note: Note, noteStore: NoteStore) {
37-
guard !isConflicting(in: noteStore) else { return }
46+
guard validationMessage(in: noteStore) == nil else { return }
3847
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
3948
if !trimmed.isEmpty, trimmed != note.title {
40-
noteStore.renameNote(note, to: trimmed)
49+
guard noteStore.renameNote(note, to: trimmed) else { return }
4150
}
4251
let isNew = newlyCreatedNoteID == note.id
4352
clear()
@@ -56,8 +65,10 @@ final class NoteRenameCoordinator {
5665
func commitOrCancel(note: Note, noteStore: NoteStore) {
5766
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
5867
let isNew = newlyCreatedNoteID == note.id
59-
if (trimmed.isEmpty && !isNew) || isConflicting(in: noteStore) {
68+
if trimmed.isEmpty, !isNew {
6069
cancel(noteStore: noteStore)
70+
} else if validationMessage(in: noteStore) != nil {
71+
return
6172
} else {
6273
commit(note: note, noteStore: noteStore)
6374
}

0 commit comments

Comments
 (0)