feat: 优化分类选择器与笔记移动功能
This commit is contained in:
@@ -35,6 +35,8 @@ const saveStatus = ref('');
|
||||
let saveTimeout = null;
|
||||
let lastSavedContent = ref('');
|
||||
let isSaving = ref(false);
|
||||
// 维护当前最新的笔记数据
|
||||
const currentData = ref({ ...props.editData });
|
||||
|
||||
const initVditor = () => {
|
||||
if (vditor.value) {
|
||||
@@ -49,12 +51,12 @@ const initVditor = () => {
|
||||
},
|
||||
after: () => {
|
||||
isInitialized.value = true;
|
||||
if (props.editData && props.editData.content) {
|
||||
vditor.value.setValue(props.editData.content);
|
||||
lastSavedContent.value = props.editData.content;
|
||||
if (currentData.value && currentData.value.content) {
|
||||
vditor.value.setValue(currentData.value.content);
|
||||
lastSavedContent.value = currentData.value.content;
|
||||
}
|
||||
if (props.editData && props.editData.id) {
|
||||
currentId.value = props.editData.id;
|
||||
if (currentData.value && currentData.value.id) {
|
||||
currentId.value = currentData.value.id;
|
||||
}
|
||||
vditor.value.focus();
|
||||
},
|
||||
@@ -101,31 +103,42 @@ const save = async (value) => {
|
||||
try {
|
||||
saveStatus.value = '正在保存...';
|
||||
|
||||
// 确保groupingId不会丢失:优先使用currentData中的值
|
||||
const groupingId = currentData.value.groupingId || props.editData.groupingId;
|
||||
|
||||
// 将ID转为字符串以避免JavaScript精度丢失
|
||||
const idString = currentId.value ? String(currentId.value) : (currentData.value.id ? String(currentData.value.id) : null);
|
||||
const groupingIdString = groupingId ? String(groupingId) : null;
|
||||
|
||||
const payload = {
|
||||
id: currentId.value || props.editData.id || null,
|
||||
id: idString,
|
||||
content: content,
|
||||
title: props.editData.title,
|
||||
groupingId: props.editData.groupingId,
|
||||
fileName: props.editData.fileName,
|
||||
isPrivate: props.editData.isPrivate
|
||||
title: currentData.value.title || props.editData.title,
|
||||
groupingId: groupingIdString,
|
||||
fileName: currentData.value.fileName || props.editData.fileName,
|
||||
isPrivate: currentData.value.isPrivate !== undefined ? currentData.value.isPrivate : props.editData.isPrivate
|
||||
};
|
||||
|
||||
|
||||
|
||||
const response = await updateMarkdown(payload);
|
||||
|
||||
if (response && response.id) {
|
||||
currentId.value = response.id;
|
||||
lastSavedContent.value = content;
|
||||
|
||||
// 使用后端返回的数据,但确保groupingId不会丢失
|
||||
// 注意:后端返回的ID是字符串,保持字符串格式避免精度丢失
|
||||
const updatedFile = {
|
||||
...props.editData,
|
||||
id: response.id,
|
||||
...response,
|
||||
content: content,
|
||||
groupingId: response.groupingId,
|
||||
groupingName: response.groupingName,
|
||||
title: response.title,
|
||||
isPrivate: response.isPrivate
|
||||
// 如果后端返回的groupingId为空,使用原来的值(保持字符串格式)
|
||||
groupingId: response.groupingId || groupingIdString,
|
||||
groupingName: response.groupingName || currentData.value.groupingName
|
||||
};
|
||||
|
||||
// 更新currentData为最新数据
|
||||
currentData.value = updatedFile;
|
||||
emit('update:editData', updatedFile);
|
||||
saveStatus.value = '已保存';
|
||||
}
|
||||
@@ -143,12 +156,17 @@ const handleBack = async () => {
|
||||
await save(content);
|
||||
}
|
||||
|
||||
// 确保groupingId不会丢失(保持字符串格式)
|
||||
const groupingId = currentData.value.groupingId || props.editData.groupingId;
|
||||
const groupingName = currentData.value.groupingName || props.editData.groupingName;
|
||||
|
||||
const returnData = {
|
||||
...currentData.value,
|
||||
...props.editData,
|
||||
id: currentId.value || props.editData.id,
|
||||
id: currentId.value ? String(currentId.value) : (currentData.value.id ? String(currentData.value.id) : null),
|
||||
content: content,
|
||||
groupingId: props.editData.groupingId,
|
||||
groupingName: props.editData.groupingName
|
||||
groupingId: groupingId ? String(groupingId) : null,
|
||||
groupingName: groupingName
|
||||
};
|
||||
|
||||
emit('back', returnData);
|
||||
@@ -170,6 +188,8 @@ onBeforeUnmount(() => {
|
||||
|
||||
watch(() => props.editData, (newVal, oldVal) => {
|
||||
if (vditor.value && isInitialized.value && newVal && newVal.id !== oldVal?.id) {
|
||||
// 更新currentData为最新的props数据
|
||||
currentData.value = { ...newVal };
|
||||
vditor.value.setValue(newVal.content || '');
|
||||
lastSavedContent.value = newVal.content || '';
|
||||
currentId.value = newVal.id;
|
||||
|
||||
Reference in New Issue
Block a user