forked from github/cinny
* add commands hook * add commands in editor * add command auto complete menu * add commands in room input * remove old reply code from room input * fix video component css * do not auto focus input on android or ios * fix crash on enable block after selection * fix circular deps in editor * fix autocomplete return focus move editor cursor * remove unwanted keydown from room input * fix emoji alignment in editor * test ipad user agent * refactor isAndroidOrIOS to mobileOrTablet * update slate & slate-react * downgrade slate-react to 0.98.4 0.99.0 has breaking changes with ReactEditor.focus * add sql to readable ext mimetype * fix empty editor formatting gets saved as draft * add option to use enter for newline * remove empty msg draft from atom family * prevent msg ctx menu from open on text selection
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),
|
|
};
|
|
};
|