98de414784d84146dbec125d9d8439538545ef04.svn-base
3.88 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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _yogaLayoutPrebuilt = _interopRequireDefault(require("yoga-layout-prebuilt"));
var _output = _interopRequireDefault(require("./output"));
var _dom = require("./dom");
var _buildLayout = _interopRequireDefault(require("./build-layout"));
var _renderNodeToOutput = _interopRequireDefault(require("./render-node-to-output"));
var _calculateWrappedText = _interopRequireDefault(require("./calculate-wrapped-text"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Since <Static> components can be placed anywhere in the tree, this helper finds and returns them
const getStaticNodes = element => {
const staticNodes = [];
for (const childNode of element.childNodes) {
if (childNode.unstable__static) {
staticNodes.push(childNode);
}
if (Array.isArray(childNode.childNodes) && childNode.childNodes.length > 0) {
staticNodes.push(...getStaticNodes(childNode));
}
}
return staticNodes;
}; // Build layout, apply styles, build text output of all nodes and return it
var _default = ({
terminalWidth
}) => {
const config = _yogaLayoutPrebuilt.default.Config.create(); // Used to free up memory used by last Yoga node tree
let lastYogaNode;
let lastStaticYogaNode;
return node => {
if (lastYogaNode) {
lastYogaNode.freeRecursive();
}
if (lastStaticYogaNode) {
lastStaticYogaNode.freeRecursive();
}
const staticElements = getStaticNodes(node);
if (staticElements.length > 1) {
if (process.env.NODE_ENV !== 'production') {
console.error('Warning: There can only be one <Static> component');
}
} // <Static> component must be built and rendered separately, so that the layout of the other output is unaffected
let staticOutput;
if (staticElements.length === 1) {
const rootNode = (0, _dom.createNode)('root');
(0, _dom.appendStaticNode)(rootNode, staticElements[0]);
const {
yogaNode: staticYogaNode
} = (0, _buildLayout.default)(rootNode, {
config,
terminalWidth,
skipStaticElements: false
});
staticYogaNode.calculateLayout(_yogaLayoutPrebuilt.default.UNDEFINED, _yogaLayoutPrebuilt.default.UNDEFINED, _yogaLayoutPrebuilt.default.DIRECTION_LTR);
(0, _calculateWrappedText.default)(rootNode);
staticYogaNode.calculateLayout(_yogaLayoutPrebuilt.default.UNDEFINED, _yogaLayoutPrebuilt.default.UNDEFINED, _yogaLayoutPrebuilt.default.DIRECTION_LTR); // Save current Yoga node tree to free up memory later
lastStaticYogaNode = staticYogaNode;
staticOutput = new _output.default({
width: staticYogaNode.getComputedWidth(),
height: staticYogaNode.getComputedHeight()
});
(0, _renderNodeToOutput.default)(rootNode, staticOutput, {
skipStaticElements: false
});
}
const {
yogaNode
} = (0, _buildLayout.default)(node, {
config,
terminalWidth,
skipStaticElements: true
});
yogaNode.calculateLayout(_yogaLayoutPrebuilt.default.UNDEFINED, _yogaLayoutPrebuilt.default.UNDEFINED, _yogaLayoutPrebuilt.default.DIRECTION_LTR);
(0, _calculateWrappedText.default)(node);
yogaNode.calculateLayout(_yogaLayoutPrebuilt.default.UNDEFINED, _yogaLayoutPrebuilt.default.UNDEFINED, _yogaLayoutPrebuilt.default.DIRECTION_LTR); // Save current node tree to free up memory later
lastYogaNode = yogaNode;
const output = new _output.default({
width: yogaNode.getComputedWidth(),
height: yogaNode.getComputedHeight()
});
(0, _renderNodeToOutput.default)(node, output, {
skipStaticElements: true
});
return {
output: output.get(),
outputHeight: output.getHeight(),
staticOutput: staticOutput ? `${staticOutput.get()}\n` : undefined
};
};
};
exports.default = _default;