You've already forked Nano-Banana-AI-Image-Editor
功能性整合
This commit is contained in:
@@ -109,8 +109,10 @@ interface AppState {
|
||||
setTemperature: (temp: number) => void;
|
||||
setSeed: (seed: number | null) => void;
|
||||
|
||||
addGeneration: (generation: Generation) => void;
|
||||
addEdit: (edit: Edit) => void;
|
||||
addGeneration: (generation) => void;
|
||||
addEdit: (edit) => void;
|
||||
removeGeneration: (id: string) => void;
|
||||
removeEdit: (id: string) => void;
|
||||
selectGeneration: (id: string | null) => void;
|
||||
selectEdit: (id: string | null) => void;
|
||||
setShowHistory: (show: boolean) => void;
|
||||
@@ -119,12 +121,6 @@ interface AppState {
|
||||
|
||||
setSelectedTool: (tool: 'generate' | 'edit' | 'mask') => void;
|
||||
|
||||
// 删除历史记录
|
||||
deleteGeneration: (id: string) => void;
|
||||
deleteEdit: (id: string) => void;
|
||||
deleteGenerations: (ids: string[]) => void;
|
||||
deleteEdits: (ids: string[]) => void;
|
||||
|
||||
// Blob存储操作
|
||||
addBlob: (blob: Blob) => string;
|
||||
getBlob: (url: string) => Blob | undefined;
|
||||
@@ -651,239 +647,95 @@ export const useAppStore = create<AppState>()(
|
||||
});
|
||||
},
|
||||
|
||||
// 删除单个生成记录
|
||||
deleteGeneration: (id) => set((state) => {
|
||||
if (!state.currentProject) return {};
|
||||
|
||||
// 找到要删除的记录
|
||||
const generationToDelete = state.currentProject.generations.find(gen => gen.id === id);
|
||||
if (!generationToDelete) return {};
|
||||
|
||||
// 收集需要释放的Blob URLs
|
||||
const urlsToRevoke: string[] = [];
|
||||
|
||||
// 收集生成记录中的Blob URLs
|
||||
generationToDelete.sourceAssets.forEach(asset => {
|
||||
if (asset.blobUrl.startsWith('blob:')) {
|
||||
urlsToRevoke.push(asset.blobUrl);
|
||||
}
|
||||
});
|
||||
generationToDelete.outputAssetsBlobUrls.forEach(url => {
|
||||
if (url.startsWith('blob:')) {
|
||||
urlsToRevoke.push(url);
|
||||
}
|
||||
});
|
||||
|
||||
// 从IndexedDB中删除记录
|
||||
indexedDBService.deleteGeneration(id).catch(err => {
|
||||
console.error('从IndexedDB删除生成记录失败:', err);
|
||||
});
|
||||
|
||||
// 释放Blob URLs
|
||||
if (urlsToRevoke.length > 0) {
|
||||
set((innerState) => {
|
||||
urlsToRevoke.forEach(url => {
|
||||
URL.revokeObjectURL(url);
|
||||
const newBlobStore = new Map(innerState.blobStore);
|
||||
newBlobStore.delete(url);
|
||||
innerState = { ...innerState, blobStore: newBlobStore };
|
||||
});
|
||||
return innerState;
|
||||
});
|
||||
}
|
||||
|
||||
// 如果删除的是当前选中的记录,清除选择
|
||||
let selectedGenerationId = state.selectedGenerationId;
|
||||
if (selectedGenerationId === id) {
|
||||
selectedGenerationId = null;
|
||||
}
|
||||
|
||||
// 更新项目中的生成记录列表
|
||||
const updatedGenerations = state.currentProject.generations.filter(gen => gen.id !== id);
|
||||
|
||||
return {
|
||||
currentProject: {
|
||||
...state.currentProject,
|
||||
generations: updatedGenerations,
|
||||
updatedAt: Date.now()
|
||||
},
|
||||
selectedGenerationId
|
||||
};
|
||||
}),
|
||||
|
||||
// 删除单个编辑记录
|
||||
deleteEdit: (id) => set((state) => {
|
||||
if (!state.currentProject) return {};
|
||||
|
||||
// 找到要删除的记录
|
||||
const editToDelete = state.currentProject.edits.find(edit => edit.id === id);
|
||||
if (!editToDelete) return {};
|
||||
|
||||
// 收集需要释放的Blob URLs
|
||||
const urlsToRevoke: string[] = [];
|
||||
|
||||
// 收集编辑记录中的Blob URLs
|
||||
if (editToDelete.maskReferenceAssetBlobUrl && editToDelete.maskReferenceAssetBlobUrl.startsWith('blob:')) {
|
||||
urlsToRevoke.push(editToDelete.maskReferenceAssetBlobUrl);
|
||||
}
|
||||
editToDelete.outputAssetsBlobUrls.forEach(url => {
|
||||
if (url.startsWith('blob:')) {
|
||||
urlsToRevoke.push(url);
|
||||
}
|
||||
});
|
||||
|
||||
// 从IndexedDB中删除记录
|
||||
indexedDBService.deleteEdit(id).catch(err => {
|
||||
console.error('从IndexedDB删除编辑记录失败:', err);
|
||||
});
|
||||
|
||||
// 释放Blob URLs
|
||||
if (urlsToRevoke.length > 0) {
|
||||
set((innerState) => {
|
||||
urlsToRevoke.forEach(url => {
|
||||
URL.revokeObjectURL(url);
|
||||
const newBlobStore = new Map(innerState.blobStore);
|
||||
newBlobStore.delete(url);
|
||||
innerState = { ...innerState, blobStore: newBlobStore };
|
||||
});
|
||||
return innerState;
|
||||
});
|
||||
}
|
||||
|
||||
// 如果删除的是当前选中的记录,清除选择
|
||||
let selectedEditId = state.selectedEditId;
|
||||
if (selectedEditId === id) {
|
||||
selectedEditId = null;
|
||||
}
|
||||
|
||||
// 更新项目中的编辑记录列表
|
||||
const updatedEdits = state.currentProject.edits.filter(edit => edit.id !== id);
|
||||
|
||||
return {
|
||||
currentProject: {
|
||||
...state.currentProject,
|
||||
edits: updatedEdits,
|
||||
updatedAt: Date.now()
|
||||
},
|
||||
selectedEditId
|
||||
};
|
||||
}),
|
||||
|
||||
// 批量删除生成记录
|
||||
deleteGenerations: (ids) => set((state) => {
|
||||
// 删除生成记录
|
||||
removeGeneration: (id) => set((state) => {
|
||||
if (!state.currentProject) return {};
|
||||
|
||||
// 收集需要释放的Blob URLs
|
||||
const urlsToRevoke: string[] = [];
|
||||
const generationToRemove = state.currentProject.generations.find(gen => gen.id === id);
|
||||
|
||||
// 收集所有要删除记录中的Blob URLs
|
||||
state.currentProject.generations.forEach(gen => {
|
||||
if (ids.includes(gen.id)) {
|
||||
gen.sourceAssets.forEach(asset => {
|
||||
if (asset.blobUrl.startsWith('blob:')) {
|
||||
urlsToRevoke.push(asset.blobUrl);
|
||||
}
|
||||
});
|
||||
gen.outputAssetsBlobUrls.forEach(url => {
|
||||
if (url.startsWith('blob:')) {
|
||||
urlsToRevoke.push(url);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 从IndexedDB中批量删除记录
|
||||
indexedDBService.deleteGenerations(ids).catch(err => {
|
||||
console.error('从IndexedDB批量删除生成记录失败:', err);
|
||||
});
|
||||
|
||||
// 释放Blob URLs
|
||||
if (urlsToRevoke.length > 0) {
|
||||
set((innerState) => {
|
||||
urlsToRevoke.forEach(url => {
|
||||
URL.revokeObjectURL(url);
|
||||
const newBlobStore = new Map(innerState.blobStore);
|
||||
newBlobStore.delete(url);
|
||||
innerState = { ...innerState, blobStore: newBlobStore };
|
||||
});
|
||||
return innerState;
|
||||
});
|
||||
}
|
||||
|
||||
// 如果删除的是当前选中的记录,清除选择
|
||||
let selectedGenerationId = state.selectedGenerationId;
|
||||
if (selectedGenerationId && ids.includes(selectedGenerationId)) {
|
||||
selectedGenerationId = null;
|
||||
}
|
||||
|
||||
// 更新项目中的生成记录列表
|
||||
const updatedGenerations = state.currentProject.generations.filter(gen => !ids.includes(gen.id));
|
||||
|
||||
return {
|
||||
currentProject: {
|
||||
...state.currentProject,
|
||||
generations: updatedGenerations,
|
||||
updatedAt: Date.now()
|
||||
},
|
||||
selectedGenerationId
|
||||
};
|
||||
}),
|
||||
|
||||
// 批量删除编辑记录
|
||||
deleteEdits: (ids) => set((state) => {
|
||||
if (!state.currentProject) return {};
|
||||
|
||||
// 收集需要释放的Blob URLs
|
||||
const urlsToRevoke: string[] = [];
|
||||
|
||||
// 收集所有要删除记录中的Blob URLs
|
||||
state.currentProject.edits.forEach(edit => {
|
||||
if (ids.includes(edit.id)) {
|
||||
if (edit.maskReferenceAssetBlobUrl && edit.maskReferenceAssetBlobUrl.startsWith('blob:')) {
|
||||
urlsToRevoke.push(edit.maskReferenceAssetBlobUrl);
|
||||
if (generationToRemove) {
|
||||
// 收集要删除的生成记录中的Blob URLs
|
||||
generationToRemove.sourceAssets.forEach(asset => {
|
||||
if (asset.blobUrl.startsWith('blob:')) {
|
||||
urlsToRevoke.push(asset.blobUrl);
|
||||
}
|
||||
edit.outputAssetsBlobUrls.forEach(url => {
|
||||
if (url.startsWith('blob:')) {
|
||||
urlsToRevoke.push(url);
|
||||
}
|
||||
});
|
||||
generationToRemove.outputAssetsBlobUrls.forEach(url => {
|
||||
if (url.startsWith('blob:')) {
|
||||
urlsToRevoke.push(url);
|
||||
}
|
||||
});
|
||||
|
||||
// 释放Blob URLs
|
||||
if (urlsToRevoke.length > 0) {
|
||||
set((innerState) => {
|
||||
urlsToRevoke.forEach(url => {
|
||||
URL.revokeObjectURL(url);
|
||||
const newBlobStore = new Map(innerState.blobStore);
|
||||
newBlobStore.delete(url);
|
||||
innerState = { ...innerState, blobStore: newBlobStore };
|
||||
});
|
||||
return innerState;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 从IndexedDB中批量删除记录
|
||||
indexedDBService.deleteEdits(ids).catch(err => {
|
||||
console.error('从IndexedDB批量删除编辑记录失败:', err);
|
||||
});
|
||||
|
||||
// 释放Blob URLs
|
||||
if (urlsToRevoke.length > 0) {
|
||||
set((innerState) => {
|
||||
urlsToRevoke.forEach(url => {
|
||||
URL.revokeObjectURL(url);
|
||||
const newBlobStore = new Map(innerState.blobStore);
|
||||
newBlobStore.delete(url);
|
||||
innerState = { ...innerState, blobStore: newBlobStore };
|
||||
});
|
||||
return innerState;
|
||||
});
|
||||
}
|
||||
|
||||
// 如果删除的是当前选中的记录,清除选择
|
||||
let selectedEditId = state.selectedEditId;
|
||||
if (selectedEditId && ids.includes(selectedEditId)) {
|
||||
selectedEditId = null;
|
||||
}
|
||||
|
||||
// 更新项目中的编辑记录列表
|
||||
const updatedEdits = state.currentProject.edits.filter(edit => !ids.includes(edit.id));
|
||||
// 从项目中移除生成记录
|
||||
const updatedProject = {
|
||||
...state.currentProject,
|
||||
generations: state.currentProject.generations.filter(gen => gen.id !== id),
|
||||
updatedAt: Date.now()
|
||||
};
|
||||
|
||||
return {
|
||||
currentProject: {
|
||||
...state.currentProject,
|
||||
edits: updatedEdits,
|
||||
updatedAt: Date.now()
|
||||
},
|
||||
selectedEditId
|
||||
currentProject: updatedProject
|
||||
};
|
||||
}),
|
||||
|
||||
// 删除编辑记录
|
||||
removeEdit: (id) => set((state) => {
|
||||
if (!state.currentProject) return {};
|
||||
|
||||
// 收集需要释放的Blob URLs
|
||||
const urlsToRevoke: string[] = [];
|
||||
const editToRemove = state.currentProject.edits.find(edit => edit.id === id);
|
||||
|
||||
if (editToRemove) {
|
||||
// 收集要删除的编辑记录中的Blob URLs
|
||||
if (editToRemove.maskReferenceAssetBlobUrl && editToRemove.maskReferenceAssetBlobUrl.startsWith('blob:')) {
|
||||
urlsToRevoke.push(editToRemove.maskReferenceAssetBlobUrl);
|
||||
}
|
||||
editToRemove.outputAssetsBlobUrls.forEach(url => {
|
||||
if (url.startsWith('blob:')) {
|
||||
urlsToRevoke.push(url);
|
||||
}
|
||||
});
|
||||
|
||||
// 释放Blob URLs
|
||||
if (urlsToRevoke.length > 0) {
|
||||
set((innerState) => {
|
||||
urlsToRevoke.forEach(url => {
|
||||
URL.revokeObjectURL(url);
|
||||
const newBlobStore = new Map(innerState.blobStore);
|
||||
newBlobStore.delete(url);
|
||||
innerState = { ...innerState, blobStore: newBlobStore };
|
||||
});
|
||||
return innerState;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 从项目中移除编辑记录
|
||||
const updatedProject = {
|
||||
...state.currentProject,
|
||||
edits: state.currentProject.edits.filter(edit => edit.id !== id),
|
||||
updatedAt: Date.now()
|
||||
};
|
||||
|
||||
return {
|
||||
currentProject: updatedProject
|
||||
};
|
||||
})
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user