From 294029175efd557148fae9dd45e0613a6a8aee10 Mon Sep 17 00:00:00 2001 From: mini-bomba <55105495+mini-bomba@users.noreply.github.com> Date: Thu, 5 Mar 2026 02:35:53 +0100 Subject: [PATCH 1/3] refactor(room): move reaction code to utils --- src/app/features/room/RoomTimeline.tsx | 25 +++------------------ src/app/utils/matrix.ts | 30 ++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index c153ab52..662c48ad 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -51,7 +51,7 @@ import { import { isKeyHotkey } from 'is-hotkey'; import { Opts as LinkifyOpts } from 'linkifyjs'; import { useTranslation } from 'react-i18next'; -import { eventWithShortcode, factoryEventSentBy, getMxIdLocalPart } from '$utils/matrix'; +import { getMxIdLocalPart, toggleReaction } from '$utils/matrix'; import { useMatrixClient } from '$hooks/useMatrixClient'; import { ItemRange, useVirtualPaginator } from '$hooks/useVirtualPaginator'; import { useAlive } from '$hooks/useAlive'; @@ -85,7 +85,6 @@ import { getEventReactions, getLatestEditableEvt, getMemberDisplayName, - getReactionContent, isMembershipChanged, reactionOrEditEvent, } from '$utils/room'; @@ -1166,26 +1165,8 @@ export function RoomTimeline({ ); const handleReactionToggle = useCallback( - (targetEventId: string, key: string, shortcode?: string) => { - const relations = getEventReactions(room.getUnfilteredTimelineSet(), targetEventId); - const allReactions = relations?.getSortedAnnotationsByKey() ?? []; - const [, reactionsSet] = allReactions.find(([k]: [string, any]) => k === key) ?? []; - const reactions: MatrixEvent[] = reactionsSet ? Array.from(reactionsSet) : []; - const myReaction = reactions.find(factoryEventSentBy(mx.getUserId()!)); - - if (myReaction && !!(myReaction as any)?.isRelation()) { - mx.redactEvent(room.roomId, (myReaction as any).getId()); - return; - } - const rShortcode = - shortcode || - ((reactions.find(eventWithShortcode) as any)?.getContent().shortcode as string | undefined); - mx.sendEvent( - room.roomId, - MessageEvent.Reaction as any, - getReactionContent(targetEventId, key, rShortcode) - ); - }, + (targetEventId: string, key: string, shortcode?: string) => + toggleReaction(mx, room, targetEventId, key, shortcode), [mx, room] ); diff --git a/src/app/utils/matrix.ts b/src/app/utils/matrix.ts index 12b455b5..46d5e6ec 100644 --- a/src/app/utils/matrix.ts +++ b/src/app/utils/matrix.ts @@ -16,8 +16,8 @@ import { import to from 'await-to-js'; import { IImageInfo, IThumbnailContent, IVideoInfo } from '$types/matrix/common'; import { AccountDataEvent } from '$types/matrix/accountData'; -import { Membership, StateEvent } from '$types/matrix/room'; -import { getStateEvent } from './room'; +import { Membership, MessageEvent, StateEvent } from '$types/matrix/room'; +import { getEventReactions, getReactionContent, getStateEvent } from './room'; const DOMAIN_REGEX = /\b(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}\b/; @@ -382,3 +382,29 @@ export const creatorsSupported = (version: string): boolean => { const unsupportedVersion = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11']; return !unsupportedVersion.includes(version); }; + +export const toggleReaction = ( + mx: MatrixClient, + room: Room, + targetEventId: string, + key: string, + shortcode?: string +) => { + const relations = getEventReactions(room.getUnfilteredTimelineSet(), targetEventId); + const allReactions = relations?.getSortedAnnotationsByKey() ?? []; + const [, reactionsSet] = allReactions.find(([k]: [string, any]) => k === key) ?? []; + const reactions: MatrixEvent[] = reactionsSet ? Array.from(reactionsSet) : []; + const myReaction = reactions.find(factoryEventSentBy(mx.getUserId()!)); + + if (myReaction && !!(myReaction as any)?.isRelation()) { + mx.redactEvent(room.roomId, (myReaction as any).getId()); + return; + } + const rShortcode = + shortcode || (reactions.find(eventWithShortcode)?.getContent().shortcode as string | undefined); + mx.sendEvent( + room.roomId, + MessageEvent.Reaction as any, + getReactionContent(targetEventId, key, rShortcode) + ); +}; From bf9b6f51116c9c6d1bed0e0cbacd80aa881c451a Mon Sep 17 00:00:00 2001 From: mini-bomba <55105495+mini-bomba@users.noreply.github.com> Date: Thu, 5 Mar 2026 02:36:53 +0100 Subject: [PATCH 2/3] fix(editor): make autocomplete detection split on any whitespace --- src/app/components/editor/utils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/components/editor/utils.ts b/src/app/components/editor/utils.ts index 851bc30d..85ff437a 100644 --- a/src/app/components/editor/utils.ts +++ b/src/app/components/editor/utils.ts @@ -261,7 +261,8 @@ export const getPrevWorldRange = (editor: Editor): BaseRange | undefined => { const [cursorPoint] = Range.edges(selection); const worldStartPoint = getPointUntilChar(editor, cursorPoint, { reverse: true, - match: (char) => char === ' ', + // line breaks produce empty chars, not \n + match: (char) => /\s|^$/.test(char), }); return worldStartPoint && Editor.range(editor, worldStartPoint, cursorPoint); }; From b28160301a7a15af13475f7ffb5d355a861f30cc Mon Sep 17 00:00:00 2001 From: mini-bomba <55105495+mini-bomba@users.noreply.github.com> Date: Thu, 5 Mar 2026 02:37:58 +0100 Subject: [PATCH 3/3] feat: quick react via message editor typing `+:` in the message editor will bring up the emoticon autocomplete prompt, where choosing an emoticon will clear the editor and react with that emoticon under the latest message this commit also splits the autocomplete prefixes in two: ones that can be used anywhere in the message, and those that only work at the beginning also i set a default type param on one of the autocomplete query functions because who wants to keep typing out that type everywhere all the time --- .../autocomplete/EmoticonAutocomplete.tsx | 25 +++++--- .../editor/autocomplete/autocompleteQuery.ts | 8 ++- src/app/features/room/RoomInput.tsx | 58 +++++++++++++++++-- .../features/room/message/MessageEditor.tsx | 4 +- .../features/settings/account/BioEditor.tsx | 3 +- 5 files changed, 80 insertions(+), 18 deletions(-) diff --git a/src/app/components/editor/autocomplete/EmoticonAutocomplete.tsx b/src/app/components/editor/autocomplete/EmoticonAutocomplete.tsx index a6c7b3b6..16926645 100644 --- a/src/app/components/editor/autocomplete/EmoticonAutocomplete.tsx +++ b/src/app/components/editor/autocomplete/EmoticonAutocomplete.tsx @@ -23,10 +23,14 @@ type EmoticonCompleteHandler = (key: string, shortcode: string) => void; type EmoticonSearchItem = PackImageReader | IEmoji; type EmoticonAutocompleteProps = { + title?: string; imagePackRooms: Room[]; editor: Editor; query: AutocompleteQuery; requestClose: () => void; + // this allows you to override the default behaviour of inserting the selection + // used to implement the +: reaction shortcut + onEmoticonSelected?: EmoticonCompleteHandler; }; const SEARCH_OPTIONS: UseAsyncSearchOptions = { @@ -36,10 +40,12 @@ const SEARCH_OPTIONS: UseAsyncSearchOptions = { }; export function EmoticonAutocomplete({ + title, imagePackRooms, editor, query, requestClose, + onEmoticonSelected, }: EmoticonAutocompleteProps) { const mx = useMatrixClient(); const useAuthentication = useMediaAuthentication(); @@ -67,12 +73,14 @@ export function EmoticonAutocomplete({ else resetSearch(); }, [query.text, search, resetSearch]); - const handleAutocomplete: EmoticonCompleteHandler = (key, shortcode) => { - const emoticonEl = createEmoticonElement(key, shortcode); - replaceWithElement(editor, query.range, emoticonEl); - moveCursor(editor, true); - requestClose(); - }; + const handleAutocomplete: EmoticonCompleteHandler = + onEmoticonSelected ?? + ((key, shortcode) => { + const emoticonEl = createEmoticonElement(key, shortcode); + replaceWithElement(editor, query.range, emoticonEl); + moveCursor(editor, true); + requestClose(); + }); useKeyDown(window, (evt: KeyboardEvent) => { onTabPress(evt, () => { @@ -84,7 +92,10 @@ export function EmoticonAutocomplete({ }); return autoCompleteEmoticon.length === 0 ? null : ( - Emojis} requestClose={requestClose}> + {title ?? 'Emojis'}} + requestClose={requestClose} + > {autoCompleteEmoticon.map((emoticon) => { const isCustomEmoji = 'url' in emoticon; const key = isCustomEmoji ? emoticon.url : emoticon.unicode; diff --git a/src/app/components/editor/autocomplete/autocompleteQuery.ts b/src/app/components/editor/autocomplete/autocompleteQuery.ts index 1baa44a1..5b8294af 100644 --- a/src/app/components/editor/autocomplete/autocompleteQuery.ts +++ b/src/app/components/editor/autocomplete/autocompleteQuery.ts @@ -5,12 +5,16 @@ export enum AutocompletePrefix { UserMention = '@', Emoticon = ':', Command = '/', + Reaction = '+:', } -export const AUTOCOMPLETE_PREFIXES: readonly AutocompletePrefix[] = [ +export const ANYWHERE_AUTOCOMPLETE_PREFIXES: readonly AutocompletePrefix[] = [ AutocompletePrefix.RoomMention, AutocompletePrefix.UserMention, AutocompletePrefix.Emoticon, +]; +export const BEGINNING_AUTOCOMPLETE_PREFIXES: readonly AutocompletePrefix[] = [ AutocompletePrefix.Command, + AutocompletePrefix.Reaction, ]; export type AutocompleteQuery = { @@ -34,7 +38,7 @@ export const getAutocompleteQueryText = ( prefix: string ): string => Editor.string(editor, queryRange).slice(prefix.length); -export const getAutocompleteQuery = ( +export const getAutocompleteQuery = ( editor: Editor, queryRange: BaseRange, validPrefixes: readonly TPrefix[] diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 3c2ca181..a92e150a 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -21,7 +21,7 @@ import { StickerEventContent, } from '$types/matrix-sdk'; import { ReactEditor } from 'slate-react'; -import { Editor, Transforms } from 'slate'; +import { Editor, Point, Range, Transforms } from 'slate'; import { Box, config, @@ -51,7 +51,6 @@ import { import { useMatrixClient } from '$hooks/useMatrixClient'; import { - AUTOCOMPLETE_PREFIXES, AutocompletePrefix, AutocompleteQuery, createEmoticonElement, @@ -73,6 +72,8 @@ import { getBeginCommand, trimCommand, getMentions, + ANYWHERE_AUTOCOMPLETE_PREFIXES, + BEGINNING_AUTOCOMPLETE_PREFIXES, } from '$components/editor'; import { EmojiBoard, EmojiBoardTab } from '$components/emoji-board'; import { UseStateProvider } from '$components/UseStateProvider'; @@ -82,6 +83,7 @@ import { getImageInfo, getMxIdLocalPart, mxcUrlToHttp, + toggleReaction, } from '$utils/matrix'; import { useTypingStatusUpdater } from '$hooks/useTypingStatusUpdater'; import { useFilePicker } from '$hooks/useFilePicker'; @@ -141,6 +143,7 @@ import { } from '$utils/delayedEvents'; import { timeHourMinute, timeDayMonthYear } from '$utils/time'; import { stopPropagation } from '$utils/keyboard'; +import { MessageEvent } from '$types/matrix/room'; import { SchedulePickerDialog } from './schedule-send'; import * as css from './schedule-send/SchedulePickerDialog.css'; import { @@ -559,9 +562,19 @@ export const RoomInput = forwardRef( } const prevWordRange = getPrevWorldRange(editor); - const query = prevWordRange - ? getAutocompleteQuery(editor, prevWordRange, AUTOCOMPLETE_PREFIXES) - : undefined; + if (!prevWordRange) { + setAutocompleteQuery(undefined); + return; + } + + const firstPosition = Editor.start(editor, []); + const isRangeAtBeginning = !Point.isAfter(Range.start(prevWordRange), firstPosition); + const query = + (isRangeAtBeginning + ? getAutocompleteQuery(editor, prevWordRange, BEGINNING_AUTOCOMPLETE_PREFIXES) + : undefined) ?? + getAutocompleteQuery(editor, prevWordRange, ANYWHERE_AUTOCOMPLETE_PREFIXES); + setAutocompleteQuery(query); }, [editor, sendTypingStatus, hideActivity] @@ -572,6 +585,31 @@ export const RoomInput = forwardRef( ReactEditor.focus(editor); }, [editor]); + const handleReactionAutocomplete = (key: string, shortcode: string) => { + const lastMessage = room + .getLiveTimeline() + .getEvents() + .findLast((event) => + ( + [ + MessageEvent.RoomMessage, + MessageEvent.RoomMessageEncrypted, + MessageEvent.Sticker, + ] as string[] + ).includes(event.getType()) + ); + const lastMessageId = lastMessage?.getId(); + + if (lastMessageId) { + toggleReaction(mx, room, lastMessageId, key, shortcode); + } + + resetEditor(editor); + resetEditorHistory(editor); + sendTypingStatus(false); + handleCloseAutocomplete(); + }; + const handleEmoticonSelect = (key: string, shortcode: string) => { editor.insertNode(createEmoticonElement(key, shortcode)); moveCursor(editor); @@ -680,6 +718,16 @@ export const RoomInput = forwardRef( requestClose={handleCloseAutocomplete} /> )} + {autocompleteQuery?.prefix === AutocompletePrefix.Reaction && ( + + )} {autocompleteQuery?.prefix === AutocompletePrefix.Command && ( ( const prevWordRange = getPrevWorldRange(editor); const query = prevWordRange - ? getAutocompleteQuery(editor, prevWordRange, AUTOCOMPLETE_PREFIXES) + ? getAutocompleteQuery(editor, prevWordRange, ANYWHERE_AUTOCOMPLETE_PREFIXES) : undefined; setAutocompleteQuery(query); }, diff --git a/src/app/features/settings/account/BioEditor.tsx b/src/app/features/settings/account/BioEditor.tsx index 3bb33467..49b47d7b 100644 --- a/src/app/features/settings/account/BioEditor.tsx +++ b/src/app/features/settings/account/BioEditor.tsx @@ -16,7 +16,6 @@ import { Editor, Transforms } from 'slate'; import { ReactEditor } from 'slate-react'; import { isKeyHotkey } from 'is-hotkey'; import { - AUTOCOMPLETE_PREFIXES, AutocompletePrefix, AutocompleteQuery, CustomEditor, @@ -145,7 +144,7 @@ export function BioEditor({ value, isSaving, imagePackRooms, onSave }: BioEditor } const prevWordRange = getPrevWorldRange(editor); const query = prevWordRange - ? getAutocompleteQuery(editor, prevWordRange, AUTOCOMPLETE_PREFIXES) + ? getAutocompleteQuery(editor, prevWordRange, [AutocompletePrefix.Emoticon]) : undefined; setAutocompleteQuery(query); },