fix(notes): необязательный заголовок, защита от двойного сохранения, Ctrl+Enter
This commit is contained in:
+13
-3
@@ -4,6 +4,7 @@
|
|||||||
let notes = [];
|
let notes = [];
|
||||||
let editingId = null;
|
let editingId = null;
|
||||||
let dragId = null;
|
let dragId = null;
|
||||||
|
let saving = false;
|
||||||
|
|
||||||
function hdr() {
|
function hdr() {
|
||||||
const t = sessionStorage.getItem('authToken');
|
const t = sessionStorage.getItem('authToken');
|
||||||
@@ -148,9 +149,11 @@
|
|||||||
const rawTags = document.getElementById('notesTags').value.trim();
|
const rawTags = document.getElementById('notesTags').value.trim();
|
||||||
const tags = rawTags ? rawTags.split(',').map(t => t.trim()).filter(Boolean).slice(0, 10).join(', ') : '';
|
const tags = rawTags ? rawTags.split(',').map(t => t.trim()).filter(Boolean).slice(0, 10).join(', ') : '';
|
||||||
const color = document.getElementById('notesColor').value;
|
const color = document.getElementById('notesColor').value;
|
||||||
if (!title) { showToast('Укажите заголовок заметки', true); return; }
|
if (!title && !noteText) { showToast('Заполните текст заметки', true); return; }
|
||||||
|
if (saving) return;
|
||||||
const payload = { title: title, note: noteText, tags: tags, color: color };
|
const payload = { title: title, note: noteText, tags: tags, color: color };
|
||||||
let res;
|
let res;
|
||||||
|
saving = true;
|
||||||
try {
|
try {
|
||||||
if (editingId) {
|
if (editingId) {
|
||||||
res = await fetch(API + '/api/notes/' + editingId, { method: 'PUT', headers: Object.assign({ 'Content-Type': 'application/json' }, hdr()), body: JSON.stringify(payload) });
|
res = await fetch(API + '/api/notes/' + editingId, { method: 'PUT', headers: Object.assign({ 'Content-Type': 'application/json' }, hdr()), body: JSON.stringify(payload) });
|
||||||
@@ -158,9 +161,11 @@
|
|||||||
res = await fetch(API + '/api/notes', { method: 'POST', headers: Object.assign({ 'Content-Type': 'application/json' }, hdr()), body: JSON.stringify(payload) });
|
res = await fetch(API + '/api/notes', { method: 'POST', headers: Object.assign({ 'Content-Type': 'application/json' }, hdr()), body: JSON.stringify(payload) });
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
saving = false;
|
||||||
showToast('Ошибка сети', true);
|
showToast('Ошибка сети', true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
saving = false;
|
||||||
const data = await res.json().catch(function () { return {}; });
|
const data = await res.json().catch(function () { return {}; });
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
showToast(data.error || 'Ошибка при сохранении', true);
|
showToast(data.error || 'Ошибка при сохранении', true);
|
||||||
@@ -211,9 +216,14 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
document.addEventListener('keydown', function (e) {
|
document.addEventListener('keydown', function (e) {
|
||||||
if (e.key !== 'Escape') return;
|
|
||||||
const m = document.getElementById('notesModal');
|
const m = document.getElementById('notesModal');
|
||||||
if (m && m.classList.contains('open')) closeModal();
|
const open = m && m.classList.contains('open');
|
||||||
|
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey) && open) {
|
||||||
|
e.preventDefault();
|
||||||
|
saveNote();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (e.key === 'Escape' && open) closeModal();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -79,7 +79,7 @@
|
|||||||
<button type="button" class="icon-btn" id="notesModalClose"><i data-lucide="x"></i></button>
|
<button type="button" class="icon-btn" id="notesModalClose"><i data-lucide="x"></i></button>
|
||||||
</div>
|
</div>
|
||||||
<div class="settings-field">
|
<div class="settings-field">
|
||||||
<label>Заголовок</label>
|
<label>Заголовок (необязательно)</label>
|
||||||
<input id="notesTitle" class="settings-input" maxlength="200" placeholder="Краткое название заметки">
|
<input id="notesTitle" class="settings-input" maxlength="200" placeholder="Краткое название заметки">
|
||||||
</div>
|
</div>
|
||||||
<div class="settings-field">
|
<div class="settings-field">
|
||||||
|
|||||||
@@ -3312,7 +3312,7 @@ app.get('/api/notes', requireAuth, async (req, res) => {
|
|||||||
|
|
||||||
app.post('/api/notes', requireAuth, async (req, res) => {
|
app.post('/api/notes', requireAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const title = reqStr(req.body?.title, NOTE_TITLE_MAX);
|
const title = optStr(req.body?.title, NOTE_TITLE_MAX) || '';
|
||||||
const note = optStr(req.body?.note, NOTE_TEXT_MAX) || '';
|
const note = optStr(req.body?.note, NOTE_TEXT_MAX) || '';
|
||||||
let tags = '';
|
let tags = '';
|
||||||
if (req.body?.tags !== undefined && req.body.tags !== null) {
|
if (req.body?.tags !== undefined && req.body.tags !== null) {
|
||||||
@@ -3379,7 +3379,7 @@ app.put('/api/notes/:id', requireAuth, async (req, res) => {
|
|||||||
const note = await findNote(req.user, id);
|
const note = await findNote(req.user, id);
|
||||||
if (!note) return res.status(404).json({ error: 'Not found' });
|
if (!note) return res.status(404).json({ error: 'Not found' });
|
||||||
if (note.forbidden) return res.status(403).json({ error: 'Нет доступа' });
|
if (note.forbidden) return res.status(403).json({ error: 'Нет доступа' });
|
||||||
const title = reqStr(req.body?.title, NOTE_TITLE_MAX);
|
const title = optStr(req.body?.title, NOTE_TITLE_MAX) || '';
|
||||||
const text = optStr(req.body?.note, NOTE_TEXT_MAX) || '';
|
const text = optStr(req.body?.note, NOTE_TEXT_MAX) || '';
|
||||||
let tags = '';
|
let tags = '';
|
||||||
if (req.body?.tags !== undefined && req.body.tags !== null) {
|
if (req.body?.tags !== undefined && req.body.tags !== null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user