-
-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathReactBlessedComponent.js
More file actions
191 lines (156 loc) · 4.93 KB
/
Copy pathReactBlessedComponent.js
File metadata and controls
191 lines (156 loc) · 4.93 KB
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
/**
* React Blessed Component
* ========================
*
* React component abstraction for the blessed library.
*/
import blessed from 'blessed';
import ReactMultiChild from 'react/lib/ReactMultiChild';
import ReactBlessedIDOperations from './ReactBlessedIDOperations';
import invariant from 'invariant';
import update from './update';
import solveClass from './solveClass';
import {extend, groupBy, startCase} from 'lodash';
/**
* Variable types that must be solved as content rather than real children.
*/
const CONTENT_TYPES = {string: true, number: true};
/**
* Renders the given react element with blessed.
*
* @constructor ReactBlessedComponent
* @extends ReactMultiChild
*/
export default class ReactBlessedComponent {
constructor(tag) {
this._tag = tag.toLowerCase();
this._renderedChildren = null;
this._previousStyle = null;
this._previousStyleCopy = null;
this._rootNodeID = null;
this._wrapperState = null;
this._topLevelWrapper = null;
this._nodeWithLegacyProperties = null;
}
construct(element) {
// Setting some properties
this._currentElement = element;
this._eventListener = (type, ...args) => {
const handler = this._currentElement.props['on' + startCase(type).replace(/ /g, '')];
if (typeof handler === 'function')
handler.apply(null, args);
};
}
/**
* Mounting the root component.
*
* @internal
* @param {string} rootID - The root blessed ID for this node.
* @param {ReactBlessedReconcileTransaction} transaction
* @param {object} context
*/
mountComponent(rootID, transaction, context) {
this._rootNodeID = rootID;
// Mounting blessed node
const node = this.mountNode(
ReactBlessedIDOperations.getParent(rootID),
this._currentElement
);
ReactBlessedIDOperations.add(rootID, node);
// Mounting children
let childrenToUse = this._currentElement.props.children;
childrenToUse = childrenToUse === null ? [] : [].concat(childrenToUse);
if (childrenToUse.length) {
// Discriminating content components from real children
const {content=null, realChildren=[]} = groupBy(childrenToUse, (c) => {
return CONTENT_TYPES[typeof c] ? 'content' : 'realChildren';
});
// Setting textual content
if (content)
node.setContent('' + content.join(''));
// Mounting real children
this.mountChildren(
realChildren,
transaction,
context
);
}
// Rendering the screen
ReactBlessedIDOperations.screen.debouncedRender();
}
/**
* Mounting the blessed node itself.
*
* @param {BlessedNode|BlessedScreen} parent - The parent node.
* @param {ReactElement} element - The element to mount.
* @return {BlessedNode} - The mounted node.
*/
mountNode(parent, element) {
const {props, type} = element,
{children, ...options} = props,
blessedElement = blessed[type];
invariant(
!!blessedElement,
`Invalid blessed element "${type}".`
);
const node = blessed[type](solveClass(options));
node.on('event', this._eventListener);
parent.append(node);
return node;
}
/**
* Receive a component update.
*
* @param {ReactElement} nextElement
* @param {ReactReconcileTransaction} transaction
* @param {object} context
* @internal
* @overridable
*/
receiveComponent(nextElement, transaction, context) {
const {props: {children, ...options}} = nextElement,
node = ReactBlessedIDOperations.get(this._rootNodeID);
update(node, solveClass(options));
if (this._currentElement !== nextElement) {
this._currentElement = nextElement
}
// Updating children
const childrenToUse = children === null ? [] : [].concat(children);
// Discriminating content components from real children
const {content=null, realChildren=[]} = groupBy(childrenToUse, (c) => {
return CONTENT_TYPES[typeof c] ? 'content' : 'realChildren';
});
// Setting textual content
if (content)
node.setContent('' + content.join(''));
this.updateChildren(realChildren, transaction, context);
ReactBlessedIDOperations.screen.debouncedRender();
}
/**
* Dropping the component.
*/
unmountComponent() {
this.unmountChildren();
const node = ReactBlessedIDOperations.get(this._rootNodeID);
node.off('event', this._eventListener);
node.destroy();
ReactBlessedIDOperations.drop(this._rootNodeID);
this._rootNodeID = null;
ReactBlessedIDOperations.screen.debouncedRender();
}
/**
* Getting a public instance of the component for refs.
*
* @return {BlessedNode} - The instance's node.
*/
getPublicInstance() {
return ReactBlessedIDOperations.get(this._rootNodeID);
}
}
/**
* Extending the component with the MultiChild mixin.
*/
extend(
ReactBlessedComponent.prototype,
ReactMultiChild.Mixin
);