weex-nuke
Version:
基于 Rax 、Weex 的高性能组件体系 ~~
98 lines (75 loc) • 4.32 kB
JavaScript
/** @jsx createElement */
;
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('nuke-helper');
var _nukeEnv = require('nuke-env');
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 StyledA = function (_Component) {
_inherits(StyledA, _Component);
function StyledA(props, context) {
_classCallCheck(this, StyledA);
var _this = _possibleConstructorReturn(this, (StyledA.__proto__ || Object.getPrototypeOf(StyledA)).call(this));
_this.fixedFont = context.commonConfigs && context.commonConfigs.fixedFont;
_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(StyledA, [{
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 + (_nukeEnv.isWeb ? 'px' : 'wx');
} else {
style.lineHeight = lineHeight;
}
}
return style;
}
}, {
key: 'render',
value: function render() {
var textStyle = Object.assign({ fontSize: 32, wordWrap: 'break-word' }, this.props.style);
textStyle = this.fixedFont ? (0, _nukeHelper.calcTextSize)(textStyle) : textStyle;
textStyle = this.optimizeLineHeight ? this.calcLineHeight(textStyle) : textStyle;
return (0, _rax.createElement)('a', _extends({}, this.props, { style: textStyle, fixedFont: true, optimizeLineHeight: true }));
}
}]);
return StyledA;
}(_rax.Component);
StyledA.contextTypes = {
androidConfigs: _rax.PropTypes.any,
commonConfigs: _rax.PropTypes.any
};
StyledA.propTypes = {
optimizeLineHeight: _rax.PropTypes.boolean,
fixedFont: _rax.PropTypes.boolean,
lineHeightOptimizer: _rax.PropTypes.func
};
StyledA.defaultProps = {
style: {}
};
exports.default = StyledA;
module.exports = exports['default'];