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

Additive dragn drop #92

Merged
merged 5 commits into from
Sep 30, 2019
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
4 changes: 2 additions & 2 deletions src/Blazor.FileReader/FileReaderJsInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public FileReaderJsInterop(IJSRuntime jsRuntime, IFileReaderServiceOptions optio
_needsInitialization = options.InitializeOnFirstCall;
}

public async Task<bool> RegisterDropEvents(ElementReference elementReference)
public async Task<bool> RegisterDropEvents(ElementReference elementReference, bool additive)
{
await EnsureInitializedAsync();
return await CurrentJSRuntime.InvokeAsync<bool>($"FileReaderComponent.RegisterDropEvents", elementReference);
return await CurrentJSRuntime.InvokeAsync<bool>($"FileReaderComponent.RegisterDropEvents", elementReference, additive);
}

public async Task<bool> UnregisterDropEvents(ElementReference elementReference)
Expand Down
5 changes: 3 additions & 2 deletions src/Blazor.FileReader/FileReaderRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public interface IFileReaderRef
/// <summary>
/// Register for drop events on the source element
/// </summary>
/// <param name="additive">If set to true, drop target file list becomes additive. Defaults to false.</param>
/// <returns></returns>
Task RegisterDropEventsAsync();
Task RegisterDropEventsAsync(bool additive = false);

/// <summary>
/// Unregister drop events on the source element
Expand Down Expand Up @@ -141,7 +142,7 @@ public async Task<IEnumerable<IFileReference>> EnumerateFilesAsync() =>
Enumerable.Range(0, Math.Max(0, await this.FileReaderJsInterop.GetFileCount(this.ElementRef)))
.Select(index => (IFileReference)new FileReference(this, index));

public async Task RegisterDropEventsAsync() => await this.FileReaderJsInterop.RegisterDropEvents(this.ElementRef);
public async Task RegisterDropEventsAsync(bool additive) => await this.FileReaderJsInterop.RegisterDropEvents(this.ElementRef, additive);
public async Task UnregisterDropEventsAsync() => await this.FileReaderJsInterop.UnregisterDropEvents(this.ElementRef);

public async Task ClearValue()
Expand Down
56 changes: 51 additions & 5 deletions src/Blazor.FileReader/script/FileReaderComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,27 @@ class FileReaderComponent {
private readonly namespace = "Blazor.FileReader";
private readonly className = "FileReaderJsInterop";


private newFileStreamReference: number = 0;
private readonly fileStreams: { [reference: number]: File } = {};
private readonly dragElements: Map<HTMLElement, EventListenerOrEventListenerObject> = new Map();
private readonly elementDataTransfers: Map<HTMLElement, FileList> = new Map();
private static getStreamBuffer: MethodHandle;
private static readFileUnmarshalledAsyncCallback: MethodHandle;

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

const handler = (ev: DragEvent) => {

this.PreventDefaultHandler(ev);
if (ev.target instanceof HTMLElement) {
this.elementDataTransfers.set(ev.target, ev.dataTransfer.files);
let list = ev.dataTransfer.files;

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

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

Expand Down Expand Up @@ -244,6 +250,46 @@ class FileReaderComponent {
private PreventDefaultHandler = (ev: DragEvent) => {
ev.preventDefault();
}

static ConcatFileList = class implements FileList {
[index: number]: File;

length: number;

item(index: number): File {
return this[index];
}

constructor(existing: FileList, additions: FileList) {
for (let i = 0; i < existing.length; i++) {
this[i] = existing[i];
}

var eligebleAdditions = [];

// Check for doubles
for (let i = 0; i < additions.length; i++) {
let exists = false;
let addition = additions[i];
for (let j = 0; j < existing.length; j++) {
if (existing[j] === addition) {
exists = true;
break;
}
}

if (!exists) {
eligebleAdditions[eligebleAdditions.length] = addition;
}
}

for (let i = 0; i < eligebleAdditions.length; i++) {
this[i + existing.length] = eligebleAdditions[i];
}

this.length = existing.length + eligebleAdditions.length;
}
}
}

(window as any).FileReaderComponent = new FileReaderComponent();
40 changes: 38 additions & 2 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.

Loading