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

"Not a function" when calling setDragImage with Drag & Drop #33158

Closed
rogihee opened this issue May 31, 2021 · 4 comments
Closed

"Not a function" when calling setDragImage with Drag & Drop #33158

rogihee opened this issue May 31, 2021 · 4 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@rogihee
Copy link
Contributor

rogihee commented May 31, 2021

I have a div that I want to give a custom draggable image (which can be an image or any element):
https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/setDragImage

I have a div I want to drag:

<div class="my-dragging-div"
     draggable="true"
     @ref="@DragElement"
     ondragover="event.preventDefault();"
     @ondragstart="@OnDragStart"
     @ondragend="@OnDragEnd">
</div>

The callback is:

private async Task OnDragStart(DragEventArgs args)
{
    await JSRuntime.InvokeVoidAsync("setDragImage", args, DragElement, 0, 0);
}

This JavaScript function is:

setDragImage(event: DragEvent, element: HTMLElement, xOffset: number = 0, yOffset: number = 0): void {
        event.dataTransfer.setDragImage(element, xOffset, yOffset);
}

When calling the DataTransfer.setDragImage JavaScript function via JSRuntime via a @ondragstart Blazor callback, and passing a reference to the DragEventArgs I get an error:

Microsoft.JSInterop.JSException: event.dataTransfer.setDragImage is not a function

Apparantly, when the DragEventArgs is serialized / deserialized the function pointers are lost.

Please note that on Windows drag image elements with a width over 300px get a gradient overlay that is quite ugly depending on your underlying div and your personal feelings for aesthetics :-). Thus, for our solution we ended up with a completely custom implementation with a cloned ghost element that is following the mouse position. With a custom javascript event subscription we clear the dragImage:

clearDragImage(event: DragEvent): void {
        const img: HTMLImageElement = new Image();
        img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=";
        event.dataTransfer.setDragImage(img, 0, 0);
 };

Perhaps this information is useful when working on issue #18754.

Further technical details

  • ASP.NET Core version: 5.0.6
@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label May 31, 2021
@javiercn
Copy link
Member

javiercn commented May 31, 2021

@rogihee thanks for contacting us.

DragEventArgs is not a 1:1 map with the argument from the event. In other words, the event is being copied to .NET and copied back to JS and the object you are receiving in the function is not the same the event is raising.

I would suggest using one of the available drag and drop implementations by the community.

@javiercn javiercn added ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question labels May 31, 2021
@ghost ghost added the Status: Resolved label May 31, 2021
@rogihee
Copy link
Contributor Author

rogihee commented May 31, 2021

This is not covered by one of the community implementations AFAIK such as https://github.com/Postlagerkarte/blazor-dragdrop.

Please note that I see this as an issue rather than a question, I now have to have a double implementation for the Blazor @ondragstart and my custom event subscription in OnAfterRender. It would be nice if Blazor would solve this a better way.

@javiercn
Copy link
Member

@rogihee we have a work item for supporting drag and drop already here

In addition to that, you can potentially solve this with custom events. See more here

@ghost
Copy link

ghost commented Jun 1, 2021

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed Jun 1, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jul 1, 2021
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

2 participants