feat: 编辑弹框关闭时检查是否有修改并提示确认
- 打开编辑弹框时保存原始数据状态 - 关闭时比较当前内容和原始内容 - 如果有修改,弹出确认对话框询问是否放弃修改 - 保存成功后清除原始数据标记
This commit is contained in:
@@ -2185,7 +2185,69 @@ async function openEditModal(id) {
|
||||
// 设置重点关注状态
|
||||
document.getElementById('editStarred').checked = item.is_starred === 1;
|
||||
|
||||
new bootstrap.Modal(document.getElementById('editModal')).show();
|
||||
// 保存原始数据用于比较
|
||||
window.editOriginalData = {
|
||||
type,
|
||||
title: item.title || '',
|
||||
content: item.content || '',
|
||||
url: item.url || '',
|
||||
source: item.source || '',
|
||||
status: item.status,
|
||||
priority: item.priority,
|
||||
due_date: item.due_date,
|
||||
note: item.note || '',
|
||||
tags: item.tags.join(', '),
|
||||
is_starred: item.is_starred
|
||||
};
|
||||
|
||||
const modal = new bootstrap.Modal(document.getElementById('editModal'));
|
||||
modal.show();
|
||||
|
||||
// 监听关闭事件,检查是否有修改
|
||||
document.getElementById('editModal').addEventListener('hide.bs.modal', function(e) {
|
||||
if (hasEditChanges()) {
|
||||
if (!confirm('您已修改内容但未保存,是否放弃修改?')) {
|
||||
e.preventDefault(); // 取消关闭
|
||||
}
|
||||
}
|
||||
}, { once: true });
|
||||
}
|
||||
|
||||
// 检查编辑内容是否有修改
|
||||
function hasEditChanges() {
|
||||
if (!window.editOriginalData) return false;
|
||||
|
||||
const type = document.getElementById('editType').value;
|
||||
const current = {
|
||||
type,
|
||||
title: document.getElementById('editTitle').value,
|
||||
content: document.getElementById('editContent').value,
|
||||
url: document.getElementById('editUrl').value,
|
||||
source: document.getElementById('editSource').value,
|
||||
note: document.getElementById('editNote').value,
|
||||
tags: document.getElementById('editTags').value,
|
||||
is_starred: document.getElementById('editStarred').checked ? 1 : 0
|
||||
};
|
||||
|
||||
if (type === 'todo') {
|
||||
current.status = document.getElementById('editStatus').value;
|
||||
current.priority = document.getElementById('editPriority').value;
|
||||
current.due_date = document.getElementById('editDueDate').value;
|
||||
}
|
||||
|
||||
// 比较各字段
|
||||
return current.title !== window.editOriginalData.title ||
|
||||
current.content !== (window.editOriginalData.content || '') ||
|
||||
current.url !== (window.editOriginalData.url || '') ||
|
||||
current.source !== (window.editOriginalData.source || '') ||
|
||||
current.note !== (window.editOriginalData.note || '') ||
|
||||
current.tags !== window.editOriginalData.tags ||
|
||||
current.is_starred !== window.editOriginalData.is_starred ||
|
||||
(type === 'todo' && (
|
||||
current.status !== window.editOriginalData.status ||
|
||||
current.priority !== window.editOriginalData.priority ||
|
||||
current.due_date !== (window.editOriginalData.due_date || '')
|
||||
));
|
||||
}
|
||||
|
||||
// 根据类型更新编辑表单字段显示
|
||||
@@ -2225,6 +2287,7 @@ async function saveEdit() {
|
||||
const result = await res.json();
|
||||
|
||||
if (res.ok && result.success) {
|
||||
window.editOriginalData = null; // 清除原始数据,避免关闭时询问
|
||||
bootstrap.Modal.getInstance(document.getElementById('editModal')).hide();
|
||||
refreshData();
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user