-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewTab.js
158 lines (128 loc) · 4.8 KB
/
newTab.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
//Inserting the text into the text editor
document.addEventListener('DOMContentLoaded', function() {
navigator.clipboard
.readText()
.then(
cliptext =>
(document.getElementById('clipboard-paste').innerText = cliptext),
err => console.log(err)
);
document.getElementById("clipboard-paste").addEventListener("input", function() {
//tldr: implement auto-copy
}, false);
let storeBtn = document.querySelector('.store_items');
let txtTitle = document.querySelector('.main-title');
let txtContent = document.querySelector('#clipboard-paste');
cardDisplay();
storeBtn.addEventListener('click', () => {
let savedText = {
title: txtTitle.innerText,
content: txtContent.value
};
SaveDataToLocalStorage(savedText);
//toast msg
var x = document.getElementById("snackbar");
// Add the "show" class to DIV
x.className = "show";
// After 3 seconds, remove the show class from DIV
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
})
let cards = document.querySelectorAll('.cards');
cards.forEach((card) => {
card.addEventListener('click', onClick, false);
})
function onClick(e) {
var card = e.currentTarget;
txtTitle.innerHTML = card.querySelector('.title').innerHTML;
txtContent.innerHTML = card.querySelector('.text-body').innerHTML;
}
let addNoteBtn = document.querySelector('.add-note-btn');
addNoteBtn.addEventListener('click', () =>{
txtTitle.innerHTML = "Add title";
txtContent.value = "Add text";
})
})
/*let addButton=document.querySelector('#add-note-btn');
addButton.addEventListener('click',()=>{
document.getElementById('main-title').textContent="Enter Title";
document.getElementById('clipboard-paste').textContent="Enter Title";
console.log("Hi");
})*/
//Setting attributes for downloading the text
//Save to localStorage code
function SaveDataToLocalStorage(data)
{
var a = [];
// Parse the serialized data back into an aray of objects
a = JSON.parse(localStorage.getItem('data')) || [];
// Push the new data (whether it be an object or anything else) onto the array
a.push(data);
// Re-serialize the array back into a string and store it in localStorage
localStorage.setItem('data', JSON.stringify(a));
data = JSON.parse(localStorage.getItem('data'));
cardBuilder(data.at(-1).title, data.at(-1).content);
}
//Cardbuilder function
function cardBuilder(title, content) {
var storedData = [];
storedData = JSON.parse(localStorage.getItem('data'));
console.log(storedData);
var cards = document.createElement("div");
cards.setAttribute("class", "cards");
var cardTitle = document.createElement("h3");
cardTitle.setAttribute("class", "title");
var cardCreatedTime = document.createElement("p");
cardCreatedTime.setAttribute("class", "time");
var cardContent = document.createElement("p");
cardContent.setAttribute("class", "text-body");
const delBtn = document.createElement("button");
delBtn.textContent = "Remove";
delBtn.setAttribute("class","delBtn")
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime ="Last edited: "+ date+' '+time;
//Adding contents
cardTitle.innerHTML = title;
cardCreatedTime.innerHTML = dateTime;
cardContent.innerHTML = content;
//Appending elements
cards.append(cardTitle);
cards.append(cardCreatedTime);
cards.append(cardContent);
cards.append(delBtn);
document.querySelector('.card-container').append(cards);
delBtn.addEventListener("click", () => {
cards.parentNode.removeChild(cards);
storedData = storedData.filter(item => item.content != content);
localStorage.setItem('data', JSON.stringify(storedData));
})
}
function cardDisplay() {
if(localStorage.getItem('data') != null) {
data = JSON.parse(localStorage.getItem('data'));
data.forEach((item) => {
cardBuilder(item.title, item.content);
})
}
}
//Download button
const fname='convtxt.txt'
function downloadFile(txtcontent){
const element=document.createElement('a');
const blob=new Blob([txtcontent],{type:'plain/text'});
const fileUrl=URL.createObjectURL(blob);
element.setAttribute('href',fileUrl);
element.setAttribute('download',fname);
element.style.display='none';
document.body.appendChild(element);
element.click();
};
window.onload=()=>{
document.getElementById('download').addEventListener('click', e=>{
const txtcontent=document.getElementById('clipboard-paste').textContent;
if(txtcontent){
downloadFile(txtcontent);
}
});
};