Knowit.Umbraco.InstantBlockPreview 2.1.8

dotnet add package Knowit.Umbraco.InstantBlockPreview --version 2.1.8                
NuGet\Install-Package Knowit.Umbraco.InstantBlockPreview -Version 2.1.8                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Knowit.Umbraco.InstantBlockPreview" Version="2.1.8" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Knowit.Umbraco.InstantBlockPreview --version 2.1.8                
#r "nuget: Knowit.Umbraco.InstantBlockPreview, 2.1.8"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Knowit.Umbraco.InstantBlockPreview as a Cake Addin
#addin nuget:?package=Knowit.Umbraco.InstantBlockPreview&version=2.1.8

// Install Knowit.Umbraco.InstantBlockPreview as a Cake Tool
#tool nuget:?package=Knowit.Umbraco.InstantBlockPreview&version=2.1.8                

Knowit.Umbraco.InstantBlockPreview (and *.Headless)

A package-set for Umbraco that enables instant previews in the Umbraco Backoffice for Block Grid and Block List element types, without the need to save the document first. This package was heavily inspired by Rick Butterfield's Block Preview package.

For Umbraco 13 it also allows for preview of blocks in the Rich Text Editor. (Version 14 support of this is in the works)

Knowit.Umbraco.InstantBlockPreview currently supports Umbraco 10 to 14

Knowit.Umbraco.InstantBlockPreview.Headless currently supports Umbraco 12 to 13

If you use Umbraco 13 or old, use version 1.x.x

If you use Umbraco 14 use version 2.x.x (due to the rewrite of the backoffice)

The two packages "Knowit.Umbraco.InstantBlockPreview" and "Knowit.Umbraco.InstantBlockPreview.Headless" share this repo and readme.

Choose your section accordingly. - Version 14 support of headless is in the works.

Features

  • Instant preview for Block Grid and Block List element types.
  • No document save required to perform a preview.
  • MVC: Supports rendering through ViewComponents
  • Headless: Supports rendering with HTML fetched from an SSR app (if you're using Knowit.Umbraco.InstantBlockPreview.Headless)

MVC

Configuration

Configuration is optional, if you are happy with the standard values, you don't need to add anything to your appsettings.json.

(Intellisens support in 2.1.1 or newer) - You can add the following to appsettings-schema.json for intellisense support in your appsettings.json

{
      "$ref": "App_Plugins\\knowit-instantblockpreview\\appsettings-schema.Knowit.Umbraco.InstantBlockPreview.json#"
}

The following values are available for configuration:

"Knowit.Umbraco.InstantBlockPreview": {
  "gridViewPath": "~/Views/Partials/blockgrid/Components/",
  "blockViewPath": "~/Views/Partials/blocklist/Components/",
  "rteViewPath": "~/Views/Partials/richtext/Components/",
  "injections": []
  "areaReplace": true, 
  "CollapsibleBlocks": false,
  "divInlineStyle": "",
  "enableFor": [], // only v14 and newer, see below
  "disableFor": [], // only v14 and newer, see below
}
  • gridviewPath - The path to the Block Grid views. Default is ~/Views/Partials/blockgrid/Components/.
  • blockViewPath - The path to the Block List views. Default is ~/Views/Partials/blocklist/Components/.
  • rteViewPath - The path to the richtext Block views. Default is ~/Views/Partials/blocklist/Components/.
  • injections - In short, a way to add lines of code to the start of the preview-HTML. Use it to inject your web-components JS or your app or something else!
  • areaReplace - If set to true, will try to render grid areas wherever it finds the magic viewbag item "@ViewBag.renderGridAreaSlots"
  • CollapsibleBlocks - If set to true, will render blocks with a collapsible div around them.
  • divInlineStyle - Inline style configuration for the div element containing the block rendered as a string in a style attribute.

AreaReplace example:

@using Umbraco.Extensions
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockGridItem>

<div>
    This is my 2-col<hr />
    @await Html.GetBlockGridItemAreasHtmlAsync(Model)
    @ViewBag.renderGridAreaSlots
</div>
Version 14 specific behavior

As version 14.1 of Umbraco currently doesn't use the "advanced" tab info from the block setups, and instead lets you override "all" block renderings, I've introduced enabledForand disableFor as a way to give that control back.

Default behaviour with no configuration is to take control of all block-renderings. If something goes wrong, it will output html like Umbraco normally would without the plugin installed.

  • If enableFor is in use, it will only render blocks whose alias match the list.
  • If disableFor is in use, it will render all blocks except aliases matching the list.

Razor View

Implement your views normally.

You can include special code to run only in the back office like so:

@if (ViewBag.blockPreview != null)
{
    <p style="color:red">I am only visible in backoffice and this is my id @ViewBag.assignedContentId</p>
}

Umbraco.AssignedContentItem.Id will not work in your previews as the content is temporary and not assigned to anything.

A workaround if you must have a reference in your view to the content the element is assigned, something like this can be used.

var id = ViewBag.blockPreview ? ViewBag.assignedContentId : Umbraco.AssignedContentItem.Id;

Version 13 and earlier specific behavior

Set gridView.html for Block Grid and listview.html for Block List, which are installed by this package in app_plugins/Knowit.Umbraco.InstantBlockPreview.

Headless

Configuration

The following values are available for configuration:

"Knowit.Umbraco.InstantBlockPreview": {
  "ssrApiUrl": "https://localhost:3000/api/blockPreview",
  "ssrUrl": "https://localhost:3000/blockPreview",
  "ssrSecret": "my-ssr-secret",
  "ssrSelector": "#__nuxt",
  "injections": []
}
  • ssrApiUrl - The URL to the API that will recieve the JSON from Umbraco.
  • ssrUrl - The URL to the API that will return the HTML to Umbraco.
  • ssrSecret - A secret key that will be sent to the API to verify that the request is coming from Umbraco.
  • injections - In short, a way to add lines of code to the start of the preview-HTML. Use it to inject your web-components JS or your app or something else!

Read more under "Headless Preview"

Umbraco Cloud

Currently to deploy this package on a Umbraco Cloud solution, the following is required to be added to your web projects .csproj file:

<PropertyGroup>
    <ErrorOnDuplicatePublishOutputFiles>false</ErrorOnDuplicatePublishOutputFiles>
</PropertyGroup>

Headless Preview

To allow an Headless app (Next/Nuxt etc) to provide a preview for the plugin, you will need to wire up some stuff yourself.

It will send a POST to the "ssrApiUrl" with whatever you've put in "ssrSecret" as an "knowit-umbraco-instantblock-preview-secret" header. The POST BODY will include standard Content Delivery API Json in the form of a Grid or Blocklist entry depending on what you're doing.

It will expect to receive a string back which will act as a key for the next call. It will then perform a GET call to "ssrUrl" with ?key=returnedKeyFromApi appended and again with the authorization header.

Your application needs to return server-side rendered HTML back from that request.

So on your end.

  • Api needs to recieve JSON from POST and save/cache it somehow with a key, and return the key as response
  • Preview Url needs to return preview-HTML when given the key through a GET request

"ssrSelector" is an optional option to narrow the amount of HTML returned to Umbraco. You can put in body/#__nuxt or any other valid css selector and the API will only return the INNERHTML of that selector to Umbraco for a cleaner HTML output.

Example

You've set up a grid element with a rich text editor to use Instant Block Preview and have just now entered some text and hit save.

  1. The Javascript sends your changes to Instant Block Preview API
  2. Instant Block Preview API sends a POST request to https://localhost:3000/api/blockPreview with following BODY:
{"gridColumns":12,"items":[{"rowSpan":0,"columnSpan":0,"areaGridColumns":12,"areas":[],"content":{"id":"554991cb-dfa4-43fd-85ba-9c9b0213438e","contentType":"text","properties":{"richText":{"markup":"<p>Test 123</p>","blocks":[]}}},"settings":null}]}

as well as an header with your secret key

  1. Your app, whatever it may be, recieves the call, verifies the secret key and caches the json in a way where your app will be able to retrieve it with just a string key you've just generated. Could just be current time in microseconds or a guid or something else
  2. Your app returns the string key as response (example key "lutvdx1hghiz4w9suye")
  3. Instant Block Preview now makes a GET request to https://localhost:3000/blockPreview?key=lutvdx1hghiz4w9suye with the same authorization headers set
  4. Your app retrieves the JSON from it's cache with the key, and renders the HTML just like you would with a normal response from the Content Delivery API
  5. Instant Block Preview recieves the Html and (optionally) filters it down to the css selector you've sendt in "ssrSelector" and returns that Html to be previewed in Umbraco

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.8 99 8/27/2024
2.1.7 96 8/22/2024
2.1.6 92 8/22/2024
2.1.5 95 8/22/2024
2.1.4 97 8/22/2024
2.1.3 90 8/12/2024
2.1.2 182 8/8/2024
2.1.1 35 8/5/2024
2.1.0 81 8/1/2024
2.0.12 34 7/31/2024
2.0.11 29 7/31/2024
2.0.10 72 7/30/2024
2.0.9 118 7/29/2024
2.0.8 64 7/29/2024
1.1.1 96 8/22/2024
1.1.0 93 8/22/2024
1.0.7 116 7/30/2024
1.0.6 69 7/29/2024
1.0.5 67 7/29/2024
1.0.4 67 7/29/2024
1.0.3 118 7/5/2024
1.0.2 90 6/28/2024
1.0.1 75 6/28/2024
1.0.0 75 6/28/2024
0.7.0 354 4/11/2024
0.6.2 85 4/10/2024
0.6.1 450 2/13/2024
0.6.0 117 2/12/2024
0.5.2 191 1/17/2024
0.5.1 222 12/14/2023
0.5.0 112 12/14/2023
0.4.0 199 12/1/2023
0.3.8 523 11/1/2023
0.3.3 161 10/30/2023
0.3.2 119 10/26/2023
0.3.1 135 10/25/2023
0.3.0 109 10/23/2023