Example of Keyboard Interaction in ASP.NET MVC Diagram Control
Keyboard Interaction: everything below works with the focus on a node or a connector (Tab reaches them). The last two are the page's own commands.
| Keys | What they do |
|---|---|
| Tab, Shift+Tab | The next or previous node or connector (it is selected) |
| Alt+Arrow keys | The nearest node that way |
| Arrow keys (Shift: 10 pixels) | Move the selection |
| F2 | Edit the text |
| Delete | Delete the selection |
| Escape | Hide the tooltip, then clear the selection |
| Ctrl+Z, Ctrl+Y | Undo, redo |
| Ctrl+C, Ctrl+X, Ctrl+V, Ctrl+A | Copy, cut, paste, select all |
| Ctrl+G, Ctrl+Shift+U | Group, ungroup |
| Ctrl+Shift+F, Ctrl+Shift+B, Ctrl+], Ctrl+[ | Bring to front, send to back, forward, backward |
| Ctrl+= , Ctrl+-, Ctrl+0 | Zoom in, zoom out, 100% |
| Ctrl+Shift+N | Add a node under the selected one (the page's command) |
| Ctrl+Shift+H | Highlight the selection (the page's command) |
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Keyboard Interaction: everything below works with the focus on a node or a connector (Tab reaches them). The last two are the page's
own commands.
</p>
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p class="small text-muted mt-2" id="diagramStatus" role="status" aria-live="polite"></p>
<table class="table table-sm small mt-2" style="max-width:720px">
<caption>The keys</caption>
<thead><tr><th scope="col">Keys</th><th scope="col">What they do</th></tr></thead>
<tbody>
<tr><td>Tab, Shift+Tab</td><td>The next or previous node or connector (it is selected)</td></tr>
<tr><td>Alt+Arrow keys</td><td>The nearest node that way</td></tr>
<tr><td>Arrow keys (Shift: 10 pixels)</td><td>Move the selection</td></tr>
<tr><td>F2</td><td>Edit the text</td></tr>
<tr><td>Delete</td><td>Delete the selection</td></tr>
<tr><td>Escape</td><td>Hide the tooltip, then clear the selection</td></tr>
<tr><td>Ctrl+Z, Ctrl+Y</td><td>Undo, redo</td></tr>
<tr><td>Ctrl+C, Ctrl+X, Ctrl+V, Ctrl+A</td><td>Copy, cut, paste, select all</td></tr>
<tr><td>Ctrl+G, Ctrl+Shift+U</td><td>Group, ungroup</td></tr>
<tr><td>Ctrl+Shift+F, Ctrl+Shift+B, Ctrl+], Ctrl+[</td><td>Bring to front, send to back, forward, backward</td></tr>
<tr><td>Ctrl+= , Ctrl+-, Ctrl+0</td><td>Zoom in, zoom out, 100%</td></tr>
<tr><td>Ctrl+Shift+N</td><td>Add a node under the selected one (the page's command)</td></tr>
<tr><td>Ctrl+Shift+H</td><td>Highlight the selection (the page's command)</td></tr>
</tbody>
</table>
<script>
window.DiagramSamples = (function () {
'use strict';
function status(text) {
var el = document.getElementById('diagramStatus');
if (el) { el.textContent = text; }
}
/** The page's command Ctrl+Shift+N: a node under the selected one, connected to it. */
function addChild(args) {
var d = args.diagram;
var parent = args.selectedItems.nodes[0];
if (!parent) { status('Select a node first.'); return; }
var below = d.nodes.filter(function (n) { return Math.abs(n.offsetY - (parent.offsetY + 130)) < 1; }).length;
var made = d.addNode({ offsetX: parent.offsetX + below * 150, offsetY: parent.offsetY + 130, width: 120, height: 54, shape: { type: 'Flow', shape: 'Process' }, annotations: [{ content: 'New' }] });
d.addConnector({ sourceID: parent.id, targetID: made.id, type: 'Orthogonal' });
status('Added ' + made.id + ' under ' + parent.id + '.');
}
/** The page's command Ctrl+Shift+H: the selected nodes highlighted (or not any more). */
function highlight(args) {
var d = args.diagram;
var list = args.selectedItems.nodes;
if (!list.length) { status('Select a node first.'); return; }
list.forEach(function (n) { n.style.fill = n.style.fill === '#fff59d' ? null : '#fff59d'; });
d.dataBind();
status('Highlight changed for ' + list.map(function (n) { return n.id; }).join(', ') + '.');
}
/** OnSelectionChange: what is selected. */
function selected(args) {
status(args.newValue.length ? 'Selected: ' + args.newValue.map(function (o) { return o.id; }).join(', ') : 'Nothing selected.');
}
return { addChild: addChild, highlight: highlight, selected: selected };
})();
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DiagramController : Controller
{
// The page is Views/Diagram/KeyboardInteraction.cshtml; it draws what ViewData holds.
public ActionResult KeyboardInteraction()
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "keyboardDiagram",
Height = "440px",
AriaLabel = "Diagram for the keyboard",
Nodes = new List<UiDiagramNode>
{
Node("ceo", 340, 70, "Chief executive"),
Node("cto", 160, 200, "Technology"),
Node("cfo", 340, 200, "Finance"),
Node("coo", 520, 200, "Operations"),
Node("dev", 160, 330, "Development")
},
Connectors = new List<UiDiagramConnector> { Link("k1", "ceo", "cto"), Link("k2", "ceo", "cfo"), Link("k3", "ceo", "coo"), Link("k4", "cto", "dev") },
Commands = new List<UiDiagramCommand>
{
new UiDiagramCommand { Name = "addChild", Key = "n", Ctrl = true, Shift = true, Execute = "DiagramSamples.addChild" },
new UiDiagramCommand { Name = "highlight", Key = "h", Ctrl = true, Shift = true, Execute = "DiagramSamples.highlight" }
},
OnSelectionChange = "DiagramSamples.selected"
};
return View();
}
private static UiDiagramNode Node(string id, double x, double y, string text, UiDiagramShapeType type = UiDiagramShapeType.Flow, string shape = "Process",
double width = 120, double height = 54, string fill = null)
{
var node = new UiDiagramNode
{
Id = id, OffsetX = x, OffsetY = y, Width = width, Height = height,
Shape = new UiDiagramShape { Type = type, Shape = shape },
Style = new UiDiagramStyle { Fill = fill }
};
if (text != null) { node.Annotations.Add(new UiDiagramAnnotation { Content = text }); }
return node;
}
private static UiDiagramConnector Link(string id, string from, string to, string label = null,
UiDiagramConnectorType type = UiDiagramConnectorType.Orthogonal)
{
var c = new UiDiagramConnector { Id = id, SourceID = from, TargetID = to, Type = type };
if (label != null) { c.Annotations.Add(new UiDiagramAnnotation { Content = label, Offset = new UiDiagramPoint { X = 0.5, Y = 0 } }); }
return c;
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>