From bf05487628625aadb2a8dcfb107f023df8853050 Mon Sep 17 00:00:00 2001 From: Rick Butterfield Date: Fri, 25 Aug 2023 11:59:40 +0100 Subject: [PATCH] Initial commit --- .vscode/settings.json | 2 +- .../BlockViews/callToAction.html | 0 .../UmbracoBlockGrid/BlockViews/card.html | 0 .../UmbracoBlockGrid/BlockViews/headline.html | 0 .../UmbracoBlockGrid/BlockViews/hero.html | 0 .../UmbracoBlockGrid/BlockViews/image.html | 0 .../BlockViews/inspiration.html | 0 .../BlockViews/oneColumnSection.html | 0 .../UmbracoBlockGrid/BlockViews/richText.html | 0 .../BlockViews/twoColumnSection.html | 0 .../UmbracoBlockGrid/package.manifest | 0 .../Properties/launchSettings.json | 6 ++-- src/Umbraco.Cms.11.x/Umbraco.Cms.11.x.csproj | 1 + .../controllers/block-preview.controller.js | 5 ++- .../js/resources/preview.resource.js | 4 +-- .../Controllers/BlockPreviewApiController.cs | 7 +++-- .../IBackOfficeGridPreviewService.cs | 3 ++ .../IBackOfficeListPreviewService.cs | 3 ++ .../Services/BackOfficeGridPreviewService.cs | 31 ++++++++++++------- .../Services/BackOfficeListPreviewService.cs | 18 +++++++++-- 20 files changed, 57 insertions(+), 23 deletions(-) mode change 100644 => 100755 src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/callToAction.html mode change 100644 => 100755 src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/card.html mode change 100644 => 100755 src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/headline.html mode change 100644 => 100755 src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/hero.html mode change 100644 => 100755 src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/image.html mode change 100644 => 100755 src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/inspiration.html mode change 100644 => 100755 src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/oneColumnSection.html mode change 100644 => 100755 src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/richText.html mode change 100644 => 100755 src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/twoColumnSection.html mode change 100644 => 100755 src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/package.manifest diff --git a/.vscode/settings.json b/.vscode/settings.json index 0c0d5ea..345a2ff 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "dotnet.defaultSolution": "src\\Umbraco.Community.BlockPreview.sln" + "dotnet.defaultSolution": "src/Umbraco.Community.BlockPreview.sln" } \ No newline at end of file diff --git a/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/callToAction.html b/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/callToAction.html old mode 100644 new mode 100755 diff --git a/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/card.html b/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/card.html old mode 100644 new mode 100755 diff --git a/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/headline.html b/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/headline.html old mode 100644 new mode 100755 diff --git a/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/hero.html b/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/hero.html old mode 100644 new mode 100755 diff --git a/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/image.html b/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/image.html old mode 100644 new mode 100755 diff --git a/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/inspiration.html b/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/inspiration.html old mode 100644 new mode 100755 diff --git a/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/oneColumnSection.html b/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/oneColumnSection.html old mode 100644 new mode 100755 diff --git a/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/richText.html b/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/richText.html old mode 100644 new mode 100755 diff --git a/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/twoColumnSection.html b/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/BlockViews/twoColumnSection.html old mode 100644 new mode 100755 diff --git a/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/package.manifest b/src/Umbraco.Cms.11.x/App_Plugins/UmbracoBlockGrid/package.manifest old mode 100644 new mode 100755 diff --git a/src/Umbraco.Cms.11.x/Properties/launchSettings.json b/src/Umbraco.Cms.11.x/Properties/launchSettings.json index ade336c..2039c50 100644 --- a/src/Umbraco.Cms.11.x/Properties/launchSettings.json +++ b/src/Umbraco.Cms.11.x/Properties/launchSettings.json @@ -18,12 +18,12 @@ }, "Umbraco.Web.UI": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "https://localhost:44354;http://localhost:63790", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "dotnetRunMessages": true } } -} +} \ No newline at end of file diff --git a/src/Umbraco.Cms.11.x/Umbraco.Cms.11.x.csproj b/src/Umbraco.Cms.11.x/Umbraco.Cms.11.x.csproj index 166bfca..4db62a0 100644 --- a/src/Umbraco.Cms.11.x/Umbraco.Cms.11.x.csproj +++ b/src/Umbraco.Cms.11.x/Umbraco.Cms.11.x.csproj @@ -56,4 +56,5 @@ false + diff --git a/src/Umbraco.Community.BlockPreview/App_Plugins/Umbraco.Community.BlockPreview/js/controllers/block-preview.controller.js b/src/Umbraco.Community.BlockPreview/App_Plugins/Umbraco.Community.BlockPreview/js/controllers/block-preview.controller.js index c81198c..4cf245f 100644 --- a/src/Umbraco.Community.BlockPreview/App_Plugins/Umbraco.Community.BlockPreview/js/controllers/block-preview.controller.js +++ b/src/Umbraco.Community.BlockPreview/App_Plugins/Umbraco.Community.BlockPreview/js/controllers/block-preview.controller.js @@ -15,6 +15,9 @@ $scope.loading = true; $scope.markup = $sce.trustAsHtml('
Loading preview
'); + // There must be a better way to do this... + $scope.blockEditorAlias = $scope.$parent.$parent.$parent.$parent.$parent.$parent.vm.model.editor; + function loadPreview(content, settings) { $scope.markup = $sce.trustAsHtml('
Loading preview
'); $scope.loading = true; @@ -25,7 +28,7 @@ settingsData: [settings || $scope.block.settingsData] }; - previewResource.getPreview(formattedBlockData, $scope.id, $scope.model.constructor.name == 'BlockGridBlockController', $scope.language).then(function (data) { + previewResource.getPreview(formattedBlockData, $scope.id, $scope.blockEditorAlias, $scope.model.constructor.name == 'BlockGridBlockController', $scope.language).then(function (data) { $scope.markup = $sce.trustAsHtml(data); $scope.loading = false; }); diff --git a/src/Umbraco.Community.BlockPreview/App_Plugins/Umbraco.Community.BlockPreview/js/resources/preview.resource.js b/src/Umbraco.Community.BlockPreview/App_Plugins/Umbraco.Community.BlockPreview/js/resources/preview.resource.js index 12fc44f..926ed72 100644 --- a/src/Umbraco.Community.BlockPreview/App_Plugins/Umbraco.Community.BlockPreview/js/resources/preview.resource.js +++ b/src/Umbraco.Community.BlockPreview/App_Plugins/Umbraco.Community.BlockPreview/js/resources/preview.resource.js @@ -11,11 +11,11 @@ return resource; - function getPreview(data, pageId, isGrid, culture) { + function getPreview(data, pageId, blockEditorAlias, isGrid, culture) { culture = culture || ''; return umbRequestHelper.resourcePromise( - $http.post(apiUrl + '?pageId=' + pageId + '&isGrid=' + isGrid + '&culture=' + culture, data), + $http.post(`${apiUrl}?pageId=${pageId}&blockEditorAlias=${blockEditorAlias}&isGrid=${isGrid}&culture=${culture}`, data), 'Failed getting block preview markup' ); }; diff --git a/src/Umbraco.Community.BlockPreview/Controllers/BlockPreviewApiController.cs b/src/Umbraco.Community.BlockPreview/Controllers/BlockPreviewApiController.cs index fc5a35b..230eba4 100644 --- a/src/Umbraco.Community.BlockPreview/Controllers/BlockPreviewApiController.cs +++ b/src/Umbraco.Community.BlockPreview/Controllers/BlockPreviewApiController.cs @@ -67,6 +67,7 @@ public BlockPreviewApiController( public async Task PreviewMarkup( [FromBody] BlockValue data, [FromQuery] int pageId = 0, + [FromQuery] string blockGridAlias = "", [FromQuery] bool isGrid = false, [FromQuery] string culture = "") { @@ -93,9 +94,9 @@ public async Task PreviewMarkup( if (isGrid) { - markup = await _backOfficeGridPreviewService.GetMarkupForBlock(data, ControllerContext, currentCulture); + markup = await _backOfficeGridPreviewService.GetMarkupForBlock(page, data, blockGridAlias, ControllerContext, currentCulture); } - else markup = await _backOfficeListPreviewService.GetMarkupForBlock(data, ControllerContext, currentCulture); + else markup = await _backOfficeListPreviewService.GetMarkupForBlock(page, data, blockGridAlias, ControllerContext, currentCulture); } catch (Exception ex) { @@ -153,7 +154,7 @@ private IPublishedContent GetPublishedContentForPage(int pageId) return context.Content?.GetById(pageId) ?? context.Content?.GetById(true, pageId); } - private string CleanUpMarkup(string markup) + private static string CleanUpMarkup(string markup) { if (string.IsNullOrWhiteSpace(markup)) return markup; diff --git a/src/Umbraco.Community.BlockPreview/Interfaces/IBackOfficeGridPreviewService.cs b/src/Umbraco.Community.BlockPreview/Interfaces/IBackOfficeGridPreviewService.cs index 9b0a395..57a1ef8 100644 --- a/src/Umbraco.Community.BlockPreview/Interfaces/IBackOfficeGridPreviewService.cs +++ b/src/Umbraco.Community.BlockPreview/Interfaces/IBackOfficeGridPreviewService.cs @@ -1,13 +1,16 @@ using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; using Umbraco.Cms.Core.Models.Blocks; +using Umbraco.Cms.Core.Models.PublishedContent; namespace Umbraco.Community.BlockPreview.Interfaces { public interface IBackOfficeGridPreviewService : IBackOfficePreviewService { Task GetMarkupForBlock( + IPublishedContent page, BlockValue blockValue, + string blockGridAlias, ControllerContext controllerContext, string culture); } diff --git a/src/Umbraco.Community.BlockPreview/Interfaces/IBackOfficeListPreviewService.cs b/src/Umbraco.Community.BlockPreview/Interfaces/IBackOfficeListPreviewService.cs index f350d91..5a94b9d 100644 --- a/src/Umbraco.Community.BlockPreview/Interfaces/IBackOfficeListPreviewService.cs +++ b/src/Umbraco.Community.BlockPreview/Interfaces/IBackOfficeListPreviewService.cs @@ -1,13 +1,16 @@ using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; using Umbraco.Cms.Core.Models.Blocks; +using Umbraco.Cms.Core.Models.PublishedContent; namespace Umbraco.Community.BlockPreview.Interfaces { public interface IBackOfficeListPreviewService : IBackOfficePreviewService { Task GetMarkupForBlock( + IPublishedContent page, BlockValue blockValue, + string blockGridAlias, ControllerContext controllerContext, string culture); } diff --git a/src/Umbraco.Community.BlockPreview/Services/BackOfficeGridPreviewService.cs b/src/Umbraco.Community.BlockPreview/Services/BackOfficeGridPreviewService.cs index b1c94f6..9bf76e2 100644 --- a/src/Umbraco.Community.BlockPreview/Services/BackOfficeGridPreviewService.cs +++ b/src/Umbraco.Community.BlockPreview/Services/BackOfficeGridPreviewService.cs @@ -1,29 +1,22 @@ -using System.IO; -using System.Linq; -using System.Text.Encodings.Web; +using System.Linq; using Microsoft.AspNetCore.Mvc.ModelBinding; -using Microsoft.AspNetCore.Mvc.Rendering; using Umbraco.Cms.Core.PropertyEditors; using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.ViewComponents; using Microsoft.AspNetCore.Mvc.ViewFeatures; -using Microsoft.AspNetCore.Routing; using Umbraco.Cms.Core.Composing; using Umbraco.Cms.Core.Models.Blocks; using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.PropertyEditors.ValueConverters; using Umbraco.Extensions; using Umbraco.Community.BlockPreview.Interfaces; -using Microsoft.AspNetCore.Mvc.ViewEngines; -using Umbraco.Cms.Core; -using Newtonsoft.Json; using System.Collections.Generic; using System.Globalization; using System.Threading; +using Newtonsoft.Json; namespace Umbraco.Community.BlockPreview.Services { @@ -53,7 +46,9 @@ public BackOfficeGridPreviewService( } public async Task GetMarkupForBlock( + IPublishedContent page, BlockValue blockValue, + string blockEditorAlias, ControllerContext controllerContext, string culture) { @@ -62,6 +57,8 @@ public async Task GetMarkupForBlock( var contentData = blockValue.ContentData.FirstOrDefault(); var settingsData = blockValue.SettingsData.FirstOrDefault(); + var layoutData = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(blockValue.Layout)); + var references = new List() { new ContentAndSettingsReference(contentData?.Udi, settingsData?.Udi) }; BlockEditorData blockEditorData = new BlockEditorData(Cms.Core.Constants.PropertyEditors.Aliases.BlockGrid, references, blockValue); @@ -113,8 +110,20 @@ public async Task GetMarkupForBlock( } } - ViewDataDictionary viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()); - viewData.Model = blockInstance; + // Get block config from Umbraco + var contentProperty = page.Properties.FirstOrDefault(x => x.Alias.Equals(blockEditorAlias)); + if (contentProperty == null) return string.Empty; + + var config = contentProperty.PropertyType.DataType.Configuration as BlockGridConfiguration; + if (config == null || config.BlockGroups == null) return string.Empty; + + // Return a fully typed version of the block + var typedBlockInstance = blockInstance as BlockGridItem; + + ViewDataDictionary viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) + { + Model = blockInstance + }; viewData["blockPreview"] = true; string contentAlias = contentElement.ContentType.Alias.ToFirstUpper(); diff --git a/src/Umbraco.Community.BlockPreview/Services/BackOfficeListPreviewService.cs b/src/Umbraco.Community.BlockPreview/Services/BackOfficeListPreviewService.cs index 4c8370c..21b1150 100644 --- a/src/Umbraco.Community.BlockPreview/Services/BackOfficeListPreviewService.cs +++ b/src/Umbraco.Community.BlockPreview/Services/BackOfficeListPreviewService.cs @@ -44,7 +44,9 @@ public BackOfficeListPreviewService( } public async Task GetMarkupForBlock( + IPublishedContent page, BlockValue blockValue, + string blockEditorAlias, ControllerContext controllerContext, string culture) { @@ -101,8 +103,20 @@ public async Task GetMarkupForBlock( } } - ViewDataDictionary viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()); - viewData.Model = blockInstance; + // Get block config from Umbraco + var contentProperty = page.Properties.FirstOrDefault(x => x.Alias.Equals(blockEditorAlias)); + if (contentProperty == null) return string.Empty; + + var config = contentProperty.PropertyType.DataType.Configuration as BlockListConfiguration; + if (config == null || config.Blocks == null) return string.Empty; + + // Return a fully typed version of the block + var typedBlockInstance = blockInstance as BlockListItem; + + ViewDataDictionary viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) + { + Model = blockInstance + }; viewData["blockPreview"] = true; string contentAlias = contentElement.ContentType.Alias.ToFirstUpper();