Skip to content

Commit

Permalink
feat: auto resource loading
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Dec 22, 2023
1 parent 7e5ef40 commit 9de22df
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"CKEditorBundle": {
"Theme": "Dark"
}
}
4 changes: 0 additions & 4 deletions apps/Sitko.Blazor.CKEditor.Demo/Components/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
<link rel="stylesheet" href="bootstrap/bootstrap.min.css"/>
<link rel="stylesheet" href="app.css"/>
<link rel="stylesheet" href="Sitko.Blazor.CKEditor.Demo.styles.css"/>
<link href="_content/Sitko.Blazor.CKEditor.Bundle/ckeditor.dark.css" rel="stylesheet"/>
<link rel="icon" type="image/png" href="favicon.png"/>
<HeadOutlet/>
</head>

<body>
<Routes/>
<script src="_content/Sitko.Blazor.CKEditor.Bundle/ckeditor.dark.js"></script>
<script src="_content/Sitko.Blazor.CKEditor.Bundle/translations/ru.js"></script>
<script src="_content/Sitko.Blazor.CKEditor/ckeditor.js"></script>
<script src="_framework/blazor.web.js"></script>
</body>

Expand Down
35 changes: 35 additions & 0 deletions src/Sitko.Blazor.CKEditor.Bundle/CKEditorBundleOptions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
namespace Sitko.Blazor.CKEditor.Bundle;

using System;
using System.Collections.Generic;
using JetBrains.Annotations;

[PublicAPI]
public class CKEditorBundleOptions : CKEditorOptions
{
public CKEditorTheme Theme { get; set; } = CKEditorTheme.Light;

public static CKEditorConfig DefaultConfig => new()
{
Toolbar = new CKEditorToolbar
Expand Down Expand Up @@ -41,5 +44,37 @@ public class CKEditorBundleOptions : CKEditorOptions
Language = "en"
};

public override string ScriptPath => Theme switch
{
CKEditorTheme.Light => $"{BasePath}/ckeditor.js",
CKEditorTheme.Dark => $"{BasePath}/ckeditor.dark.js",
_ => throw new ArgumentOutOfRangeException()
};

public override string StylePath => Theme switch
{
CKEditorTheme.Light => $"{BasePath}/ckeditor.css",
CKEditorTheme.Dark => $"{BasePath}/ckeditor.dark.css",
_ => throw new ArgumentOutOfRangeException()
};

public override string EditorClassName { get; set; } = "BlazorEditor";
private string BasePath => $"/_content/{typeof(CKEditorTheme).Assembly.GetName().Name}";

public override Dictionary<string, string> GetAdditionalScripts(CKEditorConfig? config)
{
var scripts = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(config?.Language) && config.Language != DefaultConfig.Language)
{
scripts.Add("BlazorEditorLang", $"{BasePath}/translations/{config.Language}.js");
}

return scripts;
}
}

public enum CKEditorTheme
{
Light,
Dark
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,81 @@
<None Include="..\..\LICENSE.md" Pack="true" PackagePath="LICENSE.md"/>
<None Include="..\..\packageIcon.png" Pack="true" PackagePath="\"/>
</ItemGroup>

<ItemGroup>
<_ContentIncludedByDefault Remove="wwwroot\translations\af.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ar.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ast.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\az.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\bg.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\bn.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\bs.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ca.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\cs.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\da.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\de-ch.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\de.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\el.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\en-au.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\en-gb.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\eo.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\es-co.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\es.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\et.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\eu.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\fa.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\fi.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\fr.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\gl.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\gu.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\he.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\hi.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\hr.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\hu.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\hy.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\id.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\it.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ja.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\jv.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\kk.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\km.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\kn.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ko.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ku.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\lt.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\lv.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ms.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\nb.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ne.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\nl.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\no.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\oc.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\pl.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\pt-br.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\pt.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ro.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ru.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\si.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\sk.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\sl.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\sq.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\sr-latn.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\sr.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\sv.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\th.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\tk.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\tr.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\tt.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ug.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\uk.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\ur.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\uz.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\vi.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\zh-cn.js" />
<_ContentIncludedByDefault Remove="wwwroot\translations\zh.js" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
</Project>
20 changes: 19 additions & 1 deletion src/Sitko.Blazor.CKEditor/BaseCKEditor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Sitko.Blazor.CKEditor;

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
Expand All @@ -10,6 +11,7 @@
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using ScriptInjector;

[PublicAPI]
public abstract class BaseCKEditorComponent : InputText, IAsyncDisposable
Expand All @@ -24,8 +26,10 @@ public abstract class BaseCKEditorComponent : InputText, IAsyncDisposable

private bool rendered;
protected string EditorValue { get; private set; } = "";
[Inject] protected ICKEditorLoader CKEditorLoader { get; set; } = null!;
[Inject] protected ICKEditorOptionsProvider OptionsProvider { get; set; } = null!;
[Inject] protected IJSRuntime JsRuntime { get; set; } = null!;
[Inject] protected IScriptInjector ScriptInjector { get; set; } = null!;
[Inject] protected ILogger<BaseCKEditorComponent> Logger { get; set; } = null!;
[Parameter] public string Placeholder { get; set; } = "Enter text";
[Parameter] public string Class { get; set; } = "";
Expand Down Expand Up @@ -62,7 +66,21 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
if (firstRender)
{
instance = DotNetObjectReference.Create(this);
await InitializeEditorAsync(CancellationToken.None);

await CKEditorLoader.LoadAsync();

var config = GetConfig();
var scripts = new List<InjectRequest>
{
CssInjectRequest.FromUrl($"{OptionsProvider.Options.EditorClassName}Css",
OptionsProvider.Options.StylePath)
};
foreach (var (key, path) in OptionsProvider.Options.GetAdditionalScripts(config))
{
scripts.Add(ScriptInjectRequest.FromUrl(key, path));
}

await ScriptInjector.InjectAsync(scripts, InitializeEditorAsync);
}
}

Expand Down
39 changes: 39 additions & 0 deletions src/Sitko.Blazor.CKEditor/CKEditorLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Sitko.Blazor.CKEditor;

using System.Collections.Generic;
using System.Threading.Tasks;
using ScriptInjector;

class CKEditorLoader : ICKEditorLoader
{
private TaskCompletionSource? loadTaskSource;
private readonly IScriptInjector scriptInjector;
private readonly ICKEditorOptionsProvider optionsProvider;

public CKEditorLoader(IScriptInjector scriptInjector, ICKEditorOptionsProvider optionsProvider)
{
this.scriptInjector = scriptInjector;
this.optionsProvider = optionsProvider;
}

public Task LoadAsync()
{
if (loadTaskSource is null)
{
loadTaskSource = new TaskCompletionSource();
var scripts = new List<InjectRequest>
{
ScriptInjectRequest.FromResource("BlazorCkEditor", GetType().Assembly, "ckeditor.js"),
ScriptInjectRequest.FromUrl(optionsProvider.Options.EditorClassName,
optionsProvider.Options.ScriptPath),
};
return scriptInjector.InjectAsync(scripts, _ =>
{
loadTaskSource.SetResult();
return Task.CompletedTask;
});
}

return loadTaskSource.Task;
}
}
4 changes: 4 additions & 0 deletions src/Sitko.Blazor.CKEditor/CKEditorOptions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
namespace Sitko.Blazor.CKEditor;

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using JetBrains.Annotations;

[PublicAPI]
public class CKEditorOptions
{
[Required] public virtual string ScriptPath { get; set; } = "";
[Required] public virtual string StylePath { get; set; } = "";
public virtual Dictionary<string, string> GetAdditionalScripts(CKEditorConfig? config) => new();
[Required] public virtual string EditorClassName { get; set; } = "";
public virtual CKEditorConfig? CKEditorConfig { get; set; }
}
8 changes: 8 additions & 0 deletions src/Sitko.Blazor.CKEditor/ICKEditorLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Sitko.Blazor.CKEditor;

using System.Threading.Tasks;

public interface ICKEditorLoader
{
Task LoadAsync();
}
3 changes: 3 additions & 0 deletions src/Sitko.Blazor.CKEditor/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using JetBrains.Annotations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ScriptInjector;

[PublicAPI]
public static class ServiceCollectionExtensions
Expand All @@ -20,7 +21,9 @@ public static class ServiceCollectionExtensions
builder.PostConfigure(postConfigure);
}

serviceCollection.AddScoped<ICKEditorLoader, CKEditorLoader>();
serviceCollection.AddTransient<ICKEditorOptionsProvider, CKEditorOptionsProvider<TOptions>>();
serviceCollection.AddScriptInjector();
builder.ValidateDataAnnotations();

return serviceCollection;
Expand Down
6 changes: 5 additions & 1 deletion src/Sitko.Blazor.CKEditor/Sitko.Blazor.CKEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.0"/>
<PackageReference Include="Sitko.Blazor.ScriptInjector" Version="3.0.2"/>
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE.md" Pack="true" PackagePath="LICENSE.md"/>
<None Include="..\..\packageIcon.png" Pack="true" PackagePath="\"/>
<None Include="wwwroot\ckeditor.js" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="ckeditor.js" />
</ItemGroup>
</Project>
File renamed without changes.

0 comments on commit 9de22df

Please sign in to comment.