-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
651 lines (599 loc) · 18.6 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
declare namespace Malevic {
/**
* Specification for a DOM element
*/
interface NodeSpec {
/**
* Tag name.
*/
type: string;
/**
* Attributes and event listeners.
*/
props: NodeAttrs;
/**
* Child specifications.
*/
children: RecursiveArray<Child>;
}
/**
* Specification for a component virtual node
*/
interface ComponentSpec<TProps = any, TResult = any> {
/**
* A component function.
*/
type: Component<TProps, TResult>;
/**
* Properties of a component.
*/
props: TProps & {key?: any};
/**
* Child specifications.
*/
children: RecursiveArray<Child>;
}
/**
* Specification for a DOM element or a component.
*/
type Spec = NodeSpec | ComponentSpec;
/**
* Component function.
*/
type Component<TProps = any, TResult = Child> = (
props: TProps & {key?: any},
...children: RecursiveArray<Child>
) => TResult | RecursiveArray<TResult> | any;
/**
* A function that returns a spec.
*/
type InlineFunction<TContext = any, TResult = any> = (
context: TContext,
) => TResult;
/**
* Possible specification child type.
*/
type Child = Spec | string | Node | InlineFunction | null;
interface DOMEventListener<
TEvent = Event,
TElement = Element,
TTarget = Element,
> {
(
this: TElement,
e: TEvent & {target: TTarget; currentTarget: TElement},
): void;
}
/**
* Attributes and event listeners of a DOM element.
*/
interface NodeAttrs<TElement = Element, TTarget = Element> {
key?: any;
class?:
| string
| {[cls: string]: any}
| (string | {[cls: string]: any})[];
style?: string | {[prop: string]: any};
/**
* Is invoked when DOM node was created or inserted into DOM.
*/
oncreate?: (el: TElement) => void;
/**
* Is invoked when DOM node and all descendants was updated.
*/
onupdate?: (el: TElement) => void;
/**
* Is invoked when DOM node was created or updated.
*/
onrender?: (el: TElement) => void;
/**
* Is invoked when DOM node was removed.
*/
onremove?: (el: TElement) => void;
[attr: string]: any | DOMEventListener;
onclick?: DOMEventListener<MouseEvent, TElement, TTarget>;
ondblclick?: DOMEventListener<MouseEvent, TElement, TTarget>;
oncontextmenu?: DOMEventListener<MouseEvent, TElement, TTarget>;
onmousedown?: DOMEventListener<MouseEvent, TElement, TTarget>;
onmousemove?: DOMEventListener<MouseEvent, TElement, TTarget>;
onmouseenter?: DOMEventListener<MouseEvent, TElement, TTarget>;
onmouseleave?: DOMEventListener<MouseEvent, TElement, TTarget>;
onmouseup?: DOMEventListener<MouseEvent, TElement, TTarget>;
ontouchstart?: DOMEventListener<TouchEvent, TElement, TTarget>;
ontouchmove?: DOMEventListener<TouchEvent, TElement, TTarget>;
ontouchend?: DOMEventListener<TouchEvent, TElement, TTarget>;
onkeydown?: DOMEventListener<KeyboardEvent, TElement, TTarget>;
onkeyup?: DOMEventListener<KeyboardEvent, TElement, TTarget>;
onkeypress?: DOMEventListener<KeyboardEvent, TElement, TTarget>;
onscroll?: DOMEventListener<Event, TElement, TTarget>;
onwheel?: DOMEventListener<WheelEvent, TElement, TTarget>;
}
type Plugin<TProps, TResult = any> = (props: TProps) => TResult;
interface PluginsAPI<TProps, TResult = any> {
add(
type: Component,
plugin: Plugin<TProps, TResult>,
): PluginsAPI<TProps, TResult>;
}
interface RecursiveArray<T> extends Array<T | RecursiveArray<T>> {}
/**
* Creates a DOM node specification.
* @param tag Tag name.
* @param attrs Attributes and event listeners.
* @param children Child specifications.
*/
function m(
tag: string,
attrs: NodeAttrs,
...children: RecursiveArray<Child>
): NodeSpec;
/**
* Creates a component specification.
* @param component Component function.
* @param props Properties of a component.
* @param children Child specifications.
*/
function m<TProps>(
component: Component<TProps>,
props: TProps & {key?: any},
...children: RecursiveArray<Child>
): ComponentSpec<TProps>;
namespace Animation {
interface AnimationDeclaration<TValue = any, TResult = any> {
/**
* Sets the value to start animation from.
* @param from Value to start animation from.
*/
from(from: TValue): this;
/**
* Adds a new keyframe.
* @param to Keyframe value.
* @param timing Transition timing parameters.
*/
to(to: TValue, timing?: Partial<TimingSpec>): this;
/**
* Sets the initial value, that will be to start
* animation from if there were no previous value.
* @param from Initial value.
*/
initial(from: TValue): this;
/**
* Sets a function, that interpolates between
* start and end transition values.
* @param interpolate Interpolator function.
*/
interpolate(interpolate: Interpolator<any>): this;
/**
* Sets a function that transforms interpolated value
* into an attribute or CSS value.
* @param transformer A transformer function.
*/
output(transformer: (value: TValue) => TResult): this;
/**
* Sets a function to be called when animation ends.
* @param callback Callback function.
*/
done(callback: () => void): this;
}
interface Interpolator<T> {
(from: T, to: T): (t: number) => T;
}
interface TimingSpec {
/**
* Delay (milliseconds).
*/
delay: number;
/**
* Duration (milliseconds).
*/
duration: number;
/**
* Easing function name or custom function.
*/
easing:
| 'linear'
| 'ease'
| 'ease-in'
| 'ease-out'
| 'ease-in-out'
| ((t: number) => number);
}
/**
* Creates a new animation declaration.
* @param to End transition value.
* @param timing Animation timing parameters.
*/
function animate(
to?: any,
timing?: Partial<TimingSpec>,
): AnimationDeclaration;
/**
* Makes component's DOM nodes to be animatable.
* @param type Component function.
*/
function withAnimation<TComponent extends Component>(
type: TComponent,
): TComponent;
}
namespace Canvas {
/**
* Draws a spec on canvas.
* @param context Canvas rendering context.
* @param spec Component spec to draw on canvas.
*/
function draw<TContext extends RenderingContext>(
context: TContext,
spec: Child | RecursiveArray<Child>,
): void;
/**
* Returns component context.
*/
function getContext<
TContext extends RenderingContext = CanvasRenderingContext2D,
>(): TContext;
type RenderingContext =
| CanvasRenderingContext2D
| OffscreenCanvasRenderingContext2D
| ImageBitmapRenderingContext
| WebGLRenderingContext
| WebGL2RenderingContext;
}
namespace DOM {
/**
* Creates or updates child DOM nodes of an element.
* @param element Target element.
* @param spec Child specification or multiple specifications.
*/
function render<TElement extends Element | Document | DocumentFragment>(
element: TElement,
spec: Child | Child[] | RecursiveArray<Child>,
): TElement;
/**
* Synchronizes DOM element with specification.
* @param element Target DOM element.
* @param spec Specification.
*/
function sync<TElement extends Element>(
element: TElement,
spec: Spec,
): TElement;
/**
* Sets text node content.
* @param node Target text node.
* @param spec Specification or string.
*/
function sync(node: Text, spec: Spec | string): Text;
/**
* Destroys virtual DOM assigned to a DOM node.
* @param node Target DOM node.
*/
function teardown(node: Element | Text): void;
interface ComponentContext {
/**
* Specification of a component.
*/
spec: Spec;
/**
* Previous specification of a component.
*/
prev: Spec;
/**
* Store of a component.
* Used to save values between re-renders.
*/
store: any;
/**
* Returns a rendered DOM node.
*/
node: Node;
/**
* Returns a list of rendered DOM siblings.
*/
nodes: Node[];
/**
* Parent DOM node.
*/
parent: Element;
/**
* Sets a callback, that will be invoked when
* DOM node and all descendants will be created.
* @param fn Event listener.
*/
onCreate(fn: (node: Node) => void): void;
/**
* Sets a callback, that will be invoked when
* DOM node and all descendants will be updated.
* @param fn Event listener.
*/
onUpdate(fn: (node: Node) => void): void;
/**
* Sets a callback, that will be invoked when
* DOM node and all descendants will be created or updated.
* @param fn Event listener.
*/
onRender(fn: (node: Node) => void): void;
/**
* Sets a callback, that will be invoked
* when DOM node will be removed.
* @param fn Event listener.
*/
onRemove(fn: (node: Node) => void): void;
/**
* Returns component's store.
* @param defaults Default store values.
*/
getStore<T>(defaults?: T): T;
/**
* Refreshes the component subtree.
*/
refresh(): void;
/**
* Returns a special key, that stops
* updating the component subtree.
*/
leave(): any;
}
interface InlineFunctionContext {
/**
* Parent DOM element.
*/
parent: Element;
/**
* Returns a rendered DOM node.
*/
node: Node;
/**
* Returns rendered DOM nodes.
*/
nodes: Node[];
}
/**
* Returns a context of a component being unboxed.
*/
function getContext(): ComponentContext;
/**
* Creates a component for convenient use in VanillaJS.
* @param fn Component function
*/
function component<TProps = any, TResult = any>(
fn: (
context: ComponentContext,
props: TProps,
...children: RecursiveArray<Child>
) => TResult,
): Component<TProps, TResult>;
interface TagFunction {
(attrs: NodeAttrs, ...children: RecursiveArray<Child>): NodeSpec;
}
interface TagFunction {
(...children: RecursiveArray<Child>): NodeSpec;
}
/**
* Creates a shorthand for `m(tag, attrs, ...children)`
* for use with VanillaJS.
* The resulting function will generate DOM node specifications
* for the tag name specified.
* `attrs` argument can be omitted.
*/
function tag(tag: string): TagFunction;
/**
* By invoking the properties of this object,
* helper functions for generating DOM node specifications
* for corresponding tag names are created.
* This should be convenient for use in VanillaJS.
*/
const tags: {[tag: string]: TagFunction};
interface PluginCreateElementProps {
/**
* DOM element specification.
*/
spec: NodeSpec;
/**
* Parent DOM element.
*/
parent: Element;
}
interface PluginSetAttributeProps {
/**
* DOM element.
*/
element: Element;
/**
* Attribute name.
*/
attr: string;
/**
* Value.
*/
value: any;
/**
* Previous value.
*/
prev: any;
}
const plugins: {
/**
* Plugins for creating DOM elements.
*/
createElement: PluginsAPI<PluginCreateElementProps>;
/**
* Plugins for setting DOM attribute value.
*/
setAttribute: PluginsAPI<PluginSetAttributeProps>;
};
/**
* Creates a specification from a DOM element.
*/
function specFromNode(node: Node): NodeSpec;
}
namespace Forms {
/**
* Makes component's input fields react on value attribute.
* @param type Component function.
*/
function withForms<TComponent extends Component>(
type: TComponent,
): TComponent;
}
namespace State {
/**
* Returns component's state and a function
* for updating state.
* @param initialState Initial state of a component.
*/
function useState<TState extends {[prop: string]: any}>(
initialState: TState,
): {
/**
* Component's state.
*/
state: TState;
/**
* Sets state and refreshes the component.
*/
setState: (newState: Partial<TState>) => void;
};
/**
* Provides an API for reacting on state changes.
* @param type Component function.
*/
function withState<TComponent extends Component>(
type: TComponent,
): TComponent;
}
namespace String {
/**
* Converts specification into a string.
* @param spec DOM element or component specification.
* @param options Stringifying options.
*/
function stringify(
spec: Spec,
options?: Partial<StringifyOptions>,
): string;
/**
* Returns `true` if component
* is being converted to string.
*/
function isStringifying(): boolean;
interface StringifyOptions {
/**
* Characters used for indentation.
*/
indent: string;
/**
* Component's depth
* (count of initial indents)
*/
depth: number;
/**
* Indicates whether empty elements should
* use XML self closing tags.
*/
xmlSelfClosing: boolean;
}
interface PluginStringifyAttributeProps {
/**
* Attribute name.
*/
attr: string;
/**
* Attribute value.
*/
value: any;
}
interface PluginSkipAttributeProps {
/**
* Attribute name.
*/
attr: string;
/**
* Attribute value.
*/
value: any;
}
const plugins: {
/**
* Plugins for converting attribute
* value to string.
*/
stringifyAttribute: PluginsAPI<
PluginStringifyAttributeProps,
string
>;
/**
* Plugins for skipping attributes.
*/
skipAttribute: PluginsAPI<PluginSkipAttributeProps, boolean>;
/**
* Plugins for determining if DOM element is void (empty).
*/
isVoidTag: PluginsAPI<string, boolean>;
};
/**
* Converts string into a value which is safe
* to insert into HTML document.
* @param s Unsafe string value.
*/
function escapeHTML(s: string): string;
}
}
declare module 'malevic' {
export = Malevic;
}
declare module 'malevic/animation' {
export = Malevic.Animation;
}
declare module 'malevic/dom' {
export = Malevic.DOM;
}
declare module 'malevic/forms' {
export = Malevic.Forms;
}
declare module 'malevic/state' {
export = Malevic.State;
}
declare module 'malevic/string' {
export = Malevic.String;
}
declare namespace JSX {
interface IntrinsicElements {
[tag: string]: Malevic.NodeAttrs;
input: Malevic.NodeAttrs<HTMLInputElement, HTMLInputElement> & {
value?: any;
onchange?: Malevic.DOMEventListener<
Event,
HTMLInputElement,
HTMLInputElement
>;
oninput?: Malevic.DOMEventListener<
Event,
HTMLInputElement,
HTMLInputElement
>;
};
textarea: Malevic.NodeAttrs<
HTMLTextAreaElement,
HTMLTextAreaElement
> & {
onchange?: Malevic.DOMEventListener<
Event,
HTMLTextAreaElement,
HTMLTextAreaElement
>;
oninput?: Malevic.DOMEventListener<
Event,
HTMLTextAreaElement,
HTMLTextAreaElement
>;
};
form: Malevic.NodeAttrs<HTMLFormElement, HTMLFormElement> & {
onsubmit?: Malevic.DOMEventListener<
Event,
HTMLFormElement,
HTMLFormElement
>;
};
}
type Element = any;
}