-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
104 lines (84 loc) · 2.55 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';
const _ = require('lodash');
const caniuse = require('./caniuse-api');
const fs = require('fs');
const path = require('path');
module.exports = (pluginContext) => {
const shell = pluginContext.shell;
let html = '';
const browsers = ['firefox', 'chrome', 'ie', 'edge', 'opera', 'safari'];
function startup() {
html = fs.readFileSync(path.join(__dirname, 'preview.html'), 'utf8');
}
function capitalize(word) {
return word[0].toUpperCase() + word.slice(1);
}
function getDesc(property) {
const support = caniuse.getSupport(property);
var desc = "";
_.take(browsers, 6).map((x) => {
if (x == 'ie') {
desc = desc + 'IE <b>';
} else {
desc = desc + capitalize(x) + ' <b>';
}
if (support[x].y === undefined) {
desc = '<span style="color: #b51c1c">'+desc + '✘</span>';
} else {
desc = '<span style="color: #1a6510">'+desc + support[x].y + '+ </span>';
}
desc = desc + '</b>, ';
});
return desc.slice(0, -2);
}
function search(query, res) {
const query_trim = query.trim();
if (query_trim.length === 0) {
return res.add({
id: '',
payload: 'open',
title: 'Search on caniuse',
desc: 'Search on caniuse.com'
});
}
res.add({
id: query_trim,
payload: 'open',
title: 'Search property: ' + query_trim,
desc: 'Search on caniuse.com'
});
let results = caniuse.find(query_trim);
if (typeof results === "string") {
return res.add({
id: results,
payload: 'open',
title: results,
desc: getDesc(results)
});
} else {
_.take(results, 5).map((x) => {
if (x == query_trim) {
res.remove(query_trim);
}
return res.add({
id: x,
payload: 'open',
title: x,
desc: getDesc(x),
preview: true
});
});
}
}
function execute(id, payload) {
if (payload !== 'open') {
return;
}
shell.openExternal(`http://caniuse.com/#search=${id}`);
}
function renderPreview(id, payload, render) {
var preview = html.replace('%property%', id);
render(preview);
}
return {startup, search, execute, renderPreview};
};