javascript - 如何避免一个注释覆盖另一个注释? (本地存储)

标签 javascript local-storage

我正在开发这个笔记应用程序 ( 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中并重新保存。

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))
}

关于javascript - 如何避免一个注释覆盖另一个注释? (本地存储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59881894/

相关文章:

javascript - 将 jQuery 代码转换为纯 JavaScript 不显示任何内容

ios - 带有 PhoneGap 的 iOS 6 中的持久存储(WebSQL 和本地存储)

php - 将php多维数组存储在本地存储中

html - 已知的 HTML5 localStorage、WebSQL 对平板电脑的限制 (webkit)

javascript - 如何从 localStorage 读取 DraftJS 状态?

javascript - 在同一页面中打开链接,链接打开后,执行一些代码

javascript - 滚动不会随着 overflow-y 滚动和边框半径而改变

javascript - 更改 firebase.database 内的变量

javascript - 在另一个调用 ajax 结束之前调用 ajax

javascript - 在服务器上刷新后刷新 token 本地存储