A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import UnicodeEmojis from '@app/features/expressions/utils/UnicodeEmojis';
import {
$captureSelectionOffsets,
$getComposerNodeDisplayStart,
$selectComposerNodeBoundary,
$selectComposerRange,
} from '@app/features/lexical/composer/composerOffsets';
import {$isComposerBlockquoteLineNode} from '@app/features/lexical/composer/nodes/ComposerBlockquoteLineNode';
import {$createComposerCustomEmojiNode} from '@app/features/lexical/composer/nodes/ComposerCustomEmojiNode';
import {
$createComposerStandardEmojiNode,
$isComposerStandardEmojiNode,
} from '@app/features/lexical/composer/nodes/ComposerStandardEmojiNode';
import {$isSyntaxMarkerNode} from '@app/features/lexical/composer/nodes/SyntaxMarkerNode';
import {findTypedEmojiShortcode, type TypedEmojiMatch} from '@app/features/messaging/utils/markdown/TypedEmojiMatch';
import {isReactionShorthandText} from '@app/features/messaging/utils/ReactionShorthandUtils';
import type {ResolvedTypedEmoji} from '@app/features/messaging/utils/TypedEmojiShortcodeUtils';
import {$getRoot, type LexicalEditor, TextNode} from 'lexical';
export type ComposerEmojiResolver = (shortcodeName: string) => ResolvedTypedEmoji | null;
export function registerComposerEmojiShortcode(editor: LexicalEditor, resolve: ComposerEmojiResolver): () => void {
return editor.registerNodeTransform(TextNode, (node) => {
if (!editor.isComposing()) {
$convertEmojiShortcode(node, resolve);
}
});
}
function findUnicodeEmoji(text: string, startIndex: number): TypedEmojiMatch | null {
const matches = UnicodeEmojis.matchEmojiSurrogates(text, startIndex);
let match = matches.next().value;
if (match && UnicodeEmojis.isInsideRegionalIndicatorPair(text, match.end)) {
match = matches.next().value;
}
if (!match) {
return null;
}
const name = match.name;
if (!name) {
return null;
}
return {start: match.start, end: match.end, name};
}
interface EmojiToken extends TypedEmojiMatch {
surrogate: boolean;
}
function findNextEmojiToken(text: string, startIndex: number): EmojiToken | null {
const shortcode = findTypedEmojiShortcode(text, startIndex);
const surrogate = findUnicodeEmoji(text, startIndex);
if (shortcode == null) {
return surrogate == null ? null : {...surrogate, surrogate: true};
}
if (surrogate == null) {
return {...shortcode, surrogate: false};
}
return surrogate.start < shortcode.start ? {...surrogate, surrogate: true} : {...shortcode, surrogate: false};
}
interface EmojiTokenEdit {
start: number;
end: number;
replacement: ResolvedTypedEmoji | string;
}
function isEscapedAt(text: string, index: number): boolean {
let backslashes = 0;
let cursor = index - 1;
while (cursor >= 0 && text[cursor] === '\\') {
backslashes += 1;
cursor -= 1;
}
return backslashes % 2 === 1;
}
function $convertEmojiShortcode(node: TextNode, resolve: ComposerEmojiResolver): void {
if ($isSyntaxMarkerNode(node) || node.hasFormat('code') || isReactionShorthandText($getRoot().getTextContent())) {
return;
}
const parent = node.getParent();
if (parent == null || (parent.getType() !== 'paragraph' && !$isComposerBlockquoteLineNode(parent))) {
return;
}
const text = node.getTextContent();
const edits: Array<EmojiTokenEdit> = [];
let searchFrom = 0;
let backticks = 0;
while (true) {
const match = findNextEmojiToken(text, searchFrom);
if (match == null) {
break;
}
for (let index = searchFrom; index < match.start; index++) {
if (text[index] === '`') {
backticks += 1;
}
}
searchFrom = match.end;
if (backticks % 2 === 1) {
continue;
}
if (isEscapedAt(text, match.start)) {
if (match.surrogate) {
edits.push({start: match.start, end: match.end, replacement: `:${match.name}:`});
}
continue;
}
const previous = match.start === 0 ? node.getPreviousSibling() : null;
if (/^skin-tone-[1-5]$/.test(match.name) && $isComposerStandardEmojiNode(previous)) {
const baseName = previous.getEmojiName().split('::')[0]!;
const combined = resolve(`${baseName}::${match.name}`);
if (combined != null && combined.kind === 'standard') {
const selection = $captureSelectionOffsets();
const previousDisplayStart = $getComposerNodeDisplayStart(previous);
const previousOldLen = previous.getTextContentSize();
const segments = node.splitText(match.start, match.end);
const target = segments[0];
if (target == null) {
return;
}
const replacement = $createComposerStandardEmojiNode(
combined.name,
combined.surrogate,
combined.url,
combined.display,
);
previous.replace(replacement);
target.remove();
if (selection != null && previousDisplayStart != null) {
const adjusted = $adjustEmbedCaret(
selection,
previousDisplayStart,
0,
previousOldLen + (match.end - match.start),
combined.display.length,
);
$selectComposerRange(adjusted.anchor, adjusted.focus);
} else {
$selectComposerNodeBoundary(replacement, 'after');
}
return;
}
}
const resolved = resolve(match.name);
if (resolved != null) {
edits.push({start: match.start, end: match.end, replacement: resolved});
} else if (!match.surrogate) {
searchFrom = match.end - 1;
}
}
if (edits.length === 0) {
return;
}
let selection = $captureSelectionOffsets();
const nodeDisplayStart = $getComposerNodeDisplayStart(node);
for (let index = edits.length - 1; index >= 0; index--) {
const {start, end, replacement} = edits[index]!;
if (typeof replacement === 'string') {
node.spliceText(start, end - start, replacement);
} else {
const emojiNode =
replacement.kind === 'standard'
? $createComposerStandardEmojiNode(
replacement.name,
replacement.surrogate,
replacement.url,
replacement.display,
)
: $createComposerCustomEmojiNode(
replacement.emojiId,
replacement.animated,
replacement.display,
replacement.wire,
);
const target = node.splitText(start, end)[start > 0 ? 1 : 0];
if (target == null) {
break;
}
target.replace(emojiNode);
}
if (selection != null) {
const newLen = typeof replacement === 'string' ? replacement.length : replacement.display.length;
selection = $adjustEmbedCaret(selection, nodeDisplayStart, start, end, newLen);
}
}
if (selection != null) {
$selectComposerRange(selection.anchor, selection.focus);
}
}
function $adjustEmbedCaret(
selection: {anchor: number; focus: number},
nodeDisplayStart: number | null,
tokenStart: number,
tokenEnd: number,
newLen: number,
): {anchor: number; focus: number} {
if (nodeDisplayStart == null) {
return selection;
}
const start = nodeDisplayStart + tokenStart;
const end = nodeDisplayStart + tokenEnd;
const delta = newLen - (tokenEnd - tokenStart);
const adjust = (offset: number): number => {
if (offset <= start) {
return offset;
}
if (offset >= end) {
return offset + delta;
}
return start + newLen;
};
return {anchor: adjust(selection.anchor), focus: adjust(selection.focus)};
}