/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; import { LexicalTypeaheadMenuPlugin, MenuOption, useBasicTypeaheadTriggerMatch, } from '@lexical/react/LexicalTypeaheadMenuPlugin'; import { $createTextNode, $getSelection, $isRangeSelection, TextNode, } from 'lexical'; import * as React from 'react'; import { useCallback, useEffect, useMemo, useState } from 'react'; import * as ReactDOM from 'react-dom'; class EmojiOption extends MenuOption { title: string; emoji: string; keywords: Array; constructor( title: string, emoji: string, options: { keywords?: Array; } ) { super(title); this.title = title; this.emoji = emoji; this.keywords = options.keywords || []; } } function EmojiMenuItem({ index, isSelected, onClick, onMouseEnter, option, }: { index: number; isSelected: boolean; onClick: () => void; onMouseEnter: () => void; option: EmojiOption; }) { let className = 'item'; if (isSelected) { className += ' selected'; } return (
  • {option.emoji} {option.title}
  • ); } type Emoji = { emoji: string; description: string; category: string; aliases: Array; tags: Array; unicode_version: string; ios_version: string; skin_tones?: boolean; }; const MAX_EMOJI_SUGGESTION_COUNT = 10; export default function EmojiPickerPlugin() { const [editor] = useLexicalComposerContext(); const [queryString, setQueryString] = useState(null); const [emojis, setEmojis] = useState>([]); useEffect(() => { // @ts-ignore import('../utils/emoji-list.ts').then((file) => setEmojis(file.default)); }, []); const emojiOptions = useMemo( () => emojis != null ? emojis.map( ({ emoji, aliases, tags }) => new EmojiOption(aliases[0], emoji, { keywords: [...aliases, ...tags], }) ) : [], [emojis] ); const checkForTriggerMatch = useBasicTypeaheadTriggerMatch(':', { minLength: 0, }); const options: Array = useMemo(() => { return emojiOptions .filter((option: EmojiOption) => { return queryString != null ? new RegExp(queryString, 'gi').exec(option.title) || option.keywords != null ? option.keywords.some((keyword: string) => new RegExp(queryString, 'gi').exec(keyword) ) : false : emojiOptions; }) .slice(0, MAX_EMOJI_SUGGESTION_COUNT); }, [emojiOptions, queryString]); const onSelectOption = useCallback( ( selectedOption: EmojiOption, nodeToRemove: TextNode | null, closeMenu: () => void ) => { editor.update(() => { const selection = $getSelection(); if (!$isRangeSelection(selection) || selectedOption == null) { return; } if (nodeToRemove) { nodeToRemove.remove(); } selection.insertNodes([$createTextNode(selectedOption.emoji)]); closeMenu(); }); }, [editor] ); return ( { if (anchorElementRef.current == null || options.length === 0) { return null; } return anchorElementRef.current && options.length ? ReactDOM.createPortal(
      {options.map((option: EmojiOption, index) => (
      { setHighlightedIndex(index); selectOptionAndCleanUp(option); }} onMouseEnter={() => { setHighlightedIndex(index); }} option={option} />
      ))}
    , anchorElementRef.current ) : null; }} /> ); }