diff --git a/src/app/components/editor/autocomplete/EmoticonAutocomplete.tsx b/src/app/components/editor/autocomplete/EmoticonAutocomplete.tsx index a6c7b3b6b..169266454 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 1baa44a13..5b8294afb 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/components/editor/utils.ts b/src/app/components/editor/utils.ts index 851bc30d3..85ff437a1 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); }; diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index abd46d495..e8e71606d 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 { @@ -557,9 +560,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] @@ -570,6 +583,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); @@ -678,6 +716,16 @@ export const RoomInput = forwardRef( requestClose={handleCloseAutocomplete} /> )} + {autocompleteQuery?.prefix === AutocompletePrefix.Reaction && ( + + )} {autocompleteQuery?.prefix === AutocompletePrefix.Command && ( { - 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/features/room/message/MessageEditor.tsx b/src/app/features/room/message/MessageEditor.tsx index 476bf0cab..e00392dcb 100644 --- a/src/app/features/room/message/MessageEditor.tsx +++ b/src/app/features/room/message/MessageEditor.tsx @@ -27,7 +27,6 @@ import { } from '$types/matrix-sdk'; import { isKeyHotkey } from 'is-hotkey'; import { - AUTOCOMPLETE_PREFIXES, AutocompletePrefix, AutocompleteQuery, CustomEditor, @@ -47,6 +46,7 @@ import { trimCustomHtml, useEditor, getMentions, + ANYWHERE_AUTOCOMPLETE_PREFIXES, } from '$components/editor'; import { useSetting } from '$state/hooks/settings'; import { settingsAtom } from '$state/settings'; @@ -207,7 +207,7 @@ export const MessageEditor = as<'div', MessageEditorProps>( 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 3bb33467c..49b47d7b9 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); }, diff --git a/src/app/utils/matrix.ts b/src/app/utils/matrix.ts index 12b455b55..46d5e6ecc 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) + ); +};