UNPKG

nuke-text

Version:

文本

209 lines (166 loc) 6.9 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _class, _temp2; var _rax = require('rax'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var isWeex = typeof callNative === 'function'; var Text = (_temp2 = _class = function (_Component) { _inherits(Text, _Component); function Text() { var _ref; var _temp, _this, _ret; _classCallCheck(this, Text); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Text.__proto__ || Object.getPrototypeOf(Text)).call.apply(_ref, [this].concat(args))), _this), _this.renderText = function () { var props = _this.props; var nativeProps = _extends({}, props, { style: props.style || {} }); var textString = ''; if (props.children != null) { if (!Array.isArray(props.children)) { textString = props.children.toString(); } else { textString = props.children.join(''); } } if (_this.context.isInAParentText) { return (0, _rax.createElement)( 'span', nativeProps, textString ); } if (props.onPress) { nativeProps.onClick = props.onPress; } if (isWeex) { if (props.numberOfLines) { nativeProps.style.lines = props.numberOfLines; } nativeProps.value = textString; return (0, _rax.createElement)('text', nativeProps); } var styleProps = _extends({}, styles.text, nativeProps.style); var numberOfLines = props.numberOfLines; if (numberOfLines) { if (parseInt(numberOfLines) === 1) { styleProps.whiteSpace = 'nowrap'; } else { styleProps.display = '-webkit-box'; styleProps.webkitBoxOrient = 'vertical'; styleProps.webkitLineClamp = String(numberOfLines); } styleProps.overflow = 'hidden'; } return (0, _rax.createElement)( 'span', _extends({}, nativeProps, { style: styleProps }), textString ); }, _this.renderRichText = function () { var props = _this.props; var children = props.children; var nativeProps = _extends({}, props, { style: props.style || {} }); var styleProps = _extends({}, styles.richtext, nativeProps.style); if (isWeex) { children = transformChildren(children, _this); } return (0, _rax.createElement)( 'p', _extends({}, nativeProps, { style: styleProps }), children ); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Text, [{ key: 'getChildContext', value: function getChildContext() { return { isInAParentText: true }; } }, { key: 'render', value: function render() { var props = this.props; var children = props.children; if (!Array.isArray(children)) { children = [children]; } var nested = false; for (var i = 0; i < children.length; i++) { var child = children[i]; if (child && (typeof child === 'undefined' ? 'undefined' : _typeof(child)) === 'object') { nested = true; break; } } return nested ? this.renderRichText() : this.renderText(); } }]); return Text; }(_rax.Component), _class.propTypes = {}, _class.contextTypes = { isInAParentText: _rax.PropTypes.bool }, _class.childContextTypes = { isInAParentText: _rax.PropTypes.bool }, _temp2); function transformChild(child, instance) { var ChildComponent = child.type, props = child.props; var children = props.children; if (typeof ChildComponent === 'function') { var childInstance = new ChildComponent(); childInstance.props = props; if (children) { childInstance.props.children = transformChildren(children, instance); } childInstance.context = instance.getChildContext(); return childInstance.render(); } return child; } function transformChildren(children, instance) { var elements = []; if (!Array.isArray(children)) { children = [children]; } for (var i = 0; i < children.length; i++) { var child = children[i]; if (typeof child === 'string') { elements.push(child); } else if ((typeof child === 'undefined' ? 'undefined' : _typeof(child)) === 'object') { elements.push(transformChild(child, instance)); } } return elements; } var styles = { text: { border: '0 solid black', position: 'relative', boxSizing: 'border-box', display: 'block', flexDirection: 'column', alignContent: 'flex-start', flexShrink: 0, fontSize: 32 }, richtext: { marginTop: 0, marginBottom: 0 } }; exports.default = Text; module.exports = exports['default'];