mirror of
https://github.com/array-in-a-matrix/xinny.git
synced 2024-05-21 05:37:23 -04:00
2883b4c35b
* focus editor on reply click * fix emoji and sticker img object-fit * fix cursor not moving with autocomplete * stop sanitizing sending plain text body * improve autocomplete query parsing * add escape to turn off active editor toolbar item
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { BaseRange, Editor } from 'slate';
|
|
|
|
export enum AutocompletePrefix {
|
|
RoomMention = '#',
|
|
UserMention = '@',
|
|
Emoticon = ':',
|
|
}
|
|
export const AUTOCOMPLETE_PREFIXES: readonly AutocompletePrefix[] = [
|
|
AutocompletePrefix.RoomMention,
|
|
AutocompletePrefix.UserMention,
|
|
AutocompletePrefix.Emoticon,
|
|
];
|
|
|
|
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),
|
|
};
|
|
};
|