Fix wrapping emoji for avatars
This commit is contained in:
parent
1d91b109e0
commit
bb7ef5c762
|
@ -5,17 +5,34 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import wrapEmojiText from './wrapEmojiText';
|
import wrapEmojiText from './wrapEmojiText';
|
||||||
|
import emojiRegExp from '../../vendor/emoji/regex';
|
||||||
|
import {MessageEntity} from '../../layer';
|
||||||
|
import getEmojiEntityFromEmoji from './getEmojiEntityFromEmoji';
|
||||||
|
|
||||||
|
const EMOJI_REG_EXP = new RegExp(`(^${emojiRegExp})`);
|
||||||
export default function getAbbreviation(str: string, onlyFirst = false) {
|
export default function getAbbreviation(str: string, onlyFirst = false) {
|
||||||
if(!str) return '';
|
if(!str) return '';
|
||||||
const splitted = str.trim().split(' ');
|
const splitted = str.trim().split(' ');
|
||||||
if(!splitted[0]) return '';
|
if(!splitted[0]) return '';
|
||||||
|
|
||||||
const first = [...splitted[0]][0];
|
const entities: MessageEntity.messageEntityEmoji[] = [];
|
||||||
|
|
||||||
if(onlyFirst || splitted.length === 1) return wrapEmojiText(first);
|
const firstEmojiMatch = splitted[0].match(EMOJI_REG_EXP);
|
||||||
|
const first = firstEmojiMatch?.[0] || splitted[0][0];
|
||||||
|
if(firstEmojiMatch) {
|
||||||
|
entities.push(getEmojiEntityFromEmoji(first));
|
||||||
|
}
|
||||||
|
|
||||||
const last = [...splitted[splitted.length - 1]][0];
|
if(onlyFirst || splitted.length === 1) return wrapEmojiText(first, undefined, entities);
|
||||||
|
|
||||||
return wrapEmojiText(first + last);
|
const lastEmojiMatch = splitted[1].match(EMOJI_REG_EXP);
|
||||||
|
const last = lastEmojiMatch?.[0] || splitted[1][0];
|
||||||
|
if(lastEmojiMatch) {
|
||||||
|
entities.push({
|
||||||
|
...getEmojiEntityFromEmoji(last),
|
||||||
|
offset: last.length
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return wrapEmojiText(first + last, undefined, entities);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,13 @@
|
||||||
* https://github.com/morethanwords/tweb/blob/master/LICENSE
|
* https://github.com/morethanwords/tweb/blob/master/LICENSE
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import {MessageEntity} from '../../layer';
|
||||||
import parseEntities from './parseEntities';
|
import parseEntities from './parseEntities';
|
||||||
import wrapRichText from './wrapRichText';
|
import wrapRichText from './wrapRichText';
|
||||||
|
|
||||||
export default function wrapEmojiText(text: string, isDraft = false) {
|
export default function wrapEmojiText(text: string, isDraft = false, entities?: MessageEntity[]) {
|
||||||
if(!text) return wrapRichText('');
|
if(!text) return wrapRichText('');
|
||||||
|
|
||||||
const entities = parseEntities(text).filter((e) => e._ === 'messageEntityEmoji');
|
entities ??= parseEntities(text).filter((e) => e._ === 'messageEntityEmoji');
|
||||||
return wrapRichText(text, {entities, wrappingDraft: isDraft});
|
return wrapRichText(text, {entities, wrappingDraft: isDraft});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user