23008670f3
CI / Build & Quality Checks (push) Successful in 10m13s
- i18next 23->26 + react-i18next 15->17 - prettier 2->3, reformat all files - replace @fontsource/inter with @fontsource-variable/inter 5, update import path - domhandler 5->6 (aligns with transitive deps) - lint-staged 16->17
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { BaseRange, Editor } from 'slate';
|
|
|
|
export enum AutocompletePrefix {
|
|
RoomMention = '#',
|
|
UserMention = '@',
|
|
Emoticon = ':',
|
|
Command = '/',
|
|
}
|
|
export const AUTOCOMPLETE_PREFIXES: readonly AutocompletePrefix[] = [
|
|
AutocompletePrefix.RoomMention,
|
|
AutocompletePrefix.UserMention,
|
|
AutocompletePrefix.Emoticon,
|
|
AutocompletePrefix.Command,
|
|
];
|
|
|
|
export type AutocompleteQuery<TPrefix extends string> = {
|
|
range: BaseRange;
|
|
prefix: TPrefix;
|
|
text: string;
|
|
};
|
|
|
|
export const getAutocompletePrefix = <TPrefix extends string>(
|
|
editor: Editor,
|
|
queryRange: BaseRange,
|
|
validPrefixes: readonly TPrefix[],
|
|
): TPrefix | undefined => {
|
|
const world = Editor.string(editor, queryRange);
|
|
return validPrefixes.find((p) => world.startsWith(p));
|
|
};
|
|
|
|
export const getAutocompleteQueryText = (
|
|
editor: Editor,
|
|
queryRange: BaseRange,
|
|
prefix: string,
|
|
): string => Editor.string(editor, queryRange).slice(prefix.length);
|
|
|
|
export const getAutocompleteQuery = <TPrefix extends string>(
|
|
editor: Editor,
|
|
queryRange: BaseRange,
|
|
validPrefixes: readonly TPrefix[],
|
|
): AutocompleteQuery<TPrefix> | undefined => {
|
|
const prefix = getAutocompletePrefix(editor, queryRange, validPrefixes);
|
|
if (!prefix) return undefined;
|
|
return {
|
|
range: queryRange,
|
|
prefix,
|
|
text: getAutocompleteQueryText(editor, queryRange, prefix),
|
|
};
|
|
};
|