Merge pull request #6397 from matrix-org/t3chguy/fix/18040

Respect compound emojis in default avatar initial generation
pull/21833/head
Michael Telatynski 2021-07-19 15:58:40 +01:00 committed by GitHub
commit a3e5d76064
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 17 deletions

View File

@ -18,6 +18,7 @@ import { RoomMember } from "matrix-js-sdk/src/models/room-member";
import { User } from "matrix-js-sdk/src/models/user"; import { User } from "matrix-js-sdk/src/models/user";
import { Room } from "matrix-js-sdk/src/models/room"; import { Room } from "matrix-js-sdk/src/models/room";
import { ResizeMethod } from "matrix-js-sdk/src/@types/partials"; import { ResizeMethod } from "matrix-js-sdk/src/@types/partials";
import { split } from "lodash";
import DMRoomMap from './utils/DMRoomMap'; import DMRoomMap from './utils/DMRoomMap';
import { mediaFromMxc } from "./customisations/Media"; import { mediaFromMxc } from "./customisations/Media";
@ -122,27 +123,13 @@ export function getInitialLetter(name: string): string {
return undefined; return undefined;
} }
let idx = 0;
const initial = name[0]; const initial = name[0];
if ((initial === '@' || initial === '#' || initial === '+') && name[1]) { if ((initial === '@' || initial === '#' || initial === '+') && name[1]) {
idx++; name = name.substring(1);
} }
// string.codePointAt(0) would do this, but that isn't supported by // rely on the grapheme cluster splitter in lodash so that we don't break apart compound emojis
// some browsers (notably PhantomJS). return split(name, "", 1)[0];
let chars = 1;
const first = name.charCodeAt(idx);
// check if its the start of a surrogate pair
if (first >= 0xD800 && first <= 0xDBFF && name[idx+1]) {
const second = name.charCodeAt(idx+1);
if (second >= 0xDC00 && second <= 0xDFFF) {
chars++;
}
}
const firstChar = name.substring(idx, idx+chars);
return firstChar.toUpperCase();
} }
export function avatarUrlForRoom(room: Room, width: number, height: number, resizeMethod?: ResizeMethod) { export function avatarUrlForRoom(room: Room, width: number, height: number, resizeMethod?: ResizeMethod) {