UNPKG

react-native-enhance-stylesheet

Version:
2 lines (1 loc) 4.25 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var reactNative=require("react-native");function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var i=0;i<t.length;i++){var n=t[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function _createClass(e,t,i){return t&&_defineProperties(e.prototype,t),i&&_defineProperties(e,i),e}var screenW=reactNative.Dimensions.get("window").width,screenH=reactNative.Dimensions.get("window").height,fontScale=reactNative.PixelRatio.getFontScale(),pixelRatio=reactNative.PixelRatio.get(),DEFAULT_DENSITY=2,X_WIDTH=375,X_HEIGHT=812,ScreenAdapter=function(){function t(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{width:750,height:1334};_classCallCheck(this,t),this.screenW=screenW,this.screenH=screenH,this.w2=(e.width||750)/DEFAULT_DENSITY,this.h2=(e.height||1334)/DEFAULT_DENSITY,this.margin=e.margin||15,this.marginTop=e.marginTop||15,this.marginLeft=e.marginLeft||15,this.marginBottom=e.marginBottom||15,this.marginRight=e.marginRight||15,this.marginHorizontal=e.marginHorizontal||15,this.marginVertical=e.marginVertical||15,this.padding=e.padding||15,this.paddingTop=e.paddingTop||15,this.paddingLeft=e.paddingLeft||15,this.paddingBottom=e.paddingBottom||15,this.paddingRight=e.paddingRight||15,this.paddingHorizontal=e.paddingHorizontal||15,this.paddingVertical=e.paddingVertical||15}return _createClass(t,[{key:"init",value:function(e){var t=0<arguments.length&&void 0!==e?e:{width:750,height:1334};return this.w2=(t.width||750)/DEFAULT_DENSITY,this.h2=(t.height||1334)/DEFAULT_DENSITY,this.margin=t.margin||15,this.marginTop=t.marginTop||15,this.marginLeft=t.marginLeft||15,this.marginBottom=t.marginBottom||15,this.marginRight=t.marginRight||15,this.marginHorizontal=t.marginHorizontal||15,this.marginVertical=t.marginVertical||15,this.padding=t.padding||15,this.paddingTop=t.paddingTop||15,this.paddingLeft=t.paddingLeft||15,this.paddingBottom=t.paddingBottom||15,this.paddingRight=t.paddingRight||15,this.paddingHorizontal=t.paddingHorizontal||15,this.paddingVertical=t.paddingVertical||15,this}},{key:"setSpText",value:function(e){var t=screenW/this.w2,i=screenH/this.h2,n=Math.min(t,i);return(e=Math.round(e*n+.5))/DEFAULT_DENSITY}},{key:"scaleSize",value:function(e){var t=screenW/this.w2,i=screenH/this.h2,n=Math.min(t,i);return(e=Math.round(e*n+.5))/DEFAULT_DENSITY}},{key:"isiPhoneX",value:function(){return"ios"===reactNative.Platform.OS&&(screenH===X_HEIGHT&&screenW===X_WIDTH||screenH===X_WIDTH&&screenW===X_HEIGHT)}},{key:"ifiPhoneX",value:function(e,t,i){var n=1<arguments.length&&void 0!==t?t:{},a=2<arguments.length?i:void 0;return this.isiPhoneX()?e:"ios"===reactNative.Platform.OS?n:a||n}}]),t}();ScreenAdapter.screenW=screenW,ScreenAdapter.screenH=screenH,ScreenAdapter.pixelRatio=pixelRatio,ScreenAdapter.DEFAULT_DENSITY=DEFAULT_DENSITY;var sa=new ScreenAdapter,_config=new Map,EnhanceStyleSheet=function(){function e(){_classCallCheck(this,e),this.builded=!1}return _createClass(e,[{key:"build",value:function(t){this.builded=!0,Object.keys(t).forEach(function(e){_config.set(e,t[e])})}},{key:"value",value:function(e){return _config.get(e)}},{key:"create",value:function(t){var o=this,i={};return Object.keys(t).forEach(function(e){var s=t[e];Object.keys(s).forEach(function(e,t){var i=s[e];if(e.includes("fontSize$"))s.fontSize=sa.setSpText(i),delete s.fontSize$;else if(e.endsWith("$")){var n=e.replace(/\$/g,"");s[n]=sa.scaleSize(i),delete s[e]}if(o.builded&&"string"==typeof i&&i.startsWith("$")){var a=_config.get(i);if(!a)throw new Error("".concat(i," is not exist in EStyleSheet.build()."));if(e.includes("fontSize$"))s.fontSize=sa.setSpText(a),delete s.fontSize$;else if(e.endsWith("$")){var r=e.replace(/\$/g,"");s[r]=sa.scaleSize(a),delete s[e]}else s[e]=a}}),i[e]=Object.assign({},s)}),reactNative.StyleSheet.create(i)}}]),e}(),EStyleSheet=new EnhanceStyleSheet;exports.DEFAULT_DENSITY=DEFAULT_DENSITY,exports.EStyleSheet=EStyleSheet,exports.ScreenAdapter=ScreenAdapter,exports.fontScale=fontScale,exports.pixelRatio=pixelRatio,exports.sa=sa,exports.screenH=screenH,exports.screenW=screenW;