Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
193 lines (167 loc) · 5.08 KB
/
Copy pathscript.js
File metadata and controls
193 lines (167 loc) · 5.08 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
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
const addBtn = document.getElementById('new-note-btn')
const board = document.getElementById('board')
const clearBtn = document.getElementById('clear-all')
const topBar = document.querySelector('.top-bar')
let myNotes = JSON.parse(localStorage.getItem('sticky_data_v3')) || []
let searchVal = ''
function createSearch () {
const search = document.createElement('input')
search.type = 'text'
search.placeholder = 'Search notes...'
search.className = 'search-input'
search.oninput = e => {
searchVal = e.target.value.toLowerCase()
refresh()
}
topBar.insertBefore(search, topBar.lastElementChild)
}
function save () {
localStorage.setItem('sticky_data_v3', JSON.stringify(myNotes))
}
function createNote (id, content, color, date) {
const card = document.createElement('div')
card.className = `note-card ${color}`
card.innerHTML = `
<textarea class="text-area" placeholder="Write here...">${content}</textarea>
<div class="note-info">
Characters: <span class="char-count">${content.length}</span>
</div>
<div class="note-footer">
<div class="color-dots">
<div class="dot yellow" onclick="changeColor(${id}, 'yellow')"></div>
<div class="dot pink" onclick="changeColor(${id}, 'pink')"></div>
<div class="dot blue" onclick="changeColor(${id}, 'blue')"></div>
</div>
<div class="note-actions">
<button title="Download" class="btn-download" onclick="downloadNote(${id})">💾</button>
<button class="btn-del" onclick="removeNote(${id})">🗑</button>
</div>
</div>
`
const area = card.querySelector('textarea')
const countSpan = card.querySelector('.char-count')
area.oninput = e => {
const val = e.target.value
countSpan.innerText = val.length
updateText(id, val)
}
board.appendChild(card)
}
function downloadNote (id) {
const n = myNotes.find(x => x.id === id)
const blob = new Blob([n.text], { type: 'text/plain' })
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = `note_${id}.txt`
link.click()
}
function updateText (id, val) {
myNotes = myNotes.map(n => (n.id === id ? { ...n, text: val } : n))
save()
}
function changeColor (id, col) {
myNotes = myNotes.map(n => (n.id === id ? { ...n, color: col } : n))
save()
refresh()
}
function removeNote (id) {
if (confirm('Permanently delete this?')) {
myNotes = myNotes.filter(n => n.id !== id)
save()
refresh()
}
}
function addBlank () {
const n = {
id: Date.now(),
text: '',
color: 'yellow',
date: new Date().toLocaleDateString()
}
myNotes.push(n)
save()
refresh()
}
function refresh () {
board.innerHTML = ''
const filtered = myNotes.filter(n => n.text.toLowerCase().includes(searchVal))
if (filtered.length === 0 && searchVal !== '') {
board.innerHTML = `<p class="no-match-msg">No match found...</p>`
} else {
filtered.forEach(n => createNote(n.id, n.text, n.color, n.date))
}
}
function importNotes (event) {
const file = event.target.files[0]
if (!file) return
const reader = new FileReader()
reader.onload = function(e) {
try {
if (file.name.endsWith('.json')) {
const imported = JSON.parse(e.target.result)
if (Array.isArray(imported)) {
const isValid = imported.every(n => n.id && typeof n.text === 'string' && n.color)
if (isValid) {
myNotes = [...imported, ...myNotes]
save()
refresh()
alert('Success: All notes restored perfectly!')
} else {
alert('Invalid backup file format.')
}
} else {
alert('Invalid file format. File must contain an array of notes.')
}
} else {
const text = e.target.result
const n = {
id: Date.now(),
text: text,
color: 'yellow',
date: new Date().toLocaleDateString()
}
myNotes.push(n)
save()
refresh()
alert('Success: Note imported from text file!')
}
} catch (err) {
alert('Failed to parse file content.')
}
}
reader.readAsText(file)
}
function addBackupButtons () {
const btnGroup = document.querySelector('.action-buttons')
if (!btnGroup) return
const impBtn = document.createElement('button')
impBtn.innerText = 'Import'
impBtn.className = 'btn-clear'
impBtn.style.color = '#3b82f6'
impBtn.style.cursor = 'pointer'
const fileInput = document.createElement('input')
fileInput.type = 'file'
fileInput.accept = '.json,.txt'
fileInput.style.display = 'none'
fileInput.onchange = importNotes
impBtn.onclick = () => fileInput.click()
btnGroup.insertBefore(impBtn, btnGroup.firstChild)
}
clearBtn.onclick = () => {
if (confirm('Delete every single note?')) {
myNotes = []
save()
refresh()
}
}
addBtn.onclick = addBlank
document.addEventListener('DOMContentLoaded', () => {
createSearch()
addBackupButtons()
if (myNotes.length === 0) {
addBlank()
} else {
refresh()
}
})