diff --git a/apps/Sitko.Blockly.Data/Entities/Post.cs b/apps/Sitko.Blockly.Data/Entities/Post.cs index 49158ce..84c0f89 100644 --- a/apps/Sitko.Blockly.Data/Entities/Post.cs +++ b/apps/Sitko.Blockly.Data/Entities/Post.cs @@ -1,5 +1,5 @@ -using System.ComponentModel.DataAnnotations; -using Sitko.Core.Repository; +using Sitko.Core.Repository; +using Sitko.EditorJS.Data; namespace Sitko.Blockly.Data.Entities; @@ -16,4 +16,5 @@ public record Post : BaseEntityRecord public string Title { get; set; } = ""; public List Blocks { get; set; } = new(); public List SecondaryBlocks { get; set; } = new(); + public EditorJSData EditorJSBlocks { get; set; } = new(); } diff --git a/apps/Sitko.Blockly.Data/Sitko.Blockly.Data.csproj b/apps/Sitko.Blockly.Data/Sitko.Blockly.Data.csproj index 42afac8..031bb93 100644 --- a/apps/Sitko.Blockly.Data/Sitko.Blockly.Data.csproj +++ b/apps/Sitko.Blockly.Data/Sitko.Blockly.Data.csproj @@ -12,5 +12,6 @@ + diff --git a/apps/Sitko.Blockly.Demo.Client/Pages/PostFormComponent.razor b/apps/Sitko.Blockly.Demo.Client/Pages/PostFormComponent.razor index 43eb3df..792e098 100644 --- a/apps/Sitko.Blockly.Demo.Client/Pages/PostFormComponent.razor +++ b/apps/Sitko.Blockly.Demo.Client/Pages/PostFormComponent.razor @@ -7,7 +7,7 @@ @* *@ @* *@ - + Save Reset diff --git a/apps/Sitko.Blockly.Demo/Data/BlocklyContext.cs b/apps/Sitko.Blockly.Demo/Data/BlocklyContext.cs index 37ab9f9..7c695cb 100644 --- a/apps/Sitko.Blockly.Demo/Data/BlocklyContext.cs +++ b/apps/Sitko.Blockly.Demo/Data/BlocklyContext.cs @@ -17,5 +17,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) base.OnModelCreating(modelBuilder); modelBuilder.RegisterBlocklyConversion(post => post.Blocks, nameof(Post.Blocks)); modelBuilder.RegisterBlocklyConversion(post => post.SecondaryBlocks, nameof(Post.SecondaryBlocks)); + modelBuilder.Entity(post => + { + post.Property(property => property.EditorJSBlocks) + .IsRequired() + .HasColumnType("jsonb") + .HasDefaultValueSql("'{}'"); + }); } } diff --git a/apps/Sitko.Blockly.Demo/Data/Migrations/20240523063901_AddEditorJSBlocks.Designer.cs b/apps/Sitko.Blockly.Demo/Data/Migrations/20240523063901_AddEditorJSBlocks.Designer.cs new file mode 100644 index 0000000..bb2c933 --- /dev/null +++ b/apps/Sitko.Blockly.Demo/Data/Migrations/20240523063901_AddEditorJSBlocks.Designer.cs @@ -0,0 +1,72 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using Sitko.Blockly.Demo.Data; +using Sitko.EditorJS.Data; + +#nullable disable + +namespace Sitko.Blockly.Demo.Migrations +{ + [DbContext(typeof(BlocklyContext))] + [Migration("20240523063901_AddEditorJSBlocks")] + partial class AddEditorJSBlocks + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Sitko.Blockly.Data.Entities.Post", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Blocks") + .IsRequired() + .ValueGeneratedOnAdd() + .HasColumnType("jsonb") + .HasColumnName("Blocks") + .HasDefaultValueSql("'[]'"); + + b.Property("DateAdded") + .HasColumnType("timestamp with time zone"); + + b.Property("DateUpdated") + .HasColumnType("timestamp with time zone"); + + b.Property("EditorJSBlocks") + .IsRequired() + .ValueGeneratedOnAdd() + .HasColumnType("jsonb") + .HasDefaultValueSql("'{}'"); + + b.Property("SecondaryBlocks") + .IsRequired() + .ValueGeneratedOnAdd() + .HasColumnType("jsonb") + .HasColumnName("SecondaryBlocks") + .HasDefaultValueSql("'[]'"); + + b.Property("Title") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Posts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/apps/Sitko.Blockly.Demo/Data/Migrations/20240523063901_AddEditorJSBlocks.cs b/apps/Sitko.Blockly.Demo/Data/Migrations/20240523063901_AddEditorJSBlocks.cs new file mode 100644 index 0000000..e9b3e66 --- /dev/null +++ b/apps/Sitko.Blockly.Demo/Data/Migrations/20240523063901_AddEditorJSBlocks.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Sitko.EditorJS.Data; + +#nullable disable + +namespace Sitko.Blockly.Demo.Migrations +{ + /// + public partial class AddEditorJSBlocks : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "EditorJSBlocks", + table: "Posts", + type: "jsonb", + nullable: false, + defaultValueSql: "'{}'"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "EditorJSBlocks", + table: "Posts"); + } + } +} diff --git a/apps/Sitko.Blockly.Demo/Data/Migrations/BlocklyContextModelSnapshot.cs b/apps/Sitko.Blockly.Demo/Data/Migrations/BlocklyContextModelSnapshot.cs index 5e2e47e..54cd925 100644 --- a/apps/Sitko.Blockly.Demo/Data/Migrations/BlocklyContextModelSnapshot.cs +++ b/apps/Sitko.Blockly.Demo/Data/Migrations/BlocklyContextModelSnapshot.cs @@ -5,6 +5,9 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; using Sitko.Blockly.Demo.Data; +using Sitko.EditorJS.Data; + +#nullable disable namespace Sitko.Blockly.Demo.Migrations { @@ -15,11 +18,12 @@ protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("Relational:MaxIdentifierLength", 63) - .HasAnnotation("ProductVersion", "5.0.7") - .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); - modelBuilder.Entity("Sitko.Blockly.Demo.Data.Entities.Post", b => + modelBuilder.Entity("Sitko.Blockly.Data.Entities.Post", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -38,6 +42,12 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("DateUpdated") .HasColumnType("timestamp with time zone"); + b.Property("EditorJSBlocks") + .IsRequired() + .ValueGeneratedOnAdd() + .HasColumnType("jsonb") + .HasDefaultValueSql("'{}'"); + b.Property("SecondaryBlocks") .IsRequired() .ValueGeneratedOnAdd() diff --git a/src/Sitko.EditorJS.Blazor/EditorJS.razor b/src/Sitko.EditorJS.Blazor/EditorJS.razor index ebac1e1..5a6bf33 100644 --- a/src/Sitko.EditorJS.Blazor/EditorJS.razor +++ b/src/Sitko.EditorJS.Blazor/EditorJS.razor @@ -1,10 +1,11 @@ -@using System.Text.Json +@inherits Microsoft.AspNetCore.Components.Forms.InputBase +@using System.Text.Json
-@if (Data is not null) +@if (CurrentValue is not null) {
-@JsonSerializer.Serialize(Data, PrettyPrintJsonOptions)
+@JsonSerializer.Serialize(CurrentValue, PrettyPrintJsonOptions)
 
} diff --git a/src/Sitko.EditorJS.Blazor/EditorJS.razor.cs b/src/Sitko.EditorJS.Blazor/EditorJS.razor.cs index 65ef1ec..396b825 100644 --- a/src/Sitko.EditorJS.Blazor/EditorJS.razor.cs +++ b/src/Sitko.EditorJS.Blazor/EditorJS.razor.cs @@ -11,7 +11,7 @@ namespace Sitko.EditorJS.Blazor; -public partial class EditorJS : ComponentBase, IAsyncDisposable +public partial class EditorJS : InputBase, IAsyncDisposable { private static readonly JsonSerializerOptions PrettyPrintJsonOptions = new() { WriteIndented = true }; @@ -25,19 +25,6 @@ public partial class EditorJS : ComponentBase, IAsyncDisposable [Parameter] public string? Config { get; set; } - private EditorJSData? Data { get; set; } = new() - { - Time = DateTime.UtcNow.Ticks, - Version = "somever", - Blocks = - [ - new ParagraphBlock - { - Id = Guid.NewGuid().ToString(), Data = new ParagraphBlockData { Text = "Мой клёвый текст" } - } - ] - }; - public Guid Id { get; } = Guid.NewGuid(); public ValueTask DisposeAsync() @@ -81,7 +68,7 @@ await ScriptInjector.InjectAsync(ScriptInjectRequest.Inline(Id.ToString(), $$""" """), async _ => { await JsRuntime.InvokeVoidAsync("window.SitkoEditorJS.init", cancellationToken, Id.ToString(), - instance, Data); + instance, CurrentValue); rendered = true; }, cancellationToken); @@ -96,11 +83,19 @@ private ValueTask DestroyEditor() [JSInvokable] public Task OnSave(EditorJSData data) { - Data = data; + CurrentValue = data; StateHasChanged(); return Task.CompletedTask; } + protected override bool TryParseValueFromString(string? value, out EditorJSData result, + out string validationErrorMessage) + { + result = default!; + validationErrorMessage = ""; + return false; + } + // private async ValueTask UpdateEditorAsync() => // await JsRuntime.InvokeVoidAsync("window.SitkoBlazorCKEditor.update", Id, EditorValue); }