-
Notifications
You must be signed in to change notification settings - Fork 1
/
contract.ts
80 lines (72 loc) · 1.71 KB
/
contract.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
import { AnyInstance, Container, NodeType } from "./reconciler/reconciler";
export enum ElementType {
view = "view",
text = "text",
btn = "btn",
raw = "raw",
}
export type TransferViewData = {
elements: ViewElement[];
};
export type ViewElementText = {
tag: ElementType.text;
color?: string;
type?: "header" | "paragraph" | "span";
text: string | number | undefined;
children?: ViewElement[];
};
export type ViewElement =
| {
tag: ElementType.view;
border?: number;
children?: ViewElement[];
}
| {
tag: ElementType.btn;
id: number;
children?: ViewElement[];
}
| ViewElementText
| {
tag: ElementType.raw;
text: string | number;
};
const toElement = (instance: AnyInstance): ViewElement => {
switch (instance.tag) {
case NodeType.view: {
const { children, border } = instance;
return {
tag: ElementType.view,
border,
children: children.length > 0 ? children.map(toElement) : undefined,
};
}
case NodeType.text: {
const { text, type, children } = instance;
return {
tag: ElementType.text,
text,
type,
children: children.length > 0 ? children.map(toElement) : undefined,
};
}
case NodeType.btn: {
const { id, children } = instance;
return {
tag: ElementType.btn,
id,
children: children.length > 0 ? children.map(toElement) : undefined,
};
}
case "raw": {
const { text } = instance;
return {
tag: ElementType.raw,
text,
};
}
}
};
export const prepareToTransfer = (container: Container): TransferViewData => ({
elements: container.children.map(toElement),
});