-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
123 lines (99 loc) · 4.3 KB
/
Copy pathscript.js
File metadata and controls
123 lines (99 loc) · 4.3 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
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
const btnEle = document.querySelector(".search-btn");
const containerEle = document.querySelector(".search-container");
const suggestionsContainer = document.querySelector(".suggestions-container");
const searchInput = document.querySelector(".search-input");
let currentFocus = -1;
btnEle.addEventListener("click", () => {
containerEle.classList.toggle("active");
searchInput.focus();
suggestionsContainer.innerHTML = "";
});
searchInput.addEventListener("input", async () => {
const query = searchInput.value;
const cursorPosition = searchInput.selectionStart;
// Split text into words and identify the word being edited
const beforeCursor = query.slice(0, cursorPosition);
const afterCursor = query.slice(cursorPosition);
// Find word boundaries around cursor
const wordsBeforeCursor = beforeCursor.split(" ");
const wordsAfterCursor = afterCursor.split(" ");
// Get the partial word at cursor
const currentWordBefore = wordsBeforeCursor[wordsBeforeCursor.length - 1];
const currentWordAfter = wordsAfterCursor[0];
// Combine the parts of the current word
const currentWord = (currentWordBefore + currentWordAfter).trim();
// Get the part of the word before the cursor for matching
const searchPrefix = currentWordBefore.trim();
if (searchPrefix.length > 0) {
// Convert the search prefix to lowercase before sending to the server
const lowercasePrefix = searchPrefix.toLowerCase();
const response = await fetch("http://localhost:8080", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query: lowercasePrefix })
});
const suggestions = await response.json();
console.log("Suggestions from server:", suggestions);
// Calculate prefix and suffix for suggestion display
const prefixWords = wordsBeforeCursor.slice(0, -1);
const suffixWords = wordsAfterCursor.slice(1);
const prefixText = prefixWords.length > 0 ? prefixWords.join(" ") + " " : "";
const suffixText = suffixWords.length > 0 ? " " + suffixWords.join(" ") : "";
displaySuggestions(suggestions, prefixText, suffixText, searchPrefix);
} else {
suggestionsContainer.innerHTML = "";
}
});
function displaySuggestions(suggestions, prefixText, suffixText, searchPrefix) {
suggestionsContainer.innerHTML = "";
currentFocus = -1;
suggestions.forEach(suggestion => {
const item = document.createElement("div");
item.classList.add("suggestion-item");
// Construct full suggestion text with surrounding words
const fullText = `${prefixText}${suggestion}${suffixText}`.trim();
item.textContent = fullText;
item.addEventListener("click", () => {
searchInput.value = fullText;
searchInput.focus();
suggestionsContainer.innerHTML = "";
});
suggestionsContainer.appendChild(item);
});
}
searchInput.addEventListener("keydown", (e) => {
const items = suggestionsContainer.querySelectorAll(".suggestion-item");
if (e.key === "ArrowDown") {
currentFocus++;
addActive(items);
} else if (e.key === "ArrowUp") {
currentFocus--;
addActive(items);
} else if (e.key === "Enter") {
e.preventDefault();
if (currentFocus > -1 && items[currentFocus]) {
searchInput.value = items[currentFocus].textContent;
suggestionsContainer.innerHTML = "";
}
}
});
function addActive(items) {
if (!items) return;
removeActive(items);
if (currentFocus >= items.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = items.length - 1;
items[currentFocus].classList.add("active-suggestion");
// Scroll the active item into view
items[currentFocus].scrollIntoView({
behavior: "smooth",
block: "nearest"
});
}
function removeActive(items) {
items.forEach(item => item.classList.remove("active-suggestion"));
}
document.addEventListener("click", (e) => {
if (!containerEle.contains(e.target)) {
suggestionsContainer.innerHTML = "";
}
});