forked from kyleshevlin/shevyjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (81 loc) · 3.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'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 _constants = require('./constants');
var _utils = require('./utils');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Shevy = function () {
function Shevy(options) {
var _this = this;
_classCallCheck(this, Shevy);
var mergedOptions = _extends({}, _constants.defaultOptions, options);
var baseFontSize = mergedOptions.baseFontSize,
baseLineHeight = mergedOptions.baseLineHeight,
baseFontScale = mergedOptions.baseFontScale,
addMarginBottom = mergedOptions.addMarginBottom,
proximity = mergedOptions.proximity,
proximityFactor = mergedOptions.proximityFactor;
this.baseFontSize = baseFontSize;
this.baseFontUnit = (0, _utils.getFontUnit)(baseFontSize);
this.baseLineHeight = baseLineHeight;
this.baseFontScale = (0, _utils.trimArrayToMaxOf6)(baseFontScale);
this.addMarginBottom = addMarginBottom;
this.proximity = proximity;
this.proximityFactor = proximityFactor;
// Binding methods
this.lineHeightSpacing = this.lineHeightSpacing.bind(this);
this.baseSpacing = this.baseSpacing.bind(this);
// Set headings
baseFontScale.forEach(function (factor, index) {
var heading = _constants.headings[index];
_this[heading] = {
fontSize: (0, _utils.calcHeadingFontSize)(_this, factor),
lineHeight: (0, _utils.calcHeadingLineHeight)(_this, factor),
marginBottom: (0, _utils.calcHeadingMarginBottom)(_this, factor, addMarginBottom)
};
});
// Set Body
this.body = {
fontSize: this.baseFontSize,
lineHeight: this.baseLineHeight
};
// Set Content
this.content = {
fontSize: this.baseFontSize,
lineHeight: this.baseLineHeight,
marginBottom: addMarginBottom ? this.baseSpacing() : undefined
};
}
_createClass(Shevy, [{
key: 'lineHeightSpacing',
value: function lineHeightSpacing() {
var factor = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var baseFontSize = this.baseFontSize,
baseLineHeight = this.baseLineHeight;
var value = (0, _utils.getFontValue)(baseFontSize);
var unit = (0, _utils.getFontUnit)(baseFontSize);
return '' + value * baseLineHeight * factor + unit;
}
}, {
key: 'baseSpacing',
value: function baseSpacing() {
var factor = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var baseFontSize = this.baseFontSize,
baseLineHeight = this.baseLineHeight,
proximity = this.proximity,
proximityFactor = this.proximityFactor;
var value = (0, _utils.getFontValue)(baseFontSize);
var unit = (0, _utils.getFontUnit)(baseFontSize);
var spacing = value * baseLineHeight * factor;
if (proximity) {
spacing = spacing * proximityFactor;
}
return '' + spacing + unit;
}
}]);
return Shevy;
}();
exports.default = Shevy;