Skip to content

Commit c4068c6

Browse files
committed
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.
1 parent 1e2ffbb commit c4068c6

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

textarea/textarea.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,9 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
12351235
m.Err = msg
12361236
}
12371237

1238+
// Make sure we set the content of the viewport before updating it.
1239+
view := m.view()
1240+
m.viewport.SetContent(view)
12381241
vp, cmd := m.viewport.Update(msg)
12391242
m.viewport = &vp
12401243
cmds = append(cmds, cmd)
@@ -1257,9 +1260,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
12571260
return m, tea.Batch(cmds...)
12581261
}
12591262

1260-
// View renders the text area in its current state.
1261-
func (m Model) View() string {
1262-
if m.Value() == "" && m.row == 0 && m.col == 0 && m.Placeholder != "" {
1263+
func (m *Model) view() string {
1264+
if len(m.Value()) == 0 && m.row == 0 && m.col == 0 && m.Placeholder != "" {
12631265
return m.placeholderView()
12641266
}
12651267
m.virtualCursor.TextStyle = m.activeStyle().computedCursorLine()
@@ -1352,8 +1354,22 @@ func (m Model) View() string {
13521354
s.WriteRune('\n')
13531355
}
13541356

1355-
m.viewport.SetContent(s.String())
1356-
return styles.Base.Render(m.viewport.View())
1357+
return s.String()
1358+
}
1359+
1360+
// View renders the text area in its current state.
1361+
func (m Model) View() string {
1362+
view := strings.TrimSpace(m.viewport.View())
1363+
if view == "" {
1364+
// XXX: This is a workaround for the case where the viewport hasn't
1365+
// been initialized yet like during the initial render. In that case,
1366+
// we need to render the view again because Update hasn't been called
1367+
// yet to set the content of the viewport.
1368+
m.viewport.SetContent(m.view())
1369+
view = m.viewport.View()
1370+
}
1371+
styles := m.activeStyle()
1372+
return styles.Base.Render(view)
13571373
}
13581374

13591375
// promptView renders a single line of the prompt.

0 commit comments

Comments
 (0)