This repository was archived by the owner on Dec 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (78 loc) · 2.7 KB
/
Copy pathindex.js
File metadata and controls
87 lines (78 loc) · 2.7 KB
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
import {plugin} from 'postcss';
import valueParser, {stringify, unit} from 'postcss-value-parser';
import resembleImage from './resembleImage';
import simpleGradient from './simpleGradient';
const resembleFunction = 'resemble-image';
export complexGradient from './complexGradient';
export simpleGradient from './simpleGradient';
function matchesSelector (node, selectors) {
return selectors.some(selector => ~node.selectors.indexOf(selector));
}
function resemblePromise (decl, opts, wrapped = true) {
const promises = [];
let url;
let toString;
decl.value = valueParser(decl.value).walk((node, index, nodes) => {
const {type, value} = node;
if (type !== 'function') {
return false;
}
if (wrapped && value === resembleFunction) {
url = node.nodes[0].nodes[0].value;
toString = node.nodes[0];
}
if (!wrapped && value === 'url') {
url = node.nodes[0].value;
toString = node;
}
if (!url) {
return false;
}
const second = node.nodes[2];
const fidelity = unit(second && second.value || opts.fidelity.toString());
promises.push(
resembleImage(
url,
{
...opts,
fidelity,
}
).then(gradient => {
// Remove the resemble-image function wrapper if there is any
node.value = stringify(toString);
node.type = 'word';
// Add the gradient at the end of the value
nodes.push({
type: 'word',
value: `, ${gradient}`,
after: true,
});
})
);
return false;
});
return Promise.all(promises).then(() => (decl.value = decl.value.toString()));
}
export default plugin('postcss-resemble-image', (opts = {}) => {
const {selectors} = opts = {
fidelity: '25%',
generator: simpleGradient,
selectors: [],
...opts,
};
return css => {
return new Promise((resolve, reject) => {
const promises = [];
css.walkDecls(/^background(?:-image)?/, node => {
const {value} = node;
if (~value.indexOf(resembleFunction)) {
return promises.push(resemblePromise(node, opts));
}
if (~value.indexOf('url') && matchesSelector(node.parent, selectors)) {
return promises.push(resemblePromise(node, opts, false));
}
});
return Promise.all(promises).then(resolve, reject);
});
};
});