Skip to content

Commit

Permalink
WaxImageInput added
Browse files Browse the repository at this point in the history
  • Loading branch information
lllggghhhaaa committed Feb 11, 2023
1 parent 8bac8eb commit 54d3097
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Nuget: https://www.nuget.org/packages/WaxComponents
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

<!-- Scripts -->
<script src="_content/WaxComponents/Scripts/waxComponents.js"></script>
```

- Importar a biblioteca
Expand Down
2 changes: 2 additions & 0 deletions WaxComponents.Tests/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
<WaxIcon>reply all</WaxIcon>
</WaxContainer>

<WaxImageInput OnUpload="(_, args) => Console.WriteLine(args.Url)"></WaxImageInput>

@code {

private string _text = String.Empty;
Expand Down
1 change: 1 addition & 0 deletions WaxComponents.Tests/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<a class="dismiss">🗙</a>
</div>

<script src="_content/WaxComponents/Scripts/waxComponents.js"></script>
<script src="_content/Blazor.Extensions.Canvas/blazor.extensions.canvas.js"></script>
<script src="_framework/blazor.webassembly.js"></script>

Expand Down
12 changes: 12 additions & 0 deletions WaxComponents/Utils/JsBinds.cs
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);
}
}
2 changes: 1 addition & 1 deletion WaxComponents/WaxComponents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>0.2.5</Version>
<Version>0.2.6</Version>
<Title>WaxComponents</Title>
<Authors>lllggghhhaaa</Authors>
<Description>Simple components for blazor, free and opensource</Description>
Expand Down
5 changes: 5 additions & 0 deletions WaxComponents/WaxImageInput.razor
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"/>
48 changes: 48 additions & 0 deletions WaxComponents/WaxImageInput.razor.cs
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);
}
}
6 changes: 6 additions & 0 deletions WaxComponents/WaxImageInput.razor.css
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;
}
127 changes: 127 additions & 0 deletions WaxComponents/wwwroot/Images/upload.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions WaxComponents/wwwroot/Scripts/waxComponents.js
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);
2 changes: 1 addition & 1 deletion WaxComponents/wwwroot/Styles/Themes/lightPink.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
--slider-thumb-color: #ffd3da;
--slider-thumb-color-hover: #ffb6c1;
--slider-fill-start: rgba(95, 195, 228, .35);
--slider-fill-end: rgba(229, 93, 135, .35)
--slider-fill-end: rgba(229, 93, 135, .35);
}

0 comments on commit 54d3097

Please sign in to comment.