-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.ts
661 lines (567 loc) · 16.9 KB
/
index.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
652
653
654
655
656
657
658
659
660
661
import HaikuBase from '../HaikuBase';
import {RFO} from '../reflection/functionToRFO';
export interface IHaikuElement extends HaikuBase {
tagName: string|HaikuBytecode;
node: BytecodeNode;
parentNode?: BytecodeNode;
target?: Element;
originX: number;
originY: number;
size: ThreeDimensionalLayoutProperty;
visit: (
iteratee: (component: IHaikuElement) => any,
filter?: (value: IHaikuElement, index: number, array: IHaikuElement[]) => boolean,
) => any;
getComponentId: () => string;
getNearestDefinedNonZeroAncestorSizeX: () => number;
getNearestDefinedNonZeroAncestorSizeY: () => number;
isHovered: boolean;
}
export interface IHaikuClock {
destroy (): void;
getExplicitTime (): number;
getFrameDuration (): number;
getTime (): number;
setTime (time: number): void;
isRunning (): boolean;
run (): void;
start (): void;
assignOptions (options: ClockConfig): void;
GLOBAL_ANIMATION_HARNESS?: {
cancel (): void;
};
}
export interface IHaikuComponent extends IHaikuElement {
bytecode: HaikuBytecode;
config: BytecodeOptions;
patches: BytecodeNode[];
context: IHaikuContext;
doPreserve3d: boolean;
state: {[key in string]: any};
host?: IHaikuComponent;
eachEventHandler: (
iteratee: (eventSelector: string, eventName: string, descriptor: BytecodeEventHandlerDescriptor) => void,
) => void;
callHook: (hookName: string, ...args: any[]) => void;
clearCaches: () => void;
markForFullFlush: () => void;
getClock: () => IHaikuClock;
emitFromRootComponent: (eventName: string, attachedObject: any) => void;
routeEventToHandlerAndEmit: (eventSelectorGiven: string, eventNameGiven: string, eventArgs: any) => void;
routeEventToHandlerAndEmitWithoutBubbling: (
eventSelectorGiven: string,
eventNameGiven: string,
eventArgs: any,
) => void;
getTimelineDescriptor: (timelineName: string) => BytecodeTimeline;
lastHoveredElement: IHaikuElement;
isDeactivated: boolean;
routeEventToHandlerAndEmitWithoutBubblingAndWithoutGlobalHandlers: (...args: any[]) => void;
}
export interface MountLayout {
layout?: {
computed: {
size: {
x: number;
y: number;
};
};
};
}
export interface IRenderer {
mount?: Element;
mountEventListener: (component: IHaikuComponent, selector: string, name: string, listener: Function) => void;
}
export interface IHaikuContext {
// #FIXME: This is not necessarily going to be the renderer.
renderer: IRenderer;
config: BytecodeOptions;
clock: IHaikuClock;
getClock: () => IHaikuClock;
getContainer: (doForceRecalc: boolean) => MountLayout;
getGlobalUserState: () => any;
contextMount: () => void;
contextUnmount: () => void;
tick: () => void;
destroy: () => void;
assignConfig: (config: BytecodeOptions, options?: {skipComponentAssign?: boolean}) => void;
}
export type PrimitiveType = string|number|object|boolean|null;
/**
* Allowed state types, including function return type for timeline functions,
* state getter and setter.
*/
export type BytecodeStateType = PrimitiveType|PrimitiveType[];
/**
* `BytecodeSummonable` defines functions that can be called on timeline
* property value
*/
export interface BytecodeSummonable {
(...params: any[]): BytecodeStateType;
specification: RFO;
}
/**
* All possible types for timeline property value
*/
export type BytecodeInjectable = BytecodeStateType|BytecodeSummonable;
export type BytecodeNodeStyle = {
[key in string]: PrimitiveType;
};
export interface BytecodeNodeAttributes {
[attribute: string]: any;
style?: BytecodeNodeStyle;
/**
* @deprecated
*/
source?: string;
identifier?: string;
}
export type PlaceholderSurrogate = any;
export interface RepeaterSpec {
changed: boolean;
instructions: any[];
repeatees: BytecodeNode[];
}
export interface RepeateeSpec {
index: number;
instructions: any[];
payload: any;
source: BytecodeNode;
}
export interface IfSpec {
answer: boolean;
}
export interface PlaceholderSpec {
surrogate: PlaceholderSurrogate;
}
export interface BytecodeNodeMemoryObject {
context?: IHaikuContext;
element?: IHaikuElement;
containee?: IHaikuComponent;
children?: (string|BytecodeNode)[];
if?: IfSpec;
instance?: IHaikuComponent;
listener?: Function; // Bound event listener function
parent?: BytecodeNode;
placeholder?: PlaceholderSpec;
repeatee?: RepeateeSpec;
repeater?: RepeaterSpec;
scope?: string;
subcomponent?: IHaikuComponent;
targets?: any[]; // DOM (or platform-specific) render targets
}
/**
* Haiku bytecode element tree. eg. <div><svg>...</svg></div>.
* `source` and `identifier` are rarely used.
*/
export interface BytecodeNode {
elementName: string|HaikuBytecode;
attributes: BytecodeNodeAttributes;
isRootNode?: boolean;
layout?: LayoutSpec;
children?: (BytecodeNode|string)[];
__memory?: BytecodeNodeMemoryObject;
/**
* @deprecated
*/
rect?: DomRect;
}
export type MaybeBytecodeNode = BytecodeNode|null;
/**
* Haiku bytecode state.
*/
export interface BytecodeState {
value: BytecodeStateType;
type?: string;
access?: string;
edited?: boolean;
mock?: boolean;
getter?: () => BytecodeStateType;
setter?: (param: BytecodeStateType) => void;
}
/**
* Haiku bytecode state list.
*/
export interface BytecodeStates {
[stateName: string]: BytecodeState;
}
/**
* Haiku bytecode function `handler` for a specific `eventSelector`.
*/
export interface BytecodeEventHandlerCallable {
(target?: any, event?: any): void;
__rfo?: RFO;
}
export interface BytecodeEventHandlerDescriptor {
handler: BytecodeEventHandlerCallable;
}
export interface BytecodeEventHandler {
[eventSelector: string]: BytecodeEventHandlerDescriptor;
}
/**
* Tuples of `elementName` and `BytecodeEventHandler`.
*/
export interface BytecodeEventHandlers {
[elementName: string]: BytecodeEventHandler;
}
/**
* Tuples of `elementName` and `BytecodeEventHandler`.
*/
export interface BytecodeHelpers {
[helperName: string]: (...inputs: any[]) => any;
}
/**
* Value of an element property in a given frame.
*/
export interface BytecodeTimelineValue {
value: BytecodeInjectable;
edited?: boolean;
curve?: CurveDefinition;
}
/**
* Map `frameNum` to a to a `BytecodeTimelineValue`.
*/
export interface BytecodeTimelineProperty {
[frameNum: string]: BytecodeTimelineValue;
}
/**
* Map `propertyName` to a `BytecodeTimelineProperty`
*/
export interface BytecodeTimelineProperties {
[propertyName: string]: BytecodeTimelineProperty;
}
/**
* Tuples of `haikuId` and `BytecodeTimelineProperties`.
*/
export interface BytecodeTimeline {
[haikuId: string]: BytecodeTimelineProperties;
}
/**
* Tuples of `timelineName` and `BytecodeTimeline`.
*/
export interface BytecodeTimelines {
[timelineId: string]: BytecodeTimeline;
}
/**
* Haiku bytecode metadata.
*/
export interface BytecodeMetadata {
root?: string;
folder?: string;
uuid?: string;
player?: string;
type?: string;
name?: string;
relpath?: string;
core?: string;
version?: string;
username?: string;
organization?: string;
project?: string;
branch?: string;
title?: string;
/**
* @deprecated as of 3.2.20
*/
}
export type ComponentEventHandler = (component: IHaikuComponent) => void;
export interface ClockConfig {
frameDuration?: number;
frameDelay?: number;
marginOfErrorForDelta?: number;
run?: boolean;
}
/**
* Bytecode options.
*/
export interface BytecodeOptions {
// Random seed used for producing deterministic randomness
// and namespacing CSS selector behavior
seed?: string;
// Timestamp reflecting the point in time that rendering begin,
// for deterministic timestamp production
timestamp?: number;
// Whitelist of events that should fire during Edit Mode
editModeEvents?: string[];
// Whether we should mount the given context to the mount
// element automatically
automount?: boolean;
// Whether we should begin playing the context's animation automatically
autoplay?: boolean;
// If enabled (e.g. in the Haiku desktop app), bytecode is not cloned when a component is instantiated, and made be
// live-edited.
hotEditingMode?: boolean;
// String used for asset resolution; normally enabled in Haiku desktop.
folder?: string;
// Whether to fully flush the component on every single frame
// (warning: this can severely deoptimize animation)
forceFlush?: boolean;
// Whether we should freeze timelines and not update per global
// timeline; useful in headless
freeze?: boolean;
// Whether we should loop the animation, i.e. restart from
// the first frame after reaching the last
loop?: boolean;
// Optional function that we will call on every frame, provided
// for developer convenience
frame?: (() => void);
hooks?: {[key: string]: Function};
helpers?: {[key: string]: Function};
// Configuration options that will be passed to the HaikuClock instance.
// See HaikuClock.js for info.
clock?: ClockConfig;
// Configures the sizing mode of the component; may be 'normal',
// 'stretch', 'contain', or 'cover'. See HaikuComponent.js for info.
sizing?: string;
// Whether we should always assume the size of the mount will change on every tick. There is a significant
// performance boost for all non-'normal' sizing modes if we *don't* always assume this, but the size of the
// mount might change underneath for reasons other than changes in media queries. To be safe, we leave this on
// by default.
alwaysComputeSizing?: boolean;
// Placeholder for an option to control whether to enable
// preserve-3d mode in DOM environments. [UNUSED]
preserve3d?: string;
// Whether or not the Haiku context menu should display when the
// component is right-clicked; may be 'enabled' or 'disabled'.
contextMenu?: string;
// CSS position setting for the root of the component in DOM;
// recommended to keep as 'relative'.
position?: string;
// CSS overflow-x setting for the component. Convenience for allows user
// to specify the overflow setting without needing a wrapper element.
overflowX?: string;
// CSS overflow-x setting for the component. Convenience for allows user
// to specify the overflow setting without needing a wrapper element.
overflowY?: string;
// CSS overflow setting for the component. Use this OR overflowX/overflowY
overflow?: string;
// If provided, a Mixpanel tracking instance will be created using this
// string as the API token. The default token is Haiku's production token.
mixpanel?: string|false;
// Control how this instance handles interaction, e.g. preview mode
// TODO: create an use an enum from @haiku/core/src/helpers/interactionModes.ts
interactionMode?: number;
// A unique ID used during migrations.
referenceUniqueness?: string;
// Allow states to be passed in at runtime (ASSIGNED)
states?: object;
// Allow custom event handlers to be passed in at runtime (ASSIGNED)
eventHandlers?: object;
// Allow timelines to be passed in at runtime (ASSIGNED)
timelines?: object;
// Allow vanities to be passed in at runtime (ASSIGNED)
vanities?: object;
// Children may be passed in, typically via the React adapter
children?: PrimitiveType[];
// Key/values representing placeholders to inject, usually via React adapter
placeholder?: object;
// Event handlers.
onHaikuComponentWillInitialize?: ComponentEventHandler;
onHaikuComponentDidInitialize?: ComponentEventHandler;
onHaikuComponentDidMount?: ComponentEventHandler;
onHaikuComponentWillUnmount?: ComponentEventHandler;
}
/**
* Bytecode definition. Properties are *rarely* used.
*/
export interface HaikuBytecode {
template: BytecodeNode;
states?: BytecodeStates;
eventHandlers?: BytecodeEventHandlers;
helpers?: BytecodeHelpers;
timelines: BytecodeTimelines;
metadata?: BytecodeMetadata;
methods?: {
[key in string]: Function;
};
/**
* @deprecated as of 3.2.20
*/
properties?: any[];
options?: BytecodeOptions;
}
export type Mat4 = number[];
export interface ThreeDimensionalLayoutProperty {
x: number;
y: number;
z: number;
}
export interface DomRect {
width: number;
height: number;
left: number;
top: number;
}
// The layout specification naming in createLayoutSpec is derived in part from:
// https://github.com/Famous/engine/blob/master/core/Transform.js which is MIT licensed.
export interface LayoutSpec {
shown: boolean;
opacity: number;
offset: ThreeDimensionalLayoutProperty;
origin: ThreeDimensionalLayoutProperty;
translation: ThreeDimensionalLayoutProperty;
rotation: ThreeDimensionalLayoutProperty;
scale: ThreeDimensionalLayoutProperty;
sizeMode: ThreeDimensionalLayoutProperty;
sizeProportional: ThreeDimensionalLayoutProperty;
sizeDifferential: ThreeDimensionalLayoutProperty;
sizeAbsolute: ThreeDimensionalLayoutProperty;
shear: {
xy: number;
xz: number;
yz: number;
};
matrix?: Mat4;
format?: number;
computed?: ComputedLayoutSpec;
}
export interface LayoutSpecPartial {
shown?: boolean;
opacity?: number;
mount?: ThreeDimensionalLayoutProperty;
offset?: ThreeDimensionalLayoutProperty;
origin?: ThreeDimensionalLayoutProperty;
translation?: ThreeDimensionalLayoutProperty;
rotation?: ThreeDimensionalLayoutProperty;
scale?: ThreeDimensionalLayoutProperty;
sizeMode?: ThreeDimensionalLayoutProperty;
sizeProportional?: ThreeDimensionalLayoutProperty;
sizeDifferential?: ThreeDimensionalLayoutProperty;
sizeAbsolute?: ThreeDimensionalLayoutProperty;
shear?: {
xy: number;
xz: number;
yz: number;
};
computed: ComputedLayoutSpecPartial;
}
export interface ComputedLayoutSpec extends LayoutSpec {
matrix: Mat4;
size: ThreeDimensionalLayoutProperty;
bounds: BoundsSpecPartial;
}
export interface ComputedLayoutSpecPartial {
shown?: boolean;
opacity?: number;
mount?: ThreeDimensionalLayoutProperty;
offset?: ThreeDimensionalLayoutProperty;
origin?: ThreeDimensionalLayoutProperty;
translation?: ThreeDimensionalLayoutProperty;
rotation?: ThreeDimensionalLayoutProperty;
scale?: ThreeDimensionalLayoutProperty;
sizeMode?: ThreeDimensionalLayoutProperty;
sizeProportional?: ThreeDimensionalLayoutProperty;
sizeDifferential?: ThreeDimensionalLayoutProperty;
sizeAbsolute?: ThreeDimensionalLayoutProperty;
shear?: {
xy: number;
xz: number;
yz: number;
};
matrix: Mat4;
size: ThreeDimensionalLayoutProperty;
bounds: BoundsSpecPartial;
}
export interface StringableThreeDimensionalLayoutProperty {
x: number|string;
y: number|string;
z: number|string;
}
export interface TwoPointFiveDimensionalLayoutProperty {
x: number;
y: number;
z?: number;
}
export interface ClientRect {
left: number;
top: number;
right: number;
bottom: number;
width: number;
height: number;
}
export interface BoundsSpecX {
left: number;
right: number;
}
export interface BoundsSpecY {
top: number;
bottom: number;
}
export interface BoundsSpecZ {
front: number;
back: number;
}
export interface BoundsSpec extends BoundsSpecX, BoundsSpecY, BoundsSpecZ {}
export interface BoundsSpecPartial {
left?: number;
right?: number;
top?: number;
bottom?: number;
front?: number;
back?: number;
}
export type AxisString = 'x'|'y'|'z';
export enum Curve {
EaseInBack = 'easeInBack',
EaseInCirc = 'easeInCirc',
EaseInCubic = 'easeInCubic',
EaseInExpo = 'easeInExpo',
EaseInQuad = 'easeInQuad',
EaseInQuart = 'easeInQuart',
EaseInBounce = 'easeInBounce',
EaseInElastic = 'easeInElastic',
EaseInQuint = 'easeInQuint',
EaseInSine = 'easeInSine',
EaseOutBack = 'easeOutBack',
EaseOutCirc = 'easeOutCirc',
EaseOutCubic = 'easeOutCubic',
EaseOutExpo = 'easeOutExpo',
EaseOutQuad = 'easeOutQuad',
EaseOutQuart = 'easeOutQuart',
EaseOutBounce = 'easeOutBounce',
EaseOutElastic = 'easeOutElastic',
EaseOutQuint = 'easeOutQuint',
EaseOutSine = 'easeOutSine',
EaseInOutBack = 'easeInOutBack',
EaseInOutCirc = 'easeInOutCirc',
EaseInOutCubic = 'easeInOutCubic',
EaseInOutExpo = 'easeInOutExpo',
EaseInOutQuad = 'easeInOutQuad',
EaseInOutQuart = 'easeInOutQuart',
EaseInOutBounce = 'easeInOutBounce',
EaseInOutElastic = 'easeInOutElastic',
EaseInOutQuint = 'easeInOutQuint',
EaseInOutSine = 'easeInOutSine',
Linear = 'linear',
}
/**
* Defines a normalized curve, to be used in BytecodeTimelineValue and also in
* state transition
*/
export type CurveFunction = ((offset: number) => number);
/**
* Can be a function or a string from just-curves. The string is
* converted into CuverFunction inside Interpolate
*/
export type CurveDefinition = Curve|CurveFunction|number[];
export interface ParsedValueCluster {
parsee: {
[ms: number]: {
expression?: boolean;
value: any;
curve?: CurveDefinition;
};
};
keys: number[];
}
/**
* Similar to BytecodeTimelines, but storing parsed value clusters instead of literal timelines.
*/
export interface ParsedValueClusterCollection {
[timelineName: string]: {
[selector: string]: {
[propertyName: string]: ParsedValueCluster;
};
};
}