Beschriften Sie eine Kante, während Sie sie zeichnen
Erfassen Sie vom Benutzer die Beschriftung einer Kante, sobald diese gezeichnet wird – keine benutzerdefinierte modale Verkabelung – und wählen Sie die Benutzeroberfläche aus, die zur Geste passt. Der Kontext des Before-Create-Hooks bietet zwei Helfer: ctx.promptLabel({ mode }) löscht ein einzelnes Freitextfeld (inline am Mittelpunkt der Kante oder in einem Modal) und ctx.promptData({ fields }) öffnet ein modales Formular, das aus demselben Feldsystem wie der Knoteneditor erstellt wurde. Was auch immer eingegeben wird, wird zum data.label der neuen Kante; Durch Abbrechen (Auflösen von null) wird nichts erstellt.
Klicken Sie auf Diagramm bearbeiten (oben rechts) → Kante hinzufügen und verbinden Sie dann zwei Personen:
- Ziehen von einem Knoten zum anderen → ein schnelles Inline-Feld zum Eingeben einer beliebigen Beschriftung.
- Klicken Sie auf einen Knoten und dann auf einen anderen → ein Modal mit einem Dropdown-Menü mit vordefinierten Beziehungsbezeichnungen; Wählen Sie eine aus und Kante erstellen.
Im Null-Code-Fall fordert die statische Option UI: { editors: { edgeEditor: { labelPrompt: 'inline' } } } zur Eingabe einer Freitextbeschriftung an jeder Kante auf, ohne dass ein Rückruf erfolgt.
// The before-create hook picks the UI per gesture. A quick **drag** gets a free-text
// field inline at the edge midpoint (`ctx.promptLabel`); a deliberate **click-click**
// connect gets a modal with a dropdown of predefined labels (`ctx.promptData`). Both
// feed the chosen text into the new edge's `data.label`; a cancel (`null`) creates
// nothing.
const RELATIONSHIP_LABELS = [
{ value: 'mentors', label: 'mentors' },
{ value: 'reports to', label: 'reports to' },
{ value: 'collaborates with', label: 'collaborates with' },
{ value: 'manages', label: 'manages' }
]
const options = {
// Off: `full` mode brings a minimap and the data dock, and this card is about
// neither (see the Minimap and Data table cards for those).
UI: { mode: 'full', minimap: false, table: false },
callbacks: {
onBeforeEdgeCreate: async (ctx) => {
if (ctx.origin === 'drag') {
// Drag → quick inline free-text field.
const label = await ctx.promptLabel({ mode: 'inline', placeholder: 'Relationship…' })
if (label === null) return false
return { accept: true, data: { label }, directed: true }
}
// Click-click → modal with a dropdown of predefined labels.
const values = await ctx.promptData({
title: 'Label the connection',
submitLabel: 'Create edge',
fields: [
{
key: 'label',
label: 'Relationship',
type: 'select',
defaultValue: 'mentors',
options: RELATIONSHIP_LABELS
}
]
})
if (values === null) return false
return { accept: true, data: { label: values.label }, directed: true }
}
}
}const data = {
nodes: [
{ id: 'alice', data: { label: 'Alice' } },
{ id: 'bob', data: { label: 'Bob' } },
{ id: 'carol', data: { label: 'Carol' } },
{ id: 'dave', data: { label: 'Dave' } },
{ id: 'erin', data: { label: 'Erin' } }
],
edges: [
{ from: 'alice', to: 'bob', data: { label: 'mentors' } }
]
}