-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (57 loc) · 2.17 KB
/
Copy pathscript.js
File metadata and controls
67 lines (57 loc) · 2.17 KB
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
//words
const words = {
NOUN: ["love", "heart", "sky", "eyes", "fire"],
VERB: ["have", "come", "do", "forget", "has"],
ADJ: ["red", "warm", "dear"],
ADV: ["so", "again", "more", "first", "back"],
OTHER: ["the", "I", "by", "and", "you"],
};
//put words in first container
const wordContainer = document.querySelector(".word_container");
for (const [category, wordList] of Object.entries(words)) {
wordList.forEach((word) => {
const wordElement = document.createElement("div");
wordElement.textContent = word;
wordElement.classList.add("word");
wordElement.draggable = true; //making words draggable
wordContainer.appendChild(wordElement);
});
}
//drag and drop
const workspace = document.querySelector(".workspace");
//dragstart
wordContainer.addEventListener("dragstart", (e) => {
if (e.target.classList.contains("word")) {
e.target.classList.add("dragging");
e.dataTransfer.setData("text/plain", e.target.textContent);
}
});
//dragend event
wordContainer.addEventListener("dragend", (e) => {
if (e.target.classList.contains("word")) {
e.target.classList.remove("dragging");
}
});
//dropping
workspace.addEventListener("dragover", (e) => {
e.preventDefault();
});
workspace.addEventListener("drop", (e) => {
e.preventDefault();
const droppedWord = e.dataTransfer.getData("text/plain");
const wordElement = document.createElement("div");
wordElement.textContent = droppedWord;
wordElement.classList.add("word");
workspace.appendChild(wordElement);
//remove the word from the container
const wordsInContainer = Array.from(wordContainer.querySelectorAll(".word"));
const wordToRemove = wordsInContainer.find((word) => word.textContent === droppedWord);
if (wordToRemove) {
wordContainer.removeChild(wordToRemove);
}
});
//print poem
document.getElementById('save-poem').addEventListener('click', () => {
const poemWords = Array.from(workspace.querySelectorAll('.word')).map(word => word.textContent).join(' ');
alert(`${poemWords}`);
});