Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions resources/js/components/chat/conversation-thread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from '@/components/chat/call';
import { MentionsBell } from '@/components/chat/mentions-bell';
import {
isBareEmoji,
LiftedMessage,
MessageImages,
MessageLinkCard,
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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',
],
)}
>
<MessageText
message={message}
Expand Down
26 changes: 21 additions & 5 deletions resources/js/components/chat/message-parts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import type { ShownImage } from '@/components/commit-images';
import { ImageViewer } from '@/components/image-viewer';
import { LinkPreview } from '@/components/link-preview';
import { bodySegments } from '@/lib/code';
import { emojify } from '@/lib/emoji';
import { isEmojiOnly } from '@/lib/emoji-only';
import { isReplySwipe, replyPull, replyThreshold } from '@/lib/reply-swipe';
import { cn, laneTextColor } from '@/lib/utils';
import type {
Expand Down Expand Up @@ -57,6 +59,13 @@ export function MessageText({
);
}

/**
* Whether a message's text is only emoji, counting shortcodes like :pray:, so it shows larger and without a bubble.
*/
export function isBareEmoji(body: string): boolean {
return isEmojiOnly(emojify(body));
}

const isUnsent = (images: MessageImage[] | File[]): images is File[] =>
images[0] instanceof File;

Expand Down Expand Up @@ -92,20 +101,27 @@ export function LiftedMessage({
return null;
}

const isBare = voice === null && isBareEmoji(message.body);

return (
<div
className={cn(
'w-fit max-w-full rounded-lg shadow-[0_12px_30px_-12px_rgb(0_0_0/0.45)]',
'w-fit max-w-full rounded-lg',
voice === null
? 'px-3.5 py-2 text-[15px] wrap-anywhere'
: 'p-2',
isMine
? 'bg-primary text-primary-foreground'
: 'bg-muted text-foreground',
isBare
? 'text-3xl text-foreground drop-shadow-lg'
: [
'shadow-[0_12px_30px_-12px_rgb(0_0_0/0.45)]',
isMine
? 'bg-primary text-primary-foreground'
: 'bg-muted text-foreground',
],
)}
>
{voice === null ? (
<MessageText message={message} isMine={isMine} />
<MessageText message={message} isMine={isMine && !isBare} />
) : (
<VoiceNote voice={voice} />
)}
Expand Down
40 changes: 40 additions & 0 deletions resources/js/lib/emoji-only.ts
Original file line number Diff line number Diff line change
@@ -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;
}
33 changes: 33 additions & 0 deletions tests/Unit/EmojiOnly.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});