chore(jotts): added public/private support to TUI
529d9f4d
6 file(s) · +70 −15
| 13 | 13 | } |
|
| 14 | 14 | } |
|
| 15 | 15 | ||
| 16 | - | func saveNoteCmd(b Backend, shortID, title, content string) tea.Cmd { |
|
| 16 | + | func saveNoteCmd(b Backend, shortID, title, content string, public *bool) tea.Cmd { |
|
| 17 | 17 | return func() tea.Msg { |
|
| 18 | 18 | var ( |
|
| 19 | 19 | note *Note |
|
| 22 | 22 | if shortID == "" { |
|
| 23 | 23 | note, err = b.Create(title, content) |
|
| 24 | 24 | } else { |
|
| 25 | - | public := false |
|
| 26 | - | note, err = b.Update(shortID, title, content, &public) |
|
| 25 | + | note, err = b.Update(shortID, title, content, public) |
|
| 27 | 26 | } |
|
| 28 | 27 | return noteSavedMsg{Note: note, Err: err} |
|
| 29 | 28 | } |
|
| 33 | 32 | return func() tea.Msg { |
|
| 34 | 33 | _, err := b.Delete(shortID) |
|
| 35 | 34 | return noteDeletedMsg{ShortID: shortID, Err: err} |
|
| 35 | + | } |
|
| 36 | + | } |
|
| 37 | + | ||
| 38 | + | func togglePublicCmd(b Backend, n Note) tea.Cmd { |
|
| 39 | + | target := !n.Public |
|
| 40 | + | status := "private" |
|
| 41 | + | if target { |
|
| 42 | + | status = "public" |
|
| 43 | + | } |
|
| 44 | + | return func() tea.Msg { |
|
| 45 | + | note, err := b.Update(n.ShortID, n.Title, n.Content, &target) |
|
| 46 | + | return noteSavedMsg{Note: note, Err: err, Status: status} |
|
| 36 | 47 | } |
|
| 37 | 48 | } |
|
| 38 | 49 | ||
| 13 | 13 | shortID string |
|
| 14 | 14 | title string |
|
| 15 | 15 | body string |
|
| 16 | + | public bool |
|
| 16 | 17 | } |
|
| 17 | 18 | ||
| 18 | 19 | func newContentModel() contentModel { |
|
| 38 | 39 | ||
| 39 | 40 | func (c *contentModel) SetNote(n *Note) { |
|
| 40 | 41 | if n == nil { |
|
| 41 | - | c.shortID, c.title, c.body = "", "", "" |
|
| 42 | + | c.shortID, c.title, c.body, c.public = "", "", "", false |
|
| 42 | 43 | c.vp.SetContent("") |
|
| 43 | 44 | c.vp.GotoTop() |
|
| 44 | 45 | return |
|
| 46 | 47 | c.shortID = n.ShortID |
|
| 47 | 48 | c.title = n.Title |
|
| 48 | 49 | c.body = n.Content |
|
| 50 | + | c.public = n.Public |
|
| 49 | 51 | c.vp.GotoTop() |
|
| 50 | 52 | c.refresh() |
|
| 51 | 53 | } |
|
| 75 | 77 | c.vp.SetContent(c.renderer.render(c.shortID, c.body)) |
|
| 76 | 78 | } |
|
| 77 | 79 | ||
| 78 | - | func (c contentModel) Title() string { return c.title } |
|
| 79 | - | func (c contentModel) View() string { return c.vp.View() } |
|
| 80 | + | func (c contentModel) Title() string { return c.title } |
|
| 81 | + | func (c contentModel) Public() bool { return c.public } |
|
| 82 | + | func (c contentModel) View() string { return c.vp.View() } |
|
| 80 | 83 | ||
| 81 | 84 | func (c contentModel) ScrollUp(n int) contentModel { |
|
| 82 | 85 | c.vp.ScrollUp(n) |
|
| 1 | 1 | package tui |
|
| 2 | 2 | ||
| 3 | + | import "charm.land/bubbles/v2/key" |
|
| 4 | + | ||
| 3 | 5 | import sharedtui "github.com/stevedylandev/andromeda/pkg/tui" |
|
| 4 | 6 | ||
| 5 | 7 | type keyMap = sharedtui.KeyMap |
|
| 6 | 8 | ||
| 7 | 9 | func defaultKeys() keyMap { return sharedtui.DefaultKeys() } |
|
| 10 | + | ||
| 11 | + | // togglePublic is jotts-specific: publish/unpublish the selected note. |
|
| 12 | + | // It lives outside the shared key map because sipp and blobs have no |
|
| 13 | + | // concept of public notes. |
|
| 14 | + | var togglePublicKey = key.NewBinding(key.WithKeys("P"), key.WithHelp("P", "public")) |
|
| 15 | + | ||
| 16 | + | // fullHelp is the shared help grid with jotts's public toggle appended |
|
| 17 | + | // to the viewing actions column. |
|
| 18 | + | func fullHelp(k keyMap) [][]key.Binding { |
|
| 19 | + | cols := k.FullHelp() |
|
| 20 | + | if len(cols) > 2 { |
|
| 21 | + | cols[2] = append(cols[2], togglePublicKey) |
|
| 22 | + | return cols |
|
| 23 | + | } |
|
| 24 | + | return append(cols, []key.Binding{togglePublicKey}) |
|
| 25 | + | } |
| 8 | 8 | } |
|
| 9 | 9 | ||
| 10 | 10 | type noteSavedMsg struct { |
|
| 11 | - | Note *Note |
|
| 12 | - | Err error |
|
| 11 | + | Note *Note |
|
| 12 | + | Err error |
|
| 13 | + | Status string // optional status text; defaults to "saved" |
|
| 13 | 14 | } |
|
| 14 | 15 | ||
| 15 | 16 | type noteDeletedMsg struct { |
| 39 | 39 | } |
|
| 40 | 40 | m.state = stateList |
|
| 41 | 41 | m.form.Blur() |
|
| 42 | - | return m, tea.Batch(loadNotesCmd(m.backend), m.setStatus("saved", true)) |
|
| 42 | + | status := "saved" |
|
| 43 | + | if msg.Status != "" { |
|
| 44 | + | status = msg.Status |
|
| 45 | + | } |
|
| 46 | + | return m, tea.Batch(loadNotesCmd(m.backend), m.setStatus(status, true)) |
|
| 43 | 47 | ||
| 44 | 48 | case noteDeletedMsg: |
|
| 45 | 49 | if msg.Err != nil { |
|
| 69 | 73 | if orig == nil || strings.TrimRight(orig.Content, "\n") == strings.TrimRight(msg.Content, "\n") { |
|
| 70 | 74 | return m, nil |
|
| 71 | 75 | } |
|
| 72 | - | return m, saveNoteCmd(m.backend, msg.Tag, orig.Title, msg.Content) |
|
| 76 | + | // Keep the note's current visibility untouched. |
|
| 77 | + | var public *bool |
|
| 78 | + | return m, saveNoteCmd(m.backend, msg.Tag, orig.Title, msg.Content, public) |
|
| 73 | 79 | ||
| 74 | 80 | case submitFormMsg: |
|
| 75 | - | return m, saveNoteCmd(m.backend, msg.ShortID, msg.Title, msg.Content) |
|
| 81 | + | // Keep the note's current visibility untouched. |
|
| 82 | + | var public *bool |
|
| 83 | + | return m, saveNoteCmd(m.backend, msg.ShortID, msg.Title, msg.Content, public) |
|
| 76 | 84 | ||
| 77 | 85 | case cancelFormMsg: |
|
| 78 | 86 | m.state = stateList |
|
| 167 | 175 | return m, openExternalEditor(n.ShortID, n.Content) |
|
| 168 | 176 | } |
|
| 169 | 177 | return m, nil |
|
| 178 | + | case key.Matches(msg, togglePublicKey): |
|
| 179 | + | if n, ok := m.list.Selected(); ok { |
|
| 180 | + | return m, togglePublicCmd(m.backend, n) |
|
| 181 | + | } |
|
| 182 | + | return m, nil |
|
| 170 | 183 | case key.Matches(msg, m.keys.Delete): |
|
| 171 | 184 | if _, ok := m.list.Selected(); ok { |
|
| 172 | 185 | m.confirmDelete = true |
|
| 229 | 242 | case key.Matches(msg, m.keys.ExtEdit): |
|
| 230 | 243 | if n, ok := m.list.Selected(); ok { |
|
| 231 | 244 | return m, openExternalEditor(n.ShortID, n.Content) |
|
| 245 | + | } |
|
| 246 | + | return m, nil |
|
| 247 | + | case key.Matches(msg, togglePublicKey): |
|
| 248 | + | if n, ok := m.list.Selected(); ok { |
|
| 249 | + | return m, togglePublicCmd(m.backend, n) |
|
| 232 | 250 | } |
|
| 233 | 251 | return m, nil |
|
| 234 | 252 | case key.Matches(msg, m.keys.Copy): |
|
| 33 | 33 | var overlays []*lipgloss.Layer |
|
| 34 | 34 | if m.showHelp { |
|
| 35 | 35 | overlays = append(overlays, centerLayer(m.width, m.height, |
|
| 36 | - | modalStyle.Render(m.help.FullHelpView(m.keys.FullHelp())), 1)) |
|
| 36 | + | modalStyle.Render(m.help.FullHelpView(fullHelp(m.keys))), 1)) |
|
| 37 | 37 | } |
|
| 38 | 38 | if m.confirmDelete { |
|
| 39 | 39 | title := "" |
|
| 83 | 83 | if m.state == stateContent { |
|
| 84 | 84 | style = borderActive |
|
| 85 | 85 | } |
|
| 86 | - | header := "preview" |
|
| 86 | + | titleLine := titleStyle.Render("preview") |
|
| 87 | 87 | if t := m.cont.Title(); t != "" { |
|
| 88 | - | header = t |
|
| 88 | + | vis := hintStyle.Render("private") |
|
| 89 | + | if m.cont.Public() { |
|
| 90 | + | vis = statusOKStyle.Render("public") |
|
| 91 | + | } |
|
| 92 | + | titleLine = titleStyle.Render(t) + " " + vis |
|
| 89 | 93 | } |
|
| 90 | - | inner := lipgloss.JoinVertical(lipgloss.Left, titleStyle.Render(header), m.cont.View()) |
|
| 94 | + | inner := lipgloss.JoinVertical(lipgloss.Left, titleLine, m.cont.View()) |
|
| 91 | 95 | return style.Width(w).Height(h).Render(inner) |
|
| 92 | 96 | } |
|
| 93 | 97 | ||