Attachment Modal: Fix caption length calculation (#2226)
This commit is contained in:
parent
7a908c702a
commit
439222f951
@ -1,5 +1,5 @@
|
|||||||
import React, {
|
import React, {
|
||||||
memo, useCallback, useEffect, useRef,
|
memo, useCallback, useEffect, useMemo, useRef,
|
||||||
} from '../../../lib/teact/teact';
|
} from '../../../lib/teact/teact';
|
||||||
import { getActions } from '../../../global';
|
import { getActions } from '../../../global';
|
||||||
|
|
||||||
@ -16,6 +16,7 @@ import { getFileExtension } from '../../common/helpers/documentInfo';
|
|||||||
import captureEscKeyListener from '../../../util/captureEscKeyListener';
|
import captureEscKeyListener from '../../../util/captureEscKeyListener';
|
||||||
import getFilesFromDataTransferItems from './helpers/getFilesFromDataTransferItems';
|
import getFilesFromDataTransferItems from './helpers/getFilesFromDataTransferItems';
|
||||||
import { hasPreview } from '../../../util/files';
|
import { hasPreview } from '../../../util/files';
|
||||||
|
import { getHtmlTextLength } from './helpers/getHtmlTextLength';
|
||||||
|
|
||||||
import usePrevious from '../../../hooks/usePrevious';
|
import usePrevious from '../../../hooks/usePrevious';
|
||||||
import useMentionTooltip from './hooks/useMentionTooltip';
|
import useMentionTooltip from './hooks/useMentionTooltip';
|
||||||
@ -63,6 +64,7 @@ export type OwnProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const DROP_LEAVE_TIMEOUT_MS = 150;
|
const DROP_LEAVE_TIMEOUT_MS = 150;
|
||||||
|
const CAPTION_SYMBOLS_LEFT_THRESHOLD = 100;
|
||||||
|
|
||||||
const AttachmentModal: FC<OwnProps> = ({
|
const AttachmentModal: FC<OwnProps> = ({
|
||||||
chatId,
|
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) {
|
if (!renderingAttachments) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -257,8 +264,6 @@ const AttachmentModal: FC<OwnProps> = ({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const leftChars = (captionLimit - caption.length) <= 100 ? captionLimit - caption.length : undefined;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal
|
<Modal
|
||||||
isOpen={isOpen}
|
isOpen={isOpen}
|
||||||
|
|||||||
@ -502,7 +502,7 @@ const MessageInput: FC<OwnProps & StateProps> = ({
|
|||||||
<div ref={cloneRef} className={buildClassName(className, 'clone')} dir="auto" />
|
<div ref={cloneRef} className={buildClassName(className, 'clone')} dir="auto" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{captionLimit && (
|
{captionLimit !== undefined && (
|
||||||
<div className="max-length-indicator" dir="auto">
|
<div className="max-length-indicator" dir="auto">
|
||||||
{captionLimit}
|
{captionLimit}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
12
src/components/middle/composer/helpers/getHtmlTextLength.ts
Normal file
12
src/components/middle/composer/helpers/getHtmlTextLength.ts
Normal 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;
|
||||||
|
}
|
||||||
@ -40,6 +40,11 @@
|
|||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
margin-right: 0.375rem;
|
margin-right: 0.375rem;
|
||||||
|
|
||||||
|
.emoji-small {
|
||||||
|
width: 1rem !important;
|
||||||
|
height: 1rem !important;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.icon-channelviews {
|
.icon-channelviews {
|
||||||
|
|||||||
@ -16,7 +16,7 @@ export default function calculateAuthorWidth(text: string) {
|
|||||||
document.body.appendChild(element);
|
document.body.appendChild(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
element.innerHTML = text;
|
element.textContent = text;
|
||||||
|
|
||||||
return element.offsetWidth;
|
return element.offsetWidth;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
right: 0.75rem;
|
right: 0.75rem;
|
||||||
bottom: -0.5625rem;
|
bottom: -0.5625rem;
|
||||||
padding: 0 0.25rem;
|
padding: 0 0.25rem;
|
||||||
|
border-radius: 0.25rem;
|
||||||
color: var(--color-text-secondary);
|
color: var(--color-text-secondary);
|
||||||
font-size: 0.75rem;
|
font-size: 0.75rem;
|
||||||
background: var(--color-background);
|
background: var(--color-background);
|
||||||
|
|||||||
@ -63,7 +63,7 @@ export default function parseMessageInput(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function fixImageContent(fragment: HTMLDivElement) {
|
export function fixImageContent(fragment: HTMLDivElement) {
|
||||||
fragment.querySelectorAll('img').forEach((node) => {
|
fragment.querySelectorAll('img').forEach((node) => {
|
||||||
if (node.dataset.documentId) { // Custom Emoji
|
if (node.dataset.documentId) { // Custom Emoji
|
||||||
node.textContent = (node as HTMLImageElement).alt || '';
|
node.textContent = (node as HTMLImageElement).alt || '';
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user