Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete previous image on cover change #6368

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ class DocumentCoverState extends State<DocumentCover> {
}

Future<void> onCoverChanged(CoverType type, String? details) async {
await deletePreviousImageIfNecessary();
Copy link
Collaborator

@Xazin Xazin Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can perform this after changing the cover. (Since it's an asynchronous operation)

if (type == CoverType.file && details != null && !isURL(details)) {
if (_isLocalMode()) {
details = await saveImageToLocalStorage(details);
Expand All @@ -774,6 +775,20 @@ class DocumentCoverState extends State<DocumentCover> {
widget.onChangeCover(type, details);
}

Future<void> deletePreviousImageIfNecessary() async {
final previousType = CoverType.fromString(
widget.node.attributes[DocumentHeaderBlockKeys.coverType]);
final previousDetails =
widget.node.attributes[DocumentHeaderBlockKeys.coverDetails];

bool isFileType(CoverType type, String? details) =>
type == CoverType.file && details != null && !isURL(details);

if (isFileType(previousType, previousDetails) && _isLocalMode()) {
await deleteImageFromLocalStorage(previousDetails);
}
}

void setOverlayButtonsHidden(bool value) {
if (isOverlayButtonsHidden == value) return;
setState(() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ Future<List<ImageBlockData>> extractAndUploadImages(

return images;
}

Future<void> deleteImageFromLocalStorage(String localImagePath) async {
await File(localImagePath).delete();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we wrap this in try-catch and at least log errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ class EditorMigration {
},
};
}
} else {
extra = {
ViewExtKeys.coverKey: {
ViewExtKeys.coverTypeKey:
PageStyleCoverImageType.localImage.toString(),
ViewExtKeys.coverValueKey: coverDetails,
},
};
}
break;
default:
Expand Down
Loading