-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
351 lines (300 loc) · 11.7 KB
/
script.js
File metadata and controls
351 lines (300 loc) · 11.7 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// 冰织的阅读笔记应用
class ReadingNotesApp {
constructor() {
this.notes = JSON.parse(localStorage.getItem('readingNotes')) || [];
this.currentNoteId = null;
this.init();
}
init() {
this.bindEvents();
this.renderNotes();
this.updateStats();
}
bindEvents() {
// 添加笔记按钮
document.getElementById('addNoteBtn').addEventListener('click', () => {
this.openModal();
});
// 模态框关闭
document.querySelector('.close').addEventListener('click', () => {
this.closeModal();
});
document.getElementById('cancelBtn').addEventListener('click', () => {
this.closeModal();
});
// 点击模态框外部关闭
window.addEventListener('click', (e) => {
const modal = document.getElementById('noteModal');
if (e.target === modal) {
this.closeModal();
}
});
// 表单提交
document.getElementById('noteForm').addEventListener('submit', (e) => {
e.preventDefault();
this.saveNote();
});
// 搜索功能
document.getElementById('searchInput').addEventListener('input', (e) => {
this.filterNotes();
});
document.getElementById('filterSelect').addEventListener('change', (e) => {
this.filterNotes();
});
// 评分功能
this.bindRatingEvents();
}
bindRatingEvents() {
const ratingSpans = document.querySelectorAll('.rating span');
ratingSpans.forEach(span => {
span.addEventListener('click', (e) => {
const rating = parseInt(e.target.dataset.rating);
this.setRating(rating);
});
});
}
setRating(rating) {
const ratingSpans = document.querySelectorAll('.rating span');
ratingSpans.forEach((span, index) => {
if (index < rating) {
span.classList.add('active');
} else {
span.classList.remove('active');
}
});
}
getCurrentRating() {
const activeSpans = document.querySelectorAll('.rating span.active');
return activeSpans.length;
}
openModal(noteId = null) {
const modal = document.getElementById('noteModal');
const title = document.getElementById('modalTitle');
const form = document.getElementById('noteForm');
this.currentNoteId = noteId;
if (noteId) {
title.textContent = '✏️ 编辑笔记';
this.fillForm(this.notes.find(note => note.id === noteId));
} else {
title.textContent = '✨ 添加新笔记';
form.reset();
this.setRating(0);
}
modal.style.display = 'block';
}
closeModal() {
document.getElementById('noteModal').style.display = 'none';
this.currentNoteId = null;
}
fillForm(note) {
if (!note) return;
document.getElementById('noteTitle').value = note.title || '';
document.getElementById('noteAuthor').value = note.author || '';
document.getElementById('noteCategory').value = note.category || '小说';
document.getElementById('noteContent').value = note.content || '';
document.getElementById('noteFavorite').checked = note.favorite || false;
this.setRating(note.rating || 0);
}
saveNote() {
const title = document.getElementById('noteTitle').value.trim();
const author = document.getElementById('noteAuthor').value.trim();
const category = document.getElementById('noteCategory').value;
const content = document.getElementById('noteContent').value.trim();
const favorite = document.getElementById('noteFavorite').checked;
const rating = this.getCurrentRating();
if (!title) {
alert('请输入书名或标题!');
return;
}
const noteData = {
title,
author,
category,
content,
favorite,
rating,
updatedAt: new Date().toISOString()
};
if (this.currentNoteId) {
// 编辑现有笔记
const noteIndex = this.notes.findIndex(note => note.id === this.currentNoteId);
this.notes[noteIndex] = { ...this.notes[noteIndex], ...noteData };
} else {
// 添加新笔记
const newNote = {
id: Date.now().toString(),
...noteData,
createdAt: new Date().toISOString()
};
this.notes.unshift(newNote);
}
this.saveToLocalStorage();
this.renderNotes();
this.updateStats();
this.closeModal();
// 显示成功消息
this.showMessage(this.currentNoteId ? '笔记更新成功!' : '笔记添加成功!');
}
deleteNote(noteId) {
if (confirm('确定要删除这篇笔记吗?')) {
this.notes = this.notes.filter(note => note.id !== noteId);
this.saveToLocalStorage();
this.renderNotes();
this.updateStats();
this.showMessage('笔记删除成功!');
}
}
toggleFavorite(noteId) {
const note = this.notes.find(note => note.id === noteId);
if (note) {
note.favorite = !note.favorite;
this.saveToLocalStorage();
this.renderNotes();
this.updateStats();
}
}
saveToLocalStorage() {
localStorage.setItem('readingNotes', JSON.stringify(this.notes));
}
renderNotes() {
const container = document.getElementById('notesContainer');
const filteredNotes = this.getFilteredNotes();
if (filteredNotes.length === 0) {
container.innerHTML = `
<div style="grid-column: 1/-1; text-align: center; padding: 60px 20px; color: #7f8c8d;">
<div style="font-size: 4em; margin-bottom: 20px;">📚</div>
<h3>还没有笔记哦~</h3>
<p>点击上方按钮添加你的第一篇读书笔记吧!</p>
</div>
`;
return;
}
container.innerHTML = filteredNotes.map(note => this.createNoteCard(note)).join('');
// 绑定卡片事件
this.bindCardEvents();
}
createNoteCard(note) {
const date = new Date(note.createdAt).toLocaleDateString('zh-CN');
const ratingStars = '⭐'.repeat(note.rating || 0);
return `
<div class="note-card ${note.favorite ? 'favorite' : ''}" data-id="${note.id}">
<div class="note-date">${date}</div>
<div class="note-title">${this.escapeHtml(note.title)}</div>
${note.author ? `<div class="note-author">作者:${this.escapeHtml(note.author)}</div>` : ''}
<div class="note-category">${note.category}</div>
${note.rating ? `<div class="note-rating">${ratingStars}</div>` : ''}
${note.content ? `<div class="note-content">${this.escapeHtml(note.content.substring(0, 100))}${note.content.length > 100 ? '...' : ''}</div>` : ''}
<div class="note-actions">
<button class="btn btn-primary" onclick="app.editNote('${note.id}')">✏️ 编辑</button>
<button class="btn btn-secondary" onclick="app.toggleFavorite('${note.id}')">${note.favorite ? '💔 取消收藏' : '❤️ 收藏'}</button>
<button class="btn btn-secondary" onclick="app.deleteNote('${note.id}')">🗑️ 删除</button>
</div>
</div>
`;
}
bindCardEvents() {
// 事件已通过onclick绑定
}
editNote(noteId) {
this.openModal(noteId);
}
getFilteredNotes() {
const searchTerm = document.getElementById('searchInput').value.toLowerCase();
const filterValue = document.getElementById('filterSelect').value;
let filtered = this.notes;
// 搜索过滤
if (searchTerm) {
filtered = filtered.filter(note =>
note.title.toLowerCase().includes(searchTerm) ||
note.author.toLowerCase().includes(searchTerm) ||
note.content.toLowerCase().includes(searchTerm)
);
}
// 分类过滤
switch (filterValue) {
case 'recent':
filtered.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
break;
case 'favorite':
filtered = filtered.filter(note => note.favorite);
break;
default:
filtered.sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt));
}
return filtered;
}
filterNotes() {
this.renderNotes();
}
updateStats() {
const totalNotes = this.notes.length;
const favoriteNotes = this.notes.filter(note => note.favorite).length;
const recentNotes = this.notes.filter(note => {
const noteDate = new Date(note.createdAt);
const weekAgo = new Date();
weekAgo.setDate(weekAgo.getDate() - 7);
return noteDate > weekAgo;
}).length;
// 可以在这里更新统计显示
console.log(`总计: ${totalNotes} 篇笔记, 收藏: ${favoriteNotes} 篇, 本周新增: ${recentNotes} 篇`);
}
showMessage(message) {
// 简单的消息提示
const messageDiv = document.createElement('div');
messageDiv.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
background: #4ecdc4;
color: white;
padding: 15px 25px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
z-index: 2000;
animation: slideIn 0.3s ease;
`;
messageDiv.textContent = message;
document.body.appendChild(messageDiv);
setTimeout(() => {
messageDiv.remove();
}, 3000);
}
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
}
// 初始化应用
const app = new ReadingNotesApp();
// 添加一些示例数据
if (app.notes.length === 0) {
const sampleNotes = [
{
id: '1',
title: '小王子',
author: '安托万·德·圣-埃克苏佩里',
category: '小说',
content: '这是一个关于爱与责任的美丽寓言。小王子教会我们用心去看,才能真正看见。狐狸说的"驯养"让我深深感动,真正的联系需要时间和耐心。',
favorite: true,
rating: 5,
createdAt: new Date(Date.now() - 86400000).toISOString(),
updatedAt: new Date(Date.now() - 86400000).toISOString()
},
{
id: '2',
title: '代码大全',
author: '史蒂夫·迈克康奈尔',
category: '技术',
content: '软件构建的百科全书,从变量命名到架构设计,每一个细节都值得深思。特别是关于重构和测试的章节,对提高代码质量很有帮助。',
favorite: false,
rating: 4,
createdAt: new Date(Date.now() - 172800000).toISOString(),
updatedAt: new Date(Date.now() - 172800000).toISOString()
}
];
app.notes = sampleNotes;
app.saveToLocalStorage();
app.renderNotes();
app.updateStats();
}