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

#114 - this is a first attempt at getting the text or rich text value… #235

Merged
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
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Models.Blocks;
using Umbraco.Extensions;
using SeoToolkit.Umbraco.MetaFields.Core.Interfaces.Converters;
using Umbraco.Cms.Core.Strings;

namespace SeoToolkit.Umbraco.MetaFields.Core.Common.Converters.SeoValueConverters
{
public abstract class BaseGridSeoValueConverter<T> : ISeoValueConverter
where T : class, IBlockReference<IPublishedElement, IPublishedElement>
{
public virtual Type FromValue {get;}
public virtual Type ToValue {get;}
public virtual object Convert(object value, IPublishedContent currentContent, string fieldAlias)
{
var dataTypes = new string[]{
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextArea,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextBox,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TinyMce
};
// Walk the Block List and get any text values in a list.
List<object> values = new();
if (value is BlockModelCollection<T> blocks) {
foreach(var item in blocks.Select(b => b.Content))
{
// Go through each of the properties in the content element that are Text, Text Area or Rich Text and aggregate their values into a single string.
foreach(var property in item.Properties.Where(p => dataTypes.Contains(p.PropertyType.DataType.EditorAlias))) {
var propertyValue = item.Value(property.Alias);
if (propertyValue is IHtmlEncodedString encodedString) {
values.Add(encodedString.ToHtmlString()?.StripHtml());
} else {
values.Add(propertyValue);
}

// TODO: If there is an embedded Block Grid or Block List item, recurse into it and get the values.
}
}
}
return values.Aggregate("", (a, b) => $"{a} {b}").Trim();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using Umbraco.Cms.Core.Models.Blocks;

namespace SeoToolkit.Umbraco.MetaFields.Core.Common.Converters.SeoValueConverters
{
public class BlockGridSeoValueConverter : BaseGridSeoValueConverter<BlockGridItem>
{
public override Type FromValue => typeof(BlockGridModel);
public override Type ToValue => typeof(string);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using Umbraco.Cms.Core.Models.Blocks;

namespace SeoToolkit.Umbraco.MetaFields.Core.Common.Converters.SeoValueConverters
{
public class BlockListSeoValueConverter : BaseGridSeoValueConverter<BlockListItem>
{
public override Type FromValue => typeof(BlockListModel);
public override Type ToValue => typeof(string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public void Compose(IUmbracoBuilder builder)
.Add<PublishedContentSeoValueConverter>()
.Add<FieldSeoValueConverter>()
.Add<MultiplePublishedContentSeoValueConverter>()
.Add<BlockGridSeoValueConverter>()
.Add<BlockListSeoValueConverter>()
.Add<HtmlEncodedStringSeoConverter>();

builder.WithCollectionBuilder<MapDefinitionCollectionBuilder>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ public class OpenGraphDescriptionField : ISeoField
public string GroupAlias => SeoFieldGroupConstants.OpenGraphGroup;
public Type FieldType => typeof(string);

public ISeoFieldEditor Editor => new SeoFieldFieldsEditor(new[] { "Umbraco.TextBox", "Umbraco.TextArea", "Umbraco.TinyMCE" });
public ISeoFieldEditor Editor => new SeoFieldFieldsEditor(new[] {
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextArea,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextBox,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TinyMce,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockGrid,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockList,
});
public ISeoFieldEditEditor EditEditor => new SeoTextAreaEditEditor();

public HtmlString Render(object value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ public class SeoDescriptionField : ISeoField
public string GroupAlias => SeoFieldGroupConstants.MetaFieldsGroup;
public Type FieldType => typeof(string);

public ISeoFieldEditor Editor => new SeoFieldFieldsEditor(new[] { "Umbraco.TextBox", "Umbraco.TextArea", "Umbraco.TinyMCE" });
public ISeoFieldEditor Editor => new SeoFieldFieldsEditor(new[] {
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextArea,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextBox,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TinyMce,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockGrid,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockList,
});
public ISeoFieldEditEditor EditEditor => new SeoTextAreaEditEditor();

public HtmlString Render(object value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
vm.isContentDirty = function () {
var currentEditorItem = editorState.getCurrent();
var isDirty = false;
currentEditorItem.variants.forEach(function (variant) {
if (variant.isDirty) {
isDirty = true;
}
});
if (currentEditorItem.variants !== undefined) {
currentEditorItem.variants.forEach(function (variant) {
if (variant.isDirty) {
isDirty = true;
}
});
}
return isDirty;
}

Expand Down