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

FilePicker and Markdown: Remove file entries on destroy and file change event #5989

Open
wants to merge 4 commits into
base: rel-1.7
Choose a base branch
from
Open
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
5 changes: 0 additions & 5 deletions Source/Blazorise/Components/FileEdit/FileEdit.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ public async Task NotifyChange( FileEntry[] files )

await Changed.InvokeAsync( new( files ) );

foreach ( var file in files )
{
await file.Owner.RemoveFileEntry( file.Id );
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't we need this now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This breaks the Upload operation of FilePicker.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't that be already done within the Changed event?
Still we need to call the RemoveFileEntry somewhere..

}

await InvokeAsync( StateHasChanged );
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Blazorise/Interfaces/Modules/IJSFileModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface IJSFileModule : IBaseJSModule
/// Removes file entry from js dictionary
/// </summary>
/// <param name="elementRef">Reference to the rendered element.</param>
/// <param name="fileEntryId"></param>
/// <param name="fileEntryId">File id.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
ValueTask RemoveFileEntry( ElementReference elementRef, int fileEntryId );
}
10 changes: 5 additions & 5 deletions Source/Blazorise/Utilities/IO/IFileEntryOwner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public interface IFileEntryOwner
/// <summary>
/// Removes the file entry from js dictionary.
/// </summary>
/// <param name="fileEntryId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
async Task RemoveFileEntry( int fileEntryId, CancellationToken cancellationToken = default )
/// <param name="fileEntry">Currently processed file entry.</param>
/// <param name="cancellationToken">A cancellation token to signal the cancellation of streaming file data.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
async Task RemoveFileEntry( FileEntry fileEntry, CancellationToken cancellationToken = default )
{
await JSFileModule.RemoveFileEntry( ElementRef, fileEntryId );
await JSFileModule.RemoveFileEntry( ElementRef, fileEntry.Id );
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private async Task<Stream> OpenReadStreamAsync( CancellationToken cancellationTo
jsStreamReference = await JSModule.ReadDataAsync( ElementRef, FileEntry.Id, cancellationToken );

return await jsStreamReference.OpenReadStreamAsync(
this.maxFileSize,
maxFileSize,
cancellationToken: cancellationToken );
}

Expand Down
17 changes: 14 additions & 3 deletions Source/Blazorise/wwwroot/fileEdit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getRequiredElement } from "./utilities.js?v=1.7.4.0";
import { removeAllFileEntries } from "./io.js?v=1.7.4.0";

const _instances = [];
let nextFileId = 0;
Expand Down Expand Up @@ -39,8 +40,17 @@ export function removeFile(element, elementId, fileId) {
}

export function destroy(element, elementId) {
var instances = _instances || {};
delete instances[elementId];
const instances = _instances || {};

const instance = instances[elementId];

if (instance) {
if (element) {
removeAllFileEntries(element);
}

delete instances[elementId];
}
}

export function reset(element, elementId) {
Expand Down Expand Up @@ -75,11 +85,12 @@ export function open(element, elementId) {

// Reduce to purely serializable data, plus build an index by ID
function mapElementFilesToFileEntries(element) {

if (!element._blazorFilesById) {
element._blazorFilesById = {};
}

removeAllFileEntries(element);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the same problem as it was with markdown.js . Now we can easily delete blob from js before it was processed in c#.
Or is there a blocking mechanism that disallow interaction with fileEdit until the previous operation is completed?


let fileList = Array.prototype.map.call(element.files, function (file) {
file.id = file.id ?? ++nextFileId;
var fileEntry = {
Expand Down
8 changes: 8 additions & 0 deletions Source/Blazorise/wwwroot/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export function removeFileEntry(element, fileEntryId) {
delete element._blazorFilesById[fileEntryId];
}

export function removeAllFileEntries(element) {
if (element && element._blazorFilesById) {
for (var fileId in element._blazorFilesById) {
removeFileEntry(element, fileId);
}
}
}

function getArrayBufferFromFileAsync(element, fileId) {
var file = getFileById(element, fileId);

Expand Down
2 changes: 1 addition & 1 deletion Source/Extensions/Blazorise.Markdown/Markdown.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public async Task NotifyImageUpload( FileEntry[] fileEntries )

foreach ( var file in fileEntries )
{
await file.Owner.RemoveFileEntry( file.Id );
await file.Owner.RemoveFileEntry( file );
}

await InvokeAsync( StateHasChanged );
Expand Down
5 changes: 5 additions & 0 deletions Source/Extensions/Blazorise.Markdown/wwwroot/markdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./vendors/easymde.js?v=1.7.4.0";
import "./vendors/highlight.js?v=1.7.4.0";
import { removeAllFileEntries } from "../Blazorise/io.js?v=1.7.4.0";

document.getElementsByTagName("head")[0].insertAdjacentHTML("beforeend", "<link rel=\"stylesheet\" href=\"_content/Blazorise.Markdown/vendors/easymde.css?v=1.7.4.0\" />");

Expand Down Expand Up @@ -191,6 +192,10 @@ export function destroy(element, elementId) {
const instance = instances[elementId];

if (instance) {
if (element) {
removeAllFileEntries(element);
}

instance.editor.toTextArea();
instance.editor = null;

Expand Down
Loading