mirror of
https://github.com/OpenCut-app/OpenCut.git
synced 2026-07-13 21:52:53 +02:00
fix: removed the media from timeline after its deleted
This commit is contained in:
@@ -183,24 +183,46 @@ export const useMediaStore = create<MediaStore>((set, get) => ({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
removeMediaItem: async (projectId, id: string) => {
|
removeMediaItem: async (projectId: string, id: string) => {
|
||||||
const state = get();
|
const state = get();
|
||||||
const item = state.mediaItems.find((media) => media.id === id);
|
const item = state.mediaItems.find((media) => media.id === id);
|
||||||
|
|
||||||
// Cleanup object URLs to prevent memory leaks
|
// Cleanup object URLs to prevent memory leaks
|
||||||
if (item && item.url) {
|
if (item?.url) {
|
||||||
URL.revokeObjectURL(item.url);
|
URL.revokeObjectURL(item.url);
|
||||||
if (item.thumbnailUrl) {
|
if (item.thumbnailUrl) {
|
||||||
URL.revokeObjectURL(item.thumbnailUrl);
|
URL.revokeObjectURL(item.thumbnailUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove from local state immediately
|
// 1) Remove from local state immediately
|
||||||
set((state) => ({
|
set((state) => ({
|
||||||
mediaItems: state.mediaItems.filter((media) => media.id !== id),
|
mediaItems: state.mediaItems.filter((media) => media.id !== id),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Remove from persistent storage
|
// 2) Cascade into the timeline: remove any elements using this media ID
|
||||||
|
const timeline = useTimelineStore.getState();
|
||||||
|
const {
|
||||||
|
tracks,
|
||||||
|
removeElementFromTrack,
|
||||||
|
removeElementFromTrackWithRipple,
|
||||||
|
rippleEditingEnabled,
|
||||||
|
} = timeline;
|
||||||
|
|
||||||
|
// Iterate over a snapshot of tracks and their elements
|
||||||
|
tracks.forEach((track) => {
|
||||||
|
track.elements.forEach((el) => {
|
||||||
|
if (el.type === "media" && el.mediaId === id) {
|
||||||
|
if (rippleEditingEnabled) {
|
||||||
|
removeElementFromTrackWithRipple(track.id, el.id);
|
||||||
|
} else {
|
||||||
|
removeElementFromTrack(track.id, el.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 3) Remove from persistent storage
|
||||||
try {
|
try {
|
||||||
await storageService.deleteMediaItem(projectId, id);
|
await storageService.deleteMediaItem(projectId, id);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -219,15 +241,19 @@ export const useMediaStore = create<MediaStore>((set, get) => ({
|
|||||||
mediaItems.map(async (item) => {
|
mediaItems.map(async (item) => {
|
||||||
if (item.type === "video" && item.file) {
|
if (item.type === "video" && item.file) {
|
||||||
try {
|
try {
|
||||||
const { thumbnailUrl, width, height } = await generateVideoThumbnail(item.file);
|
const { thumbnailUrl, width, height } =
|
||||||
|
await generateVideoThumbnail(item.file);
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
thumbnailUrl,
|
thumbnailUrl,
|
||||||
width: width || item.width,
|
width: width || item.width,
|
||||||
height: height || item.height
|
height: height || item.height,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Failed to regenerate thumbnail for video ${item.id}:`, error);
|
console.error(
|
||||||
|
`Failed to regenerate thumbnail for video ${item.id}:`,
|
||||||
|
error
|
||||||
|
);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user