fix(Avatar): Display correctly any emoji/special character in a avatar initials
This commit is contained in:
parent
2e7c06d045
commit
76a976b5d1
|
@ -55,6 +55,7 @@
|
||||||
"dropbox": "10.7.0",
|
"dropbox": "10.7.0",
|
||||||
"face-api.js": "0.22.2",
|
"face-api.js": "0.22.2",
|
||||||
"focus-visible": "5.1.0",
|
"focus-visible": "5.1.0",
|
||||||
|
"grapheme-splitter": "1.0.4",
|
||||||
"i18n-iso-countries": "6.8.0",
|
"i18n-iso-countries": "6.8.0",
|
||||||
"i18next": "17.0.6",
|
"i18next": "17.0.6",
|
||||||
"i18next-browser-languagedetector": "3.0.1",
|
"i18next-browser-languagedetector": "3.0.1",
|
||||||
|
@ -10820,6 +10821,11 @@
|
||||||
"node": ">=0.4.0"
|
"node": ">=0.4.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/grapheme-splitter": {
|
||||||
|
"version": "1.0.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/grapheme-splitter/-/grapheme-splitter-1.0.4.tgz",
|
||||||
|
"integrity": "sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ=="
|
||||||
|
},
|
||||||
"node_modules/growly": {
|
"node_modules/growly": {
|
||||||
"version": "1.3.0",
|
"version": "1.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/growly/-/growly-1.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/growly/-/growly-1.3.0.tgz",
|
||||||
|
@ -28639,6 +28645,11 @@
|
||||||
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz",
|
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz",
|
||||||
"integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg="
|
"integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg="
|
||||||
},
|
},
|
||||||
|
"grapheme-splitter": {
|
||||||
|
"version": "1.0.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/grapheme-splitter/-/grapheme-splitter-1.0.4.tgz",
|
||||||
|
"integrity": "sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ=="
|
||||||
|
},
|
||||||
"growly": {
|
"growly": {
|
||||||
"version": "1.3.0",
|
"version": "1.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/growly/-/growly-1.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/growly/-/growly-1.3.0.tgz",
|
||||||
|
|
|
@ -60,6 +60,7 @@
|
||||||
"dropbox": "10.7.0",
|
"dropbox": "10.7.0",
|
||||||
"face-api.js": "0.22.2",
|
"face-api.js": "0.22.2",
|
||||||
"focus-visible": "5.1.0",
|
"focus-visible": "5.1.0",
|
||||||
|
"grapheme-splitter": "1.0.4",
|
||||||
"i18n-iso-countries": "6.8.0",
|
"i18n-iso-countries": "6.8.0",
|
||||||
"i18next": "17.0.6",
|
"i18next": "17.0.6",
|
||||||
"i18next-browser-languagedetector": "3.0.1",
|
"i18next-browser-languagedetector": "3.0.1",
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
|
import GraphemeSplitter from 'grapheme-splitter';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
const AVATAR_COLORS = [
|
const AVATAR_COLORS = [
|
||||||
|
@ -13,6 +14,8 @@ const AVATAR_COLORS = [
|
||||||
'#2AA076',
|
'#2AA076',
|
||||||
'#00A8B3'
|
'#00A8B3'
|
||||||
];
|
];
|
||||||
|
const wordSplitRegex = (/\s+|\.+|_+|;+|-+|,+|\|+|\/+|\\+/);
|
||||||
|
const splitter = new GraphemeSplitter();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates the background color of an initials based avatar.
|
* Generates the background color of an initials based avatar.
|
||||||
|
@ -40,6 +43,20 @@ export function getAvatarColor(initials: ?string, customAvatarBackgrounds: Array
|
||||||
return colorsBase[colorIndex];
|
return colorsBase[colorIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the first grapheme from a word, uppercased.
|
||||||
|
*
|
||||||
|
* @param {string} word - The string to get grapheme from.
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
function getFirstGraphemeUpper(word) {
|
||||||
|
if (!word?.length) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return splitter.splitGraphemes(word)[0].toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates initials for a simple string.
|
* Generates initials for a simple string.
|
||||||
*
|
*
|
||||||
|
@ -49,12 +66,7 @@ export function getAvatarColor(initials: ?string, customAvatarBackgrounds: Array
|
||||||
export function getInitials(s: ?string) {
|
export function getInitials(s: ?string) {
|
||||||
// We don't want to use the domain part of an email address, if it is one
|
// We don't want to use the domain part of an email address, if it is one
|
||||||
const initialsBasis = _.split(s, '@')[0];
|
const initialsBasis = _.split(s, '@')[0];
|
||||||
const words = _.words(initialsBasis);
|
const [ firstWord, secondWord ] = initialsBasis.split(wordSplitRegex);
|
||||||
let initials = '';
|
|
||||||
|
|
||||||
for (const w of words) {
|
return getFirstGraphemeUpper(firstWord) + getFirstGraphemeUpper(secondWord);
|
||||||
(initials.length < 2) && (initials += String.fromCodePoint(w.codePointAt(0)).toUpperCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
return initials;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue