mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 01:09:54 +08:00
fix(core): correct emoji extraction logic using regex (#12749)
https://github.com/user-attachments/assets/ef612f34-0388-49a2-bcad-0cac07a5f785 This PR solves the issue where a majority of emoji's are unable to become the document or folders icon. The regex used is below with the test string of a variety of emoji's: https://regex101.com/r/0anB6Z/1 Co-authored-by: DarkSky <25152247+darkskygit@users.noreply.github.com>
This commit is contained in:
@@ -9,7 +9,22 @@ test('extract-emoji-icon', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(extractEmojiIcon('❤️123')).toEqual({
|
expect(extractEmojiIcon('❤️123')).toEqual({
|
||||||
|
emoji: '❤️',
|
||||||
|
rest: '123',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(extractEmojiIcon('➡️456')).toEqual({
|
||||||
|
emoji: '➡️',
|
||||||
|
rest: '456',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(extractEmojiIcon('✈️789')).toEqual({
|
||||||
|
emoji: '✈️',
|
||||||
|
rest: '789',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(extractEmojiIcon('plain text')).toEqual({
|
||||||
emoji: null,
|
emoji: null,
|
||||||
rest: '❤️123',
|
rest: 'plain text',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
import Graphemer from 'graphemer';
|
import Graphemer from 'graphemer';
|
||||||
|
const emojiRe =
|
||||||
|
/(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/g;
|
||||||
|
|
||||||
export function extractEmojiIcon(text: string) {
|
export function extractEmojiIcon(text: string) {
|
||||||
const isStartsWithEmoji = /^(\p{Emoji_Presentation})/u.test(text);
|
emojiRe.lastIndex = 0;
|
||||||
if (isStartsWithEmoji) {
|
const match = emojiRe.exec(text);
|
||||||
|
if (match && match.index === 0) {
|
||||||
// emoji like "👨🏻❤️💋👨🏻" are combined. Graphemer can handle these.
|
// emoji like "👨🏻❤️💋👨🏻" are combined. Graphemer can handle these.
|
||||||
const emojiEnd = Graphemer.nextBreak(text, 0);
|
const emojiEnd = Graphemer.nextBreak(text, 0);
|
||||||
return {
|
return {
|
||||||
emoji: text.substring(0, emojiEnd),
|
emoji: text.slice(0, emojiEnd),
|
||||||
rest: text.substring(emojiEnd),
|
rest: text.slice(emojiEnd),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user