Etiqueta un borde a medida que lo dibujas
Recoge la etiqueta de un borde del usuario en el momento en que se dibuja (sin modal personalizado) cableado y elija la interfaz de usuario que se ajuste al gesto. El contexto del gancho antes de crear ofrece dos ayudantes: ctx.promptLabel({ mode }) suelta un único campo de texto libre (en línea en el punto medio del borde, o en un modal), y ctx.promptData({ fields }) abre un formulario modal creado a partir del mismo sistema de campo que el editor de nodos. Lo que sea que se ingrese se convierte en el data.label del nuevo borde; cancelar (resolver null) no crea nada.
Haga clic en Editar gráfico (arriba a la derecha) → Agregar borde, luego conecte a dos personas:
- Arrastre de un nodo a otro → un campo en línea rápido para escribir cualquier etiqueta.
- Haga clic en un nodo y luego en otro → un modal con un desplegable de predefinidos etiquetas de relación; elija uno y Crear borde.
Para el caso de código cero, la opción estática UI: { editors: { edgeEditor: { labelPrompt: 'inline' } } } solicita un texto libre etiqueta en cada borde sin devolución de llamada.
// 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' } }
]
}