2023-01-02 15:33:12 -05:00
|
|
|
package postcreate
|
2023-01-02 15:29:29 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/charmbracelet/bubbles/textarea"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/mrusme/gobbs/aggregator"
|
|
|
|
"github.com/mrusme/gobbs/ui/ctx"
|
2023-01-03 16:46:16 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/toolkit"
|
2023-01-02 15:29:29 -05:00
|
|
|
)
|
|
|
|
|
2023-01-02 15:33:12 -05:00
|
|
|
var (
|
|
|
|
WIN_ID = "postcreate"
|
|
|
|
)
|
|
|
|
|
2023-01-02 15:29:29 -05:00
|
|
|
type Model struct {
|
2023-01-03 16:46:16 -05:00
|
|
|
ctx *ctx.Ctx
|
|
|
|
tk *toolkit.ToolKit
|
|
|
|
|
|
|
|
xywh [4]int
|
2023-01-03 13:19:21 -05:00
|
|
|
|
2023-01-02 18:46:26 -05:00
|
|
|
textarea textarea.Model
|
2023-01-02 15:29:29 -05:00
|
|
|
|
2023-01-03 13:19:21 -05:00
|
|
|
a *aggregator.Aggregator
|
2023-01-02 15:29:29 -05:00
|
|
|
|
2023-01-02 20:34:39 -05:00
|
|
|
replyToIdx int
|
|
|
|
replyTo string
|
|
|
|
replyToIface interface{}
|
2023-01-02 18:46:26 -05:00
|
|
|
|
|
|
|
viewcache string
|
|
|
|
viewcacheTextareaXY []int
|
2023-01-02 15:29:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Init() tea.Cmd {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewModel(c *ctx.Ctx) Model {
|
|
|
|
m := Model{
|
2023-01-03 16:46:16 -05:00
|
|
|
ctx: c,
|
|
|
|
tk: toolkit.New(
|
|
|
|
WIN_ID,
|
|
|
|
c.Theme,
|
|
|
|
c.Logger,
|
|
|
|
),
|
|
|
|
xywh: [4]int{0, 0, 0, 0},
|
2023-01-02 18:46:26 -05:00
|
|
|
|
2023-01-02 20:34:39 -05:00
|
|
|
replyToIdx: 0,
|
|
|
|
replyTo: "",
|
|
|
|
replyToIface: nil,
|
2023-01-02 18:46:26 -05:00
|
|
|
|
|
|
|
viewcache: "",
|
|
|
|
viewcacheTextareaXY: []int{0, 0, 0, 0},
|
2023-01-02 15:29:29 -05:00
|
|
|
}
|
|
|
|
|
2023-01-02 18:46:26 -05:00
|
|
|
m.textarea = textarea.New()
|
|
|
|
m.textarea.Placeholder = "Type in your reply ..."
|
|
|
|
m.textarea.Prompt = ""
|
|
|
|
|
2023-01-03 16:46:16 -05:00
|
|
|
m.tk.KeymapAdd("submit", "submit", "ctrl+s")
|
|
|
|
|
2023-01-02 15:29:29 -05:00
|
|
|
m.a, _ = aggregator.New(m.ctx)
|
|
|
|
|
2023-01-03 16:46:16 -05:00
|
|
|
m.tk.SetViewFunc(buildView)
|
|
|
|
m.tk.SetMsgHandling(toolkit.MsgHandling{
|
|
|
|
OnKeymapKey: []toolkit.MsgHandlingKeymapKey{
|
|
|
|
{
|
|
|
|
ID: "submit",
|
|
|
|
Handler: handleSubmit,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
OnWinOpenCmd: handleWinOpenCmd,
|
|
|
|
OnWinCloseCmd: handleWinCloseCmd,
|
|
|
|
})
|
|
|
|
|
2023-01-02 15:29:29 -05:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
2023-01-03 16:46:16 -05:00
|
|
|
ret, cmds := m.tk.HandleMsg(&m, msg)
|
|
|
|
if ret {
|
|
|
|
return m, tea.Batch(cmds...)
|
2023-01-02 15:29:29 -05:00
|
|
|
}
|
|
|
|
|
2023-01-02 18:46:26 -05:00
|
|
|
var tcmd tea.Cmd
|
2023-01-02 15:29:29 -05:00
|
|
|
|
2023-01-02 18:46:26 -05:00
|
|
|
if !m.textarea.Focused() {
|
|
|
|
cmds = append(cmds, m.textarea.Focus())
|
|
|
|
}
|
|
|
|
m.textarea, tcmd = m.textarea.Update(msg)
|
|
|
|
cmds = append(cmds, tcmd)
|
2023-01-02 15:29:29 -05:00
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|