UNPKG

5.2 kBTypeScriptView Raw
1import type { ExtensionPriority } from '@remirror/core-constants';
2import type { CustomHandlerKeyList, EmptyShape, GetCustomHandler, GetHandler, GetStatic, HandlerKeyList, IfEmpty, IfHasRequiredProperties, Shape, StaticKeyList } from '@remirror/core-types';
3import type { AnyExtensionConstructor, DefaultExtensionOptions } from './';
4import type { HandlerKeyOptions } from './base-class';
5interface DefaultOptionsProps<Options extends Shape = EmptyShape> {
6 /**
7 * The default options.
8 *
9 * All non required options must have a default value provided.
10 *
11 * Please note that as mentioned in this issue
12 * [#624](https://github.com/remirror/remirror/issues/624), partial options
13 * can cause trouble when setting a default.
14 *
15 * If you need to accept `undefined `as an acceptable default option there are
16 * two possible ways to resolve this.
17 *
18 * #### Use `AcceptUndefined`
19 *
20 * This is the preferred solution and should be used instead of the following
21 * `null` union.
22 *
23 * ```ts
24 * import { AcceptUndefined } from 'remirror';
25 *
26 * interface Options {
27 * optional?: AcceptUndefined<string>;
28 * }
29 * ```
30 *
31 * Now when the options are consumed by this decorator there should be no
32 * errors when setting the value to `undefined`.
33 *
34 * #### `null` union
35 *
36 * If you don't mind using nulls in your code then this might appeal to you.
37 *
38 * ```ts
39 * interface Options {
40 * optional?: string | null;
41 * }
42 * ```
43 *
44 * @defaultValue {}
45 */
46 defaultOptions: DefaultExtensionOptions<Options>;
47}
48interface DefaultPriorityProps {
49 /**
50 * The default priority for this extension.
51 *
52 * @defaultValue {}
53 */
54 defaultPriority?: ExtensionPriority;
55}
56interface StaticKeysProps<Options extends Shape = EmptyShape> {
57 /**
58 * The list of all keys which are static and can only be set at the start.
59 */
60 staticKeys: StaticKeyList<Options>;
61}
62/**
63 * This notifies the extension which options are handlers. Handlers typically
64 * represent event handlers that are called in response to something happening.
65 *
66 * An `onChange` option could be a handler. When designing the API I had to
67 * consider that often times, you might want to listen to a handler in several
68 * places.
69 *
70 * A limitation of the static and dynamic options is that there is only one
71 * value per extension. So if there is a `minValue` option and that min value
72 * option is set in the extension then it becomes the value for all consumers of
73 * the extension. Handlers don't have the same expected behaviour. It is
74 * generally expected that you should be able to subscribe to an event in
75 * multiple places.
76 *
77 * In order to make this possible with `remirror` the handlers are automatically
78 * created based on the handler keys you provide. Each handler is an array and
79 * when the handler is called with `this.options.onChange`, each item in the
80 * array is called based on the rules provided.
81 */
82interface HandlerKeysProps<Options extends Shape = EmptyShape> {
83 /**
84 * The list of the option names which are event handlers.
85 */
86 handlerKeys: HandlerKeyList<Options>;
87 /**
88 * Customize how the handler should work.
89 *
90 * This allows you to decide how the handlers will be composed together.
91 * Currently it only support function handlers, but you can tell the extension
92 * to exit early when a certain return value is received.
93 *
94 * ```ts
95 * const handlerOptions = { onChange: { earlyReturnValue: true }};
96 * ```
97 *
98 * The above setting means that onChange will exit early as soon as one of the
99 * methods returns true.
100 */
101 handlerKeyOptions?: MappedHandlerKeyOptions<Options>;
102}
103declare type MappedHandlerKeyOptions<Options extends Shape = EmptyShape> = {
104 [Key in keyof GetHandler<Options>]?: HandlerKeyOptions<ReturnType<GetHandler<Options>[Key]>, Parameters<GetHandler<Options>[Key]>>;
105} & {
106 __ALL__?: HandlerKeyOptions;
107};
108interface CustomHandlerKeysProps<Options extends Shape = EmptyShape> {
109 customHandlerKeys: CustomHandlerKeyList<Options>;
110}
111export declare type ExtensionDecoratorOptions<Options extends Shape = EmptyShape> = DefaultPriorityProps & IfHasRequiredProperties<DefaultExtensionOptions<Options>, DefaultOptionsProps<Options>, Partial<DefaultOptionsProps<Options>>> & IfEmpty<GetStatic<Options>, Partial<StaticKeysProps<Options>>, StaticKeysProps<Options>> & IfEmpty<GetHandler<Options>, Partial<HandlerKeysProps<Options>>, HandlerKeysProps<Options>> & IfEmpty<GetCustomHandler<Options>, Partial<CustomHandlerKeysProps<Options>>, CustomHandlerKeysProps<Options>> & Partial<Remirror.StaticExtensionOptions>;
112/**
113 * A decorator for the remirror extension.
114 *
115 * This adds static properties to the extension constructor.
116 */
117export declare function extension<Options extends Shape = EmptyShape>(options: ExtensionDecoratorOptions<Options>): <Type extends AnyExtensionConstructor>(ReadonlyConstructor: Type) => Type;
118/**
119 * @deprecated use `extension` instead.
120 */
121export declare const extensionDecorator: typeof extension;
122export {};