From 90d3121da820ca1be97bc55957d898d7fdf7007d Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 3 Mar 2023 14:30:25 +0100 Subject: [PATCH] Message: Fix custom emoji position after editing (#2703) --- src/components/common/MessageText.tsx | 12 +++++++++++- .../common/helpers/renderTextWithEntities.tsx | 5 +++++ src/hooks/useCacheBuster.ts | 4 ++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/components/common/MessageText.tsx b/src/components/common/MessageText.tsx index 25cb81f8f..b357452d4 100644 --- a/src/components/common/MessageText.tsx +++ b/src/components/common/MessageText.tsx @@ -1,4 +1,6 @@ -import React, { memo, useMemo, useRef } from '../../lib/teact/teact'; +import React, { + memo, useMemo, useRef, +} from '../../lib/teact/teact'; import type { ApiFormattedText, ApiMessage } from '../../api/types'; import type { ObserveFn } from '../../hooks/useIntersectionObserver'; @@ -7,6 +9,7 @@ import { ApiMessageEntityTypes } from '../../api/types'; import trimText from '../../util/trimText'; import { getMessageText, stripCustomEmoji } from '../../global/helpers'; import { renderTextWithEntities } from './helpers/renderTextWithEntities'; +import useSyncEffect from '../../hooks/useSyncEffect'; interface OwnProps { message: ApiMessage; @@ -44,12 +47,18 @@ function MessageText({ // eslint-disable-next-line no-null/no-null const sharedCanvasHqRef = useRef(null); + const textCacheBusterRef = useRef(0); + const formattedText = translatedText || message.content.text || undefined; const adaptedFormattedText = isForAnimation && formattedText ? stripCustomEmoji(formattedText) : formattedText; const { text, entities } = adaptedFormattedText || {}; + useSyncEffect(() => { + textCacheBusterRef.current += 1; + }, [text, entities]); + const withSharedCanvas = useMemo(() => { const hasSpoilers = entities?.some((e) => e.type === ApiMessageEntityTypes.Spoiler); if (hasSpoilers) { @@ -84,6 +93,7 @@ function MessageText({ withTranslucentThumbs, sharedCanvasRef, sharedCanvasHqRef, + textCacheBusterRef.current.toString(), ), ].flat().filter(Boolean)} diff --git a/src/components/common/helpers/renderTextWithEntities.tsx b/src/components/common/helpers/renderTextWithEntities.tsx index 54738e324..c205d85ef 100644 --- a/src/components/common/helpers/renderTextWithEntities.tsx +++ b/src/components/common/helpers/renderTextWithEntities.tsx @@ -41,6 +41,7 @@ export function renderTextWithEntities( withTranslucentThumbs?: boolean, sharedCanvasRef?: React.RefObject, sharedCanvasHqRef?: React.RefObject, + cacheBuster?: string, ) { if (!entities || !entities.length) { return renderMessagePart(text, highlight, emojiSize, shouldRenderAsHtml, isSimple); @@ -129,6 +130,7 @@ export function renderTextWithEntities( emojiSize, sharedCanvasRef, sharedCanvasHqRef, + cacheBuster, ); if (Array.isArray(newEntity)) { @@ -311,6 +313,7 @@ function processEntity( emojiSize?: number, sharedCanvasRef?: React.RefObject, sharedCanvasHqRef?: React.RefObject, + cacheBuster?: string, ) { const entityText = typeof entityContent === 'string' && entityContent; const renderedContent = nestedEntityContent.length ? nestedEntityContent : entityContent; @@ -334,6 +337,7 @@ function processEntity( if (entity.type === ApiMessageEntityTypes.CustomEmoji) { return ( { - const [cacheBuster, setCacheBuster] = useState(false); + const [cacheBuster, setCacheBuster] = useState(0); const updateCacheBuster = useCallback(() => { - setCacheBuster((current) => !current); + setCacheBuster((current) => current + 1); }, []); return [cacheBuster, updateCacheBuster] as const;