Attachment Modal: Fix caption length calculation (#2226)

This commit is contained in:
Alexander Zinchuk 2022-12-27 02:46:13 +01:00
parent 7a908c702a
commit 439222f951
7 changed files with 29 additions and 6 deletions

View File

@ -1,5 +1,5 @@
import React, {
memo, useCallback, useEffect, useRef,
memo, useCallback, useEffect, useMemo, useRef,
} from '../../../lib/teact/teact';
import { getActions } from '../../../global';
@ -16,6 +16,7 @@ import { getFileExtension } from '../../common/helpers/documentInfo';
import captureEscKeyListener from '../../../util/captureEscKeyListener';
import getFilesFromDataTransferItems from './helpers/getFilesFromDataTransferItems';
import { hasPreview } from '../../../util/files';
import { getHtmlTextLength } from './helpers/getHtmlTextLength';
import usePrevious from '../../../hooks/usePrevious';
import useMentionTooltip from './hooks/useMentionTooltip';
@ -63,6 +64,7 @@ export type OwnProps = {
};
const DROP_LEAVE_TIMEOUT_MS = 150;
const CAPTION_SYMBOLS_LEFT_THRESHOLD = 100;
const AttachmentModal: FC<OwnProps> = ({
chatId,
@ -200,6 +202,11 @@ const AttachmentModal: FC<OwnProps> = ({
}
}
const leftChars = useMemo(() => {
const captionLeftBeforeLimit = captionLimit - getHtmlTextLength(caption);
return captionLeftBeforeLimit <= CAPTION_SYMBOLS_LEFT_THRESHOLD ? captionLeftBeforeLimit : undefined;
}, [caption, captionLimit]);
if (!renderingAttachments) {
return undefined;
}
@ -257,8 +264,6 @@ const AttachmentModal: FC<OwnProps> = ({
);
}
const leftChars = (captionLimit - caption.length) <= 100 ? captionLimit - caption.length : undefined;
return (
<Modal
isOpen={isOpen}

View File

@ -502,7 +502,7 @@ const MessageInput: FC<OwnProps & StateProps> = ({
<div ref={cloneRef} className={buildClassName(className, 'clone')} dir="auto" />
</div>
</div>
{captionLimit && (
{captionLimit !== undefined && (
<div className="max-length-indicator" dir="auto">
{captionLimit}
</div>

View File

@ -0,0 +1,12 @@
import { fixImageContent } from '../../../../util/parseMessageInput';
const div = document.createElement('div');
export function getHtmlTextLength(html: string) {
div.innerHTML = html;
fixImageContent(div);
div.querySelectorAll('br').forEach((br) => {
br.replaceWith('\n');
});
return div.textContent?.trim().length || 0;
}

View File

@ -40,6 +40,11 @@
overflow: hidden;
text-overflow: ellipsis;
margin-right: 0.375rem;
.emoji-small {
width: 1rem !important;
height: 1rem !important;
}
}
.icon-channelviews {

View File

@ -16,7 +16,7 @@ export default function calculateAuthorWidth(text: string) {
document.body.appendChild(element);
}
element.innerHTML = text;
element.textContent = text;
return element.offsetWidth;
}

View File

@ -3,6 +3,7 @@
right: 0.75rem;
bottom: -0.5625rem;
padding: 0 0.25rem;
border-radius: 0.25rem;
color: var(--color-text-secondary);
font-size: 0.75rem;
background: var(--color-background);

View File

@ -63,7 +63,7 @@ export default function parseMessageInput(
};
}
function fixImageContent(fragment: HTMLDivElement) {
export function fixImageContent(fragment: HTMLDivElement) {
fragment.querySelectorAll('img').forEach((node) => {
if (node.dataset.documentId) { // Custom Emoji
node.textContent = (node as HTMLImageElement).alt || '';