如何避免一个音符覆盖另一个音符?(本地存储)



我正在研究这个笔记应用程序(https://github.com/matteobarone/notepadjs/tree/local-storage(。我基本上是在尝试调整addItem函数,以便将所有笔记保存在localStorage中,而不是只保存最后写的笔记。如何确保将 storeItems 变量传递给 setItem 方法?

下面的代码(请只检查JS(:

function set(key, value) {
window.localStorage.setItem(key, value);
}
function get(key) {
return window.localStorage.getItem(key);
}
function remove(key) {
window.localStorage.removeItem(key);
}
export const localStorage = {
set,
get,
remove,
}
const storeItems = [];
document.addEventListener('DOMContentLoaded', () => {
const listItemElement = document.querySelector('.notepad__list');
const inputElement = document.querySelector('.notepad__input');
inputElement.addEventListener('keyup', (event) => {
if (event.keyCode === 13) {
addItem(event.target.value, listItemElement);
event.target.value = null;
}
})
});
function addItem(text, list) {
const el = document.createElement('li');
const textNode = document.createTextNode(text);
el.appendChild(textNode);
el.classList.add('notepad__item');
list.appendChild(el);
localStorage.set('notes', JSON.stringify([text]));
console.log('ho aggiunto', text);
}
:root {
--color: #5f5f5f;
}
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
height: 100vh;
background: linear-gradient(to left, #B24592 , #F15F79);
display: flex;
justify-content: center;
align-items: center;
font-family: Helvetica Neue, sans-serif;
}
@keyframes fadein {
from {opacity: 0}
top {opacity: 1}
}
.notepad {
position: relative;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: flex-start;
width: 400px;
height: 80vh;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 20px 80px rgba(0, 0, 0, 0.6);
animation: fadein 1s;
}
.notepad__heading {
width: 100%;
padding: 60px 40px 15px;
border-bottom: 1px solid #e0e0e0;
color: var(--color);
font-weight: 400;
}
.notepad__input {
border: 0;
width: 100%;
line-height: 1.3;
margin: 0;
font-size: 19px;
color: var(--color);
padding: 30px 40px;
background-color: transparent;
transition: background-color 300ms ease;
}
.notepad__input:focus {
outline: none;
}
.notepad__list {
overflow: scroll;
width: 100%;
}
.notepad__item:first-of-type {
border-top: 1px solid #e0e0e0;
}
.notepad__item {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
padding: 15px 40px;
border: 0;
border-bottom: 1px solid #e0e0e0;
line-height: 1.3;
font-size: 19px;
list-style-type: none;
color: var(--color);
background-color: rgba(0,0,0,.03);
}
.notepad__item::after {
content: 'x';
border: 1px solid var(--color);
padding: 0 8px 3px;
border-radius: 50%;
}
.notepad__clear {
position: absolute;
bottom: 0;
display: none;
width: 100%;
padding: 20px;
border: 0;
font-size: 12px;
text-transform: uppercase;
background-color: #fff;
color: #333;
cursor: pointer;
font-family: Helvetica Neue, sans-serif;
letter-spacing: 2px;
transition: color 300ms ease, background-color 300ms ease;
border-radius: 0 0 5px 5px;
}
.notepad__clear--display {
display: block;
}
.notepad__clear:hover {
color: #fff;
background-color: #ea3860;
}
<!doctype html>
<html lang="en">
<head>
<title>Notepadjs</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="./dist/bundle.js"></script>
</head>
<body>
<div class="notepad">
<h1 class="notepad__heading">Notes</h1>
<input class="notepad__input" placeholder="Start typing…" id="notepad-input">
<ul class="notepad__list">
</ul>
<button class="notepad__clear notepad__clear--display">Clear list</button>
</div>
</body>
</html>

当您想要插入新备忘录时,可以从本地存储中检索所有notes,然后将新备忘录推送到notes并重新存储。

function addItem(text, list) {
const el = document.createElement('li');
const textNode = document.createTextNode(text);
el.appendChild(textNode);
el.classList.add('notepad__item');
list.appendChild(el);
// retrieve the notes that are already in localstorage
const notes = JSON.parse(localStorage.get('notes'))
// pushing your new note into the notes array - mutation
notes.push(text)
// setting the notes array JSON into localStorage
localStorage.set('notes', JSON.stringify(nextNotes))
}

最新更新