react-qml
Version:
258 lines (210 loc) • 7.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.flattenStyle = flattenStyle;
exports.mapStyleToProps = mapStyleToProps;
exports.setStyle = setStyle;
exports.default = void 0;
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
var FONT_WEIGHT_MAP = {
normal: 50,
bold: 75,
'100': 0,
'200': 12,
'300': 25,
'400': 50,
'500': 57,
'600': 63,
'700': 75,
'800': 81,
'900': 87
};
function getFontWeight(value) {
if (Object.keys(FONT_WEIGHT_MAP).indexOf(value) > -1) {
return FONT_WEIGHT_MAP[value];
}
return 400;
}
var FONT_CAPITALIZATION_MAP = {
none: 'MixedCase',
uppercase: 'AllUppercase',
lowercase: 'AllLowercase',
capitalize: 'Capitalize'
};
function getFontCapitalization(value) {
if (Object.keys(FONT_CAPITALIZATION_MAP).indexOf(value) > -1) {
return FONT_CAPITALIZATION_MAP[value];
}
return 'MixedCase';
}
var TEXT_ALIGN_MAP = {
left: 'AlignLeft',
right: 'AlignRight',
center: 'AlignHCenter',
justify: 'AlignJustify'
};
function getTextAlignment(value) {
if (Object.keys(TEXT_ALIGN_MAP).indexOf(value) > -1) {
return TEXT_ALIGN_MAP[value];
}
return undefined;
}
var TEXT_ALIGN_VERTICAL_MAP = {
top: 'AlignTop',
bottom: 'AlignBottom',
center: 'AlignVCenter'
};
function getTextAlignmentVertical(value) {
if (Object.keys(TEXT_ALIGN_VERTICAL_MAP).indexOf(value) > -1) {
return TEXT_ALIGN_VERTICAL_MAP[value];
}
return undefined;
}
var LAYOUT_MAP = {
alignment: 'alignment',
minimumWidth: 'minimumWidth',
minWidth: 'minimumWidth',
minimumHeight: 'minimumHeight',
minHeight: 'minimumHeight',
preferredWidth: 'preferredWidth',
preferredHeight: 'preferredHeight',
maximumWidth: 'maximumWidth',
maxWidth: 'maximumWidth',
maximumHeight: 'maximumHeight',
maxHeight: 'maximumHeight',
fillHeight: 'fillHeight',
fillWidth: 'fillWidth',
row: 'row',
rowSpan: 'rowSpan',
columnSpan: 'columnSpan',
margins: 'margins',
margin: 'margins',
leftMargin: 'leftMargin',
marginLeft: 'leftMargin',
topMargin: 'topMargin',
marginTop: 'topMargin',
rightMargin: 'rightMargin',
marginRight: 'rightMargin',
bottomMargin: 'bottomMargin',
marginBottom: 'bottomMargin'
};
function flattenStyle(style) {
if (style === null || _typeof(style) !== 'object') {
return undefined;
}
if (!Array.isArray(style)) {
return style;
}
var result = {};
for (var i = 0, styleLength = style.length; i < styleLength; ++i) {
var computedStyle = flattenStyle(style[i]);
if (computedStyle) {
for (var _key in computedStyle) {
result[_key] = computedStyle[_key];
}
}
}
return result;
}
function mapStyleToProps(style) {
// no validation, for now
var props = {};
var font = {};
var Layout = {};
for (var styleName in style) {
var styleValue = style[styleName];
if (Object.keys(LAYOUT_MAP).indexOf(styleName) > -1) {
var layoutKey = LAYOUT_MAP[styleName];
Layout[layoutKey] = style[styleName];
continue;
}
switch (styleName) {
case 'fontSize':
font.pointSize = styleValue;
break;
case 'fontFamily':
font.family = styleValue;
break;
case 'fontStyle':
if (styleValue === 'italic') {
font.italic = true;
}
break;
case 'fontWeight':
font.weight = getFontWeight(String(styleValue));
break;
case 'textAlign':
props.horizontalAlignment = getTextAlignment(String(styleValue));
break;
case 'textAlignVertical':
props.verticalAlignment = getTextAlignmentVertical(String(styleValue));
break;
case 'textTransform':
font.capitalization = getFontCapitalization(String(styleValue));
break;
case 'letterSpacing':
font.letterSpacing = styleValue;
break;
case 'wordSpacing':
font.wordSpacing = styleValue;
break;
default:
// passthrough
props[styleName] = styleValue;
break;
}
}
return _objectSpread({}, props, {
Layout: Layout,
font: font
});
}
var isEmpty = function isEmpty(obj) {
return Object.keys(obj).length === 0 && obj.constructor === Object;
};
function setStyle(qmlElement, style) {
var flatStyle = flattenStyle(style);
var _mapStyleToProps = mapStyleToProps(flatStyle),
Layout = _mapStyleToProps.Layout,
font = _mapStyleToProps.font,
props = _objectWithoutProperties(_mapStyleToProps, ["Layout", "font"]);
if (!isEmpty(font)) {
for (var fontPropName in font) {
if (qmlElement.font.hasOwnProperty(fontPropName)) {
qmlElement.font[fontPropName] = font[fontPropName];
}
}
}
if (!isEmpty(Layout)) {
for (var layoutPropName in Layout) {
if (qmlElement.Layout.hasOwnProperty(layoutPropName)) {
qmlElement.Layout[layoutPropName] = Layout[layoutPropName];
}
}
}
for (var propName in props) {
if (qmlElement.hasOwnProperty(propName)) {
var propValue = props[propName];
if (propValue === undefined) {
try {
qmlElement[propName] = propValue;
} catch (ex) {
console.warn("Cannot unset property \"".concat(propName, "\" of object ").concat(qmlElement));
}
} else {
qmlElement[propName] = propValue;
}
}
}
}
var _default = {
flattenStyle: flattenStyle,
mapStyleToProps: mapStyleToProps,
setStyle: setStyle
};
exports.default = _default;