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

Notes/RenderMacro - added Deploy Connectors #225

Merged
merged 1 commit into from
Jun 6, 2022
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 @@ -160,7 +160,7 @@ public IEnumerable<DataListItem> GetItems(Dictionary<string, object> config)

public object ConvertValue(Type type, string value)
{
return UdiParser.TryParse(value, out var udi) == true
return UdiParser.TryParse(value, out Udi udi) == true
? _umbracoContextAccessor.GetRequiredUmbracoContext().Content.GetById(udi)
: default;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* Copyright © 2022 Lee Kelleher.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System.Collections.Generic;
#if NET472
using Umbraco.Core;
using Umbraco.Core.Deploy;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using UmbConstants = Umbraco.Core.Constants;
#else
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Deploy;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Extensions;
using UmbConstants = Umbraco.Cms.Core.Constants;
#endif

namespace Umbraco.Community.Contentment.DataEditors
{
internal sealed class EditorNotesConfigurationConnector : IDataTypeConfigurationConnector
{
#if NET472 == false
private readonly IConfigurationEditorJsonSerializer _configurationEditorJsonSerializer;
#endif
private readonly ILocalLinkParser _localLinkParser;
private readonly IImageSourceParser _imageSourceParser;
private readonly IMacroParser _macroParser;

public IEnumerable<string> PropertyEditorAliases => new[] { EditorNotesDataEditor.DataEditorName };

public EditorNotesConfigurationConnector(
#if NET472 == false
IConfigurationEditorJsonSerializer configurationEditorJsonSerializer,
#endif
ILocalLinkParser localLinkParser,
IImageSourceParser imageSourceParser,
IMacroParser macroParser)
{
#if NET472 == false
_configurationEditorJsonSerializer = configurationEditorJsonSerializer;
#endif
_localLinkParser = localLinkParser;
_imageSourceParser = imageSourceParser;
_macroParser = macroParser;
}

public object FromArtifact(IDataType dataType, string configuration)
{
var dataTypeConfigurationEditor = dataType.Editor.GetConfigurationEditor();
#if NET472
var db = dataTypeConfigurationEditor.FromDatabase(configuration);
#else
var db = dataTypeConfigurationEditor.FromDatabase(configuration, _configurationEditorJsonSerializer);
#endif
if (db is Dictionary<string, object> config &&
config.TryGetValueAs(EditorNotesConfigurationEditor.Message, out string notes) == true &&
string.IsNullOrWhiteSpace(notes) == false)
{
notes = _localLinkParser.FromArtifact(notes);
notes = _imageSourceParser.FromArtifact(notes);
notes = _macroParser.FromArtifact(notes);

config[EditorNotesConfigurationEditor.Message] = notes;

return config;
}

return db;
}

public string ToArtifact(IDataType dataType, ICollection<ArtifactDependency> dependencies)
{
if (dataType.Configuration is Dictionary<string, object> config &&
config.TryGetValueAs(EditorNotesConfigurationEditor.Message, out string notes) == true &&
string.IsNullOrWhiteSpace(notes) == false)
{
var udis = new List<Udi>();

notes = _localLinkParser.ToArtifact(notes, udis);
notes = _imageSourceParser.ToArtifact(notes, udis);
notes = _macroParser.ToArtifact(notes, udis);

foreach (var udi in udis)
{
var mode = udi.EntityType == UmbConstants.UdiEntityType.Macro
? ArtifactDependencyMode.Match
: ArtifactDependencyMode.Exist;

dependencies.Add(new ArtifactDependency(udi, false, mode));
}

config[EditorNotesConfigurationEditor.Message] = notes;
}

#if NET472
return ConfigurationEditor.ToDatabase(dataType.Configuration);
#else
return ConfigurationEditor.ToDatabase(dataType.Configuration, _configurationEditorJsonSerializer);
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Umbraco.Community.Contentment.DataEditors
{
internal sealed class EditorNotesConfigurationEditor : ConfigurationEditor
{
internal const string Message = "message";

public EditorNotesConfigurationEditor(IIOHelper ioHelper)
: base()
{
Expand Down Expand Up @@ -62,8 +64,8 @@ public EditorNotesConfigurationEditor(IIOHelper ioHelper)

Fields.Add(new ConfigurationField
{
Key = "message",
Name = "Message",
Key = Message,
Name = nameof(Message),
Description = "Enter the notes to be displayed for the content editor.",
View = ioHelper.ResolveRelativeOrVirtualUrl("~/umbraco/views/propertyeditors/rte/rte.html"),
Config = new Dictionary<string, object>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* Copyright © 2022 Lee Kelleher.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System.Collections.Generic;
#if NET472
using Umbraco.Core;
using Umbraco.Core.Deploy;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using UmbConstants = Umbraco.Core.Constants;
#else
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Deploy;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Extensions;
using UmbConstants = Umbraco.Cms.Core.Constants;
#endif

namespace Umbraco.Community.Contentment.DataEditors
{
internal sealed class NotesConfigurationConnector : IDataTypeConfigurationConnector
{
#if NET472 == false
private readonly IConfigurationEditorJsonSerializer _configurationEditorJsonSerializer;
#endif
private readonly ILocalLinkParser _localLinkParser;
private readonly IImageSourceParser _imageSourceParser;
private readonly IMacroParser _macroParser;

public IEnumerable<string> PropertyEditorAliases => new[] { NotesDataEditor.DataEditorName };

public NotesConfigurationConnector(
#if NET472 == false
IConfigurationEditorJsonSerializer configurationEditorJsonSerializer,
#endif
ILocalLinkParser localLinkParser,
IImageSourceParser imageSourceParser,
IMacroParser macroParser)
{
#if NET472 == false
_configurationEditorJsonSerializer = configurationEditorJsonSerializer;
#endif
_localLinkParser = localLinkParser;
_imageSourceParser = imageSourceParser;
_macroParser = macroParser;
}

public object FromArtifact(IDataType dataType, string configuration)
{
var dataTypeConfigurationEditor = dataType.Editor.GetConfigurationEditor();
#if NET472
var db = dataTypeConfigurationEditor.FromDatabase(configuration);
#else
var db = dataTypeConfigurationEditor.FromDatabase(configuration, _configurationEditorJsonSerializer);
#endif

if (db is Dictionary<string, object> config &&
config.TryGetValueAs(NotesConfigurationField.Notes, out string notes) == true &&
string.IsNullOrWhiteSpace(notes) == false)
{
notes = _localLinkParser.FromArtifact(notes);
notes = _imageSourceParser.FromArtifact(notes);
notes = _macroParser.FromArtifact(notes);

config[NotesConfigurationField.Notes] = notes;

return config;
}

return db;
}

public string ToArtifact(IDataType dataType, ICollection<ArtifactDependency> dependencies)
{
if (dataType.Configuration is Dictionary<string, object> config &&
config.TryGetValueAs(NotesConfigurationField.Notes, out string notes) == true &&
string.IsNullOrWhiteSpace(notes) == false)
{
var udis = new List<Udi>();

notes = _localLinkParser.ToArtifact(notes, udis);
notes = _imageSourceParser.ToArtifact(notes, udis);
notes = _macroParser.ToArtifact(notes, udis);

foreach (var udi in udis)
{
var mode = udi.EntityType == UmbConstants.UdiEntityType.Macro
? ArtifactDependencyMode.Match
: ArtifactDependencyMode.Exist;

dependencies.Add(new ArtifactDependency(udi, false, mode));
}

config[NotesConfigurationField.Notes] = notes;
}

#if NET472
return ConfigurationEditor.ToDatabase(dataType.Configuration);
#else
return ConfigurationEditor.ToDatabase(dataType.Configuration, _configurationEditorJsonSerializer);
#endif
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Copyright © 2022 Lee Kelleher.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System.Collections.Generic;
using Newtonsoft.Json.Linq;
#if NET472
using Umbraco.Core;
using Umbraco.Core.Deploy;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
#else
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Deploy;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Extensions;
#endif

namespace Umbraco.Community.Contentment.DataEditors
{
internal sealed class RenderMacroConfigurationConnector : IDataTypeConfigurationConnector
{
public IEnumerable<string> PropertyEditorAliases => new[] { RenderMacroDataEditor.DataEditorName };

#if NET472 == false
private readonly IConfigurationEditorJsonSerializer _configurationEditorJsonSerializer;

public RenderMacroConfigurationConnector(IConfigurationEditorJsonSerializer configurationEditorJsonSerializer)
{
_configurationEditorJsonSerializer = configurationEditorJsonSerializer;
}
#endif

public object FromArtifact(IDataType dataType, string configuration)
{
var dataTypeConfigurationEditor = dataType.Editor.GetConfigurationEditor();

#if NET472
return dataTypeConfigurationEditor.FromDatabase(configuration);
#else
return dataTypeConfigurationEditor.FromDatabase(configuration, _configurationEditorJsonSerializer);
#endif
}

public string ToArtifact(IDataType dataType, ICollection<ArtifactDependency> dependencies)
{
if (dataType.Configuration is Dictionary<string, object> config &&
config.TryGetValueAs(RenderMacroConfigurationEditor.Macro, out JArray array) == true &&
array.Count > 0 &&
array[0] is JObject obj &&
obj.ContainsKey("udi") == true &&
UdiParser.TryParse(obj.Value<string>("udi"), out Udi udi) == true)
{
dependencies.Add(new ArtifactDependency(udi, false, ArtifactDependencyMode.Match));
}

#if NET472
return ConfigurationEditor.ToDatabase(dataType.Configuration);
#else
return ConfigurationEditor.ToDatabase(dataType.Configuration, _configurationEditorJsonSerializer);
#endif
}
}
}
5 changes: 5 additions & 0 deletions src/Umbraco.Community.Contentment/Polyfill/UdiParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace Umbraco.Core
[EditorBrowsable(EditorBrowsableState.Never)]
internal sealed class UdiParser
{
public static bool TryParse(string s, out Udi udi)
{
return Udi.TryParse(s, out udi);
}

public static bool TryParse(string s, out GuidUdi udi)
{
return GuidUdi.TryParse(s, out udi);
Expand Down