-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
80 lines (75 loc) · 2.62 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
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;
}
});
});
}
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();
}
}