notes: AI titles for new notes, personal-only access, modal polish
- new note created without a title gets one generated by a background worker (createNoteTitleWorker, ai_status column, wake channel, notes_changed SSE event, settings toggle notes_ai_enabled) - notes are strictly personal: list/update/delete/reorder always scoped to req.user.id, admin no longer sees other users' notes (403) - notes modal: focus goes to the text body, random color for new notes, ctrl+enter hint, tags hint styling, empty-title 400 fix (optText) - backup/restore: user_notes.ai_status normalized and restored
This commit is contained in:
+40
-3
@@ -5,6 +5,11 @@
|
||||
let editingId = null;
|
||||
let dragId = null;
|
||||
let saving = false;
|
||||
const NOTE_COLORS = ['#2563eb', '#4f46e5', '#7c3aed', '#db2777', '#dc2626', '#ea580c', '#ca8a04', '#16a34a', '#0d9488', '#0891b2'];
|
||||
|
||||
function randomColor() {
|
||||
return NOTE_COLORS[Math.floor(Math.random() * NOTE_COLORS.length)];
|
||||
}
|
||||
|
||||
function hdr() {
|
||||
const t = sessionStorage.getItem('authToken');
|
||||
@@ -29,6 +34,12 @@
|
||||
const body = n.note
|
||||
? esc(n.note)
|
||||
: '<span class="note-empty-note">Без текста</span>';
|
||||
const generating = !n.title && (n.ai_status === 'pending' || n.ai_status === 'processing');
|
||||
const titleHtml = n.title
|
||||
? esc(n.title)
|
||||
: generating
|
||||
? '<span class="note-empty-note">Заголовок генерируется…</span>'
|
||||
: esc('Без заголовка');
|
||||
return '<div class="note-card" data-id="' + n.id + '" style="--ncolor:' + color + '">' +
|
||||
'<div class="note-card-head">' +
|
||||
'<span class="note-drag" draggable="true" title="Перетащите, чтобы изменить порядок">⠿</span>' +
|
||||
@@ -36,7 +47,7 @@
|
||||
'<input type="color" id="noteColorPicker-' + n.id + '" value="' + color + '" style="display:none">' +
|
||||
'<span class="note-color-swatch" style="background:' + color + '" title="Изменить цвет"></span>' +
|
||||
'</div>' +
|
||||
'<div class="note-card-title">' + esc(n.title || 'Без заголовка') + '</div>' +
|
||||
'<div class="note-card-title">' + titleHtml + '</div>' +
|
||||
tagsHtml +
|
||||
'</div>' +
|
||||
'<div class="note-card-note">' + body + '</div>' +
|
||||
@@ -127,14 +138,14 @@
|
||||
document.getElementById('notesTitle').value = note ? note.title : '';
|
||||
document.getElementById('notesNote').value = note ? note.note : '';
|
||||
document.getElementById('notesTags').value = note ? note.tags : '';
|
||||
const color = note && /^#[0-9a-fA-F]{6}$/.test(note.color || '') ? note.color : '#2563eb';
|
||||
const color = note && /^#[0-9a-fA-F]{6}$/.test(note.color || '') ? note.color : randomColor();
|
||||
document.getElementById('notesColor').value = color;
|
||||
document.getElementById('notesColorPreview').style.background = color;
|
||||
const row = document.getElementById('notesColorRow');
|
||||
if (row) row.style.setProperty('--ncolor', color);
|
||||
document.getElementById('notesModal').classList.add('open');
|
||||
renderIcons();
|
||||
setTimeout(() => document.getElementById('notesTitle').focus(), 50);
|
||||
setTimeout(() => document.getElementById('notesNote').focus(), 50);
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
@@ -291,6 +302,31 @@
|
||||
}
|
||||
}
|
||||
|
||||
let liveSource = null;
|
||||
let liveReconnectTimer = null;
|
||||
let liveReloadTimer = null;
|
||||
|
||||
function connectNotesLive() {
|
||||
if (liveSource) { liveSource.close(); liveSource = null; }
|
||||
const connect = () => {
|
||||
const t = sessionStorage.getItem('authToken');
|
||||
if (!t) return;
|
||||
liveSource = new EventSource(`${API}/api/events?token=${encodeURIComponent(t)}`);
|
||||
const scheduleReload = () => {
|
||||
if (liveReloadTimer) clearTimeout(liveReloadTimer);
|
||||
liveReloadTimer = setTimeout(() => { liveReloadTimer = null; loadNotes(); }, 300);
|
||||
};
|
||||
liveSource.addEventListener('notes_changed', scheduleReload);
|
||||
liveSource.onerror = () => {
|
||||
if (liveSource) { liveSource.close(); liveSource = null; }
|
||||
if (liveReconnectTimer) clearTimeout(liveReconnectTimer);
|
||||
liveReconnectTimer = setTimeout(connect, 5000);
|
||||
};
|
||||
};
|
||||
connect();
|
||||
window.addEventListener('beforeunload', () => { if (liveSource) liveSource.close(); });
|
||||
}
|
||||
|
||||
async function init() {
|
||||
if (!(await checkAuth())) return;
|
||||
buildSidebar(document.body.dataset.page);
|
||||
@@ -298,6 +334,7 @@
|
||||
bindPage();
|
||||
renderIcons();
|
||||
await loadNotes();
|
||||
connectNotesLive();
|
||||
}
|
||||
|
||||
init();
|
||||
|
||||
Reference in New Issue
Block a user