Skip to content

Javascript location customization #3

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

Open
wants to merge 1 commit into
base: main
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
2 changes: 1 addition & 1 deletion src/KristofferStrube.Blazor.FileAPI/BaseJSWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class BaseJSWrapper : IAsyncDisposable
/// <param name="jSReference">A JS reference to an existing JS instance that should be wrapped.</param>
internal BaseJSWrapper(IJSRuntime jSRuntime, IJSObjectReference jSReference)
{
helperTask = new(jSRuntime.GetHelperAsync);
helperTask = new(() => jSRuntime.GetHelperAsync());
JSReference = jSReference;
this.jSRuntime = jSRuntime;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using Microsoft.JSInterop;
using KristofferStrube.Blazor.FileAPI.Options;
using Microsoft.JSInterop;

namespace KristofferStrube.Blazor.FileAPI;

internal static class IJSRuntimeExtensions
{
internal static async Task<IJSObjectReference> GetHelperAsync(this IJSRuntime jSRuntime)
internal static async Task<IJSObjectReference> GetHelperAsync(this IJSRuntime jSRuntime, FileApiOptions? options = null)
{
options ??= FileApiOptions.DefaultInstance;
return await jSRuntime.InvokeAsync<IJSObjectReference>(
"import", "./_content/KristofferStrube.Blazor.FileAPI/KristofferStrube.Blazor.FileAPI.js");
"import", options.FullScriptPath);
}
internal static async Task<IJSInProcessObjectReference> GetInProcessHelperAsync(this IJSRuntime jSRuntime)
internal static async Task<IJSInProcessObjectReference> GetInProcessHelperAsync(this IJSRuntime jSRuntime, FileApiOptions? options = null)
{
options ??= FileApiOptions.DefaultInstance;
return await jSRuntime.InvokeAsync<IJSInProcessObjectReference>(
"import", "./_content/KristofferStrube.Blazor.FileAPI/KristofferStrube.Blazor.FileAPI.js");
"import", options.FullScriptPath);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
using KristofferStrube.Blazor.FileAPI.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;

namespace KristofferStrube.Blazor.FileAPI;

public static class IServiceCollectionExtensions
{
public static IServiceCollection AddURLService(this IServiceCollection serviceCollection)
public static IServiceCollection AddURLService(this IServiceCollection serviceCollection, Action<FileApiOptions>? configure)
{
serviceCollection.ConfigureFaOptions(configure);
return serviceCollection.AddScoped<IURLService, URLService>();
}

public static IServiceCollection AddURLServiceInProcess(this IServiceCollection serviceCollection)
public static IServiceCollection AddURLServiceInProcess(this IServiceCollection serviceCollection, Action<FileApiOptions>? configure)
{
serviceCollection.ConfigureFaOptions(configure);
return serviceCollection.AddScoped<IURLServiceInProcess>(sp => new URLServiceInProcess((IJSInProcessRuntime)sp.GetRequiredService<IJSRuntime>()));
}
}

private static void ConfigureFaOptions(this IServiceCollection services, Action<FileApiOptions>? configure)
{
if (configure is null) return;

services.Configure(configure);
configure(FileApiOptions.DefaultInstance);
}
}
18 changes: 18 additions & 0 deletions src/KristofferStrube.Blazor.FileAPI/Options/FileApiOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Reflection;

namespace KristofferStrube.Blazor.FileAPI.Options;

public class FileApiOptions()
{
public const string DefaultBasePath = "./_content/";
public static readonly string DefaultNamespace = Assembly.GetExecutingAssembly().GetName().Name ?? "KristofferStrube.Blazor.FileAPI";
public static readonly string DefaultScriptPath = $"{DefaultNamespace}/{DefaultNamespace}.js";

public string BasePath { get; set; } = DefaultBasePath;
public string ScriptPath { get; set; } = DefaultScriptPath;

public string FullScriptPath => Path.Combine(this.BasePath, this.ScriptPath);

internal static FileApiOptions DefaultInstance = new();

}