Skip to content

Commit

Permalink
Fix Monaco and Trumbowyg editor settings (#15768)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Apr 16, 2024
1 parent 4e0f746 commit dc25168
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<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
Expand Up @@ -7,6 +7,7 @@
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Mvc.ModelBinding;
using OrchardCore.Mvc.Utilities;

namespace OrchardCore.ContentFields.Settings
Expand All @@ -15,9 +16,9 @@ public class HtmlFieldMonacoEditorSettingsDriver : ContentPartFieldDefinitionDis
{
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 @@ -40,19 +41,21 @@ public override async Task<IDisplayResult> UpdateAsync(ContentPartFieldDefinitio
if (partFieldDefinition.Editor() == "Monaco")
{
var model = new MonacoSettingsViewModel();
var settings = new HtmlFieldMonacoEditorSettings();

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

if (!model.Options.IsJson())
{
context.Updater.ModelState.AddModelError(Prefix + "." + nameof(MonacoSettingsViewModel.Options), S["The options are written in an incorrect format."]);
context.Updater.ModelState.AddModelError(Prefix, nameof(model.Options), S["The options are written in an incorrect format."]);
}
else
{
var jsonSettings = JObject.Parse(model.Options);
jsonSettings["language"] = "html";
settings.Options = jsonSettings.ToString();
var settings = new HtmlFieldMonacoEditorSettings
{
Options = jsonSettings.ToString()
};
context.Builder.WithSettings(settings);
}
}
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 @@ -35,21 +37,34 @@ public override async Task<IDisplayResult> UpdateAsync(ContentPartFieldDefinitio
if (partFieldDefinition.Editor() == "Trumbowyg")
{
var model = new TrumbowygSettingsViewModel();
var settings = new HtmlFieldTrumbowygEditorSettings();

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

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

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

var parser = new JavaScriptParser();

var optionsScript = parser.ParseScript("var config = " + options);

var settings = new HtmlFieldTrumbowygEditorSettings
{
InsertMediaWithUrl = model.InsertMediaWithUrl,
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Mvc.ModelBinding;
using OrchardCore.Mvc.Utilities;

namespace OrchardCore.ContentFields.Settings
Expand Down Expand Up @@ -39,17 +40,19 @@ public override async Task<IDisplayResult> UpdateAsync(ContentPartFieldDefinitio
if (partFieldDefinition.Editor() == "Monaco")
{
var model = new MonacoSettingsViewModel();
var settings = new TextFieldMonacoEditorSettings();

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

if (!model.Options.IsJson())
{
context.Updater.ModelState.AddModelError(Prefix + "." + nameof(MonacoSettingsViewModel.Options), S["The options are written in an incorrect format."]);
context.Updater.ModelState.AddModelError(Prefix, nameof(model.Options), S["The options are written in an incorrect format."]);
}
else
{
settings.Options = model.Options;
var settings = new TextFieldMonacoEditorSettings
{
Options = model.Options
};
context.Builder.WithSettings(settings);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
<Title>OrchardCore Html</Title>
<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
Expand Up @@ -8,6 +8,7 @@
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Html.Models;
using OrchardCore.Html.ViewModels;
using OrchardCore.Mvc.ModelBinding;
using OrchardCore.Mvc.Utilities;

namespace OrchardCore.Html.Settings
Expand All @@ -16,9 +17,9 @@ public class HtmlBodyPartMonacoEditorSettingsDriver : ContentTypePartDefinitionD
{
protected readonly IStringLocalizer S;

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

public override IDisplayResult Edit(ContentTypePartDefinition contentTypePartDefinition, IUpdateModel updater)
Expand All @@ -40,19 +41,21 @@ public override async Task<IDisplayResult> UpdateAsync(ContentTypePartDefinition
if (contentTypePartDefinition.Editor() == "Monaco")
{
var model = new MonacoSettingsViewModel();
var settings = new HtmlBodyPartMonacoEditorSettings();

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

if (!model.Options.IsJson())
{
context.Updater.ModelState.AddModelError(Prefix + "." + nameof(MonacoSettingsViewModel.Options), S["The options are written in an incorrect format."]);
context.Updater.ModelState.AddModelError(Prefix, nameof(model.Options), S["The options are written in an incorrect format."]);
}
else
{
var jsonSettings = JObject.Parse(model.Options);
jsonSettings["language"] = "html";
settings.Options = jsonSettings.ToString();
var settings = new HtmlBodyPartMonacoEditorSettings
{
Options = jsonSettings.ToString()
};
context.Builder.WithSettings(settings);
}
}
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 @@ -36,23 +37,37 @@ public override async Task<IDisplayResult> UpdateAsync(ContentTypePartDefinition
if (contentTypePartDefinition.Editor() == "Trumbowyg")
{
var model = new TrumbowygSettingsViewModel();
var settings = new HtmlBodyPartTrumbowygEditorSettings();

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

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

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

var parser = new JavaScriptParser();

var optionsScript = parser.ParseScript("var config = " + options);

var settings = new HtmlBodyPartTrumbowygEditorSettings
{
InsertMediaWithUrl = model.InsertMediaWithUrl,
Options = options
};

context.Builder.WithSettings(settings);
}
else
catch
{
settings.InsertMediaWithUrl = model.InsertMediaWithUrl;
settings.Options = model.Options;
context.Builder.WithSettings(settings);
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);
}
}
}

0 comments on commit dc25168

Please sign in to comment.