-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
88 lines (76 loc) · 2.88 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
import init, { VecSearch } from "./pkg/doc_wasm.js";
const k = 5;
function cleanString(inputString) {
// Remove line breaks and collapse consecutive whitespaces
var cleanedString = inputString.replace(/\s+/g, " ").replace(/\n/g, " ");
// Remove leading and trailing whitespaces
cleanedString = cleanedString.trim();
return cleanedString;
}
function highlightTextElement(divCell, text, opacityValue) {
var elements = divCell.querySelectorAll("*");
// Loop over elements and collect text nodes
var normalizedOpacity = Math.min(Math.max(opacityValue / k, 0), 1);
elements.forEach(function (element) {
var textNode = element.textContent;
textNode = cleanString(textNode);
if (textNode.includes(text)) {
console.log("Element matched : ", element);
element.classList.add("highlight");
console.log("opacityValue", opacityValue);
console.log("normal_opacity", normalizedOpacity);
element.style.opacity = normalizedOpacity;
}
});
}
function highlightTextInElement(element, searchString) {
var nodeIterator = document.createNodeIterator(element, NodeFilter.SHOW_TEXT);
var currentNode;
while ((currentNode = nodeIterator.nextNode())) {
var originalContent = currentNode.nodeValue;
var textContent = cleanString(originalContent);
if (textContent.includes(searchString)) {
console.log("Found : {%s} in node : %s", searchString, textContent);
// Update node value
// currentNode.nodeValue = textContent;
var startIndex = textContent.indexOf(searchString);
var endIndex = startIndex + searchString.length;
var range = document.createRange();
range.setStart(currentNode, startIndex);
range.setEnd(currentNode, endIndex);
var span = document.createElement("span");
span.className = "highlight";
range.surroundContents(span);
}
}
}
function clearBG() {
var highlightedElements = document.querySelectorAll(".highlight");
// Loop through each element and remove the "highlight" class
highlightedElements.forEach(function (element) {
element.classList.remove("highlight");
element.style.opacity = 1;
});
}
(async () => {
const initResult = await init();
// NOTE: Testing random inference
const search_module = await new VecSearch();
console.log(search_module);
var elements = document.getElementById("text-contents");
// Search
const button = document.getElementById("searchButton");
button.addEventListener("click", function () {
clearBG();
const txt = document.getElementById("searchText").value;
console.log(`Performing search for: ${txt}`);
// Get elements
// Search for nearest
search_module.search(txt, k).then((search_results) => {
console.log("VecSearch results:", search_results);
search_results.forEach((searchText, index) => {
highlightTextElement(elements, searchText, k - index + 1);
});
});
});
})();