Add support for emojis in Avatar (#1074)

This commit is contained in:
Oscar Hinton 2021-07-08 16:54:11 +02:00 committed by GitHub
parent e1e2edf2e9
commit 7c9ebed93f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

2
jslib

@ -1 +1 @@
Subproject commit 119699b82c26296cdd518c0c66e0cd3d61586309
Subproject commit 75fff66f98e3d174baf369afbb747c1380fad4d4

View File

@ -58,7 +58,12 @@ export class AvatarComponent implements OnChanges, OnInit {
chars = this.getFirstLetters(upperData, this.charCount);
}
if (chars == null) {
chars = upperData.substr(0, this.charCount);
chars = this.unicodeSafeSubstring(upperData, this.charCount);
}
// If the chars contain an emoji, only show it.
if (chars.match(Utils.regexpEmojiPresentation)) {
chars = chars.match(Utils.regexpEmojiPresentation)[0];
}
const charObj = this.getCharText(chars);
@ -91,7 +96,7 @@ export class AvatarComponent implements OnChanges, OnInit {
if (parts.length > 1) {
let text = '';
for (let i = 0; i < count; i++) {
text += parts[i].substr(0, 1);
text += this.unicodeSafeSubstring(parts[i], 1);
}
return text;
}
@ -125,4 +130,9 @@ export class AvatarComponent implements OnChanges, OnInit {
textTag.style.fontSize = this.fontSize + 'px';
return textTag;
}
private unicodeSafeSubstring(str: string, count: number) {
const characters = str.match(/./ug);
return characters != null ? characters.slice(0, count).join('') : '';
}
}