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

Javascript location customization #16

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
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.Streams;

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

namespace KristofferStrube.Blazor.Streams;

public static class IServiceCollectionExtensions
{
public static IServiceCollection AddStreams(this IServiceCollection serviceCollection, Action<StreamsOptions>? configure)
{
serviceCollection.ConfigureStreamOptions(configure);
return serviceCollection;
}

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

services.Configure(configure);
configure(StreamsOptions.DefaultInstance);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using KristofferStrube.Blazor.DOM;
using KristofferStrube.Blazor.DOM.Abort;
using System.Text.Json.Serialization;

namespace KristofferStrube.Blazor.Streams;
Expand Down
18 changes: 18 additions & 0 deletions src/KristofferStrube.Blazor.Streams/Options/StreamsOptions.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 StreamsOptions
{
public const string DefaultBasePath = "./_content/";
public static readonly string DefaultNamespace = Assembly.GetExecutingAssembly().GetName().Name ?? "KristofferStrube.Blazor.Streams";
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 StreamsOptions DefaultInstance = new();

}
2 changes: 1 addition & 1 deletion src/KristofferStrube.Blazor.Streams/Options/Transformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static Task<Transformer> CreateAsync(IJSRuntime jSRuntime)
/// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param>
protected Transformer(IJSRuntime jSRuntime)
{
helperTask = new(jSRuntime.GetHelperAsync);
helperTask = new(() => jSRuntime.GetHelperAsync());
this.jSRuntime = jSRuntime;
ObjRef = DotNetObjectReference.Create(this);
}
Expand Down