1
0

update.go 934 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package editor
  2. import (
  3. "charm.land/bubbles/v2/key"
  4. tea "charm.land/bubbletea/v2"
  5. )
  6. func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  7. switch msg := msg.(type) {
  8. case tea.WindowSizeMsg:
  9. m.Width = msg.Width
  10. m.Height = msg.Height
  11. m.Viewport.SetWidth(msg.Width)
  12. h := msg.Height - 2
  13. if h < 0 {
  14. h = 0
  15. }
  16. m.Viewport.SetHeight(h)
  17. return m, nil
  18. case tea.MouseWheelMsg:
  19. return m, nil
  20. case tea.KeyPressMsg:
  21. found := false
  22. for i := range m.Actions {
  23. action := m.Actions[i]
  24. if key.Matches(msg, action.Binding) {
  25. found = true
  26. cmd := action.Callback(m, []string{})
  27. if cmd != nil {
  28. m.Viewport.SetContent(m.renderedContent())
  29. return m, cmd
  30. }
  31. break
  32. }
  33. }
  34. if !found {
  35. key := msg.Key()
  36. if key.Text != "" {
  37. for _, r := range key.Text {
  38. m.Buffer.Insert(r)
  39. }
  40. }
  41. }
  42. m.Viewport.SetContent(m.renderedContent())
  43. return m, nil
  44. }
  45. return m, nil
  46. }