-
Notifications
You must be signed in to change notification settings - Fork 1
/
madlibs.js
287 lines (227 loc) · 8.6 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* *
* Complete the implementation of parseStory.
*
* parseStory retrieves the story as a single string from story.txt
* (I have written this part for you).
*
* In your code, you are required (please read this carefully):
* - to return a list of objects
* - each object should definitely have a field, `word`
* - each object should maybe have a field, `pos` (part of speech)
*
* So for example, the return value of this for the example story.txt
* will be an object that looks like so (note the comma! periods should
* be handled in the same way).
*
* Input: "Louis[n] went[v] to the store[n], and it was fun[a]."
* Output: [
* { word: "Louis", pos: "noun" },
* { word: "went", pos: "verb", },
* { word: "to", },
* { word: "the", },
* { word: "store", pos: "noun" }
* { word: "," }
* ....
*
* There are multiple ways to do this, but you may want to use regular expressions.
* Please go through this lesson: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/ */
/**
* A function that retrieves the story from story.txt and return an array of objects
* @param {String} rawStory: string
* @returns {Array<Objetc>} wordsObjetct: Array of objetcs
*/
function parseStory(rawStory) {
/* *
* An Object to maps part of speech codes to their corresponding labels
*/
const partOfSpeech = {
n: "[noun]",
v: "[verb]",
a: "[adjectif]",
ad: "[adverb]",
}
/**
* Regex matches words with part of speech codes (e.g., "word[pos]")
* Or just words
*
*/
const matchWordsRegex = /\w+\[[anvad]+\]|[.,]|\w+/g
/**
* Regex to match the format "word[pos]"
* And captures both the word and the part of speech code.
*/
const matchPosRegex = /(\w+)\[([anvad]+)\]/
return rawStory.match(matchWordsRegex)
.map(word => {
/**
* Get pos match : word[pos]
* And captures both the word and the part of speech code.
*/
const match = word.match(matchPosRegex)
/**
* create wordsObjetct objetc
* Set word field to the extracted word match[1] if there is one
* Or else simply set it to word
*/
const wordsObjetct = { word: match ? match[1] : word }
/**
* Checks if a match exists
* and if the extracted part of speech code (match[2])
* has a corresponding entry in the partOfSpeech {a,v,n}
* if so we set pos field to partOfSpeech[match[2]] .
*/
if (match && partOfSpeech[match[2]]) {
wordsObjetct.pos = partOfSpeech[match[2]];
}
return wordsObjetct;
})
}
let madLibsEdit = document.querySelector(".madLibsEdit")
let madLibsPreview = document.querySelector(".madLibsPreview")
/**
* All your other JavaScript code goes here, inside the function. Don't worry about
* the `then` and `async` syntax for now.
*
* You'll want to use the results of parseStory() to display the story on the page.
*/
getRawStory().then(parseStory).then((processedStory) => {
processedStory.map(item => {
//console.log(item)
const span_1 = document.createElement('span')
const span_2 = document.createElement('span')
const input = document.createElement('input')
const mark = document.createElement('mark')
mark.innerText = item.word
mark.setAttribute('data_default', item.word)
input.type = "text";
input.maxLength = 20;
input.classList.add("input"); // Adding the "input" class to the input element
if (item.hasOwnProperty('pos')) {
input.placeholder = item.pos
input.addEventListener("input", () => {
input.value ? mark.innerText = input.value : mark.innerText = item.word
})
madLibsEdit.appendChild(input)
madLibsPreview.appendChild(mark)
// Implement hotkey feature on enter key
const inputs = document.querySelectorAll(".input");
inputs.forEach((input, index) => {
input.addEventListener("keydown", (event) => {
if (event.key === "Enter") { // If the pressed key is "Enter"
event.preventDefault(); // Prevent the default behavior of the "Enter" key
const nextIndex = index + 1;
if (nextIndex < inputs.length) {
inputs[nextIndex].focus(); // Set focus to the next input field
}
}
});
});
} else {
span_1.innerText = item.word + ' '
span_2.innerText = item.word + ' '
madLibsEdit.appendChild(span_1)
madLibsPreview.appendChild(span_2)
}
})
// Manage theme fonctionality (Dark mode ,Ligth mode)
let theme_button = document.querySelector("#themeIcon")
theme_button.innerHTML = '<span class="darkBtn">Light Mode </span><img src="./assets/sun.png" title="light!">';
let h1 = document.getElementById('heading')
let body = document.querySelector('body')
let isLightmode = false // by default the dark mode is on
theme_button.addEventListener("click", () => {
if (isLightmode) {
//dark mode codes lies here
theme_button.innerHTML = '<span class="darkBtn">Light Mode </span><img src="./assets/sun.png" title="light!">';
body.classList.remove('body_light');
h1.classList.remove('h1_light');
isLightmode = !isLightmode;
madLibsEdit.classList.remove('madlibs_dark')
madLibsPreview.classList.add('madlibs_dark')
} else {
//light mode codes lies here
theme_button.innerHTML = '<span class="lightBtn">Dark Mode </span><img src="./assets/moon.png" title="Darkness consumes!">'; // change icon
body.classList.add('body_light');
h1.classList.add('h1_light');
madLibsEdit.classList.add('madlibs_dark')
madLibsPreview.classList.remove('madlibs_dark')
/**add the rest changes */
isLightmode = !isLightmode;
}
// const materialIcon = document.createElement('i');
// if(theme_button.firstElementChild.innerText ==="brightness_4"){
// theme_button.firstElementChild.innerText="dark_mode"
// }else{
// theme_button.firstElementChild.innerText="brightness_4"
// }
})
// reset button fonctionality
const reset = document.querySelector('#reset')
//#endregion
reset.addEventListener("click", () => {
const all_input = document.querySelectorAll('input')
const all_mark = document.querySelectorAll('mark')
all_input.forEach(item => {
item.value = ''
})
all_mark.forEach(item => {
item.innerText = item.getAttribute('data_default')
})
})
/*Adding Sound functionality*/
/*The DOMContentLoaded event fires when the DOM content is loaded,
without waiting for images and stylesheets to finish loading.*/
const soundButton = document.getElementById("soundButton");
const sound = document.getElementById("sound");
let isPlaying = false; //by default the sound is off
soundButton.addEventListener("click", function () {
if (isPlaying) {
sound.pause();
sound.currentTime = 0; // plays from the begining
isPlaying = false;
soundButton.innerHTML = '<img src="./assets/music.png" title="Play Sound">'; // change icon
} else {
sound.play();
isPlaying = true;
soundButton.innerHTML = '<img src="./assets/music.png" title="Pause Sound">';
}
});
/* I added an event listner so that the translation scripts only activates when button is clicked */
translateButton = document.getElementById("google_translate_element")
translateButton.addEventListener("click", googleTranslateElementInit)
function googleTranslateElementInit() {
new google.translate.TranslateElement(
{ pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE },
);
}
const script = document.createElement('script');
script.src = '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit';
document.head.appendChild(script);
});
const downloadButton = document.getElementById('download');
// const contentToDownload = document.getElementsByClassName('madLibsPreview');
downloadButton.addEventListener('click', () => {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF();
const Children_element = madLibsPreview.children
const innerTextArray = [];
let count = 0
for (const childElement of Children_element) {
if (count === 5) {
innerTextArray.push("\n")
innerTextArray.push(childElement.innerText)
count = 0
} else {
innerTextArray.push(childElement.innerText);
count++
}
}
const text = innerTextArray.reduce((total, val) => {
return total.concat(" ", val)
}, " ")
pdf.setFontSize(12);
pdf.setFont("courier");
pdf.text(text, 100, 20, 'center')
pdf.save('madlib_story.pdf')
alert("your story download successfuly")
});