import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import {
LexicalTypeaheadMenuPlugin,
MenuTextMatch,
MenuOption,
useBasicTypeaheadTriggerMatch,
} from '@lexical/react/LexicalTypeaheadMenuPlugin';
import { TextNode } from 'lexical';
import { useCallback, useEffect, useMemo, useState } from 'react';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { renderToStaticMarkup } from 'react-dom/server';
import { $createMentionNode } from '../nodes/MentionNode';
import { $createAutoLinkNode } from '@lexical/link';
import './MentionsPlugin.css';
type SearchData = (p: string) => Promise;
type OffsetCard = {
leftOffset: number;
topOffset: number;
};
type PopoverCard = {
card: (data: A) => JSX.Element;
offset: OffsetCard;
};
type UserCard = {
card: JSX.Element;
offset: OffsetCard;
};
type GetTypeaheadValues = (result: A) => {
url: string;
value: string;
picture: JSX.Element;
popoverCard?: PopoverCard;
};
const PUNCTUATION =
'\\.,\\+\\*\\?\\$\\@\\|#{}\\(\\)\\^\\-\\[\\]\\\\/!%\'"~=<>_:;';
const NAME = '\\b[A-Z][^\\s' + PUNCTUATION + ']';
const DocumentMentionsRegex = {
NAME,
PUNCTUATION,
};
const CapitalizedNameMentionsRegex = new RegExp(
'(^|[^#])((?:' + DocumentMentionsRegex.NAME + '{' + 1 + ',})$)'
);
const PUNC = DocumentMentionsRegex.PUNCTUATION;
const TRIGGERS = ['@'].join('');
// Chars we expect to see in a mention (non-space, non-punctuation).
const VALID_CHARS = '[^' + TRIGGERS + PUNC + '\\s]';
// Non-standard series of chars. Each series must be preceded and followed by
// a valid char.
const VALID_JOINS =
'(?:' +
'\\.[ |$]|' + // E.g. "r. " in "Mr. Smith"
' |' + // E.g. " " in "Josh Duck"
'[' +
PUNC +
']|' + // E.g. "-' in "Salier-Hellendag"
')';
const LENGTH_LIMIT = 75;
const AtSignMentionsRegex = new RegExp(
'(^|\\s|\\()(' +
'[' +
TRIGGERS +
']' +
'((?:' +
VALID_CHARS +
VALID_JOINS +
'){0,' +
LENGTH_LIMIT +
'})' +
')$'
);
// 50 is the longest alias length limit.
const ALIAS_LENGTH_LIMIT = 50;
// Regex used to match alias.
const AtSignMentionsRegexAliasRegex = new RegExp(
'(^|\\s|\\()(' +
'[' +
TRIGGERS +
']' +
'((?:' +
VALID_CHARS +
'){0,' +
ALIAS_LENGTH_LIMIT +
'})' +
')$'
);
// At most, 5 suggestions are shown in the popup.
const SUGGESTION_LIST_LENGTH_LIMIT = 5;
const mentionsCache = new Map();
function useMentionLookupService(
mentionString: string | null,
mentionData: SearchData
) {
const [results, setResults] = useState>([]);
useEffect(() => {
const cachedResults = mentionsCache.get(mentionString);
if (mentionString == null) {
setResults([]);
return;
}
if (cachedResults === null) {
return;
} else if (cachedResults !== undefined) {
setResults(cachedResults);
return;
}
mentionsCache.set(mentionString, null);
mentionData(mentionString).then((results) => {
mentionsCache.set(mentionString, results);
setResults(results);
});
}, [mentionString]);
return results;
}
function checkForCapitalizedNameMentions(
text: string,
minMatchLength: number
): MenuTextMatch | null {
const match = CapitalizedNameMentionsRegex.exec(text);
if (match !== null) {
// The strategy ignores leading whitespace but we need to know it's
// length to add it to the leadOffset
const maybeLeadingWhitespace = match[1];
const matchingString = match[2];
if (matchingString != null && matchingString.length >= minMatchLength) {
return {
leadOffset: match.index + maybeLeadingWhitespace.length,
matchingString,
replaceableString: matchingString,
};
}
}
return null;
}
function checkForAtSignMentions(
text: string,
minMatchLength: number
): MenuTextMatch | null {
let match = AtSignMentionsRegex.exec(text);
if (match === null) {
match = AtSignMentionsRegexAliasRegex.exec(text);
}
if (match !== null) {
// The strategy ignores leading whitespace but we need to know it's
// length to add it to the leadOffset
const maybeLeadingWhitespace = match[1];
const matchingString = match[3];
if (matchingString.length >= minMatchLength) {
return {
leadOffset: match.index + maybeLeadingWhitespace.length,
matchingString,
replaceableString: match[2],
};
}
}
return null;
}
function getPossibleMenuTextMatch(text: string): MenuTextMatch | null {
const match = checkForAtSignMentions(text, 1);
return match === null ? checkForCapitalizedNameMentions(text, 3) : match;
}
class MentionMenuOption extends MenuOption {
name: string;
picture: JSX.Element;
url: string;
userCard: UserCard;
constructor(
name: string,
picture: JSX.Element,
url?: string,
userCard?: UserCard
) {
super(name);
this.name = name;
this.picture = picture;
this.url = url;
this.userCard = userCard;
}
}
function MentionsTypeaheadMenuItem({
index,
isSelected,
onClick,
onMouseEnter,
option,
}: {
index: number;
isSelected: boolean;
onClick: () => void;
onMouseEnter: () => void;
option: MentionMenuOption;
}) {
let className = 'item';
if (isSelected) {
className += ' selected';
}
return (
{option.picture}
{option.name}
);
}
export default function MentionsPlugin(props: {
searchData: SearchData;
getTypeaheadValues: GetTypeaheadValues;
}): JSX.Element | null {
const { searchData, getTypeaheadValues } = props;
const [editor] = useLexicalComposerContext();
const [queryString, setQueryString] = useState(null);
const results = useMentionLookupService(queryString, searchData);
const checkForSlashTriggerMatch = useBasicTypeaheadTriggerMatch('/', {
minLength: 0,
});
const options = useMemo(
() =>
results
.map(
(result) =>
new MentionMenuOption(
getTypeaheadValues(result).value,
getTypeaheadValues(result).picture,
getTypeaheadValues(result).url,
{
card: getTypeaheadValues(result).popoverCard.card(result),
offset: {
leftOffset:
getTypeaheadValues(result).popoverCard.offset.leftOffset,
topOffset:
getTypeaheadValues(result).popoverCard.offset.topOffset,
},
}
)
)
.slice(0, SUGGESTION_LIST_LENGTH_LIMIT),
[results]
);
const onSelectOption = useCallback(
(
selectedOption: MentionMenuOption,
nodeToReplace: TextNode | null,
closeMenu: () => void
) => {
editor.update(() => {
if (nodeToReplace) {
const popover = document.createElement('div');
const staticElement = renderToStaticMarkup(
selectedOption.userCard.card
);
popover.innerHTML = staticElement;
const mentionNode = $createMentionNode(`@${selectedOption.name}`, {
card: popover,
leftOffset: selectedOption.userCard.offset.leftOffset,
topOffset: selectedOption.userCard.offset.topOffset,
});
const linkNode = $createAutoLinkNode(selectedOption.url);
linkNode.append(mentionNode);
nodeToReplace.replace(linkNode);
linkNode.select();
}
closeMenu();
});
},
[editor]
);
const checkForMentionMatch = useCallback(
(text: string) => {
const mentionMatch = getPossibleMenuTextMatch(text);
const slashMatch = checkForSlashTriggerMatch(text, editor);
return !slashMatch && mentionMatch ? mentionMatch : null;
},
[checkForSlashTriggerMatch, editor]
);
return (
onQueryChange={setQueryString}
onSelectOption={onSelectOption}
triggerFn={checkForMentionMatch}
options={options}
menuRenderFn={(
anchorElementRef,
{ selectedIndex, selectOptionAndCleanUp, setHighlightedIndex }
) =>
anchorElementRef && results.length
? ReactDOM.createPortal(
{options.map((option, i: number) => (
{
setHighlightedIndex(i);
selectOptionAndCleanUp(option);
}}
onMouseEnter={() => {
setHighlightedIndex(i);
}}
key={option.key}
option={option}
/>
))}
,
anchorElementRef.current
)
: null
}
/>
);
}