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, storage: SuggestionStorage) => TSuggestionItem[] | Promise; /** * The event handler that is fired when a suggestion item is selected. */ onItemSelect?: (item: TSuggestionItem) => void; }; /** * The storage holding the suggestion items original array, and a collection indexed by the item id. */ type SuggestionStorage = Readonly<{ /** * The original array of suggestion items. */ items: TSuggestionItem[]; /** * A collection of suggestion items indexed by the item id. */ itemsById: { readonly [id: SuggestionNodeAttributes['id']]: TSuggestionItem | undefined; }; }>; /** * 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. * * This factory function also stores the suggestion items internally in the editor storage (as-is, * and indexed by an identifier), as a way to make sure that if a previously referenced suggestion * changes its label, the editor will always render the most up-to-date label for the suggestion by * reading it from the storage. An example use case for this is when a user mention is added to the * editor, and the user changed its name afterwards, the editor will always render the most * up-to-date user name for the mention. * * @param type A unique identifier for the suggestion extension type. * @param items An array of suggestion items to be stored in the editor storage. * @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[], ...attributesMapping: TSuggestionItem extends SuggestionNodeAttributes ? [] : [RequireAtLeastOne<{ id: ConditionalKeys; label: ConditionalKeys; }>]): SuggestionExtensionResult; //#endregion export { type SuggestionExtensionResult, type SuggestionOptions$1 as SuggestionOptions, type SuggestionRendererProps, type SuggestionRendererRef, type SuggestionStorage, createSuggestionExtension }; //# sourceMappingURL=create-suggestion-extension.d.ts.map