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 all 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 @@ -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);
}
}
}