Skip to content

Commit

Permalink
Fixes #2765 - Use the keyboard up arrow to edit the last message, use…
Browse files Browse the repository at this point in the history
… escape to cancel editing or replying.
  • Loading branch information
stefanceriu committed May 7, 2024
1 parent 3017100 commit e05e664
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ private class GlobalSearchTextField: UITextField {
}

override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
guard let key = presses.first?.key else { return }
guard let key = presses.first?.key else {
super.pressesBegan(presses, with: event)
return
}

if keyPressHandler(key.keyCode) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum ComposerToolbarVoiceMessageAction {

enum ComposerToolbarViewModelAction {
case sendMessage(plain: String, html: String?, mode: RoomScreenComposerMode, intentionalMentions: IntentionalMentions)
case editLastMessage
case attach(ComposerAttachmentType)

case handlePasteOrDrop(provider: NSItemProvider)
Expand All @@ -47,6 +48,7 @@ enum ComposerToolbarViewModelAction {
enum ComposerToolbarViewAction {
case composerAppeared
case sendMessage
case editLastMessage
case cancelReply
case cancelEdit
case attach(ComposerAttachmentType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool
sendPlainComposerText()
}
}
case .editLastMessage:
actionsSubject.send(.editLastMessage)
case .cancelReply:
set(mode: .default)
case .cancelEdit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,19 @@ struct ComposerToolbar: View {
showResizeGrabber: context.viewState.bindings.composerActionsEnabled,
isExpanded: $context.composerExpanded) {
context.send(viewAction: .sendMessage)
} editAction: {
context.send(viewAction: .editLastMessage)
} pasteAction: { provider in
context.send(viewAction: .handlePasteOrDrop(provider: provider))
} replyCancellationAction: {
context.send(viewAction: .cancelReply)
} editCancellationAction: {
context.send(viewAction: .cancelEdit)
} cancellationAction: {
switch context.viewState.composerMode {
case .edit:
context.send(viewAction: .cancelEdit)
case .reply:
context.send(viewAction: .cancelReply)
default:
break
}
} onAppearAction: {
context.send(viewAction: .composerAppeared)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Compound
import SwiftUI
import WysiwygComposer

typealias EnterKeyHandler = () -> Void
typealias GenericKeyHandler = (_ key: UIKeyboardHIDUsage) -> Void
typealias PasteHandler = (NSItemProvider) -> Void

struct MessageComposer: View {
Expand All @@ -27,10 +27,10 @@ struct MessageComposer: View {
let mode: RoomScreenComposerMode
let showResizeGrabber: Bool
@Binding var isExpanded: Bool
let sendAction: EnterKeyHandler
let sendAction: () -> Void
let editAction: () -> Void
let pasteAction: PasteHandler
let replyCancellationAction: () -> Void
let editCancellationAction: () -> Void
let cancellationAction: () -> Void
let onAppearAction: () -> Void

@State private var composerTranslation: CGFloat = 0
Expand Down Expand Up @@ -86,7 +86,7 @@ struct MessageComposer: View {
MessageComposerTextField(placeholder: L10n.richTextEditorComposerPlaceholder,
text: $plainComposerText,
maxHeight: 300,
enterKeyHandler: sendAction,
keyHandler: { handleKeyPress($0) },
pasteHandler: pasteAction)
.tint(.compound.iconAccentTertiary)
.padding(.vertical, 10)
Expand All @@ -103,9 +103,9 @@ struct MessageComposer: View {
private var header: some View {
switch mode {
case .reply(_, let replyDetails, _):
MessageComposerReplyHeader(replyDetails: replyDetails, action: replyCancellationAction)
MessageComposerReplyHeader(replyDetails: replyDetails, action: cancellationAction)
case .edit:
MessageComposerEditHeader(action: editCancellationAction)
MessageComposerEditHeader(action: cancellationAction)
case .recordVoiceMessage, .previewVoiceMessage, .default:
EmptyView()
}
Expand Down Expand Up @@ -135,6 +135,19 @@ struct MessageComposer: View {
}
}
}

private func handleKeyPress(_ key: UIKeyboardHIDUsage) {
switch key {
case .keyboardReturnOrEnter:
sendAction()
case .keyboardUpArrow:
editAction()
case .keyboardEscape:
cancellationAction()
default:
break
}
}
}

private struct MessageComposerReplyHeader: View {
Expand Down Expand Up @@ -244,9 +257,9 @@ struct MessageComposer_Previews: PreviewProvider, TestablePreview {
showResizeGrabber: false,
isExpanded: .constant(false),
sendAction: { },
editAction: { },
pasteAction: { _ in },
replyCancellationAction: { },
editCancellationAction: { },
cancellationAction: { },
onAppearAction: { viewModel.setup() })
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ struct MessageComposerTextField: View {
@Binding var text: NSAttributedString

let maxHeight: CGFloat
let enterKeyHandler: EnterKeyHandler
let keyHandler: GenericKeyHandler
let pasteHandler: PasteHandler

var body: some View {
UITextViewWrapper(text: $text,
maxHeight: maxHeight,
enterKeyHandler: enterKeyHandler,
keyHandler: keyHandler,
pasteHandler: pasteHandler)
.accessibilityLabel(placeholder)
.background(placeholderView, alignment: .topLeading)
Expand All @@ -49,7 +49,7 @@ private struct UITextViewWrapper: UIViewRepresentable {

let maxHeight: CGFloat

let enterKeyHandler: EnterKeyHandler
let keyHandler: GenericKeyHandler
let pasteHandler: PasteHandler

private let font = UIFont.preferredFont(forTextStyle: .body)
Expand Down Expand Up @@ -115,7 +115,7 @@ private struct UITextViewWrapper: UIViewRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator(text: $text,
maxHeight: maxHeight,
enterKeyHandler: enterKeyHandler,
keyHandler: keyHandler,
pasteHandler: pasteHandler)
}

Expand All @@ -124,27 +124,27 @@ private struct UITextViewWrapper: UIViewRepresentable {

private let maxHeight: CGFloat

private let enterKeyHandler: EnterKeyHandler
private let keyHandler: GenericKeyHandler
private let pasteHandler: PasteHandler

init(text: Binding<NSAttributedString>,
maxHeight: CGFloat,
enterKeyHandler: @escaping EnterKeyHandler,
keyHandler: @escaping GenericKeyHandler,
pasteHandler: @escaping PasteHandler) {
self.text = text
self.maxHeight = maxHeight
self.enterKeyHandler = enterKeyHandler
self.keyHandler = keyHandler
self.pasteHandler = pasteHandler
}

func textViewDidChange(_ textView: UITextView) {
text.wrappedValue = textView.attributedText
}

func textViewDidReceiveEnterKeyPress(_ textView: UITextView) {
enterKeyHandler()
func textViewDidReceiveKeyPress(_ textView: UITextView, key: UIKeyboardHIDUsage) {
keyHandler(key)
}

func textViewDidReceiveShiftEnterKeyPress(_ textView: UITextView) {
textView.insertText("\n")
}
Expand All @@ -157,7 +157,7 @@ private struct UITextViewWrapper: UIViewRepresentable {

private protocol ElementTextViewDelegate: AnyObject {
func textViewDidReceiveShiftEnterKeyPress(_ textView: UITextView)
func textViewDidReceiveEnterKeyPress(_ textView: UITextView)
func textViewDidReceiveKeyPress(_ textView: UITextView, key: UIKeyboardHIDUsage)
func textView(_ textView: UITextView, didReceivePasteWith provider: NSItemProvider)
}

Expand All @@ -176,9 +176,28 @@ private class ElementTextView: UITextView, PillAttachmentViewProviderDelegate {
@objc func shiftEnterKeyPressed(sender: UIKeyCommand) {
elementDelegate?.textViewDidReceiveShiftEnterKeyPress(self)
}

@objc func enterKeyPressed(sender: UIKeyCommand) {
elementDelegate?.textViewDidReceiveEnterKeyPress(self)
elementDelegate?.textViewDidReceiveKeyPress(self, key: .keyboardReturnOrEnter)
}

override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
guard let key = presses.first?.key else {
super.pressesBegan(presses, with: event)
return
}

if key.keyCode == .keyboardUpArrow, selectedRange.location == 0 {
elementDelegate?.textViewDidReceiveKeyPress(self, key: key.keyCode)
return
}

if key.keyCode == .keyboardEscape {
elementDelegate?.textViewDidReceiveKeyPress(self, key: key.keyCode)
return
}

super.pressesBegan(presses, with: event)
}

// Pasting support
Expand Down Expand Up @@ -254,7 +273,7 @@ struct MessageComposerTextField_Previews: PreviewProvider, TestablePreview {
MessageComposerTextField(placeholder: "Placeholder",
text: $text,
maxHeight: 300,
enterKeyHandler: { },
keyHandler: { _ in },
pasteHandler: { _ in })
}
}
Expand Down
16 changes: 16 additions & 0 deletions ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
mode: mode,
intentionalMentions: intentionalMentions)
}
case .editLastMessage:
editLastMessage()
case .attach(let attachment):
attach(attachment)
case .handlePasteOrDrop(let provider):
Expand Down Expand Up @@ -264,6 +266,20 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
}
}

private func editLastMessage() {
guard let item = timelineController.timelineItems.reversed().first(where: {
guard let item = $0 as? EventBasedMessageTimelineItemProtocol else {
return false
}

return item.sender.id == roomProxy.ownUserID && item.isEditable
}) else {
return
}

roomScreenInteractionHandler.processTimelineItemMenuAction(.edit, itemID: item.id)
}

private func attach(_ attachment: ComposerAttachmentType) {
switch attachment {
case .camera:
Expand Down
1 change: 1 addition & 0 deletions changelog.d/2765.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use the keyboard up arrow to edit the last message, use escape to cancel editing or replying.

0 comments on commit e05e664

Please sign in to comment.