diff --git a/Simplenote/NSTableView+Simplenote.swift b/Simplenote/NSTableView+Simplenote.swift index 0e9c9bee..202dceb9 100644 --- a/Simplenote/NSTableView+Simplenote.swift +++ b/Simplenote/NSTableView+Simplenote.swift @@ -76,3 +76,13 @@ extension NSTableView { } } } + +extension NSTableView { + static var currentRowSelectionUserInfoKey: String { + "NSTableViewCurrentRowSelectionUserInfoKey" + } + + static var previousRowSelectionUserInfoKey: String { + "NSTableViewPreviousRowSelectionUserInfoKey" + } +} diff --git a/Simplenote/NoteEditorViewController+Swift.swift b/Simplenote/NoteEditorViewController+Swift.swift index bdc148cb..b6968bca 100644 --- a/Simplenote/NoteEditorViewController+Swift.swift +++ b/Simplenote/NoteEditorViewController+Swift.swift @@ -1,4 +1,5 @@ import Foundation +import CoreSpotlight import SimplenoteFoundation import SimplenoteInterlinks import SimplenoteSearch @@ -74,6 +75,24 @@ extension NoteEditorViewController { clipView.contentInsets.top = SplitItemMetrics.editorContentTopInset scrollView.scrollerInsets.top = SplitItemMetrics.editorScrollerTopInset } + + public func deleteCurrentNoteIfEmpty() { + guard let note, + note.content == nil || note.content?.isEmpty == true else { + return + } + + delete(note: note) + } + + @objc(deleteNote:) + func delete(note noteToDelete: Note) { + + SPTracker.trackEditorNoteDeleted() + noteToDelete.deleted = true + noteActionsDelegate?.editorController(self, deletedNoteWithSimperiumKey: noteToDelete.simperiumKey) + CSSearchableIndex.default().deleteSearchableNote(noteToDelete) + } } // MARK: - Public diff --git a/Simplenote/NoteEditorViewController.m b/Simplenote/NoteEditorViewController.m index 6df29050..8f2d94a4 100644 --- a/Simplenote/NoteEditorViewController.m +++ b/Simplenote/NoteEditorViewController.m @@ -402,11 +402,8 @@ - (IBAction)deleteAction:(id)sender if (noteToDelete.deleted) { continue; } - - [SPTracker trackEditorNoteDeleted]; - noteToDelete.deleted = YES; - [self.noteActionsDelegate editorController:self deletedNoteWithSimperiumKey:noteToDelete.simperiumKey]; - [[CSSearchableIndex defaultSearchableIndex] deleteSearchableNote:noteToDelete]; + + [self deleteNote:noteToDelete]; } [self save]; diff --git a/Simplenote/NoteListViewController.swift b/Simplenote/NoteListViewController.swift index 53d40af6..db1e9de4 100644 --- a/Simplenote/NoteListViewController.swift +++ b/Simplenote/NoteListViewController.swift @@ -562,9 +562,42 @@ extension NoteListViewController: SPTableViewDelegate { } DispatchQueue.main.async { + self.deletePreviouslySelectedNoteIfEmpty(notification: notification) self.refreshPresentedNote() } } + + private func deletePreviouslySelectedNoteIfEmpty(notification: Notification) { + guard shouldCheckForEmptyNote(notification: notification), + let previousSelectedIndex = previousSelectedIndex(from: notification), + let previouslySelectedNote = listController.note(at: previousSelectedIndex), + shouldDeleteNoteAsEmpty(note: previouslySelectedNote) else { + return + } + + delete(previouslySelectedNote) + } + + private func shouldDeleteNoteAsEmpty(note: Note) -> Bool { + note.content == nil || note.content?.isEmpty == true + } + + private func shouldCheckForEmptyNote(notification: Notification) -> Bool { + guard let currentRowSelection = notification.userInfo?[NSTableView.currentRowSelectionUserInfoKey] as? NSIndexSet else { + return false + } + + return currentRowSelection.count == 1 + } + + private func previousSelectedIndex(from notification: Notification) -> Int? { + guard let previousSelectedIndexSet = notification.userInfo?[NSTableView.previousRowSelectionUserInfoKey] as? NSIndexSet, + previousSelectedIndexSet.count == 1 else { + return nil + } + + return previousSelectedIndexSet.firstIndex + } } // MARK: - NSTableViewDataSource @@ -812,14 +845,18 @@ extension NoteListViewController { @IBAction func deleteAction(_ sender: Any) { for note in selectedNotes { - SPTracker.trackListNoteDeleted() - note.deleted = true - CSSearchableIndex.default().deleteSearchableNote(note) + delete(note) } simperium.save() } + func delete(_ note: Note) { + SPTracker.trackListNoteDeleted() + note.deleted = true + CSSearchableIndex.default().deleteSearchableNote(note) + } + @IBAction func deleteFromTrashWasPressed(_ sender: Any) { guard let note = selectedNotes.first else { diff --git a/Simplenote/NoteWindow.swift b/Simplenote/NoteWindow.swift index 4cf5532c..0f678ad3 100644 --- a/Simplenote/NoteWindow.swift +++ b/Simplenote/NoteWindow.swift @@ -42,4 +42,9 @@ class NoteWindow: NSWindow { editor.displayNote(note) title = note.titlePreview } + + override func close() { + super.close() + editor.deleteCurrentNoteIfEmpty() + } }