Node Renderer recipe

Source: 05_NodeRenderer/ · draw a custom node type your way: one class deriving from NodeRendererBase, used for the search row, the preview sheet and the full page.

How it looks

The compact row (search results, lists) and the tabs of the preview sheet for a custom RecipeBook node. The renderer receives a real Node; the runnable Tesserae preview below shows the same shapes with static data.

Files

File Responsibility
RecipeBookSchema.cs Compile-time constants for the node type and its fields, so a renamed field is a compile error. A workspace's starter project generates the same constants.
RecipeBookRenderer.cs The renderer. A public class deriving from NodeRendererBase with a parameterless constructor is discovered at startup and registered for the node type its style names.
NodeRendererView.cs The recipe's landing page: explains the wiring and shows a live SearchArea filtered to RecipeBook.

Override the block you want to change

NodeRendererBase draws every surface from the style it is given (SchemaStyleInfo: name, display name, label field, colour, icon). The recipe overrides three things:

public sealed class RecipeBookRenderer : NodeRendererBase
{
    public RecipeBookRenderer() : base(new SchemaStyleInfo
    {
        Name        = RecipeN.RecipeBook.Type,
        DisplayName = "Recipe Book",
        LabelField  = RecipeN.RecipeBook.Title,
        Color       = "#8e44ad",
        Icon        = UIcons.Book.ToString(),
    })
    {
    }

    // The dense row: start from the standard row and change it.
    public override OmniResult<Node> CompactView(Node node)
        => NodeResult.For(this, node).SetText("by " + node.GetString(RecipeN.RecipeBook.Author));

    // Which properties the Overview tab shows, in this order.
    protected override IEnumerable<string> GetDisplayPropertyNames(Node node)
    {
        yield return RecipeN.RecipeBook.Title;
        yield return RecipeN.RecipeBook.Author;
        yield return RecipeN.RecipeBook.Year;
        yield return RecipeN.RecipeBook.Genre;
    }

    // The tabs of the preview sheet and the page: the standard ones, plus one of our own.
    protected override IEnumerable<NodeTab> BuildTabs(Node node, IReadOnlyList<RelationshipGroup> groups, NodeView nodeView)
    {
        foreach (var tab in base.BuildTabs(node, groups, nodeView))
        {
            yield return tab;
        }

        yield return new NodeTab
        {
            Id         = "metadata",
            Label      = "Metadata",
            TabTitle   = () => TextBlock("Metadata"),
            TabContent = () => RenderMetadata(nodeView.Node)
        };
    }
}
Method Where it shows up
CompactView Search results, dense lists, tables.
PreviewAsync The preview sheet that opens when a result is clicked. The base builds it from BuildTabs.
ViewAsync The full page when the user navigates to the node. The base builds it from PreviewAsync.
GetDisplayPropertyNames, BuildTabs, RenderPropertyValue, … One part of the preview and the page.

LabelField also takes a fallback list ("Title|Name") or an interpolated label ("$Book {Title} ({Year})"), which often makes a CompactView override unnecessary.

Where the schema and the data come from

The renderer draws RecipeBook nodes, so the workspace needs the node type and some nodes. A front end does not create schemas: they belong to the workspace, created by the connector that writes the nodes or declared in the workspace definitions. A connector built on Curiosity.Library:

[Node]
public class RecipeBook
{
    [Key]      public string ISBN   { get; set; }
    [Property] public string Title  { get; set; }
    [Property] public string Author { get; set; }
    [Property] public int    Year   { get; set; }
    [Property] public string Genre  { get; set; }
}

using var graph = Graph.Connect("http://localhost:8080", token, "Recipe books");

await graph.CreateNodeSchemaAsync<RecipeBook>();

graph.AddOrUpdate(new RecipeBook { ISBN = "978-1594204616", Title = "Salt, Fat, Acid, Heat", Author = "Samin Nosrat", Year = 2017, Genre = "Technique" });

await graph.CommitPendingAsync();

The Connector Recipes cover the same from CSV, JSON and other sources.

See also