-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
193 lines (163 loc) · 4.76 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const _ = require('lodash');
const util = require('util');
const {EventEmitter2} = require('eventemitter2');
const DEFAULT_OPTIONS = {
target: 'webview',
delay: 150,
visibleClass: '.electronSearchText-visible',
input: '.electronSearchText-input',
count: '.electronSearchText-count',
box: '.electronSearchText-box',
};
const ElectronSearchText = (function() {
const ElectronSearchText = function (options) {
options = _.assign(DEFAULT_OPTIONS, options);
EventEmitter2.call(this, options);
this.$target = document.querySelector(options.target);
this.$searchCount = document.querySelector(options.count);
this.$searchInput = document.querySelector(options.input);
this.$searchBox = document.querySelector(options.box);
this.visibleClass = options.visibleClass;
this.delay = options.delay;
this.current = 0;
this.prevText = '';
this.findInPage = _.debounce(this._findInPage, options.delay);
this._eventify();
};
util.inherits(ElectronSearchText, EventEmitter2);
_.assign(ElectronSearchText.prototype, {
_eventify() {
this.$target.addEventListener('found-in-page', this._onFoundInPage.bind(this));
this.$searchInput.addEventListener('keydown', this._onKeydown.bind(this));
this.$searchInput.addEventListener('keyup', this._onKeyup.bind(this));
this.on('toggle', this.toggleSearch.bind(this));
this.on('show', this.show.bind(this));
this.on('hide', this.hide.bind(this));
},
/*
* call findInPage
* */
_findInPage(text, forward) {
let findNext = false;
// stop search when text is empty
if (text === '') {
this.prevText = '';
this.stopFindInPage();
return;
}
// find next match when text is same as prevText
if (text === this.prevText) {
findNext = true;
}
// find head of matches when text is not same as prevText
else {
this.prevText = text;
}
this.$target.findInPage(text, {findNext, forward});
},
/*
* call stopFindInPage
* */
stopFindInPage() {
this.$target.stopFindInPage('clearSelection');
this._resetCount();
},
/*
* toggle visible/invisible to box
* */
toggleSearch() {
const visibleClass = this.visibleClass.substring(1);
const isVisible = this.$searchBox.classList.contains(visibleClass);
isVisible ? this.hide() : this.show();
},
/*
* show search input
* */
show() {
const visibleClass = this.visibleClass.substring(1);
this.$searchBox.classList.add(visibleClass);
this.$searchInput.focus();
this.emit('did-finish-show')
},
/*
* hide search input
* */
hide() {
const visibleClass = this.visibleClass.substring(1);
this.$searchBox.classList.remove(visibleClass);
this.$searchInput.blur();
this.stopFindInPage();
this.emit('did-finish-hide')
},
/*
* set matches count
* */
_setCount(count) {
this.$searchCount.textContent = count;
},
/*
* reset matches count
* */
_resetCount() {
this._setCount('');
},
/*
* display matches count
* */
_onFoundInPage({result}) {
const {activeMatchOrdinal, finalUpdate, matches} = result;
// keep match position
if (activeMatchOrdinal) {
this.current = activeMatchOrdinal;
}
// when search finished
if (finalUpdate) {
// reset match count when no matches
if (matches === 0) {
this.current = 0;
}
// display matches count
this._setCount(`${this.current}/${matches}`);
}
},
_onKeydown(e) {
// find previous when Shift pressed
var forward = !e.shiftKey;
var isMeta = e.metaKey;
var text = this.$searchInput.value;
switch(e.code) {
case 'Enter':
this.findInPage(text, forward);
break;
// exit search also disable search box
case 'Escape':
this.stopFindInPage();
this.toggleSearch();
this.emit('did-press-escape');
break;
// cmd+g : find forward
// cmd+shift+g : find previous
case 'KeyG':
if (isMeta = true) {
this.findInPage(text, forward);
}
break;
}
},
_onKeyup(e) {
var text = this.$searchInput.value;
var {keyCode} = e;
// keyCode === [0-9] or [a-z]
var isNumOrChar = (keyCode <= 90 && keyCode >= 65) || (keyCode <= 57 && keyCode >= 48);
if (isNumOrChar) {
this.findInPage(text, true);
}
// apply without delay
if (e.code === 'Backspace') {
this._findInPage(text, true);
}
}
});
return ElectronSearchText;
})();
module.exports = ElectronSearchText;