diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/ContentRepositoryBase.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/ContentRepositoryBase.cs index 65f6dc0472..c91a7e20e1 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/ContentRepositoryBase.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/ContentRepositoryBase.cs @@ -36,6 +36,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement where TRepository : class, IRepository { private readonly Lazy _propertyEditors; + private readonly DataValueReferenceCollection _dataValueReferences; /// /// @@ -49,13 +50,14 @@ namespace Umbraco.Core.Persistence.Repositories.Implement /// protected ContentRepositoryBase(IScopeAccessor scopeAccessor, AppCaches cache, ILogger logger, ILanguageRepository languageRepository, IRelationRepository relationRepository, IRelationTypeRepository relationTypeRepository, - Lazy propertyEditors) + Lazy propertyEditors, DataValueReferenceCollection dataValueReferences) : base(scopeAccessor, cache, logger) { LanguageRepository = languageRepository; RelationRepository = relationRepository; RelationTypeRepository = relationTypeRepository; _propertyEditors = propertyEditors; + _dataValueReferences = dataValueReferences; } protected abstract TRepository This { get; } @@ -826,15 +828,21 @@ namespace Umbraco.Core.Persistence.Repositories.Implement foreach (var p in entity.Properties) { if (!PropertyEditors.TryGet(p.PropertyType.PropertyEditorAlias, out var editor)) continue; - var valueEditor = editor.GetValueEditor(); - if (!(valueEditor is IDataValueReference reference)) continue; //TODO: Support variants/segments! This is not required for this initial prototype which is why there is a check here if (!p.PropertyType.VariesByNothing()) continue; var val = p.GetValue(); // get the invariant value - var refs = reference.GetReferences(val); - trackedRelations.AddRange(refs); + + // WB: Loop over our collection of references registered and add references + foreach (var item in _dataValueReferences) + { + // Check if this value reference is for this datatype/editor + // Then call it's GetReferences method - to see if the value stored + // in the dataeditor/property has referecnes to media items + if (item.IsForEditor(editor)) + trackedRelations.AddRange(item.GetReferences(val)); + } } //First delete all auto-relations for this entity diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentBlueprintRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentBlueprintRepository.cs index 60d4026ad5..5c2e73de9d 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentBlueprintRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentBlueprintRepository.cs @@ -20,8 +20,8 @@ namespace Umbraco.Core.Persistence.Repositories.Implement { public DocumentBlueprintRepository(IScopeAccessor scopeAccessor, AppCaches appCaches, ILogger logger, IContentTypeRepository contentTypeRepository, ITemplateRepository templateRepository, ITagRepository tagRepository, ILanguageRepository languageRepository, IRelationRepository relationRepository, IRelationTypeRepository relationTypeRepository, - Lazy propertyEditorCollection) - : base(scopeAccessor, appCaches, logger, contentTypeRepository, templateRepository, tagRepository, languageRepository, relationRepository, relationTypeRepository, propertyEditorCollection) + Lazy propertyEditorCollection, DataValueReferenceCollection dataValueReferences) + : base(scopeAccessor, appCaches, logger, contentTypeRepository, templateRepository, tagRepository, languageRepository, relationRepository, relationTypeRepository, propertyEditorCollection, dataValueReferences) { } diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs index 5e3e7f05b9..e1ea955972 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs @@ -46,8 +46,8 @@ namespace Umbraco.Core.Persistence.Repositories.Implement /// public DocumentRepository(IScopeAccessor scopeAccessor, AppCaches appCaches, ILogger logger, IContentTypeRepository contentTypeRepository, ITemplateRepository templateRepository, ITagRepository tagRepository, ILanguageRepository languageRepository, IRelationRepository relationRepository, IRelationTypeRepository relationTypeRepository, - Lazy propertyEditors) - : base(scopeAccessor, appCaches, logger, languageRepository, relationRepository, relationTypeRepository, propertyEditors) + Lazy propertyEditors, DataValueReferenceCollection dataValueReferences) + : base(scopeAccessor, appCaches, logger, languageRepository, relationRepository, relationTypeRepository, propertyEditors, dataValueReferences) { _contentTypeRepository = contentTypeRepository ?? throw new ArgumentNullException(nameof(contentTypeRepository)); _templateRepository = templateRepository ?? throw new ArgumentNullException(nameof(templateRepository)); diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/MediaRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/MediaRepository.cs index a3f9e45485..2297bbed2c 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/MediaRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/MediaRepository.cs @@ -29,8 +29,8 @@ namespace Umbraco.Core.Persistence.Repositories.Implement private readonly MediaByGuidReadRepository _mediaByGuidReadRepository; public MediaRepository(IScopeAccessor scopeAccessor, AppCaches cache, ILogger logger, IMediaTypeRepository mediaTypeRepository, ITagRepository tagRepository, ILanguageRepository languageRepository, IRelationRepository relationRepository, IRelationTypeRepository relationTypeRepository, - Lazy propertyEditorCollection) - : base(scopeAccessor, cache, logger, languageRepository, relationRepository, relationTypeRepository, propertyEditorCollection) + Lazy propertyEditorCollection, DataValueReferenceCollection dataValueReferences) + : base(scopeAccessor, cache, logger, languageRepository, relationRepository, relationTypeRepository, propertyEditorCollection, dataValueReferences) { _mediaTypeRepository = mediaTypeRepository ?? throw new ArgumentNullException(nameof(mediaTypeRepository)); _tagRepository = tagRepository ?? throw new ArgumentNullException(nameof(tagRepository)); diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/MemberRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/MemberRepository.cs index 2871bf1dd3..c20e990a2b 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/MemberRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/MemberRepository.cs @@ -28,8 +28,8 @@ namespace Umbraco.Core.Persistence.Repositories.Implement public MemberRepository(IScopeAccessor scopeAccessor, AppCaches cache, ILogger logger, IMemberTypeRepository memberTypeRepository, IMemberGroupRepository memberGroupRepository, ITagRepository tagRepository, ILanguageRepository languageRepository, IRelationRepository relationRepository, IRelationTypeRepository relationTypeRepository, - Lazy propertyEditors) - : base(scopeAccessor, cache, logger, languageRepository, relationRepository, relationTypeRepository, propertyEditors) + Lazy propertyEditors, DataValueReferenceCollection dataValueReferences) + : base(scopeAccessor, cache, logger, languageRepository, relationRepository, relationTypeRepository, propertyEditors, dataValueReferences) { _memberTypeRepository = memberTypeRepository ?? throw new ArgumentNullException(nameof(memberTypeRepository)); _tagRepository = tagRepository ?? throw new ArgumentNullException(nameof(tagRepository)); diff --git a/src/Umbraco.Core/PropertyEditors/DataValueReferenceCollectionBuilder.cs b/src/Umbraco.Core/PropertyEditors/DataValueReferenceCollectionBuilder.cs index 0f8ee1bd8e..c6442275b0 100644 --- a/src/Umbraco.Core/PropertyEditors/DataValueReferenceCollectionBuilder.cs +++ b/src/Umbraco.Core/PropertyEditors/DataValueReferenceCollectionBuilder.cs @@ -2,8 +2,8 @@ namespace Umbraco.Core.PropertyEditors { - public class DataValueReferenceCollectionBuilder : LazyCollectionBuilderBase + public class DataValueReferenceCollectionBuilder : OrderedCollectionBuilderBase { - protected override DataValueReferenceCollectionBuilder This => this; + protected override DataValueReferenceCollectionBuilder This => this; } } diff --git a/src/Umbraco.Core/PropertyEditors/IDataValueReference.cs b/src/Umbraco.Core/PropertyEditors/IDataValueReference.cs index 8c0806a4a4..7d46704938 100644 --- a/src/Umbraco.Core/PropertyEditors/IDataValueReference.cs +++ b/src/Umbraco.Core/PropertyEditors/IDataValueReference.cs @@ -14,5 +14,12 @@ namespace Umbraco.Core.PropertyEditors /// /// IEnumerable GetReferences(object value); + + /// + /// Gets a value indicating whether the DataValueReference lookup supports a datatype (data editor). + /// + /// The datatype. + /// A value indicating whether the converter supports a datatype. + bool IsForEditor(IDataEditor dataEditor); } } diff --git a/src/Umbraco.Core/Runtime/CoreInitialComposer.cs b/src/Umbraco.Core/Runtime/CoreInitialComposer.cs index db308d720c..94d2b68121 100644 --- a/src/Umbraco.Core/Runtime/CoreInitialComposer.cs +++ b/src/Umbraco.Core/Runtime/CoreInitialComposer.cs @@ -75,9 +75,6 @@ namespace Umbraco.Core.Runtime composition.RegisterUnique(); composition.RegisterUnique(); - // TODO: WB Add our collection - // Manually register stuff in this collection - composition.DataValueReferences(); // register a server registrar, by default it's the db registrar composition.RegisterUnique(f => diff --git a/src/Umbraco.Web/PropertyEditors/ContentPickerPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/ContentPickerPropertyEditor.cs index 683f1a05c3..2f45d98fa1 100644 --- a/src/Umbraco.Web/PropertyEditors/ContentPickerPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/ContentPickerPropertyEditor.cs @@ -29,21 +29,11 @@ namespace Umbraco.Web.PropertyEditors protected override IDataValueEditor CreateValueEditor() => new ContentPickerPropertyValueEditor(Attribute); - internal class ContentPickerPropertyValueEditor : DataValueEditor, IDataValueReference + internal class ContentPickerPropertyValueEditor : DataValueEditor { public ContentPickerPropertyValueEditor(DataEditorAttribute attribute) : base(attribute) { } - - public IEnumerable GetReferences(object value) - { - var asString = value is string str ? str : value?.ToString(); - - if (string.IsNullOrEmpty(asString)) yield break; - - if (Udi.TryParse(asString, out var udi)) - yield return new UmbracoEntityReference(udi); - } } } } diff --git a/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs index 228e058ee7..16dffb3b10 100644 --- a/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs @@ -54,7 +54,7 @@ namespace Umbraco.Web.PropertyEditors protected override IConfigurationEditor CreateConfigurationEditor() => new GridConfigurationEditor(); - internal class GridPropertyValueEditor : DataValueEditor, IDataValueReference + internal class GridPropertyValueEditor : DataValueEditor { private readonly IUmbracoContextAccessor _umbracoContextAccessor; private readonly HtmlImageSourceParser _imageSourceParser; @@ -156,30 +156,6 @@ namespace Umbraco.Web.PropertyEditors return grid; } - - /// - /// Resolve references from values - /// - /// - /// - public IEnumerable GetReferences(object value) - { - var rawJson = value == null ? string.Empty : value is string str ? str : value.ToString(); - - DeserializeGridValue(rawJson, out var richTextEditorValues, out var mediaValues); - - foreach (var umbracoEntityReference in richTextEditorValues.SelectMany(x => - _richTextPropertyValueEditor.GetReferences(x.Value))) - { - yield return umbracoEntityReference; - } - - foreach (var umbracoEntityReference in mediaValues.SelectMany(x => - _mediaPickerPropertyValueEditor.GetReferences(x.Value["udi"]))) - { - yield return umbracoEntityReference; - } - } } } } diff --git a/src/Umbraco.Web/PropertyEditors/MediaPickerPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/MediaPickerPropertyEditor.cs index ece210b9d1..6416fa3342 100644 --- a/src/Umbraco.Web/PropertyEditors/MediaPickerPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/MediaPickerPropertyEditor.cs @@ -33,21 +33,11 @@ namespace Umbraco.Web.PropertyEditors protected override IDataValueEditor CreateValueEditor() => new MediaPickerPropertyValueEditor(Attribute); - internal class MediaPickerPropertyValueEditor : DataValueEditor, IDataValueReference + internal class MediaPickerPropertyValueEditor : DataValueEditor { public MediaPickerPropertyValueEditor(DataEditorAttribute attribute) : base(attribute) { } - - public IEnumerable GetReferences(object value) - { - var asString = value is string str ? str : value?.ToString(); - - if (string.IsNullOrEmpty(asString)) yield break; - - if (Udi.TryParse(asString, out var udi)) - yield return new UmbracoEntityReference(udi); - } } } diff --git a/src/Umbraco.Web/PropertyEditors/MultiNodeTreePickerPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/MultiNodeTreePickerPropertyEditor.cs index 1da665a622..d7a7a2ed59 100644 --- a/src/Umbraco.Web/PropertyEditors/MultiNodeTreePickerPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/MultiNodeTreePickerPropertyEditor.cs @@ -23,25 +23,12 @@ namespace Umbraco.Web.PropertyEditors protected override IDataValueEditor CreateValueEditor() => new MultiNodeTreePickerPropertyValueEditor(Attribute); - public class MultiNodeTreePickerPropertyValueEditor : DataValueEditor, IDataValueReference + public class MultiNodeTreePickerPropertyValueEditor : DataValueEditor { public MultiNodeTreePickerPropertyValueEditor(DataEditorAttribute attribute): base(attribute) { } - - public IEnumerable GetReferences(object value) - { - var asString = value == null ? string.Empty : value is string str ? str : value.ToString(); - - var udiPaths = asString.Split(','); - foreach (var udiPath in udiPaths) - { - if (Udi.TryParse(udiPath, out var udi)) - yield return new UmbracoEntityReference(udi); - } - - } } } diff --git a/src/Umbraco.Web/PropertyEditors/MultiUrlPickerValueEditor.cs b/src/Umbraco.Web/PropertyEditors/MultiUrlPickerValueEditor.cs index 9c42fe6cbe..de641f69a3 100644 --- a/src/Umbraco.Web/PropertyEditors/MultiUrlPickerValueEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/MultiUrlPickerValueEditor.cs @@ -15,7 +15,7 @@ using Umbraco.Web.PublishedCache; namespace Umbraco.Web.PropertyEditors { - public class MultiUrlPickerValueEditor : DataValueEditor, IDataValueReference + public class MultiUrlPickerValueEditor : DataValueEditor { private readonly IEntityService _entityService; private readonly ILogger _logger; @@ -190,9 +190,6 @@ namespace Umbraco.Web.PropertyEditors } } - - - } } } diff --git a/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs index 3d0605c4f9..865b583624 100644 --- a/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs @@ -54,7 +54,7 @@ namespace Umbraco.Web.PropertyEditors protected override IDataValueEditor CreateValueEditor() => new NestedContentPropertyValueEditor(Attribute, PropertyEditors, _dataTypeService, _contentTypeService); - internal class NestedContentPropertyValueEditor : DataValueEditor, IDataValueReference + internal class NestedContentPropertyValueEditor : DataValueEditor { private readonly PropertyEditorCollection _propertyEditors; private readonly IDataTypeService _dataTypeService; @@ -227,32 +227,6 @@ namespace Umbraco.Web.PropertyEditors // return json return JsonConvert.SerializeObject(deserialized); } - - public IEnumerable GetReferences(object value) - { - var rawJson = value == null ? string.Empty : value is string str ? str : value.ToString(); - - var result = new List(); - - foreach (var row in _nestedContentValues.GetPropertyValues(rawJson, out _)) - { - if (row.PropType == null) continue; - - var propEditor = _propertyEditors[row.PropType.PropertyEditorAlias]; - - var valueEditor = propEditor?.GetValueEditor(); - if (!(valueEditor is IDataValueReference reference)) continue; - - var val = row.JsonRowValue[row.PropKey]?.ToString(); - - var refs = reference.GetReferences(val); - - result.AddRange(refs); - } - - return result; - } - #endregion } diff --git a/src/Umbraco.Web/PropertyEditors/RichTextPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/RichTextPropertyEditor.cs index 427e36b37a..96b1d87205 100644 --- a/src/Umbraco.Web/PropertyEditors/RichTextPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/RichTextPropertyEditor.cs @@ -57,7 +57,7 @@ namespace Umbraco.Web.PropertyEditors /// /// A custom value editor to ensure that macro syntax is parsed when being persisted and formatted correctly for display in the editor /// - internal class RichTextPropertyValueEditor : DataValueEditor, IDataValueReference + internal class RichTextPropertyValueEditor : DataValueEditor { private IUmbracoContextAccessor _umbracoContextAccessor; private readonly HtmlImageSourceParser _imageSourceParser; @@ -130,24 +130,6 @@ namespace Umbraco.Web.PropertyEditors return parsed; } - - /// - /// Resolve references from values - /// - /// - /// - public IEnumerable GetReferences(object value) - { - var asString = value == null ? string.Empty : value is string str ? str : value.ToString(); - - foreach (var udi in _imageSourceParser.FindUdisFromDataAttributes(asString)) - yield return new UmbracoEntityReference(udi); - - foreach (var udi in _localLinkParser.FindUdisFromLocalLinks(asString)) - yield return new UmbracoEntityReference(udi); - - //TODO: Detect Macros too ... but we can save that for a later date, right now need to do media refs - } } internal class RichTextPropertyIndexValueFactory : IPropertyIndexValueFactory diff --git a/src/Umbraco.Web/PropertyEditors/ValueReferences/ContentPickerPropertyValueReferences.cs b/src/Umbraco.Web/PropertyEditors/ValueReferences/ContentPickerPropertyValueReferences.cs new file mode 100644 index 0000000000..08405b6d66 --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/ValueReferences/ContentPickerPropertyValueReferences.cs @@ -0,0 +1,22 @@ +using System.Collections.Generic; +using Umbraco.Core; +using Umbraco.Core.Models.Editors; +using Umbraco.Core.PropertyEditors; + +namespace Umbraco.Web.PropertyEditors.ValueReferences +{ + public class ContentPickerPropertyValueReferences : IDataValueReference + { + public IEnumerable GetReferences(object value) + { + var asString = value is string str ? str : value?.ToString(); + + if (string.IsNullOrEmpty(asString)) yield break; + + if (Udi.TryParse(asString, out var udi)) + yield return new UmbracoEntityReference(udi); + } + + public bool IsForEditor(IDataEditor dataEditor) => dataEditor.Alias.InvariantEquals(Constants.PropertyEditors.Aliases.ContentPicker); + } +} diff --git a/src/Umbraco.Web/PropertyEditors/ValueReferences/GridPropertyValueReferences.cs b/src/Umbraco.Web/PropertyEditors/ValueReferences/GridPropertyValueReferences.cs new file mode 100644 index 0000000000..85acff7ca3 --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/ValueReferences/GridPropertyValueReferences.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using System.Linq; +using Umbraco.Core; +using Umbraco.Core.Models.Editors; +using Umbraco.Core.PropertyEditors; + +namespace Umbraco.Web.PropertyEditors.ValueReferences +{ + public class GridPropertyValueReferences : IDataValueReference + { + + /// + /// Resolve references from values + /// + /// + /// + public IEnumerable GetReferences(object value) + { + return Enumerable.Empty(); + + //var rawJson = value == null ? string.Empty : value is string str ? str : value.ToString(); + + ////TODO FIX SQUIGLES + //GridPropertyEditor.GridPropertyValueEditor.DeserializeGridValue(rawJson, out var richTextEditorValues, out var mediaValues); + + //foreach (var umbracoEntityReference in richTextEditorValues.SelectMany(x => + // _richTextPropertyValueEditor.GetReferences(x.Value))) + // yield return umbracoEntityReference; + + //foreach (var umbracoEntityReference in mediaValues.SelectMany(x => + // _mediaPickerPropertyValueEditor.GetReferences(x.Value["udi"]))) + // yield return umbracoEntityReference; + } + + public bool IsForEditor(IDataEditor dataEditor) => dataEditor.Alias.InvariantEquals(Constants.PropertyEditors.Aliases.Grid); + } +} diff --git a/src/Umbraco.Web/PropertyEditors/ValueReferences/MediaPickerPropertyValueReferences.cs b/src/Umbraco.Web/PropertyEditors/ValueReferences/MediaPickerPropertyValueReferences.cs new file mode 100644 index 0000000000..ecd46b7ea5 --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/ValueReferences/MediaPickerPropertyValueReferences.cs @@ -0,0 +1,22 @@ +using System.Collections.Generic; +using Umbraco.Core; +using Umbraco.Core.Models.Editors; +using Umbraco.Core.PropertyEditors; + +namespace Umbraco.Web.PropertyEditors.ValueReferences +{ + public class MediaPickerPropertyValueReferences : IDataValueReference + { + public IEnumerable GetReferences(object value) + { + var asString = value is string str ? str : value?.ToString(); + + if (string.IsNullOrEmpty(asString)) yield break; + + if (Udi.TryParse(asString, out var udi)) + yield return new UmbracoEntityReference(udi); + } + + public bool IsForEditor(IDataEditor dataEditor) => dataEditor.Alias.InvariantEquals(Constants.PropertyEditors.Aliases.MediaPicker); + } +} diff --git a/src/Umbraco.Web/PropertyEditors/ValueReferences/MultiNodeTreePickerPropertyValueReferences.cs b/src/Umbraco.Web/PropertyEditors/ValueReferences/MultiNodeTreePickerPropertyValueReferences.cs new file mode 100644 index 0000000000..4413f75080 --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/ValueReferences/MultiNodeTreePickerPropertyValueReferences.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using Umbraco.Core; +using Umbraco.Core.Models.Editors; +using Umbraco.Core.PropertyEditors; + +namespace Umbraco.Web.PropertyEditors.ValueReferences +{ + public class MultiNodeTreePickerPropertyValueReferences : IDataValueReference + { + public IEnumerable GetReferences(object value) + { + var asString = value == null ? string.Empty : value is string str ? str : value.ToString(); + + var udiPaths = asString.Split(','); + foreach (var udiPath in udiPaths) + if (Udi.TryParse(udiPath, out var udi)) + yield return new UmbracoEntityReference(udi); + + } + + public bool IsForEditor(IDataEditor dataEditor) => dataEditor.Alias.InvariantEquals(Constants.PropertyEditors.Aliases.MultiNodeTreePicker); + } +} diff --git a/src/Umbraco.Web/PropertyEditors/ValueReferences/MultiUrlPickerValueReferences.cs b/src/Umbraco.Web/PropertyEditors/ValueReferences/MultiUrlPickerValueReferences.cs new file mode 100644 index 0000000000..5d8dfd4f39 --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/ValueReferences/MultiUrlPickerValueReferences.cs @@ -0,0 +1,26 @@ +using Newtonsoft.Json; +using System.Collections.Generic; +using Umbraco.Core; +using Umbraco.Core.Models.Editors; +using Umbraco.Core.PropertyEditors; + +namespace Umbraco.Web.PropertyEditors.ValueReferences +{ + public class MultiUrlPickerValueReferences : IDataValueReference + { + public IEnumerable GetReferences(object value) + { + var asString = value == null ? string.Empty : value is string str ? str : value.ToString(); + + if (string.IsNullOrEmpty(asString)) yield break; + + var links = JsonConvert.DeserializeObject>(asString); + foreach (var link in links) + if (link.Udi != null) // Links can be absolute links without a Udi + yield return new UmbracoEntityReference(link.Udi); + } + + public bool IsForEditor(IDataEditor dataEditor) => dataEditor.Alias.InvariantEquals(Constants.PropertyEditors.Aliases.MultiUrlPicker); + + } +} diff --git a/src/Umbraco.Web/PropertyEditors/ValueReferences/NestedContentPropertyValueReferences.cs b/src/Umbraco.Web/PropertyEditors/ValueReferences/NestedContentPropertyValueReferences.cs new file mode 100644 index 0000000000..a872dde1c7 --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/ValueReferences/NestedContentPropertyValueReferences.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using Umbraco.Core; +using Umbraco.Core.Models.Editors; +using Umbraco.Core.PropertyEditors; +using Umbraco.Core.Services; +using static Umbraco.Web.PropertyEditors.NestedContentPropertyEditor; + +namespace Umbraco.Web.PropertyEditors.ValueReferences +{ + public class NestedContentPropertyValueReferences : IDataValueReference + { + private PropertyEditorCollection _propertyEditors; + private NestedContentValues _nestedContentValues; + + //ARGH LightInject moaning at me + public NestedContentPropertyValueReferences(PropertyEditorCollection propertyEditors, IContentTypeService contentTypeService) + { + _propertyEditors = propertyEditors; + _nestedContentValues = new NestedContentValues(contentTypeService); + } + + public IEnumerable GetReferences(object value) + { + var rawJson = value == null ? string.Empty : value is string str ? str : value.ToString(); + + var result = new List(); + + foreach (var row in _nestedContentValues.GetPropertyValues(rawJson, out _)) + { + if (row.PropType == null) continue; + + var propEditor = _propertyEditors[row.PropType.PropertyEditorAlias]; + + var valueEditor = propEditor?.GetValueEditor(); + if (!(valueEditor is IDataValueReference reference)) continue; + + var val = row.JsonRowValue[row.PropKey]?.ToString(); + + var refs = reference.GetReferences(val); + + result.AddRange(refs); + } + + return result; + } + + public bool IsForEditor(IDataEditor dataEditor) => dataEditor.Alias.InvariantEquals(Constants.PropertyEditors.Aliases.NestedContent); + } +} diff --git a/src/Umbraco.Web/PropertyEditors/ValueReferences/RichTextPropertyValueReferences.cs b/src/Umbraco.Web/PropertyEditors/ValueReferences/RichTextPropertyValueReferences.cs new file mode 100644 index 0000000000..744b0dc27c --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/ValueReferences/RichTextPropertyValueReferences.cs @@ -0,0 +1,40 @@ +using System.Collections.Generic; +using Umbraco.Core; +using Umbraco.Core.Models.Editors; +using Umbraco.Core.PropertyEditors; +using Umbraco.Web.Templates; + +namespace Umbraco.Web.PropertyEditors.ValueReferences +{ + public class RichTextPropertyValueReferences : IDataValueReference + { + private HtmlImageSourceParser _imageSourceParser; + private HtmlLocalLinkParser _localLinkParser; + + public RichTextPropertyValueReferences(HtmlImageSourceParser imageSourceParser, HtmlLocalLinkParser localLinkParser) + { + _imageSourceParser = imageSourceParser; + _localLinkParser = localLinkParser; + } + + /// + /// Resolve references from values + /// + /// + /// + public IEnumerable GetReferences(object value) + { + var asString = value == null ? string.Empty : value is string str ? str : value.ToString(); + + foreach (var udi in _imageSourceParser.FindUdisFromDataAttributes(asString)) + yield return new UmbracoEntityReference(udi); + + foreach (var udi in _localLinkParser.FindUdisFromLocalLinks(asString)) + yield return new UmbracoEntityReference(udi); + + //TODO: Detect Macros too ... but we can save that for a later date, right now need to do media refs + } + + public bool IsForEditor(IDataEditor dataEditor) => dataEditor.Alias.InvariantEquals(Constants.PropertyEditors.Aliases.TinyMce); + } +} diff --git a/src/Umbraco.Web/Runtime/WebInitialComposer.cs b/src/Umbraco.Web/Runtime/WebInitialComposer.cs index 27d0b5a1ce..295a31c461 100644 --- a/src/Umbraco.Web/Runtime/WebInitialComposer.cs +++ b/src/Umbraco.Web/Runtime/WebInitialComposer.cs @@ -42,6 +42,8 @@ using Umbraco.Web.Trees; using Umbraco.Web.WebApi; using Current = Umbraco.Web.Composing.Current; using Umbraco.Web.PropertyEditors; +using static Umbraco.Web.PropertyEditors.GridPropertyEditor; +using Umbraco.Web.PropertyEditors.ValueReferences; namespace Umbraco.Web.Runtime { @@ -274,7 +276,22 @@ namespace Umbraco.Web.Runtime .Append() .Append() .Append(); - + + // Used to determine if a datatype/editor should be storing/tracking + // references to media item/s + composition.DataValueReferences() + .Append() + + // TODO: Unsure how to call other ValueReference in collection + // When looking at grid item cells for RTE or media found + //.Append() + .Append() + .Append() + .Append() + + // TODO: LightInject problem + //.Append() + .Append(); // replace with web implementation composition.RegisterUnique(); diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 5eca5ad7fe..22680898af 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -235,6 +235,13 @@ + + + + + + +