diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Polls/WidgetContentFragment.cs b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Polls/WidgetContentFragment.cs index 9d0d8c9761..115e69e134 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Polls/WidgetContentFragment.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Polls/WidgetContentFragment.cs @@ -7,7 +7,7 @@ public class WidgetContentFragment : ContentFragment public string ViewComponentName { get; set; } - public IDictionary Properties { get; } + public IDictionary Properties { get; set; } public WidgetContentFragment(string name) { diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Polls/ContentParser.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Polls/ContentParser.cs index 38c0737012..b207a4731a 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Polls/ContentParser.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Polls/ContentParser.cs @@ -62,8 +62,9 @@ public class ContentParser : IContentParser, ITransientDependency } } - var pollNames = Regex.Matches(content, @"(?<=PollName\s*=\s*"")(.*?)(?="")").Cast().Select(p => p.Value).ToList(); - var polls = Regex.Matches(content, @"(?<=Widget Type\s*=\s*"")(.*?)(?="")").Cast().Select(p => p.Value).ToList(); + + Dictionary>> parsedWidgets = new(); + ParseWidgets(content, parsedWidgets); var contentFragments = new List(); @@ -71,17 +72,23 @@ public class ContentParser : IContentParser, ITransientDependency { if (parsedList[i] == delimeter) { - if (polls.Count > k) + var values = parsedWidgets.GetOrDefault($"{k}.Widget").Select(p => p.Value).ToList(); + var keys = parsedWidgets.GetOrDefault($"{k}.Widget").Select(p => p.Key).ToList(); + + if (parsedWidgets.Count > k) { - var name = _options.WidgetConfigs.Where(p => p.Key == polls[k]).Select(p => p.Value.Name).FirstOrDefault(); - if (name is not null && pollNames.Count > k) + var name = _options.WidgetConfigs.Where(p => p.Key == values[0]).Select(p => p.Value.Name).FirstOrDefault(); + if (name is not null && parsedWidgets.Count > k) { + var properties = new Dictionary(); + for (int kv = 0; kv < values.Count; kv++) + { + properties.Add(keys[kv], values[kv]); + } + contentFragments.Add(new WidgetContentFragment(name) { - Properties = - { - { "Name", pollNames[k]} - } + Properties = properties }); } } @@ -95,4 +102,22 @@ public class ContentParser : IContentParser, ITransientDependency return contentFragments; } + + private void ParseWidgets(string content, Dictionary>> parsedWidgets) + { + var widgets = Regex.Matches(content, @"(?<=\[Widget)(.*?)(?=\])").Cast().Select(p => p.Value).ToList(); + for (int p = 0; p < widgets.Count; p++) + { + var preparedContent = string.Join("", widgets[p]); + var keys = Regex.Matches(preparedContent, @"(?<=[\[Widget]?\s)(.*?)(?=="")").Cast().Select(p => p.Value).Where(p => p != string.Empty).ToList(); + var values = Regex.Matches(preparedContent, @"(?<=\s*[a-zA-Z]*=\s*"")(.*?)(?="")").Cast().Select(p => p.Value).ToList(); + + var list = new List>(); + for (int kv = 0; kv < keys.Count; kv++) + { + list.Add(new KeyValuePair(keys[kv], values[kv])); + } + parsedWidgets.Add($"{p}.Widget", list); + } + } } \ No newline at end of file diff --git a/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Polls/PollViewComponentManager_Test.cs b/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Polls/PollViewComponentManager_Test.cs index dbe91f2f14..3b0c4aee14 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Polls/PollViewComponentManager_Test.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Polls/PollViewComponentManager_Test.cs @@ -19,6 +19,23 @@ public class PollViewComponentManager_Test : CmsKitDomainTestBase _options = GetRequiredService>(); } + [Fact] + public async Task AA_ParseAsync_ShouldWorkMoreDynamically() + { + _options.Value.AddWidgetConfig(testData.PollName, new ContentWidgetConfig(testData.WidgetName)); + _options.Value.AddWidgetConfig("ImageGallery", new ContentWidgetConfig("ImageGallery"));//test + + var content = @"**ABP Framework** is completely open source and developed in a community-driven manner. + [Widget Type=""Poll"" PollName =""poll-name""] + Thanks _for_ *your * feedback. + [Widget Type=""ImageGallery"" GalleryName=""Xyz"" Source=""GoogleDrive""]"; + + var poll = await pollManager.ParseAsync(content); + + poll.ShouldNotBeNull(); + poll.Count.ShouldBe(4); + } + [Fact] public async Task ParseAsync_ShouldWorkWithoutConfigOptions() {