-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
481 lines (433 loc) · 20.4 KB
/
Copy pathindex.html
File metadata and controls
481 lines (433 loc) · 20.4 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kome Notepad</title>
<link rel="icon" type="image/svg+xml" href="./KomE.svg">
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Lucide Icons -->
<script src="https://unpkg.com/lucide@latest"></script>
<!-- Firebase SDKs -->
<script src="https://www.gstatic.com/firebasejs/10.8.0/firebase-app-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/10.8.0/firebase-auth-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/10.8.0/firebase-firestore-compat.js"></script>
<script>
tailwind.config = {
darkMode: 'class',
theme: {
extend: {
colors: {
brand: {
50: '#f0f7ff',
500: '#0284c7',
600: '#0369a1',
700: '#075985',
}
}
}
}
}
</script>
</head>
<body class="bg-gray-50 text-gray-800 dark:bg-gray-900 dark:text-gray-100 font-sans antialiased h-screen overflow-hidden">
<!-- ================= AUTH MODAL ================= -->
<div id="auth-modal" class="fixed inset-0 bg-black/60 backdrop-blur-sm z-50 flex items-center justify-center p-4">
<div class="bg-white dark:bg-gray-800 rounded-2xl shadow-2xl max-w-md w-full p-8 border border-gray-100 dark:border-gray-700">
<div class="text-center mb-6">
<div class="inline-flex items-center justify-center w-14 h-14 rounded-2xl bg-brand-500/10 text-brand-600 dark:text-brand-500 mb-3">
<i data-lucide="notebook-pen" class="w-8 h-8"></i>
</div>
<h1 class="text-2xl font-bold tracking-tight">Kome Notepad</h1>
<p class="text-sm text-gray-500 dark:text-gray-400 mt-1">Lightweight, fast & seamless note taking</p>
</div>
<!-- Auth Form with Email & Password -->
<div class="space-y-3">
<div>
<input id="email-input" type="email" placeholder="Email address" class="w-full px-4 py-2.5 rounded-xl border border-gray-300 dark:border-gray-600 bg-transparent text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 dark:bg-gray-700/50">
</div>
<div>
<input id="password-input" type="password" placeholder="Password" class="w-full px-4 py-2.5 rounded-xl border border-gray-300 dark:border-gray-600 bg-transparent text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 dark:bg-gray-700/50">
</div>
<button id="btn-email-auth" class="w-full bg-brand-600 hover:bg-brand-700 text-white py-2.5 px-4 rounded-xl font-medium transition text-sm">
Sign In / Sign Up
</button>
<div class="relative my-3">
<div class="absolute inset-0 flex items-center"><div class="w-full border-t border-gray-200 dark:border-gray-700"></div></div>
<div class="relative flex justify-center text-xs uppercase"><span class="bg-white dark:bg-gray-800 px-2 text-gray-400">or</span></div>
</div>
<button id="btn-google" class="w-full flex items-center justify-center gap-3 bg-white dark:bg-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 border border-gray-300 dark:border-gray-600 py-2.5 px-4 rounded-xl font-medium text-sm transition">
<svg class="w-4 h-4" viewBox="0 0 24 24"><path fill="#4285F4" d="M23.745 12.27c0-.7-.06-1.4-.19-2.07H12v4.51h6.6c-.29 1.52-1.14 2.82-2.4 3.68v3.05h3.88c2.27-2.09 3.665-5.17 3.665-9.17z"/><path fill="#34A853" d="M12 24c3.24 0 5.95-1.08 7.93-2.91l-3.88-3.05c-1.08.72-2.45 1.16-4.05 1.16-3.12 0-5.77-2.1-6.72-4.93H1.28v3.15C3.27 21.31 7.37 24 12 24z"/><path fill="#FBBC05" d="M5.28 14.27c-.25-.72-.38-1.49-.38-2.27s.13-1.55.38-2.27V6.58H1.28C.46 8.21 0 10.05 0 12s.46 3.79 1.28 5.42l4-3.15z"/><path fill="#EA4335" d="M12 4.75c1.77 0 3.35.61 4.6 1.8l3.42-3.42C17.95 1.19 15.24 0 12 0 7.37 0 3.27 2.69 1.28 6.58l4 3.15c.95-2.83 3.6-4.98 6.72-4.98z"/></svg>
Sign in with Google
</button>
<button id="btn-guest" class="w-full flex items-center justify-center gap-2 bg-gray-100 hover:bg-gray-200 dark:bg-gray-700 dark:hover:bg-gray-600 text-gray-800 dark:text-gray-200 py-2.5 px-4 rounded-xl font-medium text-sm transition mt-2">
<i data-lucide="user-check" class="w-4 h-4"></i>
Continue as Guest (Local)
</button>
</div>
</div>
</div>
<!-- ================= MAIN APP LAYOUT ================= -->
<div class="flex h-screen w-full overflow-hidden">
<!-- Sidebar / Notes List -->
<aside id="sidebar" class="w-80 bg-white dark:bg-gray-800 border-r border-gray-200 dark:border-gray-700 flex flex-col transition-all duration-300 z-10">
<!-- Top Header -->
<div class="p-4 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between">
<div class="flex items-center gap-2">
<i data-lucide="notebook-pen" class="w-6 h-6 text-brand-600 dark:text-brand-500"></i>
<span class="font-bold text-lg tracking-tight">Kome Notepad</span>
</div>
<button id="theme-toggle" class="p-2 rounded-lg text-gray-500 hover:bg-gray-100 dark:hover:bg-gray-700 transition" title="Toggle Theme">
<i data-lucide="sun" class="w-5 h-5 hidden dark:block"></i>
<i data-lucide="moon" class="w-5 h-5 block dark:hidden"></i>
</button>
</div>
<!-- Action Button -->
<div class="p-3">
<button id="btn-new-note" class="w-full flex items-center justify-center gap-2 bg-brand-600 hover:bg-brand-700 text-white font-medium py-2 px-4 rounded-xl shadow-sm transition">
<i data-lucide="plus" class="w-4 h-4"></i>
New Note
</button>
</div>
<!-- Notes List -->
<div id="notes-list" class="flex-1 overflow-y-auto p-2 space-y-1">
<!-- Notes rendered dynamically -->
</div>
<!-- Account / Mode Switch Footer (Bottom Sidebar) -->
<div class="p-3 border-t border-gray-200 dark:border-gray-700 bg-gray-50/50 dark:bg-gray-800/50">
<div class="flex items-center justify-between">
<div class="flex items-center gap-2 min-w-0">
<i data-lucide="user" class="w-4 h-4 text-gray-500 shrink-0"></i>
<div class="truncate text-xs">
<span id="user-status-text" class="font-medium text-gray-700 dark:text-gray-300 block truncate">Guest Mode</span>
<span id="sync-status-indicator" class="text-amber-600 dark:text-amber-400 block text-[10px]">Local Storage</span>
</div>
</div>
<button id="btn-account-switch" class="text-xs bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 px-2.5 py-1 rounded-lg font-medium transition shrink-0">
Switch
</button>
</div>
</div>
</aside>
<!-- Main Workspace / Editor -->
<main class="flex-1 flex flex-col bg-white dark:bg-gray-900 h-full overflow-hidden">
<!-- Top Action Bar -->
<header class="h-14 px-4 border-b border-gray-200 dark:border-gray-800 flex items-center justify-between gap-4">
<input id="note-title" type="text" placeholder="Untitled Note..." class="bg-transparent text-xl font-bold focus:outline-none flex-1 text-gray-900 dark:text-gray-100 placeholder-gray-400">
<div class="flex items-center gap-2">
<button id="btn-download" class="flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-sm font-medium border border-gray-300 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-800 text-gray-700 dark:text-gray-300 transition" title="Export as .txt">
<i data-lucide="download" class="w-4 h-4"></i>
<span>Export .txt</span>
</button>
<button id="btn-delete" class="p-2 rounded-lg text-red-500 hover:bg-red-50 dark:hover:bg-red-950/30 transition" title="Delete Note">
<i data-lucide="trash-2" class="w-5 h-5"></i>
</button>
</div>
</header>
<!-- Editor Area -->
<div class="flex-1 p-4 overflow-hidden flex flex-col">
<textarea id="note-content" placeholder="Start typing your note here..." class="w-full flex-1 bg-transparent resize-none focus:outline-none font-mono text-base text-gray-800 dark:text-gray-200 placeholder-gray-400 leading-relaxed"></textarea>
</div>
<!-- Status Bar -->
<footer class="h-8 px-4 border-t border-gray-200 dark:border-gray-800 flex items-center justify-between text-xs text-gray-400">
<span id="save-status">Ready</span>
<div class="flex gap-4">
<span id="word-count">0 words</span>
<span id="char-count">0 characters</span>
</div>
</footer>
</main>
</div>
<!-- JavaScript Logic -->
<script>
// Firebase Config
const firebaseConfig = {
apiKey: "AIzaSyBjIbVeLXbYenobP2kW_eTXGgB-oqLsvH4",
authDomain: "kome-note.firebaseapp.com",
projectId: "kome-note",
storageBucket: "kome-note.firebasestorage.app",
messagingSenderId: "742086142369",
appId: "1:742086142369:web:61e15a3a59ab38267d03e0",
measurementId: "G-D4ECNQHD58"
};
let db = null;
let auth = null;
// Kiểm tra và khởi tạo Firebase
if (firebaseConfig.apiKey && firebaseConfig.apiKey !== "AIzaSyBjIbVeLXbYenobP2kW_eTXGgB-oqLsvH4") {
try {
firebase.initializeApp(firebaseConfig);
db = firebase.firestore();
auth = firebase.auth();
console.log("Firebase initialized successfully");
} catch (e) {
console.error("Firebase Initialization Error:", e);
}
} else {
console.warn("Firebase Config Key is missing or default value used.");
}
// App State
let currentMode = 'guest';
let currentUser = null;
let notes = [];
let currentNoteId = null;
let saveTimeout = null;
// DOM Elements
const authModal = document.getElementById('auth-modal');
const emailInput = document.getElementById('email-input');
const passwordInput = document.getElementById('password-input');
const btnEmailAuth = document.getElementById('btn-email-auth');
const btnGoogle = document.getElementById('btn-google');
const btnGuest = document.getElementById('btn-guest');
const btnAccountSwitch = document.getElementById('btn-account-switch');
const userStatusText = document.getElementById('user-status-text');
const syncStatusIndicator = document.getElementById('sync-status-indicator');
const notesList = document.getElementById('notes-list');
const noteTitle = document.getElementById('note-title');
const noteContent = document.getElementById('note-content');
const btnNewNote = document.getElementById('btn-new-note');
const btnDelete = document.getElementById('btn-delete');
const btnDownload = document.getElementById('btn-download');
const themeToggle = document.getElementById('theme-toggle');
const saveStatus = document.getElementById('save-status');
const wordCount = document.getElementById('word-count');
const charCount = document.getElementById('char-count');
lucide.createIcons();
// Initialization
window.addEventListener('DOMContentLoaded', () => {
if (localStorage.getItem('theme') === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
}
const savedMode = localStorage.getItem('kome_notepad_mode');
if (savedMode === 'guest' || !auth) {
enableGuestMode();
}
});
themeToggle.addEventListener('click', () => {
document.documentElement.classList.toggle('dark');
localStorage.setItem('theme', document.documentElement.classList.contains('dark') ? 'dark' : 'light');
});
// Auth & Mode Switching
btnGuest.addEventListener('click', () => enableGuestMode());
btnAccountSwitch.addEventListener('click', () => {
authModal.classList.remove('hidden');
});
// XỬ LÝ ĐĂNG NHẬP / ĐĂNG KÝ EMAIL & PASSWORD
btnEmailAuth.addEventListener('click', async () => {
if (!auth) {
alert("Firebase Config setup is incomplete!");
return;
}
const email = emailInput.value.trim();
const password = passwordInput.value;
if (!email || !password) return alert('Please enter both Email and Password');
console.log("Attempting Auth for:", email);
try {
// Thu nhập đăng nhập trước
const cred = await auth.signInWithEmailAndPassword(email, password);
currentUser = cred.user;
console.log("Logged in:", currentUser.email);
enableCloudMode();
} catch (err) {
console.log("SignIn Error Code:", err.code);
// Nếu không tìm thấy user hoặc sai thông tin, tự động tạo tài khoản mới
if (err.code === 'auth/user-not-found' || err.code === 'auth/invalid-credential' || err.code === 'auth/wrong-password') {
try {
console.log("Attempting Registering new user...");
const cred = await auth.createUserWithEmailAndPassword(email, password);
currentUser = cred.user;
console.log("Registered & Logged in:", currentUser.email);
enableCloudMode();
} catch (createErr) {
console.error("Register Error:", createErr);
alert('Authentication failed: ' + createErr.message);
}
} else {
console.error("Auth Error:", err);
alert('Auth Error: ' + err.message);
}
}
});
// XỬ LÝ ĐĂNG NHẬP GOOGLE
btnGoogle.addEventListener('click', () => {
if (!auth) return alert("Firebase Config setup is incomplete!");
const provider = new firebase.auth.GoogleAuthProvider();
auth.signInWithPopup(provider).then(result => {
currentUser = result.user;
enableCloudMode();
}).catch(err => {
console.error("Google Auth Error:", err);
alert('Google Auth error: ' + err.message);
});
});
if (auth) {
auth.onAuthStateChanged(user => {
if (user) {
currentUser = user;
enableCloudMode();
}
});
}
function enableGuestMode() {
currentMode = 'guest';
localStorage.setItem('kome_notepad_mode', 'guest');
userStatusText.innerText = 'Guest Account';
syncStatusIndicator.innerText = 'Local Storage';
syncStatusIndicator.className = 'text-amber-600 dark:text-amber-400 block text-[10px] font-medium';
authModal.classList.add('hidden');
loadGuestNotes();
}
function enableCloudMode() {
currentMode = 'cloud';
localStorage.setItem('kome_notepad_mode', 'cloud');
userStatusText.innerText = currentUser ? currentUser.email : 'Cloud User';
syncStatusIndicator.innerText = 'Cloud Sync Active';
syncStatusIndicator.className = 'text-green-600 dark:text-green-400 block text-[10px] font-medium';
authModal.classList.add('hidden');
loadCloudNotes();
}
// Storage Management
function loadGuestNotes() {
const data = localStorage.getItem('kome_notepad_notes');
notes = data ? JSON.parse(data) : [];
if (notes.length === 0) createNewNote();
else selectNote(notes[0].id);
renderNotesList();
}
function loadCloudNotes() {
if (!db || !currentUser) return;
db.collection('notes').where('userId', '==', currentUser.uid)
.onSnapshot(snapshot => {
notes = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
notes.sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0));
if (notes.length === 0) createNewNote();
else if (!currentNoteId) selectNote(notes[0].id);
renderNotesList();
}, error => {
console.error("Firestore error:", error);
});
}
function saveCurrentNote() {
if (!currentNoteId) return;
saveStatus.innerText = 'Saving...';
const title = noteTitle.value.trim() || 'Untitled Note';
const content = noteContent.value;
const updatedAt = Date.now();
const index = notes.findIndex(n => n.id === currentNoteId);
if (index !== -1) {
notes[index] = { ...notes[index], title, content, updatedAt };
}
if (currentMode === 'guest') {
localStorage.setItem('kome_notepad_notes', JSON.stringify(notes));
saveStatus.innerText = 'Saved locally';
renderNotesList();
} else if (currentMode === 'cloud' && db) {
db.collection('notes').doc(currentNoteId).set({
userId: currentUser.uid,
title,
content,
updatedAt
}, { merge: true }).then(() => {
saveStatus.innerText = 'Saved to Cloud';
}).catch(err => {
console.error("Save Note Error:", err);
saveStatus.innerText = 'Save Error';
});
}
}
[noteTitle, noteContent].forEach(el => {
el.addEventListener('input', () => {
updateStats();
saveStatus.innerText = 'Unsaved changes...';
clearTimeout(saveTimeout);
saveTimeout = setTimeout(saveCurrentNote, 600);
});
});
// Note Actions
btnNewNote.addEventListener('click', () => createNewNote());
function createNewNote() {
const newNote = {
id: currentMode === 'guest' ? 'guest_' + Date.now() : db.collection('notes').doc().id,
title: 'Untitled Note',
content: '',
updatedAt: Date.now()
};
if (currentMode === 'guest') {
notes.unshift(newNote);
localStorage.setItem('kome_notepad_notes', JSON.stringify(notes));
selectNote(newNote.id);
renderNotesList();
} else if (currentMode === 'cloud' && db) {
db.collection('notes').doc(newNote.id).set({
userId: currentUser.uid,
...newNote
}).then(() => selectNote(newNote.id));
}
}
function selectNote(id) {
currentNoteId = id;
const note = notes.find(n => n.id === id);
if (note) {
noteTitle.value = note.title || '';
noteContent.value = note.content || '';
updateStats();
renderNotesList();
saveStatus.innerText = 'Loaded';
}
}
btnDelete.addEventListener('click', () => {
if (!currentNoteId) return;
if (!confirm('Are you sure you want to delete this note?')) return;
if (currentMode === 'guest') {
notes = notes.filter(n => n.id !== currentNoteId);
localStorage.setItem('kome_notepad_notes', JSON.stringify(notes));
currentNoteId = notes.length > 0 ? notes[0].id : null;
if (!currentNoteId) createNewNote();
else selectNote(currentNoteId);
renderNotesList();
} else if (currentMode === 'cloud' && db) {
db.collection('notes').doc(currentNoteId).delete().then(() => {
currentNoteId = null;
});
}
});
btnDownload.addEventListener('click', () => {
const title = (noteTitle.value.trim() || 'note') + '.txt';
const blob = new Blob([noteContent.value], { type: 'text/plain;charset=utf-8' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = title;
a.click();
URL.revokeObjectURL(a.href);
});
function renderNotesList() {
notesList.innerHTML = '';
notes.forEach(note => {
const isActive = note.id === currentNoteId;
const div = document.createElement('div');
div.className = `p-3 rounded-xl cursor-pointer transition select-none ${
isActive
? 'bg-brand-50 dark:bg-brand-500/10 text-brand-700 dark:text-brand-400 font-medium'
: 'hover:bg-gray-100 dark:hover:bg-gray-700/50 text-gray-700 dark:text-gray-300'
}`;
div.onclick = () => selectNote(note.id);
div.innerHTML = `
<div class="truncate text-sm font-semibold">${escapeHtml(note.title || 'Untitled Note')}</div>
<div class="truncate text-xs text-gray-400 dark:text-gray-500 mt-1">${escapeHtml(note.content || 'Empty note')}</div>
`;
notesList.appendChild(div);
});
}
function updateStats() {
const text = noteContent.value;
const chars = text.length;
const words = text.trim() ? text.trim().split(/\s+/).length : 0;
charCount.innerText = `${chars} characters`;
wordCount.innerText = `${words} words`;
}
function escapeHtml(text) {
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
}
</script>
</body>
</html>