feat: Add a generic type for suggestion items

This commit is contained in:
Ricardo Amaral
2022-03-11 13:11:13 +00:00
committed by bdbch
parent 70cb809702
commit 7cae9673f0

View File

@@ -3,7 +3,7 @@ import { EditorState, Plugin, PluginKey } from 'prosemirror-state'
import { Decoration, DecorationSet, EditorView } from 'prosemirror-view' import { Decoration, DecorationSet, EditorView } from 'prosemirror-view'
import { findSuggestionMatch } from './findSuggestionMatch' import { findSuggestionMatch } from './findSuggestionMatch'
export interface SuggestionOptions { export interface SuggestionOptions<I = any> {
pluginKey?: PluginKey, pluginKey?: PluginKey,
editor: Editor, editor: Editor,
char?: string, char?: string,
@@ -15,18 +15,18 @@ export interface SuggestionOptions {
command?: (props: { command?: (props: {
editor: Editor, editor: Editor,
range: Range, range: Range,
props: any, props: I,
}) => void, }) => void,
items?: (props: { items?: (props: {
query: string, query: string,
editor: Editor, editor: Editor,
}) => any[] | Promise<any[]>, }) => I[] | Promise<I[]>,
render?: () => { render?: () => {
onBeforeStart?: (props: SuggestionProps) => void onBeforeStart?: (props: SuggestionProps<I>) => void
onStart?: (props: SuggestionProps) => void, onStart?: (props: SuggestionProps<I>) => void,
onBeforeUpdate?: (props: SuggestionProps) => void onBeforeUpdate?: (props: SuggestionProps<I>) => void
onUpdate?: (props: SuggestionProps) => void, onUpdate?: (props: SuggestionProps<I>) => void,
onExit?: (props: SuggestionProps) => void, onExit?: (props: SuggestionProps<I>) => void,
onKeyDown?: (props: SuggestionKeyDownProps) => boolean, onKeyDown?: (props: SuggestionKeyDownProps) => boolean,
}, },
allow?: (props: { allow?: (props: {
@@ -36,13 +36,13 @@ export interface SuggestionOptions {
}) => boolean, }) => boolean,
} }
export interface SuggestionProps { export interface SuggestionProps<I = any> {
editor: Editor, editor: Editor,
range: Range, range: Range,
query: string, query: string,
text: string, text: string,
items: any[], items: I[],
command: (props: any) => void, command: (props: I) => void,
decorationNode: Element | null, decorationNode: Element | null,
clientRect: (() => DOMRect) | null, clientRect: (() => DOMRect) | null,
} }
@@ -55,7 +55,7 @@ export interface SuggestionKeyDownProps {
export const SuggestionPluginKey = new PluginKey('suggestion') export const SuggestionPluginKey = new PluginKey('suggestion')
export function Suggestion({ export function Suggestion<I = any>({
pluginKey = SuggestionPluginKey, pluginKey = SuggestionPluginKey,
editor, editor,
char = '@', char = '@',
@@ -68,9 +68,9 @@ export function Suggestion({
items = () => [], items = () => [],
render = () => ({}), render = () => ({}),
allow = () => true, allow = () => true,
}: SuggestionOptions) { }: SuggestionOptions<I>) {
let props: SuggestionProps | undefined let props: SuggestionProps<I> | undefined
const renderer = render?.() const renderer = render?.()
return new Plugin({ return new Plugin({