Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rickbutterfield committed Aug 25, 2023
1 parent 23a0e81 commit bf05487
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"dotnet.defaultSolution": "src\\Umbraco.Community.BlockPreview.sln"
"dotnet.defaultSolution": "src/Umbraco.Community.BlockPreview.sln"
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
6 changes: 3 additions & 3 deletions src/Umbraco.Cms.11.x/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
1 change: 1 addition & 0 deletions src/Umbraco.Cms.11.x/Umbraco.Cms.11.x.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@
<RazorCompileOnPublish>false</RazorCompileOnPublish>
</PropertyGroup>

<PropertyGroup Condition=" '$(RunConfiguration)' == 'Umbraco.Web.UI' " />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
$scope.loading = true;
$scope.markup = $sce.trustAsHtml('<div class="alert alert-info">Loading preview</div>');

// 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('<div class="alert alert-info">Loading preview</div>');
$scope.loading = true;
Expand All @@ -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;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public BlockPreviewApiController(
public async Task<IActionResult> PreviewMarkup(
[FromBody] BlockValue data,
[FromQuery] int pageId = 0,
[FromQuery] string blockGridAlias = "",
[FromQuery] bool isGrid = false,
[FromQuery] string culture = "")
{
Expand All @@ -93,9 +94,9 @@ public async Task<IActionResult> 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)
{
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string> GetMarkupForBlock(
IPublishedContent page,
BlockValue blockValue,
string blockGridAlias,
ControllerContext controllerContext,
string culture);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string> GetMarkupForBlock(
IPublishedContent page,
BlockValue blockValue,
string blockGridAlias,
ControllerContext controllerContext,
string culture);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -53,7 +46,9 @@ public BackOfficeGridPreviewService(
}

public async Task<string> GetMarkupForBlock(
IPublishedContent page,
BlockValue blockValue,
string blockEditorAlias,
ControllerContext controllerContext,
string culture)
{
Expand All @@ -62,6 +57,8 @@ public async Task<string> GetMarkupForBlock(
var contentData = blockValue.ContentData.FirstOrDefault();
var settingsData = blockValue.SettingsData.FirstOrDefault();

var layoutData = JsonConvert.DeserializeObject<BlockGridLayoutItem>(JsonConvert.SerializeObject(blockValue.Layout));

var references = new List<ContentAndSettingsReference>() { new ContentAndSettingsReference(contentData?.Udi, settingsData?.Udi) };
BlockEditorData blockEditorData = new BlockEditorData(Cms.Core.Constants.PropertyEditors.Aliases.BlockGrid, references, blockValue);

Expand Down Expand Up @@ -113,8 +110,20 @@ public async Task<string> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public BackOfficeListPreviewService(
}

public async Task<string> GetMarkupForBlock(
IPublishedContent page,
BlockValue blockValue,
string blockEditorAlias,
ControllerContext controllerContext,
string culture)
{
Expand Down Expand Up @@ -101,8 +103,20 @@ public async Task<string> 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();
Expand Down

0 comments on commit bf05487

Please sign in to comment.