Synertech Web/ui v0.57.5 Site Storybook

Documentation / Avancé

WorkflowEditor

Client@synertech/uiVoir dans Storybook

Un éditeur visuel de flux : des nœuds que l’on déplace, reliés par des courbes de Bézier tirées d’un port de sortie vers un port d’entrée, sur un canevas que l’on fait glisser et zoomer à la molette. La barre d’outils regroupe zoom, recentrage, ajustement au contenu et import / export JSON. Clic pour sélectionner, Suppr ou Retour arrière pour effacer, Échap pour désélectionner. Aucune dépendance externe : tout est rendu en HTML et SVG.

Le rendu des nœuds vous appartient : nodeRegistry associe chaque type de nœud à une fonction render, à ses ports (getPorts), à une règle canConnect et à des boutons d’action. Le composant est contrôlé (value + onValueChange) ou non contrôlé (defaultValue), et une ref expose zoomIn, fitToContent, addNode, toJSON, loadJSON… Le conteneur prend toute la largeur mais pas de hauteur : donnez-lui-en une via className. Composant "use client".

Exemples

Utilisation de base

Non contrôlé : defaultValue donne le flux initial, autoFit le cadre au montage.

Ouvrir dans un onglet
import { WorkflowEditor, type NodeRegistryEntry, type WorkflowValue } from "@synertech/ui";

interface StepData {
  label: string;
}

const box = (tone: string, selected: boolean, label: string) => (
  <div
    className={`flex h-full items-center justify-center rounded-lg border-2 bg-white px-3 text-xs font-medium text-gray-900 dark:bg-gray-800 dark:text-gray-100 ${selected ? "border-blue-500" : tone}`}
  >
    {label}
  </div>
);

const registry: Record<string, NodeRegistryEntry<StepData>> = {
  trigger: {
    title: "Déclencheur",
    render: ({ node, selected }) => box("border-green-300", selected, node.data.label),
    getPorts: () => [{ id: "out", kind: "out", side: "right" }],
  },
  action: {
    title: "Action",
    render: ({ node, selected }) => box("border-gray-300", selected, node.data.label),
    getPorts: () => [
      { id: "in", kind: "in", side: "left" },
      { id: "out", kind: "out", side: "right" },
    ],
    canConnect: ({ fromPort, toPort }) => fromPort.kind === "out" && toPort.kind === "in",
  },
};

const workflow: WorkflowValue<StepData> = {
  nodes: [
    {
      id: "n1",
      type: "trigger",
      x: 40,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Nouvelle commande" },
    },
    {
      id: "n2",
      type: "action",
      x: 280,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Envoyer la facture" },
    },
    {
      id: "n3",
      type: "action",
      x: 520,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Notifier l’entrepôt" },
    },
  ],
  edges: [
    { id: "e1", from: { nodeId: "n1", portId: "out" }, to: { nodeId: "n2", portId: "in" } },
    { id: "e2", from: { nodeId: "n2", portId: "out" }, to: { nodeId: "n3", portId: "in" } },
  ],
};

export function Basic() {
  return (
    <WorkflowEditor<StepData>
      nodeRegistry={registry}
      defaultValue={workflow}
      autoFit
      className="h-96 rounded-lg border"
    />
  );
}

Mode contrôlé

Chaque déplacement, connexion ou suppression passe par onValueChange.

Ouvrir dans un onglet
import { useState } from "react";
import { WorkflowEditor, type NodeRegistryEntry, type WorkflowValue } from "@synertech/ui";

interface StepData {
  label: string;
}

const workflow: WorkflowValue<StepData> = {
  nodes: [
    {
      id: "n1",
      type: "trigger",
      x: 40,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Nouvelle commande" },
    },
    {
      id: "n2",
      type: "action",
      x: 280,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Envoyer la facture" },
    },
    {
      id: "n3",
      type: "action",
      x: 520,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Notifier l’entrepôt" },
    },
  ],
  edges: [
    { id: "e1", from: { nodeId: "n1", portId: "out" }, to: { nodeId: "n2", portId: "in" } },
    { id: "e2", from: { nodeId: "n2", portId: "out" }, to: { nodeId: "n3", portId: "in" } },
  ],
};

const box = (tone: string, selected: boolean, label: string) => (
  <div
    className={`flex h-full items-center justify-center rounded-lg border-2 bg-white px-3 text-xs font-medium text-gray-900 dark:bg-gray-800 dark:text-gray-100 ${selected ? "border-blue-500" : tone}`}
  >
    {label}
  </div>
);

const registry: Record<string, NodeRegistryEntry<StepData>> = {
  trigger: {
    title: "Déclencheur",
    render: ({ node, selected }) => box("border-green-300", selected, node.data.label),
    getPorts: () => [{ id: "out", kind: "out", side: "right" }],
  },
  action: {
    title: "Action",
    render: ({ node, selected }) => box("border-gray-300", selected, node.data.label),
    getPorts: () => [
      { id: "in", kind: "in", side: "left" },
      { id: "out", kind: "out", side: "right" },
    ],
    canConnect: ({ fromPort, toPort }) => fromPort.kind === "out" && toPort.kind === "in",
  },
};

export function Controlled() {
  const [value, setValue] = useState(workflow);

  return (
    <div className="space-y-2">
      <p className="text-sm text-gray-600">
        {value.nodes.length} étapes, {value.edges.length} liaisons
      </p>
      <WorkflowEditor<StepData>
        nodeRegistry={registry}
        value={value}
        onValueChange={setValue}
        autoFit
        className="h-96 rounded-lg border"
      />
    </div>
  );
}

Pilotage par ref

addNode ajoute une étape, fitToContent recadre, toJSON sérialise le flux.

Ouvrir dans un onglet
import { useRef } from "react";
import { Button, WorkflowEditor, type NodeRegistryEntry, type WorkflowEditorRef, type WorkflowValue } from "@synertech/ui";

interface StepData {
  label: string;
}

const box = (tone: string, selected: boolean, label: string) => (
  <div
    className={`flex h-full items-center justify-center rounded-lg border-2 bg-white px-3 text-xs font-medium text-gray-900 dark:bg-gray-800 dark:text-gray-100 ${selected ? "border-blue-500" : tone}`}
  >
    {label}
  </div>
);

const registry: Record<string, NodeRegistryEntry<StepData>> = {
  trigger: {
    title: "Déclencheur",
    render: ({ node, selected }) => box("border-green-300", selected, node.data.label),
    getPorts: () => [{ id: "out", kind: "out", side: "right" }],
  },
  action: {
    title: "Action",
    render: ({ node, selected }) => box("border-gray-300", selected, node.data.label),
    getPorts: () => [
      { id: "in", kind: "in", side: "left" },
      { id: "out", kind: "out", side: "right" },
    ],
    canConnect: ({ fromPort, toPort }) => fromPort.kind === "out" && toPort.kind === "in",
  },
};

const workflow: WorkflowValue<StepData> = {
  nodes: [
    {
      id: "n1",
      type: "trigger",
      x: 40,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Nouvelle commande" },
    },
    {
      id: "n2",
      type: "action",
      x: 280,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Envoyer la facture" },
    },
    {
      id: "n3",
      type: "action",
      x: 520,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Notifier l’entrepôt" },
    },
  ],
  edges: [
    { id: "e1", from: { nodeId: "n1", portId: "out" }, to: { nodeId: "n2", portId: "in" } },
    { id: "e2", from: { nodeId: "n2", portId: "out" }, to: { nodeId: "n3", portId: "in" } },
  ],
};

export function WithRef() {
  const ref = useRef<WorkflowEditorRef>(null);

  const addStep = () =>
    ref.current?.addNode({
      id: `n${Date.now()}`,
      type: "action",
      x: 280,
      y: 200,
      width: 160,
      height: 56,
      data: { label: "Nouvelle étape" },
    });

  return (
    <div className="space-y-2">
      <div className="flex gap-2">
        <Button size="sm" onClick={addStep}>
          Ajouter une étape
        </Button>
        <Button size="sm" onClick={() => ref.current?.fitToContent()}>
          Recadrer
        </Button>
        <Button size="sm" onClick={() => console.log(ref.current?.toJSON())}>
          Exporter
        </Button>
      </div>
      <WorkflowEditor<StepData>
        ref={ref}
        nodeRegistry={registry}
        defaultValue={workflow}
        toolbar={false}
        className="h-96 rounded-lg border"
      />
    </div>
  );
}

Grille magnétique et orientation

snapToGrid aligne les nœuds sur gridSize ; onDirectionChange ajoute le bouton d’orientation à la barre d’outils.

Ouvrir dans un onglet
import { useState } from "react";
import { WorkflowEditor, type NodeRegistryEntry, type WorkflowDirection, type WorkflowValue } from "@synertech/ui";

interface StepData {
  label: string;
}

const box = (tone: string, selected: boolean, label: string) => (
  <div
    className={`flex h-full items-center justify-center rounded-lg border-2 bg-white px-3 text-xs font-medium text-gray-900 dark:bg-gray-800 dark:text-gray-100 ${selected ? "border-blue-500" : tone}`}
  >
    {label}
  </div>
);

const registry: Record<string, NodeRegistryEntry<StepData>> = {
  trigger: {
    title: "Déclencheur",
    render: ({ node, selected }) => box("border-green-300", selected, node.data.label),
    getPorts: () => [{ id: "out", kind: "out", side: "right" }],
  },
  action: {
    title: "Action",
    render: ({ node, selected }) => box("border-gray-300", selected, node.data.label),
    getPorts: () => [
      { id: "in", kind: "in", side: "left" },
      { id: "out", kind: "out", side: "right" },
    ],
    canConnect: ({ fromPort, toPort }) => fromPort.kind === "out" && toPort.kind === "in",
  },
};

const workflow: WorkflowValue<StepData> = {
  nodes: [
    {
      id: "n1",
      type: "trigger",
      x: 40,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Nouvelle commande" },
    },
    {
      id: "n2",
      type: "action",
      x: 280,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Envoyer la facture" },
    },
    {
      id: "n3",
      type: "action",
      x: 520,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Notifier l’entrepôt" },
    },
  ],
  edges: [
    { id: "e1", from: { nodeId: "n1", portId: "out" }, to: { nodeId: "n2", portId: "in" } },
    { id: "e2", from: { nodeId: "n2", portId: "out" }, to: { nodeId: "n3", portId: "in" } },
  ],
};

export function GridAndDirection() {
  const [direction, setDirection] = useState<WorkflowDirection>("horizontal");

  return (
    <WorkflowEditor<StepData>
      nodeRegistry={registry}
      defaultValue={workflow}
      snapToGrid
      gridSize={20}
      direction={direction}
      onDirectionChange={setDirection}
      className="h-96 rounded-lg border"
    />
  );
}

Lecture seule et événements

readOnly fige le flux et masque la barre d’outils ; onNodeClick reste disponible pour afficher un détail.

Ouvrir dans un onglet
import { useState } from "react";
import { WorkflowEditor, type NodeRegistryEntry, type WorkflowValue } from "@synertech/ui";

interface StepData {
  label: string;
}

const box = (tone: string, selected: boolean, label: string) => (
  <div
    className={`flex h-full items-center justify-center rounded-lg border-2 bg-white px-3 text-xs font-medium text-gray-900 dark:bg-gray-800 dark:text-gray-100 ${selected ? "border-blue-500" : tone}`}
  >
    {label}
  </div>
);

const registry: Record<string, NodeRegistryEntry<StepData>> = {
  trigger: {
    title: "Déclencheur",
    render: ({ node, selected }) => box("border-green-300", selected, node.data.label),
    getPorts: () => [{ id: "out", kind: "out", side: "right" }],
  },
  action: {
    title: "Action",
    render: ({ node, selected }) => box("border-gray-300", selected, node.data.label),
    getPorts: () => [
      { id: "in", kind: "in", side: "left" },
      { id: "out", kind: "out", side: "right" },
    ],
    canConnect: ({ fromPort, toPort }) => fromPort.kind === "out" && toPort.kind === "in",
  },
};

const workflow: WorkflowValue<StepData> = {
  nodes: [
    {
      id: "n1",
      type: "trigger",
      x: 40,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Nouvelle commande" },
    },
    {
      id: "n2",
      type: "action",
      x: 280,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Envoyer la facture" },
    },
    {
      id: "n3",
      type: "action",
      x: 520,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Notifier l’entrepôt" },
    },
  ],
  edges: [
    { id: "e1", from: { nodeId: "n1", portId: "out" }, to: { nodeId: "n2", portId: "in" } },
    { id: "e2", from: { nodeId: "n2", portId: "out" }, to: { nodeId: "n3", portId: "in" } },
  ],
};

export function ReadOnly() {
  const [selected, setSelected] = useState<string | null>(null);

  return (
    <div className="space-y-2">
      <p className="text-sm text-gray-600">Étape : {selected ?? "aucune"}</p>
      <WorkflowEditor<StepData>
        nodeRegistry={registry}
        defaultValue={workflow}
        readOnly
        autoFit
        onNodeClick={({ node }) => setSelected(node.data.label)}
        className="h-80 rounded-lg border"
      />
    </div>
  );
}

Référence d’API

Composants

WorkflowEditor

ComposantClient
import { WorkflowEditor } from "@synertech/ui";

Props — WorkflowEditorProps

PropTypeDéfautDescription
nodeRegistry• Record<string, NodeRegistryEntry<TData>> —
autoFit boolean false

Ajuste la vue aux nœuds une fois, dès qu’il y a des nœuds à ajuster : au montage, ou à l’arrivée d’un workflow chargé plus tard.

className string —
connectable boolean true
defaultValue WorkflowValue<TData> —
direction WorkflowDirection "horizontal"
disabled boolean false
draggableNodes boolean true
gridSize number 20
labels UILabelsOf<"workflowEditor" | "editor"> —

Les chaînes que l’éditeur écrit lui-même, pour cette instance : toute clé workflowEditor… de UILabels, et les clés editor… que sa barre d’outils partage avec SchemaEditor. L’emporte sur UILabelsProvider.

maxZoom number 3
minZoom number 0.1
multiSelect boolean false
onDirectionChange (direction: WorkflowDirection) => void —
onNodeAction (event: WorkflowNodeActionEvent<TData>) => void —
onNodeClick (event: WorkflowNodeEvent<TData>) => void —
onNodeContextMenu (event: WorkflowNodeEvent<TData>) => void —
onNodeDoubleClick (event: WorkflowNodeEvent<TData>) => void —
onValueChange (next: WorkflowValue<TData>) => void —
panOnDrag boolean true
readOnly boolean false
ref React.Ref<WorkflowEditorRef> —
selectable boolean true
showGrid boolean true
snapToGrid boolean false
toolbar boolean true
value WorkflowValue<TData> —
zoomOnWheel boolean true

Types

type NodeRegistryEntry<TData = unknown> = { … }
ChampTypeDéfautDescription
render• (args: { node: WorkflowNode<TData>; selected: boolean; disabled: boolean }) => ReactNode —
actions | WorkflowNodeAction<TData>[] | ((node: WorkflowNode<TData>) => WorkflowNodeAction<TData>[]) —
canConnect (args: { fromNode: WorkflowNode<TData>; toNode: WorkflowNode<TData>; fromPort: WorkflowPort; toPort: WorkflowPort; }) => boolean —
getPorts (node: WorkflowNode<TData>) => WorkflowPort[] —
icon ReactNode —
title string —

WorkflowEdge

Interface
type WorkflowEdge = { … }
ChampTypeDéfautDescription
from• { nodeId: string; portId: string } —
id• string —
to• { nodeId: string; portId: string } —
disabled boolean —
label string —
type WorkflowEditorRef = { … }
ChampTypeDéfautDescription
addNode• (node: WorkflowNode<unknown>) => void —
fitToContent• () => void —
loadJSON• (json: string) => boolean —

Charge un workflow écrit en JSON, et dit s’il l’a fait : un texte qui n’est pas du JSON, ou un JSON qui n’est pas un workflow que cet éditeur peut ouvrir, ne change rien.

removeNode• (nodeId: string) => void —
resetView• () => void —
toJSON• () => string —
zoomIn• () => void —
zoomOut• () => void —

WorkflowNode

Interface
type WorkflowNode<TData = unknown> = { … }
ChampTypeDéfautDescription
data• TData —
id• string —
type• string —
x• number —
y• number —
height number —
locked boolean —
ports WorkflowPort[] —
width number —
type WorkflowNodeAction<TData = unknown> = { … }
ChampTypeDéfautDescription
icon• ReactNode —
id• string —
className string —
hidden boolean | ((node: WorkflowNode<TData>) => boolean) —
label string —
position WorkflowNodeActionPosition —
type WorkflowNodeActionEvent<TData = unknown> = { … }
ChampTypeDéfautDescription
actionId• string —
nativeEvent• globalThis.MouseEvent —
node• WorkflowNode<TData> —
type WorkflowNodeEvent<TData = unknown> = { … }
ChampTypeDéfautDescription
nativeEvent• globalThis.MouseEvent —
node• WorkflowNode<TData> —

WorkflowPort

Interface
type WorkflowPort = { … }
ChampTypeDéfautDescription
id• string —
kind• "in" | "out" —
disabled boolean —
label string —
side "left" | "right" | "top" | "bottom" —

WorkflowValue

Interface
type WorkflowValue<TData = unknown> = { … }
ChampTypeDéfautDescription
edges• WorkflowEdge[] —
nodes• WorkflowNode<TData>[] —
viewport WorkflowViewport —
type WorkflowViewport = { … }
ChampTypeDéfautDescription
x• number —
y• number —
zoom• number —
type WorkflowDirection = "horizontal" | "vertical"
type WorkflowNodeActionPosition = "top-right" | "top-left" | "bottom-right" | "bottom-left"