-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmadlibs.js
79 lines (79 loc) · 2.48 KB
/
madlibs.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
const listOfWords = [];
const madLibsEdit = document.querySelector(".madLibsEdit");
const madLibsPreview = document.querySelector(".madLibsPreview");
const previewText = document.createElement("p"); // consist preInput class spans which are edited and normal spans
const editText = document.createElement("p"); // consists contenteditable inputs and spans
function edit() {
editText.innerHTML = "";
const mapPos = { n: "noun", v: "verb", a: "adjective" };
let inputCount = 0;
listOfWords.forEach((obj) => {
if (obj.pos !== null) {
const posString = obj.pos.join("").slice(1, -1);
editText.innerHTML +=
`<input id="${"input" + inputCount}" maxlength = {20} placeholder ="${
mapPos[posString.match(/n|v|a/).join("")]
}"> </input>` + " ";
inputCount++;
} else {
editText.innerHTML += `<span>${obj.word}</span>` + " ";
}
});
madLibsEdit.appendChild(editText);
}
function preview() {
const inputs = document.getElementsByTagName("input");
for (let input of inputs) {
previewText.innerHTML = "";
const editElements =
document.querySelector(".madLibsEdit").children[0].children;
for (let item of editElements) {
if (item.tagName === "INPUT") {
if (item.value !== "") {
previewText.innerHTML +=
`<span class ="preInput">${item.value}</span>` + " ";
} else {
previewText.innerHTML +=
"<span class ='preInput'>________</span>" + " ";
}
} else if (item.tagName == "SPAN") {
previewText.innerHTML += `<span>${item.textContent}</span>` + " ";
}
}
madLibsPreview.appendChild(previewText);
}
}
function hotKey() {
const inputs = document.querySelectorAll("input");
inputs.forEach((input, index) => {
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
index <= inputs.length - 2
? inputs[index + 1].focus()
: inputs[0].focus();
}
});
input.addEventListener("input", preview);
});
}
function parseStory(rawStory) {
let parts = rawStory.split(" ");
parts.forEach((part) => {
let word = part.match(/\w+/);
let pos = part.match(/\[\w+\]/);
let punc = part.match(/,|\./);
let obj1 = { word, pos };
listOfWords.push(obj1);
if (punc) {
listOfWords.push({ word: punc.join(""), pos: null });
}
});
return listOfWords;
}
getRawStory()
.then(parseStory)
.then((processedStory) => {
edit(processedStory);
hotKey();
preview();
});