Skip to content

Commit

Permalink
v2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rickbutterfield committed Jan 17, 2025
1 parent 3ba3e6b commit ba3160c
Show file tree
Hide file tree
Showing 13 changed files with 698 additions and 636 deletions.
18 changes: 15 additions & 3 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,29 @@ The Umbraco v14.2+ version of this package is [available via NuGet](https://www.
To install the package, you can use either .NET CLI:

```
dotnet add package Umbraco.Community.BlockPreview --version 2.0.3
dotnet add package Umbraco.Community.BlockPreview --version 2.1.0
```

or the NuGet Package Manager:

```
Install-Package Umbraco.Community.BlockPreview -Version 2.0.3
Install-Package Umbraco.Community.BlockPreview -Version 2.1.0
```

## Setup
The package can be configured in the `Program.cs` file, before the call to the `.Build()` method:
`Umbraco:Cms:ModelsBuilder:ModelsBuilderMode` **must** be set to either `SourceCodeAuto` or `SourceCodeManual` for BlockPreview to work.

```json
"Umbraco": {
"CMS": {
"ModelsBuilder": {
"ModelsMode": "SourceCodeAuto"
}
}
}
```

The package can then be configured in the `Program.cs` file, before the call to the `.Build()` method:
```diff
+using Umbraco.Community.BlockPreview.Extensions;
builder.CreateUmbracoBuilder()
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.3
2.1.0
995 changes: 489 additions & 506 deletions src/Umbraco.Community.BlockPreview.UI/package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Umbraco.Community.BlockPreview.UI/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "assets",
"private": true,
"version": "2.0.4",
"version": "2.1.0",
"type": "module",
"scripts": {
"dev": "vite",
Expand All @@ -15,7 +15,7 @@
},
"devDependencies": {
"@hey-api/openapi-ts": "^0.48.1",
"@umbraco-cms/backoffice": "^14.2.0",
"@umbraco-cms/backoffice": "^14.3.0",
"typescript": "^5.2.2",
"vite": "^5.2.8"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "../umbraco-package-schema.json",
"name": "Umbraco.Community.BlockPreview",
"id": "Umbraco.Community.BlockPreview",
"version": "2.0.4",
"version": "2.1.0",
"allowTelemetry": true,
"extensions": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ export class BlockGridPreviewCustomView
}
.preview-alert-warning {
background-color: var(--uui-color-danger, #f0ac00);
background-color: var(--uui-color-warning, #f0ac00);
border-color: transparent;
color: #fff;
color: #000;
}
.preview-alert-info {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class BlockListPreviewCustomView
.preview-alert-warning {
background-color: var(--uui-color-warning, #f0ac00);
border-color: transparent;
color: #fff;
color: #000;
}
.preview-alert-info {
Expand Down

Large diffs are not rendered by default.

69 changes: 52 additions & 17 deletions src/Umbraco.Community.BlockPreview/Services/BlockPreviewService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
Expand Down Expand Up @@ -42,8 +43,7 @@ public class BlockPreviewService : IBlockPreviewService
private readonly IJsonSerializer _jsonSerializer;
private readonly IDataTypeService _dataTypeService;
private readonly IContentTypeService _contentTypeService;
private readonly IBlockEditorElementTypeCache _elementTypeCache;
private readonly ILogger<BlockPreviewService> _logger;
private readonly IWebHostEnvironment _webHostEnvironment;

public BlockPreviewService(
ITempDataProvider tempDataProvider,
Expand All @@ -57,8 +57,7 @@ public BlockPreviewService(
IJsonSerializer jsonSerializer,
IContentTypeService contentTypeService,
IDataTypeService dataTypeService,
IBlockEditorElementTypeCache elementTypeCache,
ILogger<BlockPreviewService> logger)
IWebHostEnvironment webHostEnvironment)
{
_tempDataProvider = tempDataProvider;
_viewComponentHelperWrapper = viewComponentHelperWrapper;
Expand All @@ -71,8 +70,7 @@ public BlockPreviewService(
_jsonSerializer = jsonSerializer;
_dataTypeService = dataTypeService;
_contentTypeService = contentTypeService;
_elementTypeCache = elementTypeCache;
_logger = logger;
_webHostEnvironment = webHostEnvironment;
}

#region Public
Expand Down Expand Up @@ -110,6 +108,11 @@ public async Task<string> RenderGridBlock(
Type? contentBlockType = FindBlockType(contentElement?.ContentType.Alias);
Type? settingsBlockType = settingsElement != null ? FindBlockType(settingsElement.ContentType.Alias) : default;

if (contentBlockType == null || (settingsElement != null && settingsBlockType == null))
{
return $"<div class=\"preview-alert preview-alert-warning\">ModelsBuilder is enabled but the generated model(s) could not be found. Please try regenerating models and restarting the application.</div>";
}

BlockGridItem? blockInstance = CreateBlockInstance(
BlockType.BlockGrid,
contentBlockType, contentElement,
Expand Down Expand Up @@ -342,9 +345,17 @@ private void ConvertNestedValuesToString(BlockItemData? blockData)
return matchingLayout;
}

private Type? FindBlockType(string? contentTypeAlias) =>
_typeFinder.FindClassesWithAttribute<PublishedModelAttribute>().FirstOrDefault(x =>
x.GetCustomAttribute<PublishedModelAttribute>(false)?.ContentTypeAlias == contentTypeAlias);
private Type? FindBlockType(string? contentTypeAlias)
{
if (string.IsNullOrEmpty(contentTypeAlias))
return null;

var type = _typeFinder
.FindClassesWithAttribute<PublishedModelAttribute>()
.FirstOrDefault(x => x.GetCustomAttribute<PublishedModelAttribute>(false)?.ContentTypeAlias == contentTypeAlias);

return type;
}

private ViewDataDictionary CreateViewData(object? typedBlockInstance, BlockType? blockType = default)
{
Expand Down Expand Up @@ -497,23 +508,47 @@ private void ConfigureBlockInstanceAreas(

private ViewEngineResult? GetViewResult(string? contentAlias, BlockType blockType)
{
if (string.IsNullOrEmpty(contentAlias))
return null;

var viewPaths = _options.GetViewLocations(blockType);

if (viewPaths == null || !viewPaths.Any())
return null;

ViewEngineResult? viewResult = null;
string appRoot = _webHostEnvironment.ContentRootPath;

foreach (var viewPath in viewPaths)
{
var formattedViewPath = $"~{viewPath}";
var viewResult = _razorViewEngine.GetView("", string.Format(formattedViewPath, contentAlias), false);
string baseViewPath = viewPath.TrimStart($"~{Path.DirectorySeparatorChar}").TrimStart("/");

if (viewResult.Success)
return viewResult;
var pathNonPascal = string.Format(baseViewPath, contentAlias ?? "");
var viewPathNonPascal = Path.Combine(appRoot, pathNonPascal);

viewResult = _razorViewEngine.GetView("", string.Format(formattedViewPath, contentAlias?.ToPascalCase()), false);
if (System.IO.File.Exists(viewPathNonPascal))
{
viewResult = _razorViewEngine.GetView("", pathNonPascal, false);

if (viewResult.Success)
return viewResult;
if (viewResult.Success)
return viewResult;
}

else
{
var pathPascal = string.Format(baseViewPath, contentAlias?.ToPascalCase() ?? "");
var viewPathPascal = Path.Combine(appRoot, pathPascal);

if (System.IO.File.Exists(viewPathPascal))
{
viewResult = _razorViewEngine.GetView("", pathPascal, false);

if (viewResult.Success)
return viewResult;
}
}

return null;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

<VersionPrefix>2.0.4</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<Authors>Rick Butterfield, Dave Woestenborghs, Matthew Wise</Authors>
<Copyright>$([System.DateTime]::UtcNow.ToString(`yyyy`)) © Rick Butterfield</Copyright>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "../umbraco-package-schema.json",
"name": "Umbraco.Community.BlockPreview",
"id": "Umbraco.Community.BlockPreview",
"version": "2.0.4",
"version": "2.1.0",
"allowTelemetry": true,
"extensions": [
{
Expand Down

0 comments on commit ba3160c

Please sign in to comment.