Replies: 1 comment
-
|
For those of you, who wants to have block preview registering happen automatically, I've added this extension method to one of my projects: public static class AddBlockPreviewsExtension
{
public static IUmbracoBuilder AddBlockPreviews(this IUmbracoBuilder builder)
{
var stylesheet = "/build/css/main.css";
builder.AddBlockPreview();
builder.Services.AddSingleton<IConfigureOptions<BlockPreviewOptions>>(sp =>
{
var env = sp.GetRequiredService<IWebHostEnvironment>();
var contentTypeService = sp.GetRequiredService<IContentTypeService>();
var contentTypeAliases = contentTypeService.GetAllContentTypeAliases().ToList();
return new ConfigureOptions<BlockPreviewOptions>(options =>
{
options.RichText = GetSettings(env, contentTypeAliases, BlockPreviewConstants.DefaultViewLocations.RichText, stylesheet: stylesheet);
options.BlockGrid = GetSettings(env, contentTypeAliases, BlockPreviewConstants.DefaultViewLocations.BlockGrid, stylesheet: stylesheet);
options.BlockList = GetSettings(env, contentTypeAliases, BlockPreviewConstants.DefaultViewLocations.BlockList, stylesheet: stylesheet);
});
});
return builder;
}
private static BlockWithStylesheetSettings GetSettings(IWebHostEnvironment hostingEnvironment, List<string> contentTypeAliases, string defaultViewLocation, List<string>? viewLocations = null, string? stylesheet = null)
{
viewLocations = viewLocations.EmptyIfNull().ToList();
var contentTypes = GetViewNames(hostingEnvironment, contentTypeAliases, viewLocations, defaultViewLocation);
return new() { ViewLocations = viewLocations, ContentTypes = contentTypes, Enabled = contentTypes.Any(), Stylesheet = stylesheet };
}
private static List<string> GetViewNames(IWebHostEnvironment hostingEnvironment, List<string> contentTypeAliases, List<string>? viewLocations, string? defaultLocation = null)
{
var locations = (viewLocations ?? [])
.Append(defaultLocation)
.WhereNotNull()
.Select(CleanPath)
.Select(x => x.TrimEnd("{0}.cshtml"))
.ToList();
var files = locations
.Select(hostingEnvironment.MapPathWebRoot)
.Where(Directory.Exists)
.SelectMany(location => Directory.EnumerateFiles(location, "*.cshtml", SearchOption.AllDirectories))
.Select(CleanPath)
.WhereNotNull();
var assembly = Assembly.GetExecutingAssembly();
var allRazorViews = assembly.GetCustomAttributes<RazorCompiledItemAttribute>()
.Select(x => x.Identifier)
.Select(CleanPath)
.WhereNotNull();
var razorViews = locations
.SelectMany(location => allRazorViews
.Where(view => view.InvariantStartsWith(location))
);
return ((List<string>)[..files, ..razorViews])
.Select(Path.GetFileNameWithoutExtension)
.Select(view => contentTypeAliases.FirstOrDefault(alias => alias.InvariantEquals(view)))
.WhereNotNull()
.ToList();
}
private static string CleanPath(string path)
{
return path.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar).EnsureStartsWith(Path.DirectorySeparatorChar);
}
}The extension method can then be added to a composer or Program.cs in stead of the normal way to enable Block Preview. What it does, is that it looks through your view locations, both on disk and in the assembly, to find any content types with a corresponding view. And then it registers that content type to use block preview. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Feature summary
In addtion to #141 it would be nice if the content type configuration allowed the use of regex.
Eg. we typically divide blocks into presentation blocks and data blocks. Presentation blocks being used to set up the content of a website, and data blocks for things not needing their own visual representation (think Nested Content). We have a naming convention, so presentation blocks always ends with
Block, while data blocks are suffixed withElementBecause of this, we could simply set the ContentTypes setting to
.+Block$and be done with that.Would still be nice to have an ignore list too though, eg. for
HtmlSnippetBlockthat often just contains tracking codes :)Additional details
No response
Beta Was this translation helpful? Give feedback.
All reactions