-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
211 lines (181 loc) · 5.93 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
document.addEventListener('DOMContentLoaded', function () {
const noteTitle = document.getElementById('note-title');
const noteContent = document.getElementById('note-content');
const noteTags = document.getElementById('note-tags');
const saveButton = document.getElementById('save-button');
const clearButton = document.getElementById('clear-button');
let isEditing = false; // Flag to track whether a note is being edited
let editedNoteIndex = -1; // Index of the note being edited
// Load the note data
chrome.storage.local.get(['notes'], function (result) {
const notes = result.notes || [];
renderNotes(notes);
});
// Render the notes
function renderNotes(notes) {
const noteList = document.getElementById('note-list');
noteList.innerHTML = '';
if (notes.length === 0) {
const noNotesMessage = document.createElement('p');
noNotesMessage.textContent = 'No notes available';
noteList.appendChild(noNotesMessage);
} else {
for (let i = 0; i < notes.length; i++) {
const note = notes[i];
const listItem = document.createElement('li');
listItem.classList.add('note-item');
const noteTitle = document.createElement('span');
noteTitle.classList.add('font-bold', 'mr-2');
noteTitle.textContent = note.title;
listItem.appendChild(noteTitle);
const noteContentWrapper = document.createElement('div');
noteContentWrapper.classList.add('note-content-wrapper');
listItem.appendChild(noteContentWrapper);
const noteContent = document.createElement('span');
noteContent.classList.add('text-gray-600');
noteContent.textContent = note.content;
noteContent.style.display = 'none';
noteContentWrapper.appendChild(noteContent);
if (note.tags.length > 0) {
const noteTags = document.createElement('div');
noteTags.classList.add('flex', 'flex-wrap', 'mb-2');
listItem.appendChild(noteTags);
for (let j = 0; j < note.tags.length; j++) {
const tag = note.tags[j];
const tagSpan = document.createElement('span');
tagSpan.classList.add(
'bg-blue-200',
'text-blue-800',
'px-2',
'py-1',
'rounded',
'mr-1',
'mb-1'
);
tagSpan.textContent = tag;
noteTags.appendChild(tagSpan);
}
} else {
const noteTags = document.createElement('div');
noteTags.classList.add('mb-2');
listItem.appendChild(noteTags);
}
// Handle the click event to toggle note content display
listItem.addEventListener('click', function () {
toggleNoteContent(listItem);
});
const editButton = document.createElement('button');
editButton.classList.add(
'ml-2',
'py-1',
'px-2',
'bg-yellow-500',
'text-white',
'rounded'
);
editButton.textContent = 'Edit';
editButton.addEventListener('click', function () {
editNoteForm(note, i);
});
listItem.appendChild(editButton);
const deleteButton = document.createElement('button');
deleteButton.classList.add(
'ml-2',
'py-1',
'px-2',
'bg-red-500',
'text-white',
'rounded'
);
deleteButton.textContent = 'Delete';
deleteButton.addEventListener('click', function () {
deleteNoteForm(i);
});
listItem.appendChild(deleteButton);
noteList.appendChild(listItem);
}
}
}
function toggleNoteContent(listItem) {
const noteContent = listItem.querySelector('span:not(.font-bold)');
if (noteContent) {
if (!isTextSelected()) {
if (noteContent.style.display === 'none') {
noteContent.style.display = 'inline';
} else {
noteContent.style.display = 'none';
}
}
}
}
function isTextSelected() {
const selectedText = window.getSelection().toString();
return selectedText.length > 0;
}
saveButton.addEventListener('click', function () {
const title = noteTitle.value.trim();
const content = noteContent.value.trim();
const tags = noteTags.value.trim().split(',');
if (!title && !content && !tags.length) {
return;
}
chrome.storage.local.get(['notes'], function (result) {
const notes = result.notes || [];
if (isEditing && editedNoteIndex >= 0) {
// If in editing mode, update the existing note
if (editedNoteIndex < notes.length) {
const editedNote = {
title: title,
content: content,
tags: tags,
};
notes[editedNoteIndex] = editedNote;
}
isEditing = false;
editedNoteIndex = -1;
} else {
// Otherwise, create a new note
const newNote = {
title: title,
content: content,
tags: tags,
};
notes.push(newNote);
}
// Save the updated notes
chrome.storage.local.set({ notes: notes }, function () {
// Clear the note form
noteTitle.value = '';
noteContent.value = '';
noteTags.value = '';
// Render the updated notes
renderNotes(notes);
});
});
});
clearButton.addEventListener('click', function () {
noteTitle.value = '';
noteContent.value = '';
noteTags.value = '';
});
function editNoteForm(note, index) {
noteTitle.value = note.title;
noteContent.value = note.content;
noteTags.value = note.tags.join(',');
isEditing = true;
editedNoteIndex = index;
}
function deleteNoteForm(index) {
chrome.storage.local.get(['notes'], function (result) {
const notes = result.notes || [];
if (index >= 0 && index < notes.length) {
notes.splice(index, 1);
}
// Save the updated notes
chrome.storage.local.set({ notes: notes }, function () {
// Render the updated notes
renderNotes(notes);
});
});
}
});