-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
102 lines (95 loc) · 2.43 KB
/
main.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
const cont = document.getElementById("container");
const Http = new XMLHttpRequest();
let url = "http://localhost:8080/";
const SIZE = 2;
let notepads = [];
function init() {
for (let i = 0; i < 1; i++) {
notepads.push(new NotePad());
}
notepads.forEach(pad => {
cont.appendChild(pad.elem);
});
}
class NotePad {
constructor() {
this.elem = elemMaker("div", {
classList: "notepad"
});
this.elem.addEventListener("click", e => {
if (e.path[0].classList == "notepad")
this.lines[this.lines.length - 1].focus();
});
this.elem.addEventListener("keydown", e => {
if (e.key == "Enter") {
e.preventDefault();
console.log(e.path);
this.addLine();
}
});
this.lines = [new Line()];
this.lines.forEach(e => {
this.elem.appendChild(e.elem);
});
}
addLine(i) {
let line = new Line();
this.lines.push(line);
this.elem.appendChild(line.elem);
line.focus();
}
}
class Line {
constructor() {
this.elem = elemMaker("div", {
classList: "line"
});
this.editPart = elemMaker("div", {
contentEditable: "true",
classList: "edit"
});
this.elem.appendChild(this.editPart);
this.suggestor = elemMaker("span", {
classList: "sug"
});
this.elem.appendChild(this.suggestor);
this.editPart.addEventListener("keyup", e => {
if (e.key != "Tab") {
suggest(this.editPart.innerText, this.suggestor);
}
});
this.editPart.addEventListener("keydown", e => {
if (e.key == "Tab") {
e.preventDefault();
let selection = window.getSelection();
let range = selection.getRangeAt(0);
range.deleteContents();
let node = document.createTextNode(this.suggestor.innerText);
range.insertNode(node);
selection.modify("move", "right", "paragraphboundary");
}
});
}
focus() {
this.editPart.focus();
}
}
function suggest(words, elem) {
let splitWords = words.toLowerCase().split(" ");
console.log(splitWords);
let query = splitWords
.slice(splitWords.length - SIZE, splitWords.length)
.join(" ");
console.log(query);
Http.open("GET", url + query);
Http.send();
Http.onreadystatechange = e => {
elem.innerText = " " + Http.responseText;
};
}
function elemMaker(elem, config) {
let element = document.createElement(elem);
for (key in config) element[key] = config[key];
return element;
}
init();