-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8bac8eb
commit 54d3097
Showing
11 changed files
with
216 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.JSInterop; | ||
|
||
namespace WaxComponents.Utils; | ||
|
||
public static class JsBinds | ||
{ | ||
public static async void SetImage(this IJSRuntime runtime, Stream stream, string elementId) | ||
{ | ||
var dotnetStream = new DotNetStreamReference(stream); | ||
await runtime.InvokeVoidAsync("setImage", elementId, dotnetStream); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@using Microsoft.JSInterop | ||
@inject IJSRuntime JSRuntime | ||
|
||
<input @ref="_inputRef" type="file" accept="image/*" onchange="@FileUploaded" style="display:none;"/> | ||
<img @ref="_imgRef" class="waxComponent waxImageInput" src="@DefaultImageUrl" width="@Width" height="@Height" alt="@Alt" role="button" onclick="@Click"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.JSInterop; | ||
|
||
namespace WaxComponents; | ||
|
||
public partial class WaxImageInput : IDisposable | ||
{ | ||
[Parameter] public string Width { get; set; } = "128px"; | ||
|
||
[Parameter] public string Height { get; set; } = "128px"; | ||
[Parameter] public string Alt { get; set; } = String.Empty; | ||
|
||
[Parameter] public string DefaultImageUrl { get; set; } = "_content/WaxComponents/Images/upload.svg"; | ||
|
||
[Parameter] public EventHandler<WaxFileUploadEventArgs>? OnUpload { get; set; } | ||
|
||
public string? Url { get; private set; } | ||
|
||
private ElementReference _inputRef; | ||
private ElementReference _imgRef; | ||
|
||
public async void Click() => await JSRuntime.InvokeVoidAsync("inputClick", _inputRef); | ||
|
||
private async void FileUploaded(ChangeEventArgs args) | ||
{ | ||
Url = await JSRuntime.InvokeAsync<string>("setImage", _inputRef, _imgRef); | ||
OnUpload?.Invoke(this, new WaxFileUploadEventArgs(Url)); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (Url is not null) | ||
JSRuntime.InvokeVoidAsync("disposeObjectURL", Url); | ||
} | ||
} | ||
|
||
public class WaxFileUploadEventArgs : EventArgs | ||
{ | ||
public readonly string Url; | ||
|
||
public WaxFileUploadEventArgs(string url) => Url = url; | ||
|
||
public async Task<Stream> DownloadFileAsync() | ||
{ | ||
using HttpClient client = new HttpClient(); | ||
return await client.GetStreamAsync(Url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.waxImageInput { | ||
border: 1px solid white; | ||
border-image: var(--component-bg) 1; | ||
|
||
object-fit: scale-down; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
window.setImage = async (input, image) => { | ||
const url = URL.createObjectURL(input.files[0]); | ||
image.src = url; | ||
|
||
return url; | ||
} | ||
|
||
window.inputClick = async element => element.click(); | ||
|
||
window.disposeObjectURL = async url => URL.revokeObjectURL(url); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters