mirror of
https://github.com/zoldar/jenot.git
synced 2026-01-05 07:02:55 +00:00
Add ability to edit and remove notes
This commit is contained in:
parent
6c6a6d515a
commit
6e4b32debf
5 changed files with 79 additions and 16 deletions
17
index.html
17
index.html
|
|
@ -30,15 +30,28 @@
|
||||||
<div id="content">
|
<div id="content">
|
||||||
<h1>Jenot</h1>
|
<h1>Jenot</h1>
|
||||||
|
|
||||||
<note-form id="new-note">
|
<note-form mode="add" id="new-note">
|
||||||
<div class="note">
|
<div class="note">
|
||||||
<p class="content">
|
<p class="content">
|
||||||
</p>
|
</p>
|
||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
|
<button class="add">➕</button>
|
||||||
|
<button class="tasklist-mode">☑️</button>
|
||||||
|
<button class="note-mode">📑</button>
|
||||||
|
<button class="remove">🗑️</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</note-form>
|
||||||
|
|
||||||
|
<note-form mode="edit" id="edit-note" class="hidden">
|
||||||
|
<div class="note">
|
||||||
|
<p class="content">
|
||||||
|
</p>
|
||||||
|
<div class="toolbar">
|
||||||
|
<button class="save">💾</button>
|
||||||
<button class="tasklist-mode">☑️</button>
|
<button class="tasklist-mode">☑️</button>
|
||||||
<button class="note-mode">📑</button>
|
<button class="note-mode">📑</button>
|
||||||
<button class="remove">🗑️</button>
|
<button class="remove">🗑️</button>
|
||||||
<button class="add">➕</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</note-form>
|
</note-form>
|
||||||
|
|
|
||||||
|
|
@ -447,11 +447,13 @@ class NoteForm extends HTMLElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
|
this.mode = this.attributes.mode.value;
|
||||||
this.content = this.querySelector(".content");
|
this.content = this.querySelector(".content");
|
||||||
this.tasklistModeButton = this.querySelector(".tasklist-mode");
|
this.tasklistModeButton = this.querySelector(".tasklist-mode");
|
||||||
this.noteModeButton = this.querySelector(".note-mode");
|
this.noteModeButton = this.querySelector(".note-mode");
|
||||||
this.removeButton = this.querySelector(".remove");
|
this.removeButton = this.querySelector(".remove");
|
||||||
this.addButton = this.querySelector(".add");
|
this.addButton = this.querySelector(".add");
|
||||||
|
this.saveButton = this.querySelector(".save");
|
||||||
|
|
||||||
this.tasklistModeButton.addEventListener("click", (e) => {
|
this.tasklistModeButton.addEventListener("click", (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
@ -469,19 +471,40 @@ class NoteForm extends HTMLElement {
|
||||||
|
|
||||||
this.removeButton.addEventListener("click", (e) => {
|
this.removeButton.addEventListener("click", (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
if (this.mode === "edit") {
|
||||||
|
this.dispatchEvent(
|
||||||
|
new CustomEvent("deleteNote", {
|
||||||
|
bubbles: true,
|
||||||
|
detail: this.note,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
this.#reset();
|
this.#reset();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.addButton.addEventListener("click", (e) => {
|
if (this.mode === "add") {
|
||||||
e.preventDefault();
|
this.addButton.addEventListener("click", (e) => {
|
||||||
this.dispatchEvent(
|
e.preventDefault();
|
||||||
new CustomEvent("addNote", {
|
this.dispatchEvent(
|
||||||
bubbles: true,
|
new CustomEvent("addNote", {
|
||||||
detail: this.note,
|
bubbles: true,
|
||||||
}),
|
detail: this.note,
|
||||||
);
|
}),
|
||||||
this.#reset();
|
);
|
||||||
});
|
this.#reset();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.saveButton.addEventListener("click", (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
this.dispatchEvent(
|
||||||
|
new CustomEvent("updateNote", {
|
||||||
|
bubbles: true,
|
||||||
|
detail: this.note,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
this.#reset();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
this.addEventListener("contentChange", () => {
|
this.addEventListener("contentChange", () => {
|
||||||
this.note.content = this.content.firstChild.value;
|
this.note.content = this.content.firstChild.value;
|
||||||
|
|
@ -491,6 +514,12 @@ class NoteForm extends HTMLElement {
|
||||||
this.#setContent();
|
this.#setContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
load(note) {
|
||||||
|
this.note = note;
|
||||||
|
this.#updateUI();
|
||||||
|
this.#setContent();
|
||||||
|
}
|
||||||
|
|
||||||
#reset() {
|
#reset() {
|
||||||
this.note = {
|
this.note = {
|
||||||
type: "note",
|
type: "note",
|
||||||
|
|
|
||||||
23
js/jenot.js
23
js/jenot.js
|
|
@ -5,19 +5,34 @@ import "./components.js";
|
||||||
const Notes = new NoteStore("jenot-app");
|
const Notes = new NoteStore("jenot-app");
|
||||||
|
|
||||||
const newNote = document.querySelector("#new-note");
|
const newNote = document.querySelector("#new-note");
|
||||||
|
const editNote = document.querySelector("#edit-note");
|
||||||
|
|
||||||
Notes.addEventListener("save", render.bind(this));
|
Notes.addEventListener("save", render.bind(this));
|
||||||
|
|
||||||
render();
|
render();
|
||||||
|
|
||||||
newNote.addEventListener("addNote", (e) => {
|
newNote.addEventListener("addNote", (e) => {
|
||||||
|
console.log(e.detail);
|
||||||
Notes.add(e.detail);
|
Notes.add(e.detail);
|
||||||
Notes.saveStorage();
|
Notes.saveStorage();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
editNote.addEventListener("updateNote", (e) => {
|
||||||
|
newNote.classList.remove("hidden");
|
||||||
|
editNote.classList.add("hidden");
|
||||||
|
Notes.update(e.detail);
|
||||||
|
Notes.saveStorage();
|
||||||
|
});
|
||||||
|
|
||||||
|
editNote.addEventListener("deleteNote", (e) => {
|
||||||
|
newNote.classList.remove("hidden");
|
||||||
|
editNote.classList.add("hidden");
|
||||||
|
Notes.remove(e.detail);
|
||||||
|
Notes.saveStorage();
|
||||||
|
});
|
||||||
|
|
||||||
function render() {
|
function render() {
|
||||||
const notes = Notes.all();
|
const notes = Notes.all();
|
||||||
console.log(notes)
|
|
||||||
const notesContainer = document.querySelector("#notes");
|
const notesContainer = document.querySelector("#notes");
|
||||||
notesContainer.replaceChildren();
|
notesContainer.replaceChildren();
|
||||||
|
|
||||||
|
|
@ -47,5 +62,11 @@ function render() {
|
||||||
}
|
}
|
||||||
|
|
||||||
notesContainer.appendChild(container);
|
notesContainer.appendChild(container);
|
||||||
|
|
||||||
|
container.addEventListener("click", (e) => {
|
||||||
|
newNote.classList.add("hidden");
|
||||||
|
editNote.classList.remove("hidden");
|
||||||
|
editNote.load(Notes.get(container.id));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ export class NoteStore extends EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
all = () => this.notes;
|
all = () => this.notes;
|
||||||
get = () => this.notes.find((note) => note.id === id);
|
get = (id) => this.notes.find((note) => note.id === id);
|
||||||
getEditedNoteId = () => this.editedNoteId;
|
getEditedNoteId = () => this.editedNoteId;
|
||||||
|
|
||||||
add(note) {
|
add(note) {
|
||||||
|
|
|
||||||
|
|
@ -167,13 +167,13 @@ note-form .note {
|
||||||
}
|
}
|
||||||
|
|
||||||
div#content {
|
div#content {
|
||||||
width: 270px;
|
/* width: 270px; */
|
||||||
max-width: 1200px;
|
max-width: 1200px;
|
||||||
margin: 10px auto 0 auto;
|
margin: 10px auto 0 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
.note {
|
.note {
|
||||||
max-width: 240px;
|
/* min-width: 240px; */
|
||||||
background: beige;
|
background: beige;
|
||||||
box-shadow: 2px 2px 4px 0px rgb(0 0 0 / 0.8);
|
box-shadow: 2px 2px 4px 0px rgb(0 0 0 / 0.8);
|
||||||
padding: 4px 8px;
|
padding: 4px 8px;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue