-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
643 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace Sitko.EditorJS.Blocks; | ||
|
||
internal class BlocksAccessor(IEnumerable<IContentBlockAccessor> blockAccessors) : IBlocksAccessor | ||
{ | ||
private readonly IContentBlockAccessor[] blockAccessors = blockAccessors.ToArray(); | ||
|
||
public EditorJSConfig GetConfig(string holder) | ||
{ | ||
var config = new EditorJSConfig { Holder = holder }; | ||
foreach (var blockOptionsAccessor in blockAccessors) | ||
{ | ||
config.Tools[blockOptionsAccessor.Key] = blockOptionsAccessor.Options.GetConfig(); | ||
} | ||
|
||
return config; | ||
} | ||
|
||
public IReadOnlyDictionary<string, string> GetScripts() => | ||
blockAccessors.ToDictionary(accessor => accessor.Key, accessor => accessor.Options.ScriptUrl); | ||
|
||
public IReadOnlyDictionary<string, Type> GetBlockTypes() => | ||
blockAccessors.ToDictionary(block => block.Key, block => block.Type); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Text.Json.Serialization; | ||
using Sitko.EditorJS.Json; | ||
|
||
namespace Sitko.EditorJS.Blocks; | ||
|
||
[JsonConverter(typeof(ContentBlockConverter))] | ||
public record ContentBlock : IContentBlock | ||
{ | ||
[JsonPropertyName("id")] public required string Id { get; init; } = ""; | ||
} | ||
|
||
public record ContentBlock<TData> | ||
: ContentBlock where TData : ContentBlockData, new() | ||
{ | ||
[JsonPropertyName("data")] public required TData Data { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Sitko.EditorJS.Blocks; | ||
|
||
public class ContentBlockAccessor<TBlock, TBlockOptions>(IOptions<TBlockOptions> options) : IContentBlockAccessor | ||
where TBlock : ContentBlock | ||
where TBlockOptions : class, IContentBlockOptions<TBlock> | ||
{ | ||
public Type Type => typeof(TBlock); | ||
public IContentBlockOptions Options { get; } = options.Value; | ||
public string Key { get; } = ContentBlocksRegistry.GetKey<TBlock>().Key; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Sitko.EditorJS.Blocks; | ||
|
||
[AttributeUsage(AttributeTargets.Class)] | ||
public class ContentBlockAttribute(string key) : Attribute | ||
{ | ||
public string Key { get; } = key; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Sitko.EditorJS.Blocks; | ||
|
||
public record ContentBlockConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Sitko.EditorJS.Blocks; | ||
|
||
public abstract record ContentBlockData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Sitko.EditorJS.Blocks; | ||
|
||
public abstract record ContentBlockOptions<TBlock, TConfig> : IContentBlockOptions<TBlock, TConfig> | ||
where TBlock : ContentBlock | ||
where TConfig : ContentBlockConfig, new() | ||
{ | ||
public abstract string ScriptUrl { get; set; } | ||
public abstract string ClassName { get; set; } | ||
public EditorJSToolConfig GetConfig() => new() { ClassName = ClassName, Config = Config }; | ||
|
||
public TConfig Config { get; } = new(); | ||
} | ||
|
||
public abstract record ContentBlockOptions<TBlock> : ContentBlockOptions<TBlock, ContentBlockConfig> | ||
where TBlock : ContentBlock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Sitko.EditorJS.Blocks; | ||
|
||
internal record ContentBlockRegistration(string Key, Type BlockType, Type OptionsType); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
namespace Sitko.EditorJS.Blocks; | ||
|
||
public static class ContentBlocksRegistry | ||
{ | ||
private static readonly Dictionary<string, ContentBlockRegistration> BlocksByKey = new(); | ||
private static readonly Dictionary<Type, ContentBlockRegistration> BlocksByType = new(); | ||
|
||
|
||
public static IReadOnlyDictionary<string, Type> GetBlockTypes() => | ||
BlocksByType.ToDictionary(block => block.Value.Key, block => block.Key); | ||
|
||
public static void Register<TBlock, TBlockOptions>() where TBlock : ContentBlock | ||
where TBlockOptions : IContentBlockOptions<TBlock> | ||
{ | ||
var attribute = typeof(TBlock).GetCustomAttributes(typeof(ContentBlockAttribute), false) | ||
.OfType<ContentBlockAttribute>().FirstOrDefault() ?? | ||
throw new InvalidOperationException( | ||
$"Class {typeof(TBlock)} should have {typeof(ContentBlockAttribute)} attribute"); | ||
if (BlocksByType.ContainsKey(typeof(TBlock))) | ||
{ | ||
throw new InvalidOperationException($"Block {typeof(TBlock)} already registered"); | ||
} | ||
|
||
if (BlocksByKey.TryGetValue(attribute.Key, out var blockRegistration)) | ||
{ | ||
throw new InvalidOperationException( | ||
$"Block with key {attribute.Key} already registered: {blockRegistration.BlockType}"); | ||
} | ||
|
||
var registration = new ContentBlockRegistration(attribute.Key, typeof(TBlock), typeof(TBlockOptions)); | ||
BlocksByType[typeof(TBlock)] = registration; | ||
BlocksByKey[attribute.Key] = registration; | ||
//PolymorphismOptions.DerivedTypes.Add(new JsonDerivedType(typeof(TBlock), attribute.Key)); | ||
} | ||
|
||
internal static ContentBlockRegistration GetKey<TBlock>() where TBlock : ContentBlock => GetKey(typeof(TBlock)); | ||
internal static ContentBlockRegistration GetKey(Type blockType) => BlocksByType[blockType]; | ||
|
||
internal static ContentBlockRegistration GetBlockMetadata(string key) => BlocksByKey[key]; | ||
|
||
internal static void Clear() | ||
{ | ||
BlocksByType.Clear(); | ||
BlocksByKey.Clear(); | ||
} | ||
} | ||
|
||
// TODO: In .NET 9 we can use this with https://github.com/dotnet/runtime/issues/72604 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Sitko.EditorJS.Blocks; | ||
|
||
public interface IBlocksAccessor | ||
{ | ||
EditorJSConfig GetConfig(string holder); | ||
IReadOnlyDictionary<string, string> GetScripts(); | ||
IReadOnlyDictionary<string, Type> GetBlockTypes(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Sitko.EditorJS.Blocks; | ||
|
||
internal interface IContentBlock | ||
{ | ||
string Id { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Sitko.EditorJS.Blocks; | ||
|
||
internal interface IContentBlockAccessor | ||
{ | ||
public Type Type { get; } | ||
public IContentBlockOptions Options { get; } | ||
public string Key { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Sitko.EditorJS.Blocks; | ||
|
||
public interface IContentBlockOptions | ||
{ | ||
string ScriptUrl { get; } | ||
EditorJSToolConfig GetConfig(); | ||
} | ||
|
||
public interface IContentBlockOptions<TBlock> : IContentBlockOptions where TBlock : ContentBlock; | ||
|
||
public interface IContentBlockOptions<TBlock, out TConfig> : IContentBlockOptions<TBlock> | ||
where TConfig : ContentBlockConfig, new() where TBlock : ContentBlock | ||
{ | ||
TConfig Config { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Sitko.EditorJS.Blocks.Paragraph; | ||
|
||
[ContentBlock("paragraph")] | ||
public record ParagraphBlock : ContentBlock<ParagraphBlockData>; |
10 changes: 10 additions & 0 deletions
10
src/Sitko.EditorJS/Blocks/Paragraph/ParagraphBlockConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Sitko.EditorJS.Blocks.Paragraph; | ||
|
||
public record ParagraphBlockConfig : ContentBlockConfig | ||
{ | ||
[JsonPropertyName("placeholder")] public string Placeholder { get; set; } = ""; | ||
|
||
[JsonPropertyName("preserveBlank")] public bool PreserveBlank { get; set; } = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Sitko.EditorJS.Blocks.Paragraph; | ||
|
||
public record ParagraphBlockData : ContentBlockData | ||
{ | ||
[JsonPropertyName("text")] public string Text { get; set; } = ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Sitko.EditorJS.Blocks.Paragraph; | ||
|
||
public record ParagraphBlockOptions : ContentBlockOptions<ParagraphBlock, ParagraphBlockConfig> | ||
{ | ||
public override string ScriptUrl { get; set; } = "https://cdn.jsdelivr.net/npm/@editorjs/paragraph@latest"; | ||
public override string ClassName { get; set; } = "Paragraph"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Sitko.EditorJS.Blocks.SimpleImage; | ||
|
||
[ContentBlock("image")] | ||
public record SimpleImageBlock : ContentBlock<SimpleImageBlockData>; |
16 changes: 16 additions & 0 deletions
16
src/Sitko.EditorJS/Blocks/SimpleImage/SimpleImageBlockData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Sitko.EditorJS.Blocks.SimpleImage; | ||
|
||
public record SimpleImageBlockData : ContentBlockData | ||
{ | ||
[JsonPropertyName("url")] public string Url { get; set; } = ""; | ||
|
||
[JsonPropertyName("caption")] public string Caption { get; set; } = ""; | ||
|
||
[JsonPropertyName("withBorder")] public bool? WithBorder { get; set; } | ||
|
||
[JsonPropertyName("withBackground")] public bool? WithBackground { get; set; } | ||
|
||
[JsonPropertyName("stretched")] public bool? Stretched { get; set; } | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Sitko.EditorJS/Blocks/SimpleImage/SimpleImageBlockOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Sitko.EditorJS.Blocks.SimpleImage; | ||
|
||
public record SimpleImageBlockOptions : ContentBlockOptions<SimpleImageBlock> | ||
{ | ||
public override string ScriptUrl { get; set; } = "https://cdn.jsdelivr.net/npm/@editorjs/simple-image@latest"; | ||
public override string ClassName { get; set; } = "SimpleImage"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Scrutor; | ||
using Sitko.Blazor.ScriptInjector; | ||
using Sitko.EditorJS.Blocks; | ||
|
||
namespace Sitko.EditorJS.Configuration; | ||
|
||
internal class EditorJSBuilder : IEditorJSBuilder | ||
{ | ||
private readonly IServiceCollection serviceCollection; | ||
|
||
public EditorJSBuilder(IServiceCollection serviceCollection) | ||
{ | ||
this.serviceCollection = serviceCollection; | ||
serviceCollection.AddOptions<EditorJSOptions>(); | ||
serviceCollection.AddScriptInjector(); | ||
serviceCollection.AddSingleton<IBlocksAccessor, BlocksAccessor>(); | ||
} | ||
|
||
public IEditorJSBuilder AddBlock<TBlock, TBlockOptions>(Action<IConfiguration, TBlockOptions> configure) | ||
where TBlock : ContentBlock where TBlockOptions : class, IContentBlockOptions<TBlock> | ||
{ | ||
serviceCollection.Scan(selector => | ||
selector.FromType<TBlock>().AsSelfWithInterfaces().WithScopedLifetime()); | ||
serviceCollection.AddOptions<TBlockOptions>().PostConfigure<IConfiguration>((options, | ||
configuration) => | ||
{ | ||
configure(configuration, options); | ||
}); | ||
serviceCollection.AddSingleton<IContentBlockAccessor, ContentBlockAccessor<TBlock, TBlockOptions>>(); | ||
ContentBlocksRegistry.Register<TBlock, TBlockOptions>(); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Sitko.EditorJS.Configuration; | ||
|
||
public record EditorJSOptions | ||
{ | ||
public string EditorJSScriptUrl { get; set; } = "https://cdn.jsdelivr.net/npm/@editorjs/editorjs@latest"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Sitko.EditorJS.Blocks; | ||
|
||
namespace Sitko.EditorJS.Configuration; | ||
|
||
public interface IEditorJSBuilder | ||
{ | ||
IEditorJSBuilder AddBlock<TBlock, TBlockOptions>(Action<IConfiguration, TBlockOptions> configure) | ||
where TBlock : ContentBlock where TBlockOptions : class, IContentBlockOptions<TBlock>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Text.Json.Serialization; | ||
using Sitko.EditorJS.Blocks; | ||
using Sitko.EditorJS.Helpers; | ||
|
||
namespace Sitko.EditorJS.Data; | ||
|
||
public record EditorJSData | ||
{ | ||
[JsonPropertyName("time")] public long Time { get; set; } | ||
[JsonPropertyName("version")] public string Version { get; set; } = "unknown"; | ||
[JsonPropertyName("blocks")] public ValueCollection<ContentBlock> Blocks { get; set; } = new(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.