From ae4c85e97d4b10350ca52e2ebff14d6982c2c633 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Tue, 17 Nov 2020 10:27:57 -0500 Subject: [PATCH] Maintain positions when renaming documents in a filtered listing --- ui/markdown.go | 11 ++++++++++- ui/stash.go | 29 ++++++++++++++++++----------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/ui/markdown.go b/ui/markdown.go index 9f924bf..1e7e33d 100644 --- a/ui/markdown.go +++ b/ui/markdown.go @@ -20,7 +20,16 @@ const ( // markdown wraps charm.Markdown. type markdown struct { markdownType markdownType - localPath string // only relevant to local files and converted files that are newly stashed + + // Full path of a local markdown file. Only relevant to local documents and + // those that have been stashed in this session. + localPath string + + // Value we filter against. This exists so that we can maintain positions + // of filtered items if notes are edited while a filter is active. This + // field is ephemeral, and should only be referenced during filtering. + filterValue string + charm.Markdown } diff --git a/ui/stash.go b/ui/stash.go index f59df57..3f7c615 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -234,17 +234,7 @@ func (m *stashModel) getNotes() []*markdown { targets := []string{} for _, t := range m.markdowns { - note, err := normalize(t.Note) - if err != nil && debug { - log.Printf("error normalizing '%s': %v", t.Note, err) - note = t.Note - } - - if t.markdownType == newsMarkdown { - note = "News: " + note - } - - targets = append(targets, note) + targets = append(targets, t.filterValue) } ranks := fuzzy.Find(m.filterInput.Value(), targets) @@ -494,6 +484,23 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) { case "/": m.hideStatusMessage() + // Build values we'll filter against + for _, md := range m.markdowns { + note, err := normalize(md.Note) + if err != nil { + if debug { + log.Printf("error normalizing '%s': %v", md.Note, err) + } + md.filterValue = md.Note + continue + } + if md.markdownType == newsMarkdown { + md.filterValue = "News: " + note + continue + } + md.filterValue = note + } + m.paginator.Page = 0 m.index = 0 m.state = stashStateFilterNotes