From d6f296c44b35123ccd4a8a7c34409a6f25bdcf6e Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Fri, 21 Aug 2020 16:24:37 -0400 Subject: [PATCH] Load document body before stashing when stashing from the file listing --- ui/pager.go | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/ui/pager.go b/ui/pager.go index 60f7434..f138254 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -3,6 +3,7 @@ package ui import ( "errors" "fmt" + "io/ioutil" "log" "math" "path" @@ -455,32 +456,43 @@ func saveDocumentNote(cc *charm.Client, id int, note string) tea.Cmd { } func stashDocument(cc *charm.Client, md markdown) tea.Cmd { - if cc == nil { - return func() tea.Msg { - err := errors.New("can't stash; no charm client") - if debug { - log.Println("error stash document:", err) - } - return stashErrMsg{err} - } - } - - // Turn local markdown into a stashed markdown - md.markdownType = stashedMarkdown - md.CreatedAt = time.Now() - - // Set the note as the filename without the extension - p := md.localPath - md.Note = strings.Replace(path.Base(p), path.Ext(p), "", 1) - md.localPath = "" - return func() tea.Msg { + if cc == nil { + return func() tea.Msg { + err := errors.New("can't stash; no charm client") + if debug { + log.Println("error stash document:", err) + } + return stashErrMsg{err} + } + } + + // Is the document missing a body? If so, it likely means it needs to + // be loaded. If the document body is really empty then we'll still + // stash it. + if len(md.Body) == 0 { + data, err := ioutil.ReadFile(md.localPath) + if err != nil { + return stashErrMsg{err} + } + md.Body = string(data) + } + + // Turn local markdown into a stashed markdown + md.markdownType = stashedMarkdown + md.CreatedAt = time.Now() + + // Set the note as the filename without the extension + p := md.localPath + md.Note = strings.Replace(path.Base(p), path.Ext(p), "", 1) + md.localPath = "" + newMd, err := cc.StashMarkdown(md.Note, md.Body) if err != nil { if debug { log.Println("error stashing document:", err) } - return errMsg{err} + return stashErrMsg{err} } // We really just need to know the ID so we can operate on this newly