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

Cropper: behavior on invalid Source #5916

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Source/Extensions/Blazorise.Cropper/Cropper.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ internal async Task NotifyImageReady()
if ( ImageReady is not null )
await ImageReady.Invoke();
}

internal async Task NotifyImageLoadingFailed( string errorMessage )
{
if ( ImageLoadingFailed is not null )
await ImageLoadingFailed.Invoke(errorMessage);
}

#endregion

Expand All @@ -278,7 +284,7 @@ internal async Task NotifyImageReady()
/// <summary>
/// The original image source.
/// </summary>
[Parameter] public string Source { get; set; }
[Parameter, EditorRequired] public string Source { get; set; }

/// <summary>
/// The alt text of the image.
Expand Down Expand Up @@ -324,6 +330,12 @@ internal async Task NotifyImageReady()
/// This event fires when the image is ready / loaded.
/// </summary>
[Parameter] public Func<Task> ImageReady { get; set; }

/// <summary>
/// This event fires when the image cannot be loaded. Usually because of 404 or <see cref="Source"/> being null.
/// Error message inside the string parameter.
/// </summary>
tesar-tech marked this conversation as resolved.
Show resolved Hide resolved
[Parameter] public Func<string,Task> ImageLoadingFailed { get; set; }

/// <summary>
/// Indicates whether this element is disabled.
Expand Down
3 changes: 3 additions & 0 deletions Source/Extensions/Blazorise.Cropper/CropperAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ public CropperAdapter( Cropper cropper )

[JSInvokable( "ImageReady" )]
public async ValueTask ImageReady() => await cropper.NotifyImageReady();

[JSInvokable( nameof(ImageLoadingFailed) )]
public async ValueTask ImageLoadingFailed(string errorMessage) => await cropper.NotifyImageLoadingFailed(errorMessage);
}
28 changes: 22 additions & 6 deletions Source/Extensions/Blazorise.Cropper/wwwroot/blazorise.cropper.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ export function initialize(dotNetAdapter, element, elementId, options) {
const cropperSelection = cropper.getCropperSelection();
const cropperImage = cropper.getCropperImage();

cropperImage.$ready((image) => {
invokeDotNetMethodAsync(dotNetAdapter, "ImageReady");
});
cropperImageReady(cropperImage, cropperCanvas, instance);

registerEvents(cropperCanvas, cropperSelection);

Expand Down Expand Up @@ -106,9 +104,7 @@ export function updateOptions(element, elementId, options) {
cropperImage.src = options.source.value;

// Callback needs to be setup again after each source changed.
cropperImage.$ready((image) => {
invokeDotNetMethodAsync(instance.adapter, "ImageReady");
});
cropperImageReady(cropperImage, cropperCanvas, instance);
}

if (options.alt.changed) {
Expand Down Expand Up @@ -298,6 +294,26 @@ export function resetSelection(element, elementId) {
}
}

let disabledBeforeImageLoadFailed;
let wasDisabled;
tesar-tech marked this conversation as resolved.
Show resolved Hide resolved
function cropperImageReady(cropperImage, cropperCanvas, instance) {
cropperImage.$ready((image) => {
if(wasDisabled)
{
cropperCanvas.disabled = disabledBeforeImageLoadFailed;
wasDisabled =false;
}
invokeDotNetMethodAsync(instance.adapter, "ImageReady");
})
.catch((err) => {
invokeDotNetMethodAsync(instance.adapter, "ImageLoadingFailed", err.message);
disabledBeforeImageLoadFailed = cropperCanvas.disabled;
wasDisabled = true;
cropperCanvas.disabled = true;
});
}


function onCropperStartHandler(event) {
let parentElementId = event.srcElement.parentElement.id;

Expand Down
Loading