diff --git a/resources/js/components/chat/conversation-thread.tsx b/resources/js/components/chat/conversation-thread.tsx index dd6e51d58f..414d197c58 100644 --- a/resources/js/components/chat/conversation-thread.tsx +++ b/resources/js/components/chat/conversation-thread.tsx @@ -24,6 +24,7 @@ import { } from '@/components/chat/call'; import { MentionsBell } from '@/components/chat/mentions-bell'; import { + isBareEmoji, LiftedMessage, MessageImages, MessageLinkCard, @@ -1325,7 +1326,9 @@ function BubbleWithMeta({ ? target.partner.lane : (pulledBy[0]?.lane ?? user.lane); - const isOnPrimary = isMine && status !== 'staged' && status !== 'rejected'; + const isBare = isBareEmoji(message.body); + const isOnPrimary = + isMine && !isBare && status !== 'staged' && status !== 'rejected'; const side = isMine ? 'mine' : 'theirs'; const replyToThis = onReply !== null && bubble.kind === 'message' @@ -1479,7 +1482,13 @@ function BubbleWithMeta({ ? bubble.message.id : undefined } - className={bubbleClass} + className={cn( + bubbleClass, + isBare && [ + 'bg-transparent text-foreground', + isCompact ? 'text-2xl' : 'text-3xl', + ], + )} > images[0] instanceof File; @@ -92,20 +101,27 @@ export function LiftedMessage({ return null; } + const isBare = voice === null && isBareEmoji(message.body); + return (
{voice === null ? ( - + ) : ( )} diff --git a/resources/js/lib/emoji-only.ts b/resources/js/lib/emoji-only.ts new file mode 100644 index 0000000000..5d0555e9d9 --- /dev/null +++ b/resources/js/lib/emoji-only.ts @@ -0,0 +1,40 @@ +const graphemes = new Intl.Segmenter(undefined, { granularity: 'grapheme' }); + +const whitespace = /^\s+$/u; +const pictographic = /^\p{Extended_Pictographic}/u; +const drawnAsEmoji = /\p{Emoji_Presentation}|️|\p{Emoji_Modifier}|‍/u; +const flag = /^\p{Regional_Indicator}{2}$/u; +const keycap = /^[#*0-9]️?⃣$/u; + +/** + * Whether one character as people see it is an emoji. Symbols that browsers draw as text unless asked otherwise, + * like © or ™, don't count. + */ +function isEmoji(grapheme: string): boolean { + return ( + (pictographic.test(grapheme) && drawnAsEmoji.test(grapheme)) || + flag.test(grapheme) || + keycap.test(grapheme) + ); +} + +/** + * Whether a text is nothing but emoji, with any spaces or line breaks between them. + */ +export function isEmojiOnly(text: string): boolean { + let hasEmoji = false; + + for (const { segment } of graphemes.segment(text)) { + if (whitespace.test(segment)) { + continue; + } + + if (!isEmoji(segment)) { + return false; + } + + hasEmoji = true; + } + + return hasEmoji; +} diff --git a/tests/Unit/EmojiOnly.test.ts b/tests/Unit/EmojiOnly.test.ts new file mode 100644 index 0000000000..22dfb28404 --- /dev/null +++ b/tests/Unit/EmojiOnly.test.ts @@ -0,0 +1,33 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { isEmojiOnly } from '../../resources/js/lib/emoji-only.ts'; + +await test('counts a message of only emoji, spaces and line breaks', () => { + assert.equal(isEmojiOnly('🙏'), true); + assert.equal(isEmojiOnly('🔥🔥 🚀'), true); + assert.equal(isEmojiOnly(' 🎉\n🎉 '), true); +}); + +await test('counts skin tones, families, flags, keycaps and emoji-styled symbols', () => { + assert.equal(isEmojiOnly('👍🏽'), true); + assert.equal(isEmojiOnly('👨‍👩‍👧'), true); + assert.equal(isEmojiOnly('🇳🇱'), true); + assert.equal(isEmojiOnly('🏴󠁧󠁢󠁳󠁣󠁴󠁿'), true); + assert.equal(isEmojiOnly('1️⃣'), true); + assert.equal(isEmojiOnly('❤️'), true); +}); + +await test('does not count text, digits, or symbols drawn as text', () => { + assert.equal(isEmojiOnly('thanks 🙏'), false); + assert.equal(isEmojiOnly('🙏!'), false); + assert.equal(isEmojiOnly('42'), false); + assert.equal(isEmojiOnly('#'), false); + assert.equal(isEmojiOnly('©'), false); + assert.equal(isEmojiOnly('™'), false); + assert.equal(isEmojiOnly(':)'), false); +}); + +await test('does not count an empty or blank message', () => { + assert.equal(isEmojiOnly(''), false); + assert.equal(isEmojiOnly(' \n '), false); +});