-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
153 lines (144 loc) · 4.88 KB
/
script.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
document.addEventListener("keydown", (e)=>{
if(e.metaKey && e.key==="s"){
e.preventDefault();
SaveNoteData();
}
});
const Container=document.getElementById("container");
let selectedText="", rangeAt="";
let noteData=localStorage.getItem("noteData");
readNoteData();
function readNoteData(){
noteData=JSON.parse(noteData);
noteData.forEach((element)=>{
CreateNewNote(element.value);
})
}
function CreateNewNote(e){
let div=document.createElement("div");
div.classList.add("note-row");
let newNote=` <div class="note-row">
<div class="note-editor" contenteditable="true" onmouseup="getSelectedText()" id="note-editor">`+e+`</div>
<div class="note-control">
<div onclick="getSelectedStyle('capitalize')" class="capitalize">Aa</div>
<div onclick="getSelectedStyle('bold')" class="bold">B</div>
<div onclick="getSelectedStyle('italic')" class="italic">I</div>
<div onclick="getSelectedStyle('underline')" class="underline">U</div>
<div onclick="getSelectedStyle('linethrough')" class="linethrough">ab</div>
<hr/>
<img src="img2.png" onclick="DeleteNode(this)"/>
</div>
</div>`;
div.innerHTML=newNote;
Container.appendChild(div);
const noteEditorData=document.querySelectorAll(".note-editor");
noteEditorData.forEach((element)=>{
element.addEventListener("keypress", (e)=>{
if(e.key==="Enter"){
document.execCommand("insertHTML", false, "<br/>");
return false;
}
});
});
const option=document.getElementById("options");
option.style.display="none";
}
function CreateTodoList(e){
let id=Date.now();
let div=document.createElement("div");
div.classList.add("todo-app");
let newList=`<div class="todo-app" id="todo-app-${id}">
<h2>To-Do List<img src="icon.png"></h2>
<div class="row">
<input type="text" id="input-box-${id}" placeholder="Add your text">
<button class="add" onclick="addTask(${id})">Add</button>
</div>
<ul id="list-container-${id}">
</ul>
</div>`;
div.innerHTML=newList;
Container.appendChild(div);
const listcontainer=document.getElementById(`list-container-${id}`);
listcontainer.addEventListener("click", function(e)
{
if(e.target.tagName==="LI"){
e.target.classList.toggle("checked");
saveData(id);
}
else if(e.target.tagName==="SPAN"){
e.target.parentElement.remove();
saveData(id);
}
}, false);
const option=document.getElementById("options");
option.style.display="none";
}
function addTask(id){
const inputBox=document.getElementById(`input-box-${id}`);
const listcontainer=document.getElementById(`list-container-${id}`);
//if input box is empty
if(inputBox.value===''){
alert("You must write something");
}
else{
let li=document.createElement("li");
li.innerHTML=inputBox.value;
listcontainer.appendChild(li);
let span=document.createElement("span");
span.innerHTML="\u00d7";
li.appendChild(span);
}
inputBox.value="";
saveData(id);
}
// to store the data
function saveData(id){
const listcontainer=document.getElementById(`list-container-${id}`);
localStorage.setItem(`data-${id}`, listcontainer.innerHTML);
}
// to show data
function showTask(){
const todoApps=document.querySelectorAll(".todo-app");
todoApps.forEach((element)=>{
const id=element.id.split('-')[2];
const listcontainer=document.getElementById(`list-container-${id}`);
listcontainer.innerHTML=localStorage.getItem(`data-${id}`);
});
}
function SaveNoteData(){
noteData=[];
localStorage.setItem("noteData", []);
const noteEditorData=document.querySelectorAll(".note-editor");
noteEditorData.forEach((element)=>{
if(element.innerHTML!==""){
let html={value: element.innerHTML};
noteData.push(html);
}
});
localStorage.setItem("noteData", JSON.stringify(noteData));
}
function getSelectedText(){
selectedText=window.getSelection().toString();
rangeAt=window.getSelection().getRangeAt(0);
}
function getSelectedStyle(e){
if(selectedText){
let div=document.createElement("span");
div.classList.add(e);
div.innerHTML=selectedText;
rangeAt.deleteContents();
rangeAt.insertNode(div);
}
}
function DeleteNode(e){
let confirmed=confirm("Are you sure? Do you want to delete it?");
if(confirmed){
e.parentNode.parentNode.remove();
SaveNoteData();
}
}
function showoptions(){
const option=document.getElementById("options");
option.style.display="block";
}
showTask();