diff --git a/src/app/features/room/message/Message.tsx b/src/app/features/room/message/Message.tsx index c29f6ef1..f78d415e 100644 --- a/src/app/features/room/message/Message.tsx +++ b/src/app/features/room/message/Message.tsx @@ -28,13 +28,16 @@ import React, { MouseEventHandler, ReactNode, useCallback, + useRef, useState, + useEffect, } from 'react'; import FocusTrap from 'focus-trap-react'; import { useHover, useFocusWithin } from 'react-aria'; -import { EventTimeline, MatrixEvent, Room } from 'matrix-js-sdk'; +import { MatrixEvent, Room } from 'matrix-js-sdk'; import { Relations } from 'matrix-js-sdk/lib/models/relations'; import classNames from 'classnames'; +import { useAtomValue, useSetAtom } from 'jotai'; import { RoomPinnedEventsEventContent } from 'matrix-js-sdk/lib/types'; import { AvatarBase, @@ -60,7 +63,6 @@ import { } from '../../../utils/matrix'; import { MessageLayout, MessageSpacing } from '../../../state/settings'; import { nicknamesAtom, setNicknameAtom } from '../../../state/nicknames'; -import { useAtomValue, useSetAtom } from 'jotai'; import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { useRecentEmoji } from '../../../hooks/useRecentEmoji'; import * as css from './styles.css'; @@ -79,11 +81,10 @@ import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication'; import { useRoomPinnedEvents } from '../../../hooks/useRoomPinnedEvents'; import { MemberPowerTag, StateEvent } from '../../../../types/matrix/room'; import { PowerIcon } from '../../../components/power'; -import colorMXID from '../../../../util/colorMXID'; import { getPowerTagIconSrc } from '../../../hooks/useMemberPowerTag'; -import { AccountDataEvent } from '../../../../types/matrix/accountData'; import { useSableCosmetics } from '../../../hooks/useSableCosmetics'; import { SwipeableMessageWrapper } from '../../../components/SwipeableMessageWrapper'; +import { mobileOrTablet } from '../../../utils/user-agent'; export type ReactionHandler = (keyOrMxc: string, shortcode: string) => void; @@ -688,6 +689,40 @@ export type MessageProps = { hour24Clock: boolean; dateFormatString: string; }; + +function useMobileLongPress(callback: (e: React.PointerEvent) => void, delay = 700) { + const timerRef = useRef(); + const startPos = useRef<{ x: number; y: number } | null>(null); + + const start = useCallback((e: React.PointerEvent) => { + if (!mobileOrTablet()) return; + startPos.current = { x: e.clientX, y: e.clientY }; + timerRef.current = setTimeout(() => { + callback(e); + startPos.current = null; + }, delay); + }, [callback, delay]); + + const clear = useCallback(() => { + if (timerRef.current) clearTimeout(timerRef.current); + startPos.current = null; + }, []); + + const move = useCallback((e: React.PointerEvent) => { + if (!startPos.current) return; + const dx = Math.abs(e.clientX - startPos.current.x); + const dy = Math.abs(e.clientY - startPos.current.y); + if (dx > 10 || dy > 10) clear(); + }, [clear]); + + return { + onPointerDown: start, + onPointerMove: move, + onPointerUp: clear, + onPointerCancel: clear, + }; +} + export const Message = as<'div', MessageProps>( ( { @@ -725,9 +760,14 @@ export const Message = as<'div', MessageProps>( const useAuthentication = useMediaAuthentication(); const senderId = mEvent.getSender() ?? ''; - const [hover, setHover] = useState(false); - const { hoverProps } = useHover({ onHoverChange: setHover }); - const { focusWithinProps } = useFocusWithin({ onFocusWithinChange: setHover }); + const [isDesktopHover, setIsDesktopHover] = useState(false); + const { hoverProps } = useHover({ + onHoverChange: (h) => { if (!mobileOrTablet()) setIsDesktopHover(h); } + }); + const { focusWithinProps } = useFocusWithin({ + onFocusWithinChange: (f) => { if (!mobileOrTablet()) setIsDesktopHover(f); } + }); + const [menuAnchor, setMenuAnchor] = useState(); const [emojiBoardAnchor, setEmojiBoardAnchor] = useState(); const [nickEditOpen, setNickEditOpen] = useState(false); @@ -745,6 +785,20 @@ export const Message = as<'div', MessageProps>( const { color: usernameColor, font: usernameFont } = useSableCosmetics(senderId, room); + const [mobileOptionsOpen, setMobileOptionsOpen] = useState(false); + const optionsRef = useRef(null); + + useEffect(() => { + if (!mobileOptionsOpen) return undefined; + const handleClickOutside = (e: globalThis.Event) => { + if (optionsRef.current && !optionsRef.current.contains(e.target as Node)) { + setMobileOptionsOpen(false); + } + }; + document.addEventListener('pointerdown', handleClickOutside, { capture: true }); + return () => document.removeEventListener('pointerdown', handleClickOutside, { capture: true }); + }, [mobileOptionsOpen]); + const headerJSX = !collapse && ( ( {tagIconSrc && } - {messageLayout === MessageLayout.Modern && hover && ( + {messageLayout === MessageLayout.Modern && isDesktopHover && ( <> {senderId} @@ -843,6 +897,11 @@ export const Message = as<'div', MessageProps>( ); const handleContextMenu: MouseEventHandler = (evt) => { + if (mobileOrTablet()) { + evt.preventDefault(); + return; + } + if (evt.altKey || !window.getSelection()?.isCollapsed || edit) return; const tag = (evt.target as any).tagName; if (typeof tag === 'string' && tag.toLowerCase() === 'a') return; @@ -863,18 +922,17 @@ export const Message = as<'div', MessageProps>( const closeMenu = () => { setMenuAnchor(undefined); setNickEditOpen(false); + setMobileOptionsOpen(false); }; const handleOpenEmojiBoard: MouseEventHandler = (evt) => { const target = evt.currentTarget.parentElement?.parentElement ?? evt.currentTarget; setEmojiBoardAnchor(target.getBoundingClientRect()); }; + const handleAddReactions: MouseEventHandler = () => { const rect = menuAnchor; closeMenu(); - // open it with timeout because closeMenu - // FocusTrap will return focus from emojiBoard - setTimeout(() => { setEmojiBoardAnchor(rect); }, 100); @@ -890,6 +948,10 @@ export const Message = as<'div', MessageProps>( onReplyClick(mockEvent); }; + const bindLongPress = useMobileLongPress(() => { + setMobileOptionsOpen(true); + }, 700); + const isThreadedMessage = mEvent.threadRootId !== undefined; return ( @@ -907,8 +969,8 @@ export const Message = as<'div', MessageProps>( {...focusWithinProps} ref={ref} > - {!edit && (hover || !!menuAnchor || !!emojiBoardAnchor) && ( -
+ {!edit && (isDesktopHover || !!menuAnchor || !!emojiBoardAnchor || mobileOptionsOpen) && ( +
{canSendReaction && ( @@ -925,10 +987,12 @@ export const Message = as<'div', MessageProps>( onEmojiSelect={(key) => { onReactionToggle(mEvent.getId()!, key); setEmojiBoardAnchor(undefined); + setMobileOptionsOpen(false); }} onCustomEmojiSelect={(mxc, shortcode) => { onReactionToggle(mEvent.getId()!, mxc, shortcode); setEmojiBoardAnchor(undefined); + setMobileOptionsOpen(false); }} requestClose={() => { setEmojiBoardAnchor(undefined); @@ -948,7 +1012,10 @@ export const Message = as<'div', MessageProps>( )} { + onReplyClick(ev); + setMobileOptionsOpen(false); + }} data-event-id={mEvent.getId()} variant="SurfaceVariant" size="300" @@ -958,7 +1025,10 @@ export const Message = as<'div', MessageProps>( {!isThreadedMessage && ( onReplyClick(ev, true)} + onClick={(ev) => { + onReplyClick(ev, true); + setMobileOptionsOpen(false); + }} data-event-id={mEvent.getId()} variant="SurfaceVariant" size="300" @@ -969,7 +1039,10 @@ export const Message = as<'div', MessageProps>( )} {canEditEvent(mx, mEvent) && onEditId && ( onEditId(mEvent.getId())} + onClick={() => { + onEditId(mEvent.getId()); + setMobileOptionsOpen(false); + }} variant="SurfaceVariant" size="300" radii="300" @@ -1118,7 +1191,7 @@ export const Message = as<'div', MessageProps>( placeholder={senderDisplayName} onKeyDown={(e) => { if (e.key === 'Enter') { - setNickname(senderId, nickDraft || undefined); + setNickname(senderId, nickDraft || undefined, mx); closeMenu(); } if (e.key === 'Escape') closeMenu(); @@ -1136,12 +1209,12 @@ export const Message = as<'div', MessageProps>( /> { setNickname(senderId, nickDraft || undefined); closeMenu(); }}> + onClick={() => { setNickname(senderId, nickDraft || undefined, mx); closeMenu(); }}> Save {nicknames[senderId] && ( { setNickname(senderId, undefined); closeMenu(); }}> + onClick={() => { setNickname(senderId, undefined, mx); closeMenu(); }}> Clear )} @@ -1207,7 +1280,9 @@ export const Message = as<'div', MessageProps>( {messageLayout === MessageLayout.Compact && ( - {msgContentJSX} +
+ {msgContentJSX} +
) @@ -1215,7 +1290,9 @@ export const Message = as<'div', MessageProps>( {messageLayout === MessageLayout.Bubble && ( - {msgContentJSX} +
+ {msgContentJSX} +
)} @@ -1223,8 +1300,10 @@ export const Message = as<'div', MessageProps>( messageLayout !== MessageLayout.Compact && messageLayout !== MessageLayout.Bubble && ( - {headerJSX} - {msgContentJSX} +
+ {headerJSX} + {msgContentJSX} +
) @@ -1260,13 +1339,17 @@ export const Event = as<'div', EventProps>( ref ) => { const mx = useMatrixClient(); - const [hover, setHover] = useState(false); - const { hoverProps } = useHover({ onHoverChange: setHover }); - const { focusWithinProps } = useFocusWithin({ onFocusWithinChange: setHover }); - const [menuAnchor, setMenuAnchor] = useState(); const stateEvent = typeof mEvent.getStateKey() === 'string'; + const [menuAnchor, setMenuAnchor] = useState(); + const [mobileOptionsOpen, setMobileOptionsOpen] = useState(false); + const handleContextMenu: MouseEventHandler = (evt) => { + if (mobileOrTablet()) { + evt.preventDefault(); + return; + } + if (evt.altKey || !window.getSelection()?.isCollapsed) return; const tag = (evt.target as any).tagName; if (typeof tag === 'string' && tag.toLowerCase() === 'a') return; @@ -1286,8 +1369,34 @@ export const Event = as<'div', EventProps>( const closeMenu = () => { setMenuAnchor(undefined); + setMobileOptionsOpen(false); }; + const [isDesktopHover, setIsDesktopHover] = useState(false); + const { hoverProps } = useHover({ + onHoverChange: (h) => { if (!mobileOrTablet()) setIsDesktopHover(h); } + }); + const { focusWithinProps } = useFocusWithin({ + onFocusWithinChange: (f) => { if (!mobileOrTablet()) setIsDesktopHover(f); } + }); + + const optionsRef = useRef(null); + + useEffect(() => { + if (!mobileOptionsOpen) return undefined; + const handleClick = (e: globalThis.Event) => { + if (optionsRef.current && !optionsRef.current.contains(e.target as Node)) { + setMobileOptionsOpen(false); + } + }; + document.addEventListener('pointerdown', handleClick, { capture: true }); + return () => document.removeEventListener('pointerdown', handleClick, { capture: true }); + }, [mobileOptionsOpen]); + + const bindLongPress = useMobileLongPress(() => { + setMobileOptionsOpen(true); + }, 700); + return ( ( {...focusWithinProps} ref={ref} > - {(hover || !!menuAnchor) && ( -
+ {(isDesktopHover || !!menuAnchor || mobileOptionsOpen) && ( +
- setMenuAnchor(undefined), - clickOutsideDeactivates: true, - isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown', - isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp', - escapeDeactivates: stopPropagation, - }} - > - - - {!hideReadReceipts && ( - - )} - {showDeveloperTools && ( - - )} - - - {((!mEvent.isRedacted() && canDelete && !stateEvent) || - (mEvent.getSender() !== mx.getUserId() && !stateEvent)) && ( - <> - - - {!mEvent.isRedacted() && canDelete && ( - - )} - {mEvent.getSender() !== mx.getUserId() && ( - - )} - - - )} - - - } - > - setMenuAnchor(undefined), + clickOutsideDeactivates: true, + isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown', + isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp', + escapeDeactivates: stopPropagation, + }} + > + + + {!hideReadReceipts && ( + + )} + {showDeveloperTools && ( + + )} + + + {((!mEvent.isRedacted() && canDelete && !stateEvent) || + (mEvent.getSender() !== mx.getUserId() && !stateEvent)) && ( + <> + + + {!mEvent.isRedacted() && canDelete && ( + + )} + {mEvent.getSender() !== mx.getUserId() && ( + + )} + + + )} + + + } > - - - + + + + + )}
)} -
{children}
+
+ {children} +
); } -); +); \ No newline at end of file