UNPKG

12.9 kBTypeScriptView Raw
1import type { AnyFunction, CommandFunction, ConditionalPick, ConditionalReturnPick, Flavoring, LiteralUnion, NonChainableCommandFunction, ProsemirrorAttributes, StringKey, Transaction, UnionToIntersection } from '@remirror/core-types';
2import type { CommandShape, GetCommands, GetHelpers } from '../types';
3import type { AnyExtension, AnyMarkExtension, AnyNodeExtension, AnyPlainExtension } from './extension';
4export interface ExtensionListProps<Extension extends AnyExtension = AnyExtension> {
5 /**
6 * The extensions property.
7 */
8 readonly extensions: readonly Extension[];
9}
10/**
11 * A utility type which maps the passed in extension command in an action that
12 * is store in the `manager.store.actions.commandName()`.
13 */
14export declare type MapToUnchainedCommand<RawCommands extends Record<string, AnyFunction>> = {
15 [Command in keyof RawCommands]: CommandShape<Parameters<RawCommands[Command]>>;
16};
17/**
18 * A utility type which maps the chained commands.
19 */
20export declare type MapToChainedCommand<RawCommands extends Record<string, AnyFunction>> = {
21 [Command in keyof RawCommands]: ReturnType<RawCommands[Command]> extends NonChainableCommandFunction ? void : (...args: Parameters<RawCommands[Command]>) => any;
22};
23/**
24 * Utility type which receives an extension and provides the type of actions it
25 * makes available.
26 *
27 * @typeParam Extension - the extensions being used within the editor
28 * @typeParam Expanded - auto generated from `Extension`. These are the
29 * fully expanded extensions with all sub extensions automatically provided. You
30 * never need to provide this type as it is automatically calculated.
31 */
32export declare type CommandsFromExtensions<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = UnionToIntersection<MapToUnchainedCommand<GetCommands<Expanded> | GetDecoratedCommands<Expanded>>>;
33/**
34 * This uses a hack available via conditional types and `Distributive
35 * conditional types`. When a conditional is used on a union it distributes the
36 * types so that the union can avoid the case where:
37 *
38 * > access is restricted to members that are common to all types in the union
39 *
40 * A better explanation is available here
41 * https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types
42 */
43declare type GetDecoratedCommands<Type> = Type extends AnyExtension ? ConditionalPick<Type, AnyFunction<CommandFunction>> : never;
44interface UiAnnotation {
45 __uiAnnotation?: never;
46}
47export declare type UiCommandFunction = CommandFunction & UiAnnotation;
48declare type GetDecoratedUiCommands<Type> = Type extends AnyExtension ? ConditionalPick<Type, AnyFunction<UiCommandFunction>> : never;
49/**
50 * Utility type for pulling all the command names from a list.
51 *
52 * TODO - why doesn't this work.
53 */
54export declare type UiCommandNames<Extension extends AnyExtension> = StringKey<ConditionalPick<{
55 [P in keyof UnionToIntersection<GetDecoratedUiCommands<Extension>>]: keyof UnionToIntersection<GetDecoratedUiCommands<Extension>>[P] extends '__uiAnnotation' ? true : false;
56}, true>>;
57export interface ChainedCommandProps {
58 /**
59 * Dispatches the chained commands.
60 *
61 * ```ts
62 * chain.insertText('hello').run();
63 * ```
64 *
65 * This will run all commands in the chain regardless of whether a previous
66 * command was not able to be run.
67 *
68 * If `exitEarly` is set to true the commands will stop running at the first
69 * chainable command which doesn't return true.
70 */
71 run: (options?: {
72 exitEarly?: boolean;
73 }) => void;
74 /**
75 * Applies the updates to the transaction without dispatching the transaction.
76 *
77 * This can be used to update a transaction without applying the update.
78 */
79 tr: () => Transaction;
80 /**
81 * Check to see whether the command chain can be run. Returns true when the
82 * command can be run.
83 *
84 * ```ts
85 * if (chain.insertText('hello').enabled()) {
86 * doSomething();
87 * }
88 * ```
89 */
90 enabled: () => boolean;
91}
92export declare type ChainedIntersection<Extension extends AnyExtension> = UnionToIntersection<MapToChainedCommand<GetCommands<Extension> | GetDecoratedCommands<Extension>>>;
93export declare type ChainedFromExtensions<Extension extends AnyExtension, Chained extends ChainedIntersection<Extension> = ChainedIntersection<Extension>> = _ChainedFromExtensions<Extension, Chained> & ((tr: Transaction) => _ChainedFromExtensions<Extension, Chained>);
94declare type _ChainedFromExtensions<Extension extends AnyExtension, Chained extends ChainedIntersection<Extension> = ChainedIntersection<Extension>> = ChainedCommandProps & {
95 [Command in keyof Chained]: Chained[Command] extends (...args: any[]) => any ? (...args: Parameters<Chained[Command]>) => ChainedFromExtensions<Extension> : never;
96};
97/**
98 * Utility type for pulling all the command names from a list
99 */
100export declare type CommandNames<Extension extends AnyExtension> = StringKey<CommandsFromExtensions<Extension>>;
101/**
102 * A utility type which maps the passed in extension helpers to a method called
103 * with `manager.data.helpers.helperName()`.
104 */
105export declare type MapHelpers<RawHelpers extends Record<string, AnyFunction>> = {
106 [Helper in keyof RawHelpers]: RawHelpers[Helper];
107};
108/**
109 * Utility type which receives an extension and provides the type of helpers it
110 * makes available.
111 *
112 * @typeParam Extension - the extensions being used within the editor
113 * @typeParam Expanded - auto generated from `Extension`. These are the
114 * fully expanded extensions with all sub extensions automatically provided. You
115 * never need to provide this type as it is automatically calculated.
116 */
117export declare type HelpersFromExtensions<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = UnionToIntersection<MapHelpers<GetHelpers<Expanded> | GetDecoratedHelpers<Expanded>>>;
118export declare type HelperAnnotation = Flavoring<'HelperAnnotation'>;
119/**
120 * An annotation which marks decorated helper methods for an extension.
121 */
122export declare type Helper<Type> = Type extends null | undefined ? Type : Type & HelperAnnotation;
123/**
124 * Remove the helper annotation.
125 */
126declare type RemoveHelper<Type> = Type extends Helper<infer T> ? T : Type;
127declare type RemoveHelpers<Type extends Record<string, AnyFunction>> = {
128 [Key in keyof Type]: (...args: Parameters<Type[Key]>) => RemoveHelper<ReturnType<Type[Key]>>;
129};
130/**
131 * A function with a return signature annotated as a helper.
132 */
133export declare type HelperFunction<Type extends HelperAnnotation = HelperAnnotation> = AnyFunction<Type>;
134declare type GetDecoratedHelpers<Type> = Type extends object ? RemoveHelpers<ConditionalReturnPick<Type, HelperAnnotation>> : never;
135/**
136 * Utility type for pulling all the action names from a list
137 */
138export declare type HelperNames<Extension extends AnyExtension> = StringKey<HelpersFromExtensions<Extension>>;
139/**
140 * Removes [[`AnyExtension`]] from an extension union. This can be used to make
141 * typechecking stricter.
142 *
143 * @typeParam Extension - The union of extensions to remove [[`AnyExtension`]] from.
144 */
145export declare type RemoveAny<Extension> = Extension extends Extension ? AnyExtension extends Extension ? never : Extension : never;
146/**
147 * Get the extension type and the extension type of all sub extensions.
148 *
149 * This uses recursive conditional types which are only available in
150 * `typescript@4.1` https://github.com/microsoft/TypeScript/pull/40002
151 *
152 * @typeParam Extension - The union of extensions.
153 */
154export declare type GetExtensions<Extension> = AnyExtension extends Extension ? AnyExtension : Extension extends AnyExtension ? // Now create the union of the provided extension and it's recursively
155Extension | GetExtensions<Extension['~E']> : AnyExtension;
156/**
157 * Extract the function return type if the generic type is a function, otherwise
158 *
159 * @internal
160 *
161 * @example
162 *
163 * ```ts
164 * type A = () => string
165 * type B = UnpackedReturnType<A>
166 * // B = string
167 *
168 * type C = string
169 * type D = UnpackedReturnType<A>
170 * // D = string
171 * ```
172 */
173declare type UnpackedReturnType<MaybeFunction> = MaybeFunction extends (...args: any[]) => infer Returned ? Returned : MaybeFunction;
174/**
175 * Get the union extension type from an array of extensions or from a function that returns an array of extension.
176 *
177 * @example
178 *
179 * ```ts
180 * const extensions = [new BoldExtension(), new ItalicExtension()];
181 * type Extension = UnpackedExtension<typeof extensions>
182 * // Extension = BoldExtension | ItalicExtension
183 * ```
184 *
185 * @example
186 *
187 * ```ts
188 * const extensions = () => [new BoldExtension(), new ItalicExtension()];
189 * type Extension = UnpackedExtension<typeof extensions>
190 * // Extension = BoldExtension | ItalicExtension
191 * ```
192 */
193export declare type UnpackedExtension<Extension extends AnyExtension[] | (() => AnyExtension[])> = UnpackedReturnType<Extension>[number];
194/**
195 * The type which gets the active methods from the provided extensions.
196 */
197export declare type ActiveFromExtensions<Extension extends AnyExtension> = Record<GetNodeNameUnion<Extension> extends never ? string : GetNodeNameUnion<Extension>, (attrs?: ProsemirrorAttributes) => boolean> & Record<GetMarkNameUnion<Extension> extends never ? string : GetMarkNameUnion<Extension>, (attrs?: ProsemirrorAttributes) => boolean>;
198/**
199 * The type which gets the attributes for the provided node or mark. It returns
200 * undefined if the node / mark is not active.
201 */
202export declare type AttrsFromExtensions<Extension extends AnyExtension> = Record<GetNodeNameUnion<Extension> extends never ? string : GetNodeNameUnion<Extension>, (attrs?: ProsemirrorAttributes) => ProsemirrorAttributes | undefined> & Record<GetMarkNameUnion<Extension> extends never ? string : GetMarkNameUnion<Extension>, (attrs?: ProsemirrorAttributes) => ProsemirrorAttributes | undefined>;
203/**
204 * Get the names of all available extensions.
205 */
206export declare type GetNameUnion<Extension extends AnyExtension> = GetExtensions<Extension>['name'];
207/**
208 * A utility type for retrieving the name of an extension only when it's a plain
209 * extension.
210 *
211 * @typeParam Extension - the extensions being used within the editor
212 * @typeParam Expanded - auto generated from `Extension`. These are the
213 * fully expanded extensions with all sub extensions automatically provided. You
214 * never need to provide this type as it is automatically calculated.
215 */
216export declare type GetPlainNameUnion<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = Expanded extends AnyPlainExtension ? Expanded['name'] : never;
217/**
218 * A utility type for retrieving the name of an extension only when it's a mark
219 * extension.
220 *
221 * @typeParam Extension - the extensions being used within the editor
222 * @typeParam Expanded - auto generated from `Extension`. These are the
223 * fully expanded extensions with all sub extensions automatically provided. You
224 * never need to provide this type as it is automatically calculated.
225 */
226export declare type GetMarkNameUnion<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = Expanded extends AnyMarkExtension ? Expanded['name'] : never;
227/**
228 * A utility type for retrieving the name of an extension only when it's a node
229 * extension.
230 *
231 * @typeParam Extension - the extensions being used within the editor
232 * @typeParam Expanded - auto generated from `Extension`. These are the
233 * fully expanded extensions with all sub extensions automatically provided. You
234 * never need to provide this type as it is automatically calculated.
235 */
236export declare type GetNodeNameUnion<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = Expanded extends AnyNodeExtension ? Expanded['name'] : never;
237declare global {
238 namespace Remirror {
239 /**
240 * A utility type for all the globally available extension names. This is
241 * mainly used to provide autocompletion.
242 */
243 type NameUnion = LiteralUnion<GetNameUnion<Extensions>, string>;
244 /**
245 * A utility type for all the globally available plain extension names. This
246 * is mainly used to provide autocompletion.
247 */
248 type PlainNameUnion = LiteralUnion<GetPlainNameUnion<Extensions>, string>;
249 /**
250 * A utility type for all the globally available node extension names. This
251 * is mainly used to provide autocompletion.
252 */
253 type NodeNameUnion = LiteralUnion<GetNodeNameUnion<Extensions>, string>;
254 /**
255 * A utility type for all the globally available mark extension names. This
256 * is mainly used to provide autocompletion.
257 */
258 type MarkNameUnion = LiteralUnion<GetMarkNameUnion<Extensions>, string>;
259 }
260}
261export {};