import { Node } from "@tiptap/core"; import { SuggestionKeyDownProps, SuggestionOptions, SuggestionProps } from "@tiptap/suggestion"; import { ConditionalKeys, RequireAtLeastOne } from "type-fest"; //#region src/factories/create-suggestion-extension.d.ts /** * A type that describes the suggestion node attributes. */ type SuggestionNodeAttributes = { /** * The suggestion node unique identifier to be rendered by the editor as a `data-id` attribute. */ id: number | string; /** * The suggestion node label to be rendered by the editor as a `data-label` attribute and the * display text itself. */ label: string; }; /** * A type that describes the minimal props that an autocomplete dropdown must receive. */ type SuggestionRendererProps = { /** * The list of suggestion items to be rendered by the autocomplete dropdown. */ items: SuggestionProps['items']; /** * The function that must be invoked when a suggestion item is selected. */ command: SuggestionProps['command']; }; /** * A type that describes the forwarded ref that an autocomplete dropdown must implement with * `useImperativeHandle` to handle `keydown` events in the dropdown render function. */ type SuggestionRendererRef = { onKeyDown: (props: SuggestionKeyDownProps) => boolean; }; /** * The options available to customize the extension created by the factory function. */ type SuggestionOptions$1 = { /** * The character that triggers the autocomplete dropdown. */ triggerChar: string; /** * Allows or disallows spaces in suggested items. */ allowSpaces: SuggestionOptions['allowSpaces']; /** * The prefix characters that are allowed to trigger a suggestion. */ allowedPrefixes: SuggestionOptions['allowedPrefixes']; /** * Trigger the autocomplete dropdown at the start of a line only. */ startOfLine: SuggestionOptions['startOfLine']; /** * Define how the suggestion item `aria-label` attribute should be rendered. */ renderAriaLabel?: (attrs: SuggestionNodeAttributes) => string; /** * A render function for the autocomplete dropdown. */ dropdownRenderFn?: SuggestionOptions['render']; /** * The event handler that is fired when the search string has changed. */ onSearchChange?: (query: string, items: TSuggestionItem[]) => TSuggestionItem[] | Promise; /** * The event handler that is fired when a suggestion item is selected. */ onItemSelect?: (item: TSuggestionItem) => void; }; /** * The return type for a suggestion extension created by the factory function. */ type SuggestionExtensionResult = Node>; /** * A factory function responsible for creating different types of suggestion extensions with * flexibility and customizability in mind. * * Extensions created by this factory function render editor nodes with internal `data-id` and * `data-label` attributes (as a way to save and restore the editor nodes data) based on properties * of the same name (minus the `data-` prefix) from the source item type. However, in the event of * unmatched properties between the internal attributes and the source item type, you should * specify the source item type, and use the optional `attributesMapping` option to map the * source properties to the internal `data-id` and `data-label` attributes. * * When an already-inserted suggestion is rendered, its label is resolved by id from the current * items, so it always reflects the latest label, falling back to the saved `data-label` attribute * when the id is no longer present in the items. * * @param type A unique identifier for the suggestion extension type. * @param items The suggestion items, provided either as a static array or as a getter returning the * current items. The editor is created once and the extension is never rebuilt, so a static array * stays fixed for the lifetime of the extension; provide a getter when the items can change over * time, so they are read on demand instead of captured once. * @param attributesMapping An object to map the `data-id` and `data-label` attributes with the * source item type properties. * * @returns A new suggestion extension tailored to a specific use case. */ declare function createSuggestionExtension(type: string, items?: TSuggestionItem[] | (() => TSuggestionItem[]), ...attributesMapping: TSuggestionItem extends SuggestionNodeAttributes ? [] : [RequireAtLeastOne<{ id: ConditionalKeys; label: ConditionalKeys; }>]): SuggestionExtensionResult; //#endregion export { type SuggestionExtensionResult, type SuggestionOptions$1 as SuggestionOptions, type SuggestionRendererProps, type SuggestionRendererRef, createSuggestionExtension }; //# sourceMappingURL=create-suggestion-extension.d.ts.map