Skip to content

Commit

Permalink
Merge branch 'v8/dev' into v8/contrib
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Umbraco.Web.UI.Client/src/views/propertyeditors/nestedcontent/nestedcontent.controller.js
#	src/Umbraco.Web.UI.Client/test/config/karma.conf.js
  • Loading branch information
nul800sebastiaan committed Sep 9, 2020
2 parents 5faa9ae + 0662284 commit 1f44b94
Show file tree
Hide file tree
Showing 36 changed files with 1,105 additions and 317 deletions.
9 changes: 9 additions & 0 deletions src/Umbraco.Core/ContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ internal static bool HasChildren(IContentBase content, ServiceContext services)
return false;
}

/// <summary>
/// Returns all properties based on the editorAlias
/// </summary>
/// <param name="content"></param>
/// <param name="editorAlias"></param>
/// <returns></returns>
public static IEnumerable<Property> GetPropertiesByEditor(this IContentBase content, string editorAlias)
=> content.Properties.Where(x => x.PropertyType.PropertyEditorAlias == editorAlias);

/// <summary>
/// Returns properties that do not belong to a group
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions src/Umbraco.Core/Models/Blocks/BlockEditorDataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,35 @@ protected BlockEditorDataConverter(string propertyEditorAlias)
_propertyEditorAlias = propertyEditorAlias;
}

public BlockEditorData ConvertFrom(JToken json)
{
var value = json.ToObject<BlockValue>();
return Convert(value);
}

public bool TryDeserialize(string json, out BlockEditorData blockEditorData)
{
try
{
var value = JsonConvert.DeserializeObject<BlockValue>(json);
blockEditorData = Convert(value);
return true;
}
catch (System.Exception)
{
blockEditorData = null;
return false;
}
}

public BlockEditorData Deserialize(string json)
{
var value = JsonConvert.DeserializeObject<BlockValue>(json);
return Convert(value);
}

private BlockEditorData Convert(BlockValue value)
{
if (value.Layout == null)
return BlockEditorData.Empty;

Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Models/Blocks/BlockListLayoutItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class BlockListLayoutItem
[JsonConverter(typeof(UdiJsonConverter))]
public Udi ContentUdi { get; set; }

[JsonProperty("settingsUdi")]
[JsonProperty("settingsUdi", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(UdiJsonConverter))]
public Udi SettingsUdi { get; set; }
}
Expand Down
1 change: 1 addition & 0 deletions src/Umbraco.Core/Models/PropertyCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Umbraco.Core.Models
{

/// <summary>
/// Represents a collection of property values.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;

namespace Umbraco.Core.PropertyEditors
{
/// <summary>
/// Utility class for dealing with <see cref="ContentService"/> Copying/Saving events for complex editors
/// </summary>
internal class ComplexPropertyEditorContentEventHandler : IDisposable
{
private readonly string _editorAlias;
private readonly Func<string, bool, string> _formatPropertyValue;
private bool _disposedValue;

public ComplexPropertyEditorContentEventHandler(string editorAlias,
Func<string, bool, string> formatPropertyValue)
{
_editorAlias = editorAlias;
_formatPropertyValue = formatPropertyValue;
ContentService.Copying += ContentService_Copying;
ContentService.Saving += ContentService_Saving;
}

/// <summary>
/// <see cref="ContentService"/> Copying event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ContentService_Copying(IContentService sender, CopyEventArgs<IContent> e)
{
var props = e.Copy.GetPropertiesByEditor(_editorAlias);
UpdatePropertyValues(props, false);
}

/// <summary>
/// <see cref="ContentService"/> Saving event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ContentService_Saving(IContentService sender, ContentSavingEventArgs e)
{
foreach (var entity in e.SavedEntities)
{
var props = entity.GetPropertiesByEditor(_editorAlias);
UpdatePropertyValues(props, true);
}
}

private void UpdatePropertyValues(IEnumerable<Property> props, bool onlyMissingKeys)
{
foreach (var prop in props)
{
// A Property may have one or more values due to cultures
var propVals = prop.Values;
foreach (var cultureVal in propVals)
{
// Remove keys from published value & any nested properties
var updatedPublishedVal = _formatPropertyValue(cultureVal.PublishedValue?.ToString(), onlyMissingKeys);
cultureVal.PublishedValue = updatedPublishedVal;

// Remove keys from edited/draft value & any nested properties
var updatedEditedVal = _formatPropertyValue(cultureVal.EditedValue?.ToString(), onlyMissingKeys);
cultureVal.EditedValue = updatedEditedVal;
}
}
}

/// <summary>
/// Unbinds from events
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
ContentService.Copying -= ContentService_Copying;
ContentService.Saving -= ContentService_Saving;
}
_disposedValue = true;
}
}

/// <summary>
/// Unbinds from events
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
1 change: 1 addition & 0 deletions src/Umbraco.Core/Umbraco.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<Compile Include="Models\RelationTypeExtensions.cs" />
<Compile Include="Persistence\Repositories\IInstallationRepository.cs" />
<Compile Include="Persistence\Repositories\Implement\InstallationRepository.cs" />
<Compile Include="PropertyEditors\ComplexPropertyEditorContentEventHandler.cs" />
<Compile Include="Services\Implement\InstallationService.cs" />
<Compile Include="Migrations\Upgrade\V_8_6_0\AddMainDomLock.cs" />
<Compile Include="Models\Blocks\BlockListItem.cs" />
Expand Down
Loading

0 comments on commit 1f44b94

Please sign in to comment.