Skip to content

Commit

Permalink
✨ javascript isolation for facybox.js
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Oct 22, 2023
1 parent 957a047 commit bc4fc30
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 40 deletions.
8 changes: 7 additions & 1 deletion src/Blazor.Server.UI/Pages/Products/Products.razor
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
@attribute [Authorize(Policy = Permissions.Products.View)]
<PageTitle>@Title</PageTitle>
<style>
@@import url("https://cdn.jsdelivr.net/npm/@@fancyapps/[email protected]/dist/fancybox/fancybox.css");
.fancybox__container {
--fancybox-bg: rgba(24, 24, 27, 0.85);
z-index: 9999;
}
.mud-table-toolbar {
height: 120px !important;
}
</style>

<h1 class="color">test</h1>
<AdvancedSearchProductsComponent TRequest="Query" OnConditionChanged="ConditionChanged"></AdvancedSearchProductsComponent>
<MudDataGrid ServerData="@(ServerReload)"
FixedHeader="true"
Expand Down
3 changes: 2 additions & 1 deletion src/Blazor.Server.UI/Pages/Products/_ProductFormDialog.razor
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@using Blazor.Server.UI.Services.JsInterop;
@using CleanArchitecture.Blazor.Application.Features.Products.Commands.AddEdit
@using ResizeMode = SixLabors.ImageSharp.Processing.ResizeMode
@using Severity = MudBlazor.Severity
Expand Down Expand Up @@ -150,7 +151,7 @@

private async Task PreviewImage(string url, IEnumerable<ProductImage> images)
{
await JS.InvokeVoidAsync("previewImage", url, images.Select(x => x.Url).ToArray());
await new Fancybox(JS).Preview(url, images);
}

private async Task UploadFiles(InputFileChangeEventArgs e)
Expand Down
43 changes: 5 additions & 38 deletions src/Blazor.Server.UI/Pages/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
<link href="_content/CodeBeam.MudBlazor.Extensions/MudExtensions.min.css" rel="stylesheet" />
<link href="_content/Blazor-ApexCharts/css/apexcharts.css" rel="stylesheet" />
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@@fancyapps/[email protected]/dist/fancybox/fancybox.css" />

<link href="Blazor.Server.UI.styles.css" rel="stylesheet">
<style>
.fancybox__container {
--fancybox-bg: rgba(24, 24, 27, 0.85);
z-index: 9999;
}
.mud-input {
font-size: var(--mud-typography-default-size);
}
Expand Down Expand Up @@ -111,7 +109,7 @@
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/d3-flextree.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@@fancyapps/[email protected]/dist/fancybox/fancybox.umd.js"></script>


<script type="text/javascript">
Expand Down Expand Up @@ -235,37 +233,6 @@
}, 50);
}
function previewImage(url, gallery) {
if (url ==null) return;
console.log(url, gallery);
if (isImageUrl(url)) {
let images = [];
if (gallery != null) {
images = gallery.filter(l => isImageUrl(l)).map(x => ({ src: x, caption: x.split("/").pop() }));
} else {
images = [{ src: url, caption: url.split("/").pop() }];
}
const fancybox = new Fancybox(images);
} else {
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
}
}
function isImageUrl(url) {
const imageExtensions = /\.(gif|jpe?g|tiff?|png|webp|bmp)$/i;
return imageExtensions.test(url);
}
function isValidUrl(str) {
if (str == null) {
return false;
}
const urlRegex =/^([a-zA-Z]:)?(\\[a-zA-Z0-9\s_@@-~]+)+\.[a-zA-Z]{2,6}$/;
return urlRegex.test(str);
}
</script>

</body>
Expand Down
1 change: 1 addition & 0 deletions src/Blazor.Server.UI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

builder.RegisterSerilog();
builder.AddBlazorUiServices();
builder.WebHost.UseStaticWebAssets();
builder.Services.AddInfrastructureServices(builder.Configuration)
.AddApplicationServices();

Expand Down
19 changes: 19 additions & 0 deletions src/Blazor.Server.UI/Services/JsInterop/Fancybox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Security.Policy;
using Microsoft.JSInterop;

namespace Blazor.Server.UI.Services.JsInterop;

public sealed class Fancybox
{
private readonly IJSRuntime _jsRuntime;

public Fancybox(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}
public async Task<ValueTask> Preview(string defaultUrl, IEnumerable<ProductImage> images)
{
var jsmodule =await _jsRuntime.InvokeAsync<IJSObjectReference>("import", "/js/fancybox.js");
return jsmodule.InvokeVoidAsync("previewImage", defaultUrl, images.Select(x => x.Url).ToArray());
}
}
24 changes: 24 additions & 0 deletions src/Blazor.Server.UI/wwwroot/js/fancybox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Fancybox } from "https://cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/fancybox/fancybox.esm.js";
export function previewImage(url, gallery) {

if (url == null) return;
if (isImageUrl(url)) {
let images = [];
if (gallery != null) {
images = gallery.filter(l => isImageUrl(l)).map(x => ({ src: x, caption: x.split("/").pop() }));
} else {
images = [{ src: url, caption: url.split("/").pop() }];
}
const fancybox = new Fancybox(images);
} else {
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
}
}
function isImageUrl(url) {
const imageExtensions = /\.(gif|jpe?g|tiff?|png|webp|bmp)$/i;
return imageExtensions.test(url);
}

0 comments on commit bc4fc30

Please sign in to comment.