Valider et enrichir de nouveaux bords
Deux hooks de connexion permettent au consommateur de créer sa propre périphérie. isValidConnection s'exécute en direct pendant le geste — une cible invalide dessine un aperçu rouge et ne peut pas être supprimée, et le hook avant-création n'est jamais consulté pour cela. onBeforeEdgeCreate s'exécute une fois qu'une cible valide est choisie, juste avant que le bord n'existe : renvoyez false pour veto, ou { accept: true, data, style, directed } pour accepter et tamponner le nouveau bord avec une étiquette, un style et une direction. Cela peut être asynchrone : l'aperçu du bord de l'ombre reste affiché jusqu'à ce qu'il se stabilise.
Cliquez sur Modifier le graphique (en haut à droite) → Ajouter un bord, puis connectez les nœuds. Ici, vous ne pouvez lier qu'une personne à un projet — les liens du même type sont prévisualisés en rouge et sont refusés en direct. Un dépôt valide simule une sauvegarde, puis crée un bord étiqueté et dirigé - sauf sur le projet archived, auquel le hook avant-création oppose son veto après coup.
// Both hooks live under the full UI mode's Edit Graph → Add Edge tool.
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: {
// Live predicate — runs on every hover while connecting. Only cross-type
// links (person ↔ project) are valid; anything else previews red and can't
// be dropped. onBeforeEdgeCreate is never consulted for a rejected target.
isValidConnection: (source, target) =>
source.getData().type !== target.getData().type,
// Before-create hook — fires once a valid target is picked, before the edge
// exists. Async here: we simulate persisting the link, then either veto or
// accept with an enriched label / style / direction.
onBeforeEdgeCreate: async ({ source, target, origin }) => {
// Veto after the fact: no new links onto an archived project.
if (source.getData().archived || target.getData().archived) {
graph.notifier.warning('Archived', 'Project Orion is archived — no new links.')
return false
}
graph.notifier.info('Saving…', `${source.getData().label} → ${target.getData().label}`)
await wait(600) // stand-in for a backend POST that could fail
const person = source.getData().type === 'person' ? source : target
const project = person === source ? target : source
graph.notifier.success('Linked', `${person.getData().label} assigned to ${project.getData().label}`)
// Accept and stamp the new edge — the consumer decides what it carries.
return {
accept: true,
data: { label: 'assigned-to', via: origin },
style: { edge: { strokeColor: '#6366f1' } },
directed: true
}
}
}
}const data = {
nodes: [
{ id: 'alice', data: { label: 'Alice', type: 'person' } },
{ id: 'bob', data: { label: 'Bob', type: 'person' } },
{ id: 'carol', data: { label: 'Carol', type: 'person' } },
{ id: 'atlas', data: { label: 'Project Atlas', type: 'project' } },
{ id: 'nova', data: { label: 'Project Nova', type: 'project' } },
{ id: 'orion', data: { label: 'Orion (archived)', type: 'project', archived: true } }
],
edges: [
{ from: 'alice', to: 'atlas', data: { label: 'assigned-to' } }
]
}