UNPKG

weex-nuke

Version:

基于 Rax 、Weex 的高性能组件体系 ~~

121 lines (94 loc) 5.29 kB
/** @jsx createElement */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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 _rax = require('rax'); var _nukeHelper = require('../Helper/index.js'); var _text = require('./text.js'); var _text2 = _interopRequireDefault(_text); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 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 defaultLineHeightOptimizer = function defaultLineHeightOptimizer(fontSize) { return Math.ceil(9.24 * Math.pow(10, -5) * Math.pow(fontSize, 2) + 1.492 * fontSize + 2.174); }; var isWeb = typeof callNative !== 'function'; var Text = function (_Component) { _inherits(Text, _Component); function Text(props, context) { _classCallCheck(this, Text); var _this = _possibleConstructorReturn(this, (Text.__proto__ || Object.getPrototypeOf(Text)).call(this)); _this.fixedFont = context.commonConfigs && context.commonConfigs.fixedFont; _this.fontFamily = context.commonConfigs && context.commonConfigs.fontFamily; _this.optimizeLineHeight = context.commonConfigs && context.commonConfigs.optimizeLineHeight; if ('fixedFont' in props) { _this.fixedFont = props.fixedFont; } if ('optimizeLineHeight' in props) { _this.optimizeLineHeight = props.optimizeLineHeight; } return _this; } /** * This function is used to calculate line height when language is Thai or Vietnanese. * @param {obj} style */ _createClass(Text, [{ key: 'calcLineHeight', value: function calcLineHeight(style) { var lineHeightOptimizer = this.props.lineHeightOptimizer; var fontSize = parseInt(style.fontSize, 10); var lineHeight = lineHeightOptimizer(fontSize); if (lineHeight && !style.lineHeight) { if (this.fixedFont && typeof lineHeight === 'number') { style.lineHeight = lineHeight + (isWeb ? 'px' : 'wx'); } else { style.lineHeight = lineHeight; } } return style; } }, { key: 'render', value: function render() { var _props = this.props, onPress = _props.onPress, onClick = _props.onClick, style = _props.style, others = _objectWithoutProperties(_props, ['onPress', 'onClick', 'style']); var defaultStyle = { fontSize: '32rem', wordWrap: 'break-word' }; if (this.fontFamily) { defaultStyle.fontFamily = this.fontFamily; } var textStyle = Object.assign(defaultStyle, style); delete others.optimizeLineHeight; textStyle = this.fixedFont ? (0, _nukeHelper.calcTextSize)(textStyle) : textStyle; textStyle = this.optimizeLineHeight ? this.calcLineHeight(textStyle) : textStyle; return (0, _rax.createElement)(_text2.default, _extends({}, others, { style: textStyle, onClick: onPress || onClick })); } }]); return Text; }(_rax.Component); Text.contextTypes = { androidConfigs: _rax.PropTypes.any, commonConfigs: _rax.PropTypes.any }; Text.propTypes = { optimizeLineHeight: _rax.PropTypes.boolean, fixedFont: _rax.PropTypes.boolean, style: _rax.PropTypes.any, onPress: _rax.PropTypes.func, onClick: _rax.PropTypes.func, lineHeightOptimizer: _rax.PropTypes.func }; Text.defaultProps = { style: {}, lineHeightOptimizer: defaultLineHeightOptimizer }; exports.default = Text; module.exports = exports['default'];