diff options
author | Tulir Asokan <tulir@maunium.net> | 2020-02-19 01:14:02 +0200 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2020-02-19 01:14:02 +0200 |
commit | d02abd079fe58bae467672dc7d2a140f9f07bb85 (patch) | |
tree | 7972380c8aad12a3571aee7065ba450433c260dc /ui | |
parent | b4e27723d7ad6287d34f4cfc1f5a15b0074b59d1 (diff) |
Add support for editing messages
Diffstat (limited to 'ui')
-rw-r--r-- | ui/messages/base.go | 5 | ||||
-rw-r--r-- | ui/messages/parser.go | 1 | ||||
-rw-r--r-- | ui/room-view.go | 75 |
3 files changed, 74 insertions, 7 deletions
diff --git a/ui/messages/base.go b/ui/messages/base.go index 567e614..b54691e 100644 --- a/ui/messages/base.go +++ b/ui/messages/base.go @@ -17,7 +17,6 @@ package messages import ( - "encoding/json" "fmt" "time" @@ -55,7 +54,7 @@ type UIMessage struct { IsHighlight bool IsService bool Edited bool - Source json.RawMessage + Event *event.Event ReplyTo *UIMessage Renderer MessageRenderer } @@ -82,7 +81,7 @@ func newUIMessage(evt *event.Event, displayname string, renderer MessageRenderer IsHighlight: false, IsService: false, Edited: len(evt.Gomuks.Edits) > 0, - Source: evt.Content.VeryRaw, + Event: evt, Renderer: renderer, } } diff --git a/ui/messages/parser.go b/ui/messages/parser.go index e1c0053..53d30e3 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -128,6 +128,7 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *event.Event evt.Content.RemoveReplyFallback() } if len(evt.Gomuks.Edits) > 0 { + evt = evt.SomewhatDangerousCopy() evt.Content = *evt.Gomuks.Edits[len(evt.Gomuks.Edits)-1].Content.NewContent } switch evt.Content.MsgType { diff --git a/ui/room-view.go b/ui/room-view.go index 31da3c6..27c42c8 100644 --- a/ui/room-view.go +++ b/ui/room-view.go @@ -67,6 +67,9 @@ type RoomView struct { typing []string + editing *event.Event + editMoveText string + completions struct { list []string textCache string @@ -107,7 +110,9 @@ func NewRoomView(parent *MainView, room *rooms.Room) *RoomView { SetBackgroundColor(tcell.ColorDefault). SetPlaceholder("Send a message..."). SetPlaceholderTextColor(tcell.ColorGray). - SetTabCompleteFunc(view.InputTabComplete) + SetTabCompleteFunc(view.InputTabComplete). + SetPressKeyUpAtStartFunc(view.EditPrevious). + SetPressKeyDownAtEndFunc(view.EditNext) view.topic. SetTextColor(tcell.ColorWhite). @@ -149,8 +154,12 @@ func (view *RoomView) Blur() { func (view *RoomView) GetStatus() string { var buf strings.Builder + if view.editing != nil { + buf.WriteString("Editing message - ") + } + if len(view.completions.list) > 0 { - if view.completions.textCache != view.input.GetText() || view.completions.time.Add(10 * time.Second).Before(time.Now()) { + if view.completions.textCache != view.input.GetText() || view.completions.time.Add(10*time.Second).Before(time.Now()) { view.completions.list = []string{} } else { buf.WriteString(strings.Join(view.completions.list, ", ")) @@ -354,6 +363,61 @@ func (view *RoomView) autocompleteEmoji(word string) (completions []string) { return } +func (view *RoomView) SetEditing(evt *event.Event) { + if evt == nil { + view.editing = nil + view.SetInputText(view.editMoveText) + view.editMoveText = "" + } else { + if view.editing == nil { + view.editMoveText = view.GetInputText() + } + view.editing = evt + view.SetInputText(view.editing.Content.Body) + } + view.status.SetText(view.GetStatus()) +} + +func (view *RoomView) EditNext() { + if view.editing == nil { + return + } + var foundEvent *event.Event + currentFound := view.editing == nil + self := view.parent.matrix.Client().UserID + for _, msg := range view.MessageView().messages { + if currentFound { + if msg.SenderID == self { + foundEvent = msg.Event + break + } + } else if msg.EventID == view.editing.ID { + currentFound = true + } + } + view.SetEditing(foundEvent) +} + +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 + } + } + if foundEvent != nil { + view.SetEditing(foundEvent) + } +} + func (view *RoomView) InputTabComplete(text string, cursorOffset int) { debug.Print("Tab completing", cursorOffset, text) str := runewidth.Truncate(text, cursorOffset, "") @@ -396,11 +460,12 @@ func (view *RoomView) InputTabComplete(text string, cursorOffset int) { func (view *RoomView) InputSubmit(text string) { if len(text) == 0 { return - } else if cmd := view.parent.cmdProcessor.ParseCommand(view, text); cmd != nil { + } else if cmd := view.parent.cmdProcessor.ParseCommand(view, text); view.editing == nil && cmd != nil { go view.parent.cmdProcessor.HandleCommand(cmd) } else { go view.SendMessage(mautrix.MsgText, text) } + view.editMoveText = "" view.SetInputText("") } @@ -410,9 +475,11 @@ func (view *RoomView) SendMessage(msgtype mautrix.MessageType, text string) { if !view.config.Preferences.DisableEmojis { text = emoji.Sprint(text) } - evt := view.parent.matrix.PrepareMarkdownMessage(view.Room.ID, msgtype, text) + evt := view.parent.matrix.PrepareMarkdownMessage(view.Room.ID, msgtype, text, view.editing) msg := view.parseEvent(evt) view.content.AddMessage(msg, AppendMessage) + view.editing = nil + view.status.SetText(view.GetStatus()) eventID, err := view.parent.matrix.SendEvent(evt) if err != nil { msg.State = event.StateSendFail |