SoftwareDriven.Blockly.Blazor
1.8.0
dotnet add package SoftwareDriven.Blockly.Blazor --version 1.8.0
NuGet\Install-Package SoftwareDriven.Blockly.Blazor -Version 1.8.0
<PackageReference Include="SoftwareDriven.Blockly.Blazor" Version="1.8.0" />
<PackageVersion Include="SoftwareDriven.Blockly.Blazor" Version="1.8.0" />
<PackageReference Include="SoftwareDriven.Blockly.Blazor" />
paket add SoftwareDriven.Blockly.Blazor --version 1.8.0
#r "nuget: SoftwareDriven.Blockly.Blazor, 1.8.0"
#:package SoftwareDriven.Blockly.Blazor@1.8.0
#addin nuget:?package=SoftwareDriven.Blockly.Blazor&version=1.8.0
#tool nuget:?package=SoftwareDriven.Blockly.Blazor&version=1.8.0
SoftwareDriven.Blockly.Blazor
A Razor component that embeds a Blockly editor into a Blazor application.
The component hosts the Blockly JavaScript editor through JS interop and hands the workspace back to .NET as Blockly XML. That XML is executed server-side in C# by SoftwareDriven.Blockly.Code — Blockly's own JavaScript code generators are not used.
The required Blockly bundle ships inside the package; npm is only needed to build this package from source, not to consume it.
Installation
dotnet add package SoftwareDriven.Blockly.Blazor
Target framework: net10.0. The package references SoftwareDriven.Blockly.Code.
Add the bundled script to the host page — wwwroot/index.html for Blazor WebAssembly,
App.razor / _Host.cshtml for Blazor Server — after the Blazor script:
<script src="_framework/blazor.webassembly.js"></script>
<script src="_content/SoftwareDriven.Blockly.Blazor/js/index.bundle.js"></script>
Quick start
@using SoftwareDriven.Blockly.Blazor.Components
<div id="workspaceArea" style="width: auto; height: 800px;">
<Workspace @ref="workspace"
WorkspaceAreaName="workspaceArea"
Toolbox="@Toolbox"
BlocksJson="@BlocksJson"
OnWorkspaceChanged="OnWorkspaceChanged" />
</div>
@code {
private Workspace? workspace;
private Toolbox? Toolbox { get; set; }
private string? BlocksJson { get; set; }
private async Task Save()
{
var xml = await workspace!.Export(false);
// ... store or execute the XML.
}
private void OnWorkspaceChanged() { /* mark as dirty ... */ }
}
The component renders a single div that is positioned and resized over the element
named by WorkspaceAreaName. That container must have a size — without an explicit
height the editor stays invisible. If WorkspaceAreaName is not set, the parent element
is used instead.
Toolbox and BlocksJson normally come from the server, where the same
IBlockRepository that later executes the program also describes its blocks:
BlocksJson = await httpClient.GetStringAsync("api/blockly/blocks");
Toolbox = await httpClient.GetFromJsonAsync<Toolbox>("api/blockly/toolbox");
Executing the workspace
Export() returns the Blockly XML, which BlocklyXmlReader turns into an executable
program; BlocklyXmlWriter produces XML that Import() accepts again.
using SoftwareDriven.Blockly.Code;
using SoftwareDriven.Blockly.Code.Parser;
var xml = await workspace.Export(toMinimal: false);
var reader = new BlocklyXmlReader();
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
var control = reader.ReadStream(stream, new BlockControlConfig()
{
TypeRegistry = typeRegistry,
BlockRepository = blockRepository,
});
control?.Run(new RunContext() { Caller = "Editor" });
// Load a stored program back into the editor.
await workspace.Import(xml, replace: true);
Parameters
| Parameter | Description |
|---|---|
WorkspaceAreaName |
The id of the element the editor is sized to. Defaults to the parent element. |
Toolbox |
The toolbox (SoftwareDriven.Blockly.Code.Editor.Toolbox). |
BlocksJson |
The Blockly block definitions as JSON. |
VariableTypes |
The typed-variable types, a JSON array of [displayName, typeName] pairs. |
OnWorkspaceChanged |
Raised on every non-UI change in the workspace. |
OnCreateTypedVariable |
Supplies an own dialog for creating typed variables. When set, the built-in dialog is not used. |
OnGetVariableTooltip |
Returns the tooltip for a variable, given its ID, type and whether it is a set or a get block. |
OnAlert |
Raised instead of the browser alert. |
Methods and events
| Member | Description |
|---|---|
Export(bool toMinimal) |
Returns the workspace as Blockly XML. |
Import(string workspaceXml, bool replace) |
Loads XML into the workspace, replacing or appending. |
SetLocale(string localeKey) |
Switches the Blockly locale and the component's own texts. |
static AddLocalizationResource(ResourceManager) |
Adds an own .resx resource to the localization. |
static InitializeBlocks |
Raised right before the editor is created — the place to supply toolbox, blocks and types. |
static Initialized |
Raised after the editor has been created. |
static GetMembersOfTypeEvent |
Asked for the members of an object type, e.g. for the member access blocks. |
InitializeBlocks, Initialized and GetMembersOfTypeEvent are static events —
subscribe before the Workspace renders for the first time and unsubscribe in
Dispose().
Supplying toolbox, blocks and types via the initialize event
The InitializeBlocks event is the alternative to the parameters above and is the only
way to register additional JavaScript block definitions:
protected override async Task OnInitializedAsync()
{
Workspace.InitializeBlocks += OnInitializeBlocks;
Workspace.GetMembersOfTypeEvent += OnGetMembersOfType;
}
void IDisposable.Dispose()
{
Workspace.InitializeBlocks -= OnInitializeBlocks;
Workspace.GetMembersOfTypeEvent -= OnGetMembersOfType;
}
private void OnInitializeBlocks(object? sender, InitializeEventArgs args)
{
args.Toolbox = Toolbox;
args.BlocksJson = BlocksJson;
args.VariableTypes = JsonSerializer.Serialize(
VariableTypes.Select(type => new[] { type.DisplayName, type.TypeName }));
// The name of a global JS function defining additional blocks.
args.CustomBlocksFunction = "initCustomBlocks";
args.LocaleKey = "de";
}
private void OnGetMembersOfType(object? sender, GetMembersOfTypeEventArgs args)
{
args.Members = TypeRegistry?.GetMembersOfType(args.Type)?
.Select(member => new TypeMember() { Name = member.MemberName, Type = member.TypeName })
.ToList() ?? new();
}
Typed variables
When VariableTypes is set, Blockly's typed-variable dialog is used. Set
OnCreateTypedVariable to replace it with an own dialog:
private async Task<VariableModel> OnCreateTypedVariable(VariableModel[] usedVariables)
{
var result = await ShowMyDialogAsync(usedVariables);
return result ?? new VariableModel(); // An empty model cancels the creation.
}
Localization
The component ships English and German texts; Blockly itself is bundled with en, de,
es and fr. Own resources are merged in, and the locale is applied to both Blockly and
the component:
Workspace.AddLocalizationResource(Resources.ResourceManager);
await workspace.SetLocale("de");
Category and block names may use Blockly's message references, e.g.
%{BKY_CATEGORY_LOGIC}, so that they are translated from those resources.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.AspNetCore.Components (>= 10.0.11)
- Microsoft.AspNetCore.Components.Web (>= 10.0.11)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.11)
- SoftwareDriven.Blockly.Code (>= 1.8.0)
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 |
|---|---|---|
| 1.8.0 | 34 | 8/25/2026 |
| 1.7.10 | 372 | 6/20/2025 |
| 1.7.7 | 393 | 3/6/2025 |
| 1.7.1 | 255 | 12/5/2024 |
| 1.7.0 | 269 | 9/26/2024 |
| 1.6.2 | 304 | 3/6/2024 |
| 1.6.0 | 263 | 1/22/2024 |
| 1.5.2 | 269 | 12/21/2023 |
| 1.5.1 | 244 | 12/21/2023 |
| 1.4.0 | 272 | 11/27/2023 |
| 1.1.1 | 754 | 12/23/2021 |
| 1.1.0 | 478 | 12/23/2021 |
| 1.0.11 | 522 | 11/30/2021 |
| 1.0.10 | 501 | 11/30/2021 |
| 1.0.9 | 1,370 | 11/30/2021 |
| 1.0.8.1 | 772 | 11/21/2021 |