-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
155 lines (135 loc) · 5.23 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
// Global variables
let notes = [];
let currentEditId = null;
// Helper function to generate unique IDs for notes
function generateId() {
return '_' + Math.random().toString(36).substr(2, 9);
}
// Load notes when the extension is opened
document.addEventListener('DOMContentLoaded', function () {
chrome.storage.local.get(['notes'], function (result) {
if (result.notes) {
notes = result.notes;
renderNotes(notes);
renderTags(notes);
}
});
// Add new note button click handler
document.getElementById('addNoteButton').addEventListener('click', function () {
clearForm();
currentEditId = null;
document.getElementById('noteForm').style.display = 'block';
});
// Save note button click handler
document.getElementById('saveNoteButton').addEventListener('click', function () {
const url = document.getElementById('urlInput').value;
const title = document.getElementById('titleInput').value;
const content = document.getElementById('noteInput').value;
const tags = document.getElementById('tagInput').value.split(',').map(tag => tag.trim());
if (!title || !content) {
alert('Title and content are required.');
return;
}
const newNote = {
id: currentEditId ? currentEditId : generateId(), // Assign a unique ID or keep the existing one
url: url || null,
title,
content,
tags: tags.length > 0 ? tags : ['general']
};
if (currentEditId) {
// Edit existing note
const noteIndex = notes.findIndex(note => note.id === currentEditId);
if (noteIndex > -1) {
notes[noteIndex] = newNote;
}
} else {
// Add new note
notes.push(newNote);
}
// Save notes to chrome.storage.local
chrome.storage.local.set({ notes }, function () {
renderNotes(notes);
renderTags(notes);
clearForm();
document.getElementById('noteForm').style.display = 'none';
});
});
// Event delegation for edit and delete buttons
document.getElementById('notesContainer').addEventListener('click', function (event) {
const target = event.target;
if (target.classList.contains('edit-note-btn')) {
const noteId = target.dataset.id;
const note = notes.find(note => note.id === noteId);
if (note) {
document.getElementById('urlInput').value = note.url || '';
document.getElementById('titleInput').value = note.title;
document.getElementById('noteInput').value = note.content;
document.getElementById('tagInput').value = note.tags.join(', ');
currentEditId = note.id;
document.getElementById('noteForm').style.display = 'block';
}
}
if (target.classList.contains('delete-note-btn')) {
const noteId = target.dataset.id;
notes = notes.filter(note => note.id !== noteId); // Delete the note by its ID
chrome.storage.local.set({ notes }, function () {
renderNotes(notes);
renderTags(notes);
});
}
});
});
// Render notes in the notes container
function renderNotes(notes, filtered = false) {
const notesContainer = document.getElementById('notesContainer');
notesContainer.innerHTML = '';
notes.forEach(note => {
const noteDiv = document.createElement('div');
noteDiv.className = 'note';
noteDiv.innerHTML = `
<h4>${note.title}</h4>
<p>${note.content}</p>
${note.url ? `<a href="${note.url}" target="_blank">${note.url}</a>` : ''}
<p>Tags: ${note.tags.join(', ')}</p>
<button class="edit-note-btn" data-id="${note.id}">Edit</button>
<button class="delete-note-btn" data-id="${note.id}">Delete</button>
`;
notesContainer.appendChild(noteDiv);
});
}
// Render tags in the available tags container
function renderTags(notes) {
const tagsContainer = document.getElementById('availableTags');
tagsContainer.innerHTML = '';
const allTags = [...new Set(notes.flatMap(note => note.tags))];
allTags.forEach(tag => {
const tagDiv = document.createElement('div');
tagDiv.className = 'tag';
tagDiv.textContent = tag;
tagDiv.addEventListener('click', function () {
filterNotesByTag(tag);
});
tagsContainer.appendChild(tagDiv);
});
}
// Filter notes by tag
function filterNotesByTag(tag) {
const filteredNotes = notes.filter(note => note.tags.includes(tag));
renderNotes(filteredNotes, true); // Render filtered notes
}
// Clear form fields
function clearForm() {
document.getElementById('urlInput').value = '';
document.getElementById('titleInput').value = '';
document.getElementById('noteInput').value = '';
document.getElementById('tagInput').value = '';
}
// Initial load
chrome.storage.local.get(['notes'], function (result) {
if (result.notes) {
notes = result.notes;
renderNotes(notes);
renderTags(notes);
}
});