import type { EditorState } from '@prosekit/pm/state' import { defaultCanMatch } from './autocomplete-helpers.ts' /** * Options for the {@link MatchHandler} callback. */ export interface MatchHandlerOptions { /** * The editor state. */ state: EditorState /** * The result of `RegExp.exec`. */ match: RegExpExecArray /** * The start position of the matched text. */ from: number /** * The end position of the matched text. */ to: number /** * Call this function to ignore the match. You probably want to call this * function when the user presses the `Escape` key. */ ignoreMatch: () => void /** * Call this function to delete the matched text. For example, in a slash * menu, you might want to delete the matched text first then do something * else when the user presses the `Enter` key. */ deleteMatch: () => void } /** * A callback that is called when the rule starts to match, and also on * subsequent updates while the rule continues to match. */ export type MatchHandler = (options: MatchHandlerOptions) => void /** * Options for the {@link CanMatchPredicate} callback. */ export interface CanMatchOptions { /** * The editor state. */ state: EditorState } /** * A predicate to determine if the rule can be applied in the current editor state. */ export type CanMatchPredicate = (options: CanMatchOptions) => boolean /** * Options for creating an {@link AutocompleteRule} */ export interface AutocompleteRuleOptions { /** * The regular expression to match against the text before the cursor. The * last match before the cursor is used. * * For a slash menu, you might use `/(? boolean constructor(options: AutocompleteRuleOptions) { this.regex = options.regex this.onMatch = options.onEnter this.onLeave = options.onLeave this.canMatch = options.canMatch ?? defaultCanMatch } }