diff --git a/.changeset/fix-up-to-edit.md b/.changeset/fix-up-to-edit.md new file mode 100644 index 00000000..171a83d7 --- /dev/null +++ b/.changeset/fix-up-to-edit.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Fix up arrow to edit messages not editing messages. diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index f6a0f61c..f43375c6 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -231,9 +231,10 @@ interface RoomInputProps { roomId: string; room: Room; threadRootId?: string; + onEditLastMessage?: () => void; } export const RoomInput = forwardRef( - ({ editor, fileDropContainerRef, roomId, room, threadRootId }, ref) => { + ({ editor, fileDropContainerRef, roomId, room, threadRootId, onEditLastMessage }, ref) => { // When in thread mode, isolate drafts by thread root ID so thread replies // don't clobber the main room draft (and vice versa). const draftKey = threadRootId ?? roomId; @@ -891,6 +892,15 @@ export const RoomInput = forwardRef( } } + if (isKeyHotkey('arrowup', evt) && isEmptyEditor(editor)) { + const { selection } = editor; + if (selection && Editor.isStart(editor, selection.anchor, [])) { + evt.preventDefault(); + onEditLastMessage?.(); + return; + } + } + if ( (isKeyHotkey('mod+enter', evt) || (!enterForNewline && isKeyHotkey('enter', evt))) && !isComposing(evt) diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index 7b2badc4..79f43775 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -109,6 +109,7 @@ export type RoomTimelineProps = { eventId?: string; editor: Editor; onEditorReset?: () => void; + onEditLastMessageRef?: React.MutableRefObject<(() => void) | undefined>; }; export function RoomTimeline({ @@ -116,6 +117,7 @@ export function RoomTimeline({ eventId, editor, onEditorReset, + onEditLastMessageRef, }: Readonly) { const mx = useMatrixClient(); const alive = useAlive(); @@ -660,6 +662,23 @@ export function RoomTimeline({ processedEventsRef.current = processedEvents; + useEffect(() => { + if (!onEditLastMessageRef) return; + const ref = onEditLastMessageRef; + ref.current = () => { + const myUserId = mx.getUserId(); + const found = [...processedEventsRef.current] + .reverse() + .find( + (e) => + e.mEvent.getSender() === myUserId && + e.mEvent.getType() === 'm.room.message' && + !e.mEvent.isRedacted() + ); + if (found?.mEvent.getId()) actions.handleEdit(found.mEvent.getId()); + }; + }, [onEditLastMessageRef, mx, actions]); + useEffect(() => { const v = vListRef.current; if (!v) return; diff --git a/src/app/features/room/RoomView.tsx b/src/app/features/room/RoomView.tsx index 933bf9e3..42156161 100644 --- a/src/app/features/room/RoomView.tsx +++ b/src/app/features/room/RoomView.tsx @@ -73,6 +73,7 @@ const shouldFocusMessageField = (evt: KeyboardEvent): boolean => { export function RoomView({ eventId }: { eventId?: string }) { const roomInputRef = useRef(null); const roomViewRef = useRef(null); + const editLastMessageRef = useRef<(() => void) | undefined>(); const [hideReads] = useSetting(settingsAtom, 'hideReads'); const screenSize = useScreenSizeContext(); @@ -161,6 +162,7 @@ export function RoomView({ eventId }: { eventId?: string }) { eventId={eventId} editor={editor} onEditorReset={handleResetEditor} + onEditLastMessageRef={editLastMessageRef} /> @@ -186,6 +188,7 @@ export function RoomView({ eventId }: { eventId?: string }) { roomId={roomId} fileDropContainerRef={roomViewRef} ref={roomInputRef} + onEditLastMessage={() => editLastMessageRef.current?.()} /> )} {!canMessage && (