1 | import type { AnyFunction, CommandFunction, ConditionalPick, ConditionalReturnPick, Flavoring, LiteralUnion, NonChainableCommandFunction, ProsemirrorAttributes, StringKey, Transaction, UnionToIntersection } from '@remirror/core-types';
|
2 | import type { CommandShape, GetCommands, GetHelpers } from '../types';
|
3 | import type { AnyExtension, AnyMarkExtension, AnyNodeExtension, AnyPlainExtension } from './extension';
|
4 | export interface ExtensionListProps<Extension extends AnyExtension = AnyExtension> {
|
5 | |
6 |
|
7 |
|
8 | readonly extensions: readonly Extension[];
|
9 | }
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export type MapToUnchainedCommand<RawCommands extends Record<string, AnyFunction>> = {
|
15 | [Command in keyof RawCommands]: CommandShape<Parameters<RawCommands[Command]>>;
|
16 | };
|
17 |
|
18 |
|
19 |
|
20 | export 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 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | export type CommandsFromExtensions<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = UnionToIntersection<MapToUnchainedCommand<GetCommands<Expanded> | GetDecoratedCommands<Expanded>>>;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | type GetDecoratedCommands<Type> = Type extends AnyExtension ? ConditionalPick<Type, AnyFunction<CommandFunction>> : never;
|
44 | interface UiAnnotation {
|
45 | __uiAnnotation?: never;
|
46 | }
|
47 | export type UiCommandFunction = CommandFunction & UiAnnotation;
|
48 | type GetDecoratedUiCommands<Type> = Type extends AnyExtension ? ConditionalPick<Type, AnyFunction<UiCommandFunction>> : never;
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | export 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>>;
|
57 | export interface ChainedCommandProps {
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | run: (options?: {
|
72 | exitEarly?: boolean;
|
73 | }) => void;
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 | tr: () => Transaction;
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | enabled: () => boolean;
|
91 | }
|
92 | export type ChainedIntersection<Extension extends AnyExtension> = UnionToIntersection<MapToChainedCommand<GetCommands<Extension> | GetDecoratedCommands<Extension>>>;
|
93 | export type ChainedFromExtensions<Extension extends AnyExtension, Chained extends ChainedIntersection<Extension> = ChainedIntersection<Extension>> = _ChainedFromExtensions<Extension, Chained> & ((tr: Transaction) => _ChainedFromExtensions<Extension, Chained>);
|
94 | 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 |
|
99 |
|
100 | export type CommandNames<Extension extends AnyExtension> = StringKey<CommandsFromExtensions<Extension>>;
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | export type MapHelpers<RawHelpers extends Record<string, AnyFunction>> = {
|
106 | [Helper in keyof RawHelpers]: RawHelpers[Helper];
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | export type HelpersFromExtensions<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = UnionToIntersection<MapHelpers<GetHelpers<Expanded> | GetDecoratedHelpers<Expanded>>>;
|
118 | export type HelperAnnotation = Flavoring<'HelperAnnotation'>;
|
119 |
|
120 |
|
121 |
|
122 | export type Helper<Type> = Type extends null | undefined ? Type : Type & HelperAnnotation;
|
123 |
|
124 |
|
125 |
|
126 | type RemoveHelper<Type> = Type extends Helper<infer T> ? T : Type;
|
127 | type RemoveHelpers<Type extends Record<string, AnyFunction>> = {
|
128 | [Key in keyof Type]: (...args: Parameters<Type[Key]>) => RemoveHelper<ReturnType<Type[Key]>>;
|
129 | };
|
130 |
|
131 |
|
132 |
|
133 | export type HelperFunction<Type extends HelperAnnotation = HelperAnnotation> = AnyFunction<Type>;
|
134 | type GetDecoratedHelpers<Type> = Type extends object ? RemoveHelpers<ConditionalReturnPick<Type, HelperAnnotation>> : never;
|
135 |
|
136 |
|
137 |
|
138 | export type HelperNames<Extension extends AnyExtension> = StringKey<HelpersFromExtensions<Extension>>;
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | export type RemoveAny<Extension> = Extension extends Extension ? AnyExtension extends Extension ? never : Extension : never;
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | export type GetExtensions<Extension> = AnyExtension extends Extension ? AnyExtension : Extension extends AnyExtension ?
|
155 | Extension | GetExtensions<Extension['~E']> : AnyExtension;
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | type UnpackedReturnType<MaybeFunction> = MaybeFunction extends (...args: any[]) => infer Returned ? Returned : MaybeFunction;
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | export type UnpackedExtension<Extension extends AnyExtension[] | (() => AnyExtension[])> = UnpackedReturnType<Extension>[number];
|
194 | /**
|
195 | * The type which gets the active methods from the provided extensions.
|
196 | */
|
197 | export 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 |
|
200 |
|
201 |
|
202 | export 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 |
|
205 |
|
206 | export type GetNameUnion<Extension extends AnyExtension> = GetExtensions<Extension>['name'];
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | export type GetPlainNameUnion<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = Expanded extends AnyPlainExtension ? Expanded['name'] : never;
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | export type GetMarkNameUnion<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = Expanded extends AnyMarkExtension ? Expanded['name'] : never;
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | export type GetNodeNameUnion<Extension extends AnyExtension, Expanded extends AnyExtension = GetExtensions<Extension>> = Expanded extends AnyNodeExtension ? Expanded['name'] : never;
|
237 | declare global {
|
238 | namespace Remirror {
|
239 | |
240 |
|
241 |
|
242 |
|
243 | type NameUnion = LiteralUnion<GetNameUnion<Extensions>, string>;
|
244 | |
245 |
|
246 |
|
247 |
|
248 | type PlainNameUnion = LiteralUnion<GetPlainNameUnion<Extensions>, string>;
|
249 | |
250 |
|
251 |
|
252 |
|
253 | type NodeNameUnion = LiteralUnion<GetNodeNameUnion<Extensions>, string>;
|
254 | |
255 |
|
256 |
|
257 |
|
258 | type MarkNameUnion = LiteralUnion<GetMarkNameUnion<Extensions>, string>;
|
259 | }
|
260 | }
|
261 | export {};
|