/
dom.ts
187 lines (154 loc) Β· 3.93 KB
/
dom.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
import Yoga from 'yoga-layout-prebuilt';
import {measureText} from './measure-text';
import {applyStyles} from './apply-styles';
import {wrapText} from './wrap-text';
import {OutputTransformer} from './render-node-to-output';
import {Styles} from './styles';
interface InkNode {
parentNode: DOMElement | null;
yogaNode?: Yoga.YogaNode;
unstable__static?: boolean;
style: Styles;
}
export const TEXT_NAME = '#text';
export type TextName = '#text';
export type ElementNames = 'root' | 'div' | 'span' | 'ROOT' | 'DIV' | 'SPAN';
export type NodeNames = ElementNames | TextName;
export type DOMElement = {
nodeName: ElementNames;
attributes: {
[key: string]: DOMNodeAttribute;
};
textContent?: string;
childNodes: DOMNode[];
internal_transform?: OutputTransformer;
onRender?: () => void;
// Experimental properties
isStaticDirty?: boolean;
onImmediateRender?: () => void;
} & InkNode;
export type TextNode = {
nodeName: TextName;
nodeValue: string;
} & InkNode;
export type DOMNode<T = {nodeName: NodeNames}> = T extends {
nodeName: infer U;
}
? U extends '#text'
? TextNode
: DOMElement
: never;
export type DOMNodeAttribute = boolean | string | number;
export const createNode = (nodeName: ElementNames): DOMElement => ({
nodeName: nodeName.toUpperCase() as ElementNames,
style: {},
attributes: {},
childNodes: [],
parentNode: null,
yogaNode: Yoga.Node.create()
});
export const appendChildNode = (
node: DOMElement,
childNode: DOMElement
): void => {
if (childNode.parentNode) {
removeChildNode(childNode.parentNode, childNode);
}
childNode.parentNode = node;
node.childNodes.push(childNode);
if (childNode.yogaNode) {
node.yogaNode?.insertChild(
childNode.yogaNode,
node.yogaNode.getChildCount()
);
}
};
export const insertBeforeNode = (
node: DOMElement,
newChildNode: DOMNode,
beforeChildNode: DOMNode
): void => {
if (newChildNode.parentNode) {
removeChildNode(newChildNode.parentNode, newChildNode);
}
newChildNode.parentNode = node;
const index = node.childNodes.indexOf(beforeChildNode);
if (index >= 0) {
node.childNodes.splice(index, 0, newChildNode);
if (newChildNode.yogaNode) {
node.yogaNode?.insertChild(newChildNode.yogaNode, index);
}
return;
}
node.childNodes.push(newChildNode);
if (newChildNode.yogaNode) {
node.yogaNode?.insertChild(
newChildNode.yogaNode,
node.yogaNode.getChildCount()
);
}
};
export const removeChildNode = (
node: DOMElement,
removeNode: DOMNode
): void => {
if (removeNode.yogaNode) {
removeNode.parentNode?.yogaNode?.removeChild(removeNode.yogaNode);
}
removeNode.parentNode = null;
const index = node.childNodes.indexOf(removeNode);
if (index >= 0) {
node.childNodes.splice(index, 1);
}
};
export const setAttribute = (
node: DOMElement,
key: string,
value: DOMNodeAttribute
): void => {
node.attributes[key] = value;
};
export const setStyle = (node: DOMNode, style: Styles): void => {
node.style = style;
if (node.yogaNode) {
applyStyles(node.yogaNode, style);
}
};
export const createTextNode = (text: string): TextNode => {
const node: TextNode = {
nodeName: '#text',
nodeValue: text,
yogaNode: Yoga.Node.create(),
parentNode: null,
style: {}
};
setTextContent(node, text);
return node;
};
const measureTextNode = function (
node: DOMNode,
text: string,
width: number
): {width: number; height: number} {
const dimensions = measureText(text);
// Text fits into container, no need to wrap
if (dimensions.width <= width) {
return dimensions;
}
const wrapType = node.parentNode?.style?.textWrap ?? 'wrap';
const wrappedText = wrapText(text, width, wrapType);
return measureText(wrappedText);
};
export const setTextContent = (node: DOMNode, text: string): void => {
if (typeof text !== 'string') {
text = String(text);
}
if (node.nodeName === '#text') {
node.nodeValue = text;
} else {
node.textContent = text;
}
node.yogaNode?.setMeasureFunc(
measureTextNode.bind(null, node, text) as () => any
);
};