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

Fix Monaco and Trumbowyg editor settings #15768

Merged
merged 6 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -4,16 +4,22 @@
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<!-- NuGet properties-->
<Title>OrchardCore Content Fields</Title>
<Description>$(OCCMSDescription)
<Description>
$(OCCMSDescription)

Provides features to add content fields and indexing them for a content type.</Description>
Provides features to add content fields and indexing them for a content type.
</Description>
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
<PackageTags>$(PackageTags) OrchardCoreCMS ContentManagement</PackageTags>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Jint" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Admin.Abstractions\OrchardCore.Admin.Abstractions.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.ContentLocalization.Abstractions\OrchardCore.ContentLocalization.Abstractions.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Jint;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentFields.ViewModels;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Mvc.Utilities;
using OrchardCore.Mvc.ModelBinding;

namespace OrchardCore.ContentFields.Settings
{
public class HtmlFieldMonacoEditorSettingsDriver : ContentPartFieldDefinitionDisplayDriver<HtmlField>
{
protected readonly IStringLocalizer S;

public HtmlFieldMonacoEditorSettingsDriver(IStringLocalizer<HtmlFieldMonacoEditorSettingsDriver> localizer)
public HtmlFieldMonacoEditorSettingsDriver(IStringLocalizer<HtmlFieldMonacoEditorSettingsDriver> stringLocalizer)
{
S = localizer;
S = stringLocalizer;
}

public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefinition)
Expand All @@ -44,16 +45,27 @@ public override async Task<IDisplayResult> UpdateAsync(ContentPartFieldDefinitio

await context.Updater.TryUpdateModelAsync(model, Prefix);

if (!model.Options.IsJson())
try
{
context.Updater.ModelState.AddModelError(Prefix + "." + nameof(MonacoSettingsViewModel.Options), S["The options are written in an incorrect format."]);
var options = model.Options.Trim();

if (!options.StartsWith('{') || !options.EndsWith('}'))
{
throw new Exception();
}

var engine = new Engine()
.Execute("var config = " + options + "; config.language = 'html';");
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

var jsValue = engine.Evaluate("JSON.stringify(config, null, 4)");

settings.Options = jsValue.AsString();

context.Builder.WithSettings(settings);
}
else
catch
{
var jsonSettings = JObject.Parse(model.Options);
jsonSettings["language"] = "html";
settings.Options = jsonSettings.ToString();
context.Builder.WithSettings(settings);
context.Updater.ModelState.AddModelError(Prefix, nameof(model.Options), S["The options are written in an incorrect format."]);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
using System;
using System.Threading.Tasks;
using Esprima;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentFields.ViewModels;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Mvc.Utilities;
using OrchardCore.Mvc.ModelBinding;

namespace OrchardCore.ContentFields.Settings
{
public class HtmlFieldTrumbowygEditorSettingsDriver : ContentPartFieldDefinitionDisplayDriver<HtmlField>
{
protected readonly IStringLocalizer S;

public HtmlFieldTrumbowygEditorSettingsDriver(IStringLocalizer<HtmlFieldTrumbowygEditorSettingsDriver> localizer)
public HtmlFieldTrumbowygEditorSettingsDriver(IStringLocalizer<HtmlFieldTrumbowygEditorSettingsDriver> stringLocalizer)
{
S = localizer;
S = stringLocalizer;
}

public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefinition)
Expand All @@ -39,17 +41,27 @@ public override async Task<IDisplayResult> UpdateAsync(ContentPartFieldDefinitio

await context.Updater.TryUpdateModelAsync(model, Prefix);

if (!model.Options.IsJson())
{
context.Updater.ModelState.AddModelError(Prefix + '.' + nameof(TrumbowygSettingsViewModel.Options), S["The options are written in an incorrect format."]);
}
else
try
{
var options = model.Options.Trim();

if (!options.StartsWith('{') || !options.EndsWith('}'))
{
throw new Exception();
}

var parser = new JavaScriptParser();

var optionsScript = parser.ParseScript("var config = " + options);
settings.InsertMediaWithUrl = model.InsertMediaWithUrl;
settings.Options = model.Options;
settings.Options = options;

context.Builder.WithSettings(settings);
}
catch
{
context.Updater.ModelState.AddModelError(Prefix, nameof(model.Options), S["The options are written in an incorrect format."]);
}
}

return Edit(partFieldDefinition);
Expand Down
10 changes: 8 additions & 2 deletions src/OrchardCore.Modules/OrchardCore.Html/OrchardCore.Html.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<!-- NuGet properties-->
<Title>OrchardCore Html</Title>
<Description>$(OCCMSDescription)
<Description>
$(OCCMSDescription)

The Html module enables content items to have rich content using Html syntax."</Description>
The Html module enables content items to have rich content using Html syntax."
</Description>
<PackageTags>$(PackageTags) OrchardCoreCMS</PackageTags>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Jint" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Admin.Abstractions\OrchardCore.Admin.Abstractions.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Apis.GraphQL.Abstractions\OrchardCore.Apis.GraphQL.Abstractions.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Jint;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Html.Models;
using OrchardCore.Html.ViewModels;
using OrchardCore.Mvc.Utilities;
using OrchardCore.Mvc.ModelBinding;

namespace OrchardCore.Html.Settings
{
Expand Down Expand Up @@ -44,16 +46,27 @@ public override async Task<IDisplayResult> UpdateAsync(ContentTypePartDefinition

await context.Updater.TryUpdateModelAsync(model, Prefix);

if (!model.Options.IsJson())
try
{
context.Updater.ModelState.AddModelError(Prefix + "." + nameof(MonacoSettingsViewModel.Options), S["The options are written in an incorrect format."]);
var options = model.Options.Trim();

if (!options.StartsWith('{') || !options.EndsWith('}'))
{
throw new Exception();
}

var engine = new Engine()
.Execute("var config = " + options + "; config.language = 'html';");

var jsValue = engine.Evaluate("JSON.stringify(config, null, 4)");

settings.Options = jsValue.AsString();

context.Builder.WithSettings(settings);
}
else
catch
{
var jsonSettings = JObject.Parse(model.Options);
jsonSettings["language"] = "html";
settings.Options = jsonSettings.ToString();
context.Builder.WithSettings(settings);
context.Updater.ModelState.AddModelError(Prefix, nameof(model.Options), S["The options are written in an incorrect format."]);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
using System;
using System.Threading.Tasks;
using Esprima;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Html.Models;
using OrchardCore.Html.ViewModels;
using OrchardCore.Mvc.Utilities;
using OrchardCore.Mvc.ModelBinding;

namespace OrchardCore.Html.Settings
{
public class HtmlBodyPartTrumbowygEditorSettingsDriver : ContentTypePartDefinitionDisplayDriver<HtmlBodyPart>
{
protected readonly IStringLocalizer S;

public HtmlBodyPartTrumbowygEditorSettingsDriver(IStringLocalizer<HtmlBodyPartTrumbowygEditorSettingsDriver> localizer)
public HtmlBodyPartTrumbowygEditorSettingsDriver(IStringLocalizer<HtmlBodyPartTrumbowygEditorSettingsDriver> stringLocalizer)
{
S = localizer;
S = stringLocalizer;
}

public override IDisplayResult Edit(ContentTypePartDefinition contentTypePartDefinition, IUpdateModel updater)
public override IDisplayResult Edit(ContentTypePartDefinition contentTypePartDefinition)
{
return Initialize<TrumbowygSettingsViewModel>("HtmlBodyPartTrumbowygSettings_Edit", model =>
{
Expand All @@ -40,19 +41,30 @@ public override async Task<IDisplayResult> UpdateAsync(ContentTypePartDefinition

await context.Updater.TryUpdateModelAsync(model, Prefix);

if (!model.Options.IsJson())
{
context.Updater.ModelState.AddModelError(Prefix + "." + nameof(TrumbowygSettingsViewModel.Options), S["The options are written in an incorrect format."]);
}
else
try
{
var options = model.Options.Trim();

if (!options.StartsWith('{') || !options.EndsWith('}'))
{
throw new Exception();
}

var parser = new JavaScriptParser();

var optionsScript = parser.ParseScript("var config = " + options);
settings.InsertMediaWithUrl = model.InsertMediaWithUrl;
settings.Options = model.Options;
settings.Options = options;

context.Builder.WithSettings(settings);
}
catch
{
context.Updater.ModelState.AddModelError(Prefix, nameof(model.Options), S["The options are written in an incorrect format."]);
}
}

return Edit(contentTypePartDefinition, context.Updater);
return Edit(contentTypePartDefinition);
}
}
}