UNPKG

12 kBTypeScriptView Raw
1import type { AnyFunction, CommandFunction, ConditionalPick, ConditionalReturnPick, EditorSchema, 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 * @template Extension - the extensions being used within the editor
28 * @template 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 * @template Extension - the extensions being used within the editor
113 * @template 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 * @template 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 * @template 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 * The type which gets the active methods from the provided extensions.
158 */
159export 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>;
160/**
161 * The type which gets the attributes for the provided node or mark. It returns
162 * undefined if the node / mark is not active.
163 */
164export 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>;
165/**
166 * Get the names of all available extensions.
167 */
168export declare type GetNameUnion<Extension extends AnyExtension> = GetExtensions<Extension>['name'];
169/**
170 * A utility type for retrieving the name of an extension only when it's a plain
171 * extension.
172 *
173 * @template Extension - the extensions being used within the editor
174 * @template Expanded - auto generated from `Extension`. These are the
175 * fully expanded extensions with all sub extensions automatically provided. You
176 * never need to provide this type as it is automatically calculated.
177 */
178export declare type GetPlainNameUnion<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = Expanded extends AnyPlainExtension ? Expanded['name'] : never;
179/**
180 * A utility type for retrieving the name of an extension only when it's a mark
181 * extension.
182 *
183 * @template Extension - the extensions being used within the editor
184 * @template Expanded - auto generated from `Extension`. These are the
185 * fully expanded extensions with all sub extensions automatically provided. You
186 * never need to provide this type as it is automatically calculated.
187 */
188export declare type GetMarkNameUnion<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = Expanded extends AnyMarkExtension ? Expanded['name'] : never;
189/**
190 * A utility type for retrieving the name of an extension only when it's a node
191 * extension.
192 *
193 * @template Extension - the extensions being used within the editor
194 * @template Expanded - auto generated from `Extension`. These are the
195 * fully expanded extensions with all sub extensions automatically provided. You
196 * never need to provide this type as it is automatically calculated.
197 */
198export declare type GetNodeNameUnion<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = Expanded extends AnyNodeExtension ? Expanded['name'] : never;
199/**
200 * Gets the editor schema from an extension union.
201 */
202export declare type GetSchema<Extension extends AnyExtension> = EditorSchema<LiteralUnion<GetNodeNameUnion<Extension>, string>, LiteralUnion<GetMarkNameUnion<Extension>, string>>;
203declare global {
204 namespace Remirror {
205 /**
206 * A utility type for all the globally available extension names. This is
207 * mainly used to provide autocompletion.
208 */
209 type NameUnion = LiteralUnion<GetNameUnion<Extensions>, string>;
210 /**
211 * A utility type for all the globally available plain extension names. This
212 * is mainly used to provide autocompletion.
213 */
214 type PlainNameUnion = LiteralUnion<GetPlainNameUnion<Extensions>, string>;
215 /**
216 * A utility type for all the globally available node extension names. This
217 * is mainly used to provide autocompletion.
218 */
219 type NodeNameUnion = LiteralUnion<GetNodeNameUnion<Extensions>, string>;
220 /**
221 * A utility type for all the globally available mark extension names. This
222 * is mainly used to provide autocompletion.
223 */
224 type MarkNameUnion = LiteralUnion<GetMarkNameUnion<Extensions>, string>;
225 }
226}
227export {};