This repository has been archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
90 lines (78 loc) · 2.64 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
var postcss = require('postcss'),
functionCall = require('reduce-function-call'),
extend = require('util')._extend;
module.exports = postcss.plugin('postcss-grid', function (opts) {
opts = extend({
columns: 12,
maxWidth: 960,
gutter: 20,
legacy: false
}, opts);
var columnWidth = (opts.maxWidth - ((opts.columns -1 ) * opts.gutter)) / opts.columns;
var gridWidth = function (span, cols) {
var width = span * columnWidth + (span -1 ) * opts.gutter;
var container = cols * columnWidth + (cols -1) * opts.gutter;
return ((width / container) * 100).toFixed(5) * 1;
};
var gutterWidth = function (cols) {
var width = cols * columnWidth + (cols - 1) * opts.gutter;
return ((opts.gutter / width) * 100).toFixed(5) * 1;
};
var value = /\s*(\d+)\s*\/\s*(\d+)\s*/;
var isLast = /\s*!last\s*$/;
return function (css) {
css.walkDecls(function (decl) {
if (decl.value.indexOf('grid-width(') !== -1) {
decl.value = functionCall(decl.value, "grid-width", function (body) {
var match;
if (match = value.exec(body)) {
var span = match[1];
var columns = match[2];
return gridWidth(span, columns) + '%'
}
else {
throw decl.error('Invalid declaration', { plugin: 'postcss-grid' });
}
});
}
if (decl.value.indexOf('grid-gutter(') !== -1) {
decl.value = functionCall(decl.value, "grid-gutter", function (body) {
return gutterWidth(body) + '%'
});
}
if (decl.prop === 'grid-column') {
var match;
if (match = value.exec(decl.value)) {
var span = match[1];
var columns = match[2];
decl.parent.append({prop: 'float', value: 'left'}).source = decl.source;
decl.parent.append({prop: 'width', value: gridWidth(span, columns) + '%'}).source = decl.source;
if (!(decl.value.match(isLast))) {
if (opts.legacy) decl.parent.append({prop: 'display', value: 'inline'}).source = decl.source;
decl.parent.append({prop: 'margin-right', value: gutterWidth(columns) + '%'}).source = decl.source;
}
decl.remove();
}
else {
throw decl.error('Invalid declaration', { plugin: 'postcss-grid' });
}
}
if (decl.prop === 'grid-push' || decl.prop === 'grid-pull') {
var match;
if (match = value.exec(decl.value)) {
var span = match[1];
var columns = match[2];
var width = span * gridWidth(1, columns) + span * gutterWidth(columns);
decl.parent.append({
prop: decl.prop === 'grid-push' ? 'margin-left' : 'margin-right',
value: width + '%'
}).source = decl.source;
decl.remove();
}
else {
throw decl.error('Invalid declaration', { plugin: 'postcss-grid' });
}
}
});
};
});