-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
105 lines (101 loc) · 3.65 KB
/
app.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
"use strict";
const urlParams = new URLSearchParams(window.location.search);
const search = urlParams.get('q');
var quickCopyOnSearch = "YES"
var fuse;
var db;
var lasttext;
if (search !== null && search !== "") {
document.getElementById("searchbar").value = search;
}
loadFile("template-db.json");
var searchBar = document.getElementById("searchbar");
var timeout = null;
searchBar.addEventListener('keyup', (e) => {
if (e.key == "Enter") {
if (true) {
updateSearch(searchBar.value);
navigator.clipboard.writeText(lasttext);
document.getElementById("message").innerHTML = "Copied template to clipboard!";
document.getElementById("message").style.visibility = "visible";
document.getElementById("message").style.margin = "20px"
setTimeout(function(){
document.getElementById("message").style.visibility = "hidden";
document.getElementById("message").style.margin = "0px"
}, 1000);
}
} else {
clearTimeout(timeout);
timeout = setTimeout(() => {
updateSearch(searchBar.value);
}, 1000);
}
});
updateList();
document.getElementById("message").style.visibility = "hidden"
document.getElementById("message").style.margin = "0px"
function updateList() {
updateSearch(document.getElementById("searchbar").value);
}
function updateSearch(searchTerm) {
if (searchTerm == "") {
updateCards(db);
} else {
updateCards(fuse.search(searchTerm));
}
}
function updateCards(list) {
document.getElementById("result").innerHTML = "";
var missinglasttext = true;
for (const obj of list) {
var card = document.createElement("div")
card.className = "card";
var container = document.createElement("div");
container.className = "container";
container.innerHTML = "<h4><b>" + obj.title + "</b></h4>"+
"<h5>" + obj.tags + "</h5><p>" + obj.text + "</p>";
card.appendChild(container);
document.getElementById("result").appendChild(card);
card.addEventListener("click", () => {
var regex = /<br\s*[\/]?>/gi;
navigator.clipboard.writeText(obj.text.replace(regex, "\n"));
document.getElementById("message").innerHTML = "Copied template to clipboard!";
document.getElementById("message").style.visibility = "visible";
document.getElementById("message").style.margin = "20px"
setTimeout(function(){
document.getElementById("message").style.visibility = "hidden";
document.getElementById("message").style.margin = "0px"
}, 1000);
});
if (missinglasttext) {
var regex = /<br\s*[\/]?>/gi;
lasttext = obj.text.replace(regex, "\n");
missinglasttext = false;
}
}
}
function loadFile(filePath) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", filePath, false);
xmlhttp.onload = function(e) {
if (xmlhttp.status==200) {
db = JSON.parse(xmlhttp.responseText);
var options = {
shouldSort: true,
threshold: 0.6,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: [
"title",
"tags",
"text"
]
};
fuse = new Fuse(db, options);
updateList();
}
}
xmlhttp.send();
}