/
reconciler.ts
291 lines (256 loc) · 7.56 KB
/
reconciler.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
import process from 'node:process';
import createReconciler from 'react-reconciler';
import {DefaultEventPriority} from 'react-reconciler/constants.js';
// eslint-disable-next-line n/file-extension-in-import
import Yoga, {type Node as YogaNode} from 'yoga-wasm-web/auto';
import {
createTextNode,
appendChildNode,
insertBeforeNode,
removeChildNode,
setStyle,
setTextNodeValue,
createNode,
setAttribute,
type DOMNodeAttribute,
type TextNode,
type ElementNames,
type DOMElement
} from './dom.js';
import {type Styles} from './styles.js';
import {type OutputTransformer} from './render-node-to-output.js';
// We need to conditionally perform devtools connection to avoid
// accidentally breaking other third-party code.
// See https://github.com/vadimdemedes/ink/issues/384
if (process.env['DEV'] === 'true') {
try {
await import('./devtools.js');
// eslint-disable-next-line @typescript-eslint/no-implicit-any-catch
} catch (error: any) {
if (error.code === 'MODULE_NOT_FOUND') {
console.warn(
`
Debugging with React Devtools requires \`react-devtools-core\` dependency to be installed.
$ npm install --save-dev react-devtools-core
`.trim() + '\n'
);
} else {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw error;
}
}
}
const cleanupYogaNode = (node?: YogaNode): void => {
node?.unsetMeasureFunc();
node?.freeRecursive();
};
type Props = Record<string, unknown>;
type HostContext = {
isInsideText: boolean;
};
export default createReconciler<
ElementNames,
Props,
DOMElement,
DOMElement,
TextNode,
DOMElement,
unknown,
unknown,
HostContext,
Props,
unknown,
unknown,
unknown
>({
getRootHostContext: () => ({
isInsideText: false
}),
prepareForCommit: () => null,
preparePortalMount: () => null,
clearContainer: () => false,
resetAfterCommit(rootNode) {
// Since renders are throttled at the instance level and <Static> component children
// are rendered only once and then get deleted, we need an escape hatch to
// trigger an immediate render to ensure <Static> children are written to output before they get erased
if (rootNode.isStaticDirty) {
rootNode.isStaticDirty = false;
if (typeof rootNode.onImmediateRender === 'function') {
rootNode.onImmediateRender();
}
return;
}
if (typeof rootNode.onRender === 'function') {
rootNode.onRender();
}
},
getChildHostContext(parentHostContext, type) {
const previousIsInsideText = parentHostContext.isInsideText;
const isInsideText = type === 'ink-text' || type === 'ink-virtual-text';
if (previousIsInsideText === isInsideText) {
return parentHostContext;
}
return {isInsideText};
},
shouldSetTextContent: () => false,
createInstance(originalType, newProps, _root, hostContext) {
if (hostContext.isInsideText && originalType === 'ink-box') {
throw new Error(`<Box> can’t be nested inside <Text> component`);
}
const type =
originalType === 'ink-text' && hostContext.isInsideText
? 'ink-virtual-text'
: originalType;
const node = createNode(type);
for (const [key, value] of Object.entries(newProps)) {
if (key === 'children') {
continue;
}
if (key === 'style') {
setStyle(node, value as Styles);
continue;
}
if (key === 'internal_transform') {
node.internal_transform = value as OutputTransformer;
continue;
}
if (key === 'internal_static') {
node.internal_static = true;
continue;
}
setAttribute(node, key, value as DOMNodeAttribute);
}
return node;
},
createTextInstance(text, _root, hostContext) {
if (!hostContext.isInsideText) {
throw new Error(
`Text string "${text}" must be rendered inside <Text> component`
);
}
return createTextNode(text);
},
resetTextContent() {},
hideTextInstance(node) {
setTextNodeValue(node, '');
},
unhideTextInstance(node, text) {
setTextNodeValue(node, text);
},
getPublicInstance: instance => instance,
hideInstance(node) {
node.yogaNode?.setDisplay(Yoga.DISPLAY_NONE);
},
unhideInstance(node) {
node.yogaNode?.setDisplay(Yoga.DISPLAY_FLEX);
},
appendInitialChild: appendChildNode,
appendChild: appendChildNode,
insertBefore: insertBeforeNode,
finalizeInitialChildren(node, _type, _props, rootNode) {
if (node.internal_static) {
rootNode.isStaticDirty = true;
// Save reference to <Static> node to skip traversal of entire
// node tree to find it
rootNode.staticNode = node;
}
return false;
},
isPrimaryRenderer: true,
supportsMutation: true,
supportsPersistence: false,
supportsHydration: false,
scheduleTimeout: setTimeout,
cancelTimeout: clearTimeout,
noTimeout: -1,
getCurrentEventPriority: () => DefaultEventPriority,
beforeActiveInstanceBlur() {},
afterActiveInstanceBlur() {},
detachDeletedInstance() {},
getInstanceFromNode: () => null,
prepareScopeUpdate() {},
getInstanceFromScope: () => null,
appendChildToContainer: appendChildNode,
insertInContainerBefore: insertBeforeNode,
removeChildFromContainer(node, removeNode) {
removeChildNode(node, removeNode);
cleanupYogaNode(removeNode.yogaNode);
},
prepareUpdate(node, _type, oldProps, newProps, rootNode) {
if (node.internal_static) {
rootNode.isStaticDirty = true;
}
const updatePayload: Props = {};
const keys = Object.keys(newProps);
for (const key of keys) {
if (newProps[key] !== oldProps[key]) {
const isStyle =
key === 'style' &&
typeof newProps['style'] === 'object' &&
typeof oldProps['style'] === 'object';
if (isStyle) {
const newStyle = newProps['style'] as Styles;
const oldStyle = oldProps['style'] as Styles;
const styleKeys = Object.keys(newStyle) as Array<keyof Styles>;
for (const styleKey of styleKeys) {
// Always include `borderColor` and `borderStyle` to ensure border is rendered,
// and `overflowX` and `overflowY` to ensure content is clipped,
// otherwise resulting `updatePayload` may not contain them
// if they weren't changed during this update
if (styleKey === 'borderStyle' || styleKey === 'borderColor') {
if (typeof updatePayload['style'] !== 'object') {
// Linter didn't like `= {} as Style`
const style: Styles = {};
updatePayload['style'] = style;
}
(updatePayload['style'] as any).borderStyle =
newStyle.borderStyle;
(updatePayload['style'] as any).borderColor =
newStyle.borderColor;
(updatePayload['style'] as any).overflowX = newStyle.overflowX;
(updatePayload['style'] as any).overflowY = newStyle.overflowY;
}
if (newStyle[styleKey] !== oldStyle[styleKey]) {
if (typeof updatePayload['style'] !== 'object') {
// Linter didn't like `= {} as Style`
const style: Styles = {};
updatePayload['style'] = style;
}
(updatePayload['style'] as any)[styleKey] = newStyle[styleKey];
}
}
continue;
}
(updatePayload as any)[key] = newProps[key];
}
}
return updatePayload;
},
commitUpdate(node, updatePayload) {
for (const [key, value] of Object.entries(updatePayload)) {
if (key === 'children') {
continue;
}
if (key === 'style') {
setStyle(node, value as Styles);
continue;
}
if (key === 'internal_transform') {
node.internal_transform = value as OutputTransformer;
continue;
}
if (key === 'internal_static') {
node.internal_static = true;
continue;
}
setAttribute(node, key, value as DOMNodeAttribute);
}
},
commitTextUpdate(node, _oldText, newText) {
setTextNodeValue(node, newText);
},
removeChild(node, removeNode) {
removeChildNode(node, removeNode);
cleanupYogaNode(removeNode.yogaNode);
}
});