-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
147 lines (121 loc) · 4.9 KB
/
popup.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
document.addEventListener('DOMContentLoaded', function() {
const saveButton = document.getElementById('saveButton');
const saveTxtButton = document.getElementById('saveTxtButton');
const clearButton = document.getElementById('clearButton');
const noteInput = document.getElementById('noteInput');
const cherryIcon = document.querySelector('.cherry-icon');
const fullscreenLink = document.getElementById('Fullscreen');
// cherry icon blink
function blinkCherryIcon() {
cherryIcon.style.filter = 'brightness(1.6)';
setTimeout(function() {
cherryIcon.style.filter = 'brightness(1)';
}, 500);
}
// spinning animation cherry
function spinCherryIconOnce() {
cherryIcon.classList.add('spin-once');
setTimeout(function() {
cherryIcon.classList.remove('spin-once');
}, 1000); // duration
}
// save content Save button
saveButton.addEventListener('click', function() {
localStorage.setItem('note', noteInput.value);
blinkCherryIcon();
spinCherryIconOnce(); // spinning effect
});
// download .txt file button
saveTxtButton.addEventListener('click', function() {
const blob = new Blob([noteInput.value], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'CherryNote.txt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// clear
clearButton.addEventListener('click', function() {
noteInput.value = '';
localStorage.setItem('note', ''); // save the cleared state to local storage
blinkCherryIcon();
spinCherryIconOnce(); // spinning effect
});
// load saved note from local storage when the popup is opened
const savedNote = localStorage.getItem('note');
if (savedNote) {
noteInput.value = savedNote;
}
// add click event to cherry icon only if on popup.html
if (window.location.pathname.includes("popup.html")) {
cherryIcon.addEventListener("click", function() {
noteInput.value += "🍒 Click! 🍒";
});
// add click event to Fullscreen.png to save content and redirect to fullscreen
fullscreenLink.addEventListener('click', function(event) {
event.preventDefault();
saveButton.click();
setTimeout(function() {
window.open("CherryPad.html", '_blank'); // open in a new tab after 500ms
}, 500);
});
}
});
document.addEventListener('DOMContentLoaded', function() {
// load from local storage
if (localStorage.getItem('noteInput')) {
document.getElementById('noteInput').value = localStorage.getItem('noteInput');
}
// save button click
document.getElementById('saveButton').addEventListener('click', function() {
// get current date and time
var now = new Date();
var formattedTime = now.toLocaleString();
// get values from noteInput and noteInput2
var noteInputValue = document.getElementById('noteInput').value;
// save notes (storage)
localStorage.setItem('noteInput', noteInputValue);
});
});
document.getElementById('noteInput').addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key === 'z') {
const textarea = document.getElementById('noteInput');
const pastedContent = JSON.parse(textarea.dataset.lastPastedContent || '{}');
if (pastedContent.text) {
// calculate the end position of the pasted content
const endPos = pastedContent.startPos + pastedContent.text.length;
// remove the pasted content
textarea.setRangeText('', pastedContent.startPos, endPos, 'end');
// prevent the default undo behavior
e.preventDefault();
}
}
});
// AutoSave
document.addEventListener('DOMContentLoaded', function() {
const noteInput = document.getElementById('noteInput');
if (localStorage.getItem('noteInput')) {
noteInput.value = localStorage.getItem('noteInput');
}
noteInput.addEventListener('input', function() {
localStorage.setItem('noteInput', noteInput.value);
});
});
//Shortcuts Popup
//Save Ctrl+S
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.key === 's') {
event.preventDefault();
document.getElementById('saveButton').click();
}
});
//Download Ctrl+D
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.key === 'd') {
event.preventDefault();
document.getElementById('saveTxtButton').click();
}
});