fix(textarea): ensure viewport content is set during update

This ensures that the viewport content is set and persisted correctly.
Setting the content during View won't doesn't work because it mutates a
copy of the model instead without persisting the changes. Moreover, any
after effects of setting the content won't be visible because the next
Update will be called on the older model, not the new one.
This commit is contained in:
Ayman Bagabas
2025-08-01 12:22:10 -04:00
parent 1e2ffbbcf5
commit c4068c642c
+21 -5
View File
@@ -1235,6 +1235,9 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.Err = msg
}
// Make sure we set the content of the viewport before updating it.
view := m.view()
m.viewport.SetContent(view)
vp, cmd := m.viewport.Update(msg)
m.viewport = &vp
cmds = append(cmds, cmd)
@@ -1257,9 +1260,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}
// View renders the text area in its current state.
func (m Model) View() string {
if m.Value() == "" && m.row == 0 && m.col == 0 && m.Placeholder != "" {
func (m *Model) view() string {
if len(m.Value()) == 0 && m.row == 0 && m.col == 0 && m.Placeholder != "" {
return m.placeholderView()
}
m.virtualCursor.TextStyle = m.activeStyle().computedCursorLine()
@@ -1352,8 +1354,22 @@ func (m Model) View() string {
s.WriteRune('\n')
}
m.viewport.SetContent(s.String())
return styles.Base.Render(m.viewport.View())
return s.String()
}
// View renders the text area in its current state.
func (m Model) View() string {
view := strings.TrimSpace(m.viewport.View())
if view == "" {
// XXX: This is a workaround for the case where the viewport hasn't
// been initialized yet like during the initial render. In that case,
// we need to render the view again because Update hasn't been called
// yet to set the content of the viewport.
m.viewport.SetContent(m.view())
view = m.viewport.View()
}
styles := m.activeStyle()
return styles.Base.Render(view)
}
// promptView renders a single line of the prompt.