-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.js
156 lines (128 loc) · 5.28 KB
/
notes.js
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
document.addEventListener('DOMContentLoaded', () => {
const notesContainer = document.getElementById('notes-container');
const addNoteButton = document.getElementById('add-note-button');
const noteModal = document.getElementById('note-modal');
const closeNoteModal = document.getElementById('close-note-modal');
const notesListModal = document.getElementById('notes-modal');
const closeNotesListModal = document.getElementById('close-notes-modal');
const noteForm = document.getElementById('note-form');
const noteContentInput = document.getElementById('note-content');
const noteColorInput = document.getElementById('note-color');
let notes = loadNotes();
renderNotes();
addNoteButton.addEventListener('click', () => {
noteModal.style.display = 'block';
document.body.classList.add('modal-open');
});
closeNoteModal.addEventListener('click', () => {
noteModal.style.display = 'none';
document.body.classList.remove('modal-open');
});
closeNotesListModal.addEventListener('click', () => {
notesListModal.style.display = 'none';
document.body.classList.remove('modal-open');
});
noteForm.addEventListener('submit', (event) => {
event.preventDefault();
const newNote = {
id: Date.now(),
content: noteContentInput.value,
color: noteColorInput.value,
position: { x: 50, y: 50 },
size: { width: 200, height: 200 }
};
notes.push(newNote);
saveNotes();
renderNotes();
noteContentInput.value = '';
noteColorInput.value = '#ffffcc';
noteModal.style.display = 'none';
document.body.classList.remove('modal-open');
});
function renderNotes() {
notesContainer.innerHTML = '';
notes.forEach(note => {
const noteElement = document.createElement('div');
noteElement.classList.add('note');
noteElement.id = `note-${note.id}`;
noteElement.style.backgroundColor = note.color;
noteElement.style.left = `${note.position.x}px`;
noteElement.style.top = `${note.position.y}px`;
noteElement.style.width = `${note.size.width}px`;
noteElement.style.height = `${note.size.height}px`;
noteElement.innerHTML = `
<div class="note-header">
<span class="delete-icon" data-note-id="${note.id}">❌</span>
</div>
<div class="note-content">${note.content}</div>
`;
notesContainer.appendChild(noteElement);
const deleteIcon = noteElement.querySelector('.delete-icon');
deleteIcon.addEventListener('click', () => {
notes = notes.filter(n => n.id !== note.id);
saveNotes();
renderNotes();
});
makeNoteDraggable(noteElement, note);
makeNoteResizable(noteElement, note);
});
}
function makeNoteDraggable(noteElement, note) {
let isDragging = false;
let startPosX, startPosY;
noteElement.addEventListener('mousedown', (e) => {
if (e.target.classList.contains('note-header') || e.target.tagName === 'SPAN') {
isDragging = true;
startPosX = e.clientX - noteElement.offsetLeft;
startPosY = e.clientY - noteElement.offsetTop;
noteElement.style.zIndex = 1000;
}
});
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const x = e.clientX - startPosX;
const y = e.clientY - startPosY;
noteElement.style.left = `${x}px`;
noteElement.style.top = `${y}px`;
note.position = { x, y };
});
document.addEventListener('mouseup', () => {
isDragging = false;
noteElement.style.zIndex = 'auto';
saveNotes();
});
}
function makeNoteResizable(noteElement, note) {
let isResizing = false;
let startWidth, startHeight, startPosX, startPosY;
const resizer = document.createElement('div');
resizer.classList.add('resizer');
noteElement.appendChild(resizer);
resizer.addEventListener('mousedown', (e) => {
isResizing = true;
startWidth = noteElement.offsetWidth;
startHeight = noteElement.offsetHeight;
startPosX = e.clientX;
startPosY = e.clientY;
e.stopPropagation();
});
document.addEventListener('mousemove', (e) => {
if (!isResizing) return;
const width = startWidth + (e.clientX - startPosX);
const height = startHeight + (e.clientY - startPosY);
noteElement.style.width = `${width}px`;
noteElement.style.height = `${height}px`;
note.size = { width, height };
});
document.addEventListener('mouseup', () => {
isResizing = false;
saveNotes();
});
}
function loadNotes() {
return JSON.parse(localStorage.getItem('notes') || '[]');
}
function saveNotes() {
localStorage.setItem('notes', JSON.stringify(notes));
}
});