-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from DiggesT/feature/emojis
Emojis picker plugin
- Loading branch information
Showing
6 changed files
with
16,833 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/** | ||
* 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<string>; | ||
|
||
constructor( | ||
title: string, | ||
emoji: string, | ||
options: { | ||
keywords?: Array<string>; | ||
} | ||
) { | ||
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 ( | ||
<li | ||
key={option.key} | ||
tabIndex={-1} | ||
className={className} | ||
ref={option.setRefElement} | ||
role="option" | ||
aria-selected={isSelected} | ||
id={'typeahead-item-' + index} | ||
onMouseEnter={onMouseEnter} | ||
onClick={onClick} | ||
> | ||
<span className="text"> | ||
{option.emoji} {option.title} | ||
</span> | ||
</li> | ||
); | ||
} | ||
|
||
type Emoji = { | ||
emoji: string; | ||
description: string; | ||
category: string; | ||
aliases: Array<string>; | ||
tags: Array<string>; | ||
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<string | null>(null); | ||
const [emojis, setEmojis] = useState<Array<Emoji>>([]); | ||
|
||
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<EmojiOption> = 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 ( | ||
<LexicalTypeaheadMenuPlugin | ||
onQueryChange={setQueryString} | ||
onSelectOption={onSelectOption} | ||
triggerFn={checkForTriggerMatch} | ||
options={options} | ||
menuRenderFn={( | ||
anchorElementRef, | ||
{ selectedIndex, selectOptionAndCleanUp, setHighlightedIndex } | ||
) => { | ||
if (anchorElementRef.current == null || options.length === 0) { | ||
return null; | ||
} | ||
|
||
return anchorElementRef.current && options.length | ||
? ReactDOM.createPortal( | ||
<div className="typeahead-popover emoji-menu"> | ||
<ul> | ||
{options.map((option: EmojiOption, index) => ( | ||
<div key={option.key}> | ||
<EmojiMenuItem | ||
index={index} | ||
isSelected={selectedIndex === index} | ||
onClick={() => { | ||
setHighlightedIndex(index); | ||
selectOptionAndCleanUp(option); | ||
}} | ||
onMouseEnter={() => { | ||
setHighlightedIndex(index); | ||
}} | ||
option={option} | ||
/> | ||
</div> | ||
))} | ||
</ul> | ||
</div>, | ||
anchorElementRef.current | ||
) | ||
: null; | ||
}} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.