-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch-dialog.js
56 lines (46 loc) · 1.6 KB
/
search-dialog.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
const { ipcRenderer } = require('electron');
function triggerSearch() {
const query = document.getElementById('query').value.trim();
ipcRenderer.send('search-files', query);
}
document.addEventListener('DOMContentLoaded', () => {
// 自动将焦点设置到 ID 为 x 的输入框
const inputBox = document.getElementById('query');
if (inputBox) {
inputBox.focus();
}
});
document.getElementById('query').addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault(); // 防止回车键产生默认行为
triggerSearch();
}
});
document.getElementById('query').addEventListener('input', () => {
debounce(triggerSearch, 500)();
});
// Handle search button click
document.getElementById('search').addEventListener('click', () => {
triggerSearch();
});
function openFile(filePath) {
ipcRenderer.send('open-file', filePath);
}
// Listen for search results from the main process
ipcRenderer.on('search-results', (event, results) => {
const resultContainer = document.getElementById('result');
resultContainer.innerHTML = results.map(result => `
<div>
<div class="title"><strong>[<a href='#' onclick="openFile('${result.file}');return false;">${result.file}</a>] <span class='title-content'>${result.title}</span></strong></div>
<div class="snippet">${result.content}</div>
</div>
<hr>
`).join('');
});
function debounce(func, delay) {
let timer;
return function(...args) {
clearTimeout(timer);
timer = setTimeout(() => func.apply(this, args), delay);
};
}