choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
70 lines (58 loc) • 2.14 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { isValidElement } from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import isString from 'lodash/isString';
import isNumber from 'lodash/isNumber';
import toString from 'lodash/toString';
export default function getReactNodeText(_x) {
return _getReactNodeText.apply(this, arguments);
}
function _getReactNodeText() {
_getReactNodeText = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(node) {
var textDiv, textContent;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!node) {
_context.next = 13;
break;
}
if (!(isString(node) || isNumber(node))) {
_context.next = 3;
break;
}
return _context.abrupt("return", String(node));
case 3:
if (!(typeof window !== 'undefined' && /*#__PURE__*/isValidElement(node))) {
_context.next = 12;
break;
}
textDiv = document.createElement('div');
document.body.appendChild(textDiv);
_context.next = 8;
return new Promise(function (resolve) {
return render(node, textDiv, resolve);
});
case 8:
textContent = textDiv.textContent;
unmountComponentAtNode(textDiv);
if (textDiv.parentNode) {
textDiv.parentNode.removeChild(textDiv);
}
return _context.abrupt("return", textContent || '');
case 12:
return _context.abrupt("return", toString(node));
case 13:
return _context.abrupt("return", '');
case 14:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _getReactNodeText.apply(this, arguments);
}
//# sourceMappingURL=getReactNodeText.js.map