aboutsummaryrefslogtreecommitdiff
path: root/ui
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2020-02-19 21:27:37 +0200
committerTulir Asokan <tulir@maunium.net>2020-02-19 21:27:37 +0200
commit0b0a40f7353107b4bdd5fe799b8fe20bf8eb90ff (patch)
tree81abe20fce39699775f9ca14d5482784bc40ad14 /ui
parent8b02183ab198ec16e1c6843280cdd6327bd86bac (diff)
Remove duplicate code for finding message to edit
Diffstat (limited to 'ui')
-rw-r--r--ui/room-view.go48
1 files changed, 23 insertions, 25 deletions
diff --git a/ui/room-view.go b/ui/room-view.go
index 27c42c8..8b88b99 100644
--- a/ui/room-view.go
+++ b/ui/room-view.go
@@ -373,48 +373,46 @@ func (view *RoomView) SetEditing(evt *event.Event) {
view.editMoveText = view.GetInputText()
}
view.editing = evt
- view.SetInputText(view.editing.Content.Body)
+ view.input.SetText(view.editing.Content.Body)
}
view.status.SetText(view.GetStatus())
}
-func (view *RoomView) EditNext() {
- if view.editing == nil {
- return
- }
- var foundEvent *event.Event
+func (view *RoomView) findMessageToEdit(forward bool) *event.Event {
currentFound := view.editing == nil
self := view.parent.matrix.Client().UserID
- for _, msg := range view.MessageView().messages {
+ msgs := view.MessageView().messages
+ for i := 0; i < len(msgs); i++ {
+ index := i
+ if !forward {
+ index = len(msgs) - i - 1
+ }
+ evt := msgs[index]
if currentFound {
- if msg.SenderID == self {
- foundEvent = msg.Event
- break
+ if evt.SenderID == self && evt.Event.Type == mautrix.EventMessage {
+ return evt.Event
}
- } else if msg.EventID == view.editing.ID {
+ } else if evt.EventID == view.editing.ID {
currentFound = true
}
}
+ return nil
+}
+
+func (view *RoomView) EditNext() {
+ if view.editing == nil {
+ return
+ }
+ foundEvent := view.findMessageToEdit(true)
view.SetEditing(foundEvent)
+ view.input.SetCursorOffset(-1)
}
func (view *RoomView) EditPrevious() {
- var foundEvent *event.Event
- currentFound := view.editing == nil
- self := view.parent.matrix.Client().UserID
- msgs := view.MessageView().messages
- for i := len(msgs) - 1; i >= 0; i-- {
- if currentFound {
- if msgs[i].SenderID == self {
- foundEvent = msgs[i].Event
- break
- }
- } else if msgs[i].EventID == view.editing.ID {
- currentFound = true
- }
- }
+ foundEvent := view.findMessageToEdit(false)
if foundEvent != nil {
view.SetEditing(foundEvent)
+ view.input.SetCursorOffset(0)
}
}