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

Changed drag to store files on registered element, and added example … #126

Merged
merged 1 commit into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 6 additions & 11 deletions src/Blazor.FileReader/script/FileReaderComponent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
declare var Blazor: IBlazor;
declare var DotNet: IDotNet;
declare const Blazor: IBlazor;

interface IBlazor {
platform: IBlazorPlatform;
Expand Down Expand Up @@ -40,32 +39,28 @@ interface IDotNetBuffer {
toUint8Array(): Uint8Array;
}

interface IDotNet {
invokeMethodAsync<T>(assemblyName: string, methodIdentifier: string, ...args: any[]): Promise<T>
}

class FileReaderComponent {

private newFileStreamReference: number = 0;
private newFileStreamReference = 0;
private readonly fileStreams: { [reference: number]: { file: File, arrayBuffer: ArrayBuffer } } = {};
private readonly dragElements: Map<HTMLElement, EventListenerOrEventListenerObject> = new Map();
private readonly elementDataTransfers: Map<HTMLElement, FileList> = new Map();

public RegisterDropEvents = (element: HTMLElement, additive : boolean): boolean => {
public RegisterDropEvents = (element: HTMLElement, additive: boolean): boolean => {

const handler = (ev: DragEvent) => {
this.PreventDefaultHandler(ev);
if (ev.target instanceof HTMLElement) {
let list = ev.dataTransfer.files;

if (additive) {
const existing = this.elementDataTransfers.get(ev.target);
if (existing != null && existing.length > 0) {
const existing = this.elementDataTransfers.get(element);
if (existing !== undefined && existing.length > 0) {
list = new FileReaderComponent.ConcatFileList(existing, list);
}
}

this.elementDataTransfers.set(ev.target, list);
this.elementDataTransfers.set(element, list);
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/Blazor.FileReader/wwwroot/FileReaderComponent.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Blazor.FileReader/wwwroot/FileReaderComponent.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/Demo/Blazor.FileReader.Demo.Common/DragnDropCommon.razor
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ This demo reads files that was dropped in without doing anything particular with
@ondragenter=OnDragEnter
@ondragleave=OnDragLeave>

Drop Files here.
Drop Files here or on this image.
<img src="animated-icon.svg" width="40" height="36" title="You can drop files on this child element as well." />


@foreach (var fileInfo in DropFileList)
Expand All @@ -59,7 +60,13 @@ This demo reads files that was dropped in without doing anything particular with
<button @onclick="ClearDrop" class="btn btn-primary">Clear</button>
<br /><br />
<div class="@IpDropClass">
<input type="file" class="clickable" @ref=dropTargetInput @onchange="OnInputChange" @ondragenter=OnIpDragEnter @ondragleave=OnIpDragLeave multiple />
<input type="file"
class="clickable"
@ref=dropTargetInput
@onchange="OnInputChange"
@ondragenter=OnIpDragEnter
@ondragleave=OnIpDragLeave
multiple />
Drop Files here or click me.

@foreach (var fileInfo in IpFileList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Blazor.Server" Version="3.2.0-preview1.20073.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.3" />
</ItemGroup>

<ItemGroup>
Expand Down