-
Notifications
You must be signed in to change notification settings - Fork 0
/
nifty.js
117 lines (96 loc) · 2.32 KB
/
nifty.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
105
106
107
108
109
110
111
112
113
114
115
116
117
const plib = require('path');
exports.run =
function(client) {
nw.Window.open('index.html', {
id: "nifty",
title: "Nifty!",
always_on_top: true,
transparent: true,
frame: false,
width: 800,
height: 600,
resizable: false,
}, win => {
win.window._client = client;
});
}
// == lib == //
const lib =
exports.lib = {
sort(items, query) {
const sticky = items.filter(item => item.isSticky);
const nonsticky = items.filter(item => !item.isSticky);
const stickySorted = fuzzySort(sticky, 'searchText', query);
const nonstickySorted = fuzzySort(nonsticky, 'searchText', query);
return [].concat(stickySorted, nonstickySorted);
},
mkSimple({
text,
exec,
icon,
isSticky,
}) {
return lib.mkItem({
displayText: text,
searchText: text,
exec,
icon,
isSticky,
})
},
mkItem({
displayText,
searchText,
exec,
isSticky,
icon
}) {
return {
displayText,
searchText,
exec: exec || (() => {}),
isSticky: isSticky || false,
render({ document, isSelected }) {
const $item = document.createElement('div');
$item.classList.add('item');
if (isSelected) $item.classList.add('selected');
if (icon !== null && icon !== undefined) {
const $icon = document.createElement('img');
$item.append($icon);
$icon.classList.add('item-icon');
const blankImg = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
$icon.src = icon ?? blankImg;
}
const $text = document.createElement('span');
$item.append($text);
$text.classList.add('item-text');
if (displayText.trim() !== '')
$text.innerText = displayText;
else
$text.innerHTML = ' ';
return $item;
},
};
},
};
const fz = require('fuzzysort');
function fuzzySort(items, key, query) {
let results = fz.go(
query,
items,
{
key,
threshold: -Infinity,
limit: Infinity,
all: true,
}
);
results = results.map(res => res.obj);
// re-add removed items
const keys = new Set(results.map(item => item[key]));
for (const item of items) {
if (!keys.has(item[key]))
results.push(item);
}
return results;
}