-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
179 lines (153 loc) · 6.47 KB
/
app.js
File metadata and controls
179 lines (153 loc) · 6.47 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
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js').then((registration) => {
console.log('Service Worker registered with scope:', registration.scope);
}).catch((error) => {
console.log('Service Worker registration failed:', error);
});
}
window.onload = function () {
var zIndex = 0;
var notes = JSON.parse(localStorage.getItem('notes')) || [];
var nextNoteX = 0;
function disableScroll() {
document.body.style.overflow = 'hidden';
}
function enableScroll() {
document.body.style.overflow = '';
}
function addNote(note) {
var notesContainer = document.getElementById('notes-container');
var noteElement = document.createElement('div');
noteElement.className = 'note';
noteElement.innerHTML = marked.parse(note.content);
noteElement.style.position = 'absolute';
noteElement.style.zIndex = zIndex++;
noteElement.style.transform = 'translate(' + note.x + 'px, ' + note.y + 'px)';
noteElement.setAttribute('data-x', note.x);
noteElement.setAttribute('data-y', note.y);
interact(noteElement).draggable({
listeners: {
start: function (event) {
event.preventDefault();
disableScroll();
event.target.setAttribute('data-x', note.x);
event.target.setAttribute('data-y', note.y);
event.target.style.zIndex = ++zIndex;
},
move: function (event) {
var target = event.target;
var x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx;
var y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
target.style.transform = 'translate(' + x + 'px, ' + y + 'px)';
target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
note.x = x;
note.y = y;
localStorage.setItem('notes', JSON.stringify(notes));
},
end: function (event) {
enableScroll();
},
}
}).resizable({
edges: { left: true, right: true, bottom: true, top: true },
listeners: {
start: function (event) {
event.preventDefault();
disableScroll();
event.target.style.zIndex = ++zIndex;
},
move: function(event) {
let { x, y } = event.target.dataset;
x = (parseFloat(x) || 0) + event.deltaRect.left;
y = (parseFloat(y) || 0) + event.deltaRect.top;
Object.assign(event.target.style, {
width: `${event.rect.width}px`,
height: `${event.rect.height}px`,
transform: `translate(${x}px, ${y}px)`
});
Object.assign(event.target.dataset, { x, y });
note.x = x;
note.y = y;
},
end: function(event) {
enableScroll();
localStorage.setItem('notes', JSON.stringify(notes));
}
},
margin: 10
});
var deleteButton = document.getElementById('delete-button-template').cloneNode(true);
deleteButton.style.display = 'block';
deleteButton.addEventListener('click', function () {
notesContainer.removeChild(noteElement);
notes = notes.filter(function (n) { return n !== note; });
localStorage.setItem('notes', JSON.stringify(notes));
});
noteElement.appendChild(deleteButton);
notesContainer.appendChild(noteElement);
}
document.getElementById('add-note').addEventListener('click', function () {
var noteInput = document.getElementById('note-input');
var noteContent = noteInput.value.trim();
if (noteContent === '') {
return;
}
// Check all possible positions for the new note
var noteX = 0;
var noteY = noteInput.getBoundingClientRect().bottom;
while (notes.some(function (existingNote) {
return Math.abs(existingNote.x - noteX) < 220 && Math.abs(existingNote.y - noteY) < 220;
})) {
noteX += 220;
if (noteX + 220 > window.innerWidth) {
noteX = 0;
noteY += 220; // Adjust this value based on the height of your notes
}
}
// If it's going beyond the window's height, reset to the top position
if (noteY + 220 > window.innerHeight) {
noteX = 0;
noteY = 0;
}
var note = {
content: noteContent,
x: noteX,
y: noteY
};
notes.push(note);
localStorage.setItem('notes', JSON.stringify(notes));
addNote(note);
noteInput.value = '';
});
document.getElementById('delete-all-notes').addEventListener('click', function () {
localStorage.clear();
location.reload();
});
document.getElementById('export-notes').addEventListener('click', function () {
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(localStorage.getItem('notes'));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", "notes.json");
document.body.appendChild(downloadAnchorNode);
downloadAnchorNode.click();
downloadAnchorNode.remove();
});
document.getElementById('import-notes').addEventListener('click', function () {
var uploadInputNode = document.createElement('input');
uploadInputNode.setAttribute("type", "file");
uploadInputNode.addEventListener('change', function (e) {
var reader = new FileReader();
reader.onload = function (event) {
notes = JSON.parse(event.target.result);
localStorage.setItem('notes', JSON.stringify(notes));
location.reload();
};
reader.readAsText(e.target.files[0]);
});
document.body.appendChild(uploadInputNode);
uploadInputNode.click();
uploadInputNode.remove();
});
notes.forEach(addNote);
}