feat: delete the selected note
This commit is contained in:
@@ -76,6 +76,7 @@ final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
actions.selectNote = { [weak self] in self?.switchNote($0) }
|
||||
actions.newNote = { [weak self] in self?.newNote() }
|
||||
actions.browseNotes = { [weak self] in self?.browseNotes() }
|
||||
actions.deleteNote = { [weak self] in self?.deleteNote() }
|
||||
let tools = [
|
||||
("textformat.size", "Heading"), ("bold", "Bold"), ("italic", "Italic"),
|
||||
("strikethrough", "Strikethrough"), ("chevron.left.forwardslash.chevron.right", "Inline code"),
|
||||
@@ -509,6 +510,33 @@ final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
catch { showError(error) }
|
||||
}
|
||||
|
||||
private func deleteNote() {
|
||||
guard let window = view.window, window.attachedSheet == nil, let id = selectedID else { return }
|
||||
actions.close(animated: false)
|
||||
let alert = NSAlert()
|
||||
alert.alertStyle = .warning
|
||||
alert.messageText = "Delete “\(noteTitle.stringValue)”?"
|
||||
alert.informativeText = "This permanently deletes the note and its embedded images. This cannot be undone."
|
||||
alert.addButton(withTitle: "Cancel")
|
||||
alert.addButton(withTitle: "Delete Note").hasDestructiveAction = true
|
||||
alert.beginSheetModal(for: window) { [weak self] response in
|
||||
guard let self else { return }
|
||||
defer { self.focusEditor() }
|
||||
guard response == .alertSecondButtonReturn, self.selectedID == id,
|
||||
self.save(), let store = self.store else { return }
|
||||
do {
|
||||
try store.delete(id: id)
|
||||
self.selectedID = nil
|
||||
self.defaults.removeObject(forKey: "selectedNote")
|
||||
self.editor.setMarkdownContents(NSAttributedString(string: ""))
|
||||
self.editor.undoManager?.removeAllActions()
|
||||
self.editor.isEditable = false
|
||||
self.noteTitle.stringValue = "Untitled Note"
|
||||
self.select(try store.notes.first?.id ?? store.create())
|
||||
} catch { self.showError(error) }
|
||||
}
|
||||
}
|
||||
|
||||
private func switchNote(_ id: UUID) {
|
||||
guard save() else { return }
|
||||
select(id)
|
||||
@@ -532,7 +560,7 @@ final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
|
||||
private func showError(_ error: Error) {
|
||||
status.isHidden = false
|
||||
status.stringValue = "Couldn’t save or load notes: \(error.localizedDescription)"
|
||||
status.stringValue = "Couldn’t save, load, or delete notes: \(error.localizedDescription)"
|
||||
status.toolTip = status.stringValue
|
||||
status.textColor = .systemRed
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user