Message: Fix custom emoji position after editing (#2703)
This commit is contained in:
parent
13c47b1727
commit
90d3121da8
@ -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 { ApiFormattedText, ApiMessage } from '../../api/types';
|
||||||
import type { ObserveFn } from '../../hooks/useIntersectionObserver';
|
import type { ObserveFn } from '../../hooks/useIntersectionObserver';
|
||||||
@ -7,6 +9,7 @@ import { ApiMessageEntityTypes } from '../../api/types';
|
|||||||
import trimText from '../../util/trimText';
|
import trimText from '../../util/trimText';
|
||||||
import { getMessageText, stripCustomEmoji } from '../../global/helpers';
|
import { getMessageText, stripCustomEmoji } from '../../global/helpers';
|
||||||
import { renderTextWithEntities } from './helpers/renderTextWithEntities';
|
import { renderTextWithEntities } from './helpers/renderTextWithEntities';
|
||||||
|
import useSyncEffect from '../../hooks/useSyncEffect';
|
||||||
|
|
||||||
interface OwnProps {
|
interface OwnProps {
|
||||||
message: ApiMessage;
|
message: ApiMessage;
|
||||||
@ -44,12 +47,18 @@ function MessageText({
|
|||||||
// eslint-disable-next-line no-null/no-null
|
// eslint-disable-next-line no-null/no-null
|
||||||
const sharedCanvasHqRef = useRef<HTMLCanvasElement>(null);
|
const sharedCanvasHqRef = useRef<HTMLCanvasElement>(null);
|
||||||
|
|
||||||
|
const textCacheBusterRef = useRef(0);
|
||||||
|
|
||||||
const formattedText = translatedText || message.content.text || undefined;
|
const formattedText = translatedText || message.content.text || undefined;
|
||||||
|
|
||||||
const adaptedFormattedText = isForAnimation && formattedText ? stripCustomEmoji(formattedText) : formattedText;
|
const adaptedFormattedText = isForAnimation && formattedText ? stripCustomEmoji(formattedText) : formattedText;
|
||||||
|
|
||||||
const { text, entities } = adaptedFormattedText || {};
|
const { text, entities } = adaptedFormattedText || {};
|
||||||
|
|
||||||
|
useSyncEffect(() => {
|
||||||
|
textCacheBusterRef.current += 1;
|
||||||
|
}, [text, entities]);
|
||||||
|
|
||||||
const withSharedCanvas = useMemo(() => {
|
const withSharedCanvas = useMemo(() => {
|
||||||
const hasSpoilers = entities?.some((e) => e.type === ApiMessageEntityTypes.Spoiler);
|
const hasSpoilers = entities?.some((e) => e.type === ApiMessageEntityTypes.Spoiler);
|
||||||
if (hasSpoilers) {
|
if (hasSpoilers) {
|
||||||
@ -84,6 +93,7 @@ function MessageText({
|
|||||||
withTranslucentThumbs,
|
withTranslucentThumbs,
|
||||||
sharedCanvasRef,
|
sharedCanvasRef,
|
||||||
sharedCanvasHqRef,
|
sharedCanvasHqRef,
|
||||||
|
textCacheBusterRef.current.toString(),
|
||||||
),
|
),
|
||||||
].flat().filter(Boolean)}
|
].flat().filter(Boolean)}
|
||||||
</>
|
</>
|
||||||
|
|||||||
@ -41,6 +41,7 @@ export function renderTextWithEntities(
|
|||||||
withTranslucentThumbs?: boolean,
|
withTranslucentThumbs?: boolean,
|
||||||
sharedCanvasRef?: React.RefObject<HTMLCanvasElement>,
|
sharedCanvasRef?: React.RefObject<HTMLCanvasElement>,
|
||||||
sharedCanvasHqRef?: React.RefObject<HTMLCanvasElement>,
|
sharedCanvasHqRef?: React.RefObject<HTMLCanvasElement>,
|
||||||
|
cacheBuster?: string,
|
||||||
) {
|
) {
|
||||||
if (!entities || !entities.length) {
|
if (!entities || !entities.length) {
|
||||||
return renderMessagePart(text, highlight, emojiSize, shouldRenderAsHtml, isSimple);
|
return renderMessagePart(text, highlight, emojiSize, shouldRenderAsHtml, isSimple);
|
||||||
@ -129,6 +130,7 @@ export function renderTextWithEntities(
|
|||||||
emojiSize,
|
emojiSize,
|
||||||
sharedCanvasRef,
|
sharedCanvasRef,
|
||||||
sharedCanvasHqRef,
|
sharedCanvasHqRef,
|
||||||
|
cacheBuster,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (Array.isArray(newEntity)) {
|
if (Array.isArray(newEntity)) {
|
||||||
@ -311,6 +313,7 @@ function processEntity(
|
|||||||
emojiSize?: number,
|
emojiSize?: number,
|
||||||
sharedCanvasRef?: React.RefObject<HTMLCanvasElement>,
|
sharedCanvasRef?: React.RefObject<HTMLCanvasElement>,
|
||||||
sharedCanvasHqRef?: React.RefObject<HTMLCanvasElement>,
|
sharedCanvasHqRef?: React.RefObject<HTMLCanvasElement>,
|
||||||
|
cacheBuster?: string,
|
||||||
) {
|
) {
|
||||||
const entityText = typeof entityContent === 'string' && entityContent;
|
const entityText = typeof entityContent === 'string' && entityContent;
|
||||||
const renderedContent = nestedEntityContent.length ? nestedEntityContent : entityContent;
|
const renderedContent = nestedEntityContent.length ? nestedEntityContent : entityContent;
|
||||||
@ -334,6 +337,7 @@ function processEntity(
|
|||||||
if (entity.type === ApiMessageEntityTypes.CustomEmoji) {
|
if (entity.type === ApiMessageEntityTypes.CustomEmoji) {
|
||||||
return (
|
return (
|
||||||
<CustomEmoji
|
<CustomEmoji
|
||||||
|
key={cacheBuster ? `${cacheBuster}-${entity.offset}` : undefined}
|
||||||
documentId={entity.documentId}
|
documentId={entity.documentId}
|
||||||
size={emojiSize}
|
size={emojiSize}
|
||||||
withSharedAnimation
|
withSharedAnimation
|
||||||
@ -458,6 +462,7 @@ function processEntity(
|
|||||||
case ApiMessageEntityTypes.CustomEmoji:
|
case ApiMessageEntityTypes.CustomEmoji:
|
||||||
return (
|
return (
|
||||||
<CustomEmoji
|
<CustomEmoji
|
||||||
|
key={cacheBuster ? `${cacheBuster}-${entity.offset}` : undefined}
|
||||||
documentId={entity.documentId}
|
documentId={entity.documentId}
|
||||||
size={emojiSize}
|
size={emojiSize}
|
||||||
withSharedAnimation
|
withSharedAnimation
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
import { useCallback, useState } from '../lib/teact/teact';
|
import { useCallback, useState } from '../lib/teact/teact';
|
||||||
|
|
||||||
const useCacheBuster = () => {
|
const useCacheBuster = () => {
|
||||||
const [cacheBuster, setCacheBuster] = useState<boolean>(false);
|
const [cacheBuster, setCacheBuster] = useState(0);
|
||||||
|
|
||||||
const updateCacheBuster = useCallback(() => {
|
const updateCacheBuster = useCallback(() => {
|
||||||
setCacheBuster((current) => !current);
|
setCacheBuster((current) => current + 1);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return [cacheBuster, updateCacheBuster] as const;
|
return [cacheBuster, updateCacheBuster] as const;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user