Implement very, very crude search

This commit is contained in:
Adrian Gruntkowski 2025-02-14 00:12:44 +01:00
parent 58507f59ef
commit dc3c9bc744
3 changed files with 40 additions and 1 deletions

View file

@ -63,6 +63,9 @@
<div> <div>
<a href="#" id="reset-app">Reset</a> <a href="#" id="reset-app">Reset</a>
</div> </div>
<div>
<input type="text" id="search" placeholder="Search" autocomplete="off" />
</div>
</nav> </nav>
<note-form mode="add" id="new-note"> <note-form mode="add" id="new-note">

View file

@ -92,6 +92,36 @@ if (notificationsAvailable()) {
}); });
} }
// Search
let filter = "";
const search = document.querySelector("#search");
search.addEventListener("input", (e) => {
filter = e.target.value.trim().toLowerCase();
Notes.saveStorage();
});
function filterNotes(notes) {
if (filter !== "") {
return notes.filter(
(n) =>
n.title.toLowerCase().indexOf(filter) > -1 ||
textOf(n.content).toLowerCase().indexOf(filter) > -1,
);
}
return notes;
}
function textOf(content) {
if (typeof content === "object") {
return content.map((n) => n.content).join("");
} else {
return content;
}
}
// There are two note-form component instances - one for // There are two note-form component instances - one for
// composing new notes and another one for editing existing notes. // composing new notes and another one for editing existing notes.
@ -155,7 +185,8 @@ function notesEqual(note1, note2) {
} }
async function render() { async function render() {
const notes = await Notes.all(); const allNotes = await Notes.all();
const notes = filterNotes(allNotes);
const notesContainer = document.querySelector("#notes"); const notesContainer = document.querySelector("#notes");
let previousId = null; let previousId = null;

View file

@ -311,6 +311,11 @@ button {
gap: var(--gap-1); gap: var(--gap-1);
} }
#search {
width: 10em;
min-width: 0;
}
#notes { #notes {
display: flex; display: flex;
flex-direction: column; flex-direction: column;