From 7c9ebed93f09a7188bc43173e8f8f0aa79b041bc Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Thu, 8 Jul 2021 16:54:11 +0200 Subject: [PATCH] Add support for emojis in Avatar (#1074) --- jslib | 2 +- src/app/components/avatar.component.ts | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/jslib b/jslib index 119699b82c..75fff66f98 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit 119699b82c26296cdd518c0c66e0cd3d61586309 +Subproject commit 75fff66f98e3d174baf369afbb747c1380fad4d4 diff --git a/src/app/components/avatar.component.ts b/src/app/components/avatar.component.ts index 83e05ed8c6..1760c521f2 100644 --- a/src/app/components/avatar.component.ts +++ b/src/app/components/avatar.component.ts @@ -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('') : ''; + } }