All files / src/components WorkflowViewer.vue

93.5% Statements 72/77
65.21% Branches 30/46
86.36% Functions 19/22
95.45% Lines 63/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336  30x                       30x                                       2x       2x                 2x                                             27x         27x 27x   27x 27x         59x       20x 2x       20x 2x           30x 30x 13x 13x     17x 17x 17x 17x   17x 34x 34x                         17x 17x 17x 34x   17x 17x 17x 17x 17x               17x                           17x       30x 30x 13x 13x     17x   17x 17x 17x 34x 51x 17x   17x             17x               17x               17x                           17x                               17x       29x 29x 29x 29x                       29x       27x 28x   1x 1x                                                                                                                                                                
<template>
  <div class="workflow-viewer">
    <VueFlow
      :id="flowId"
      :nodes="nodes"
      :edges="edges"
      :nodes-draggable="false"
      :nodes-connectable="false"
      :pan-on-scroll="false"
      :zoom-on-double-click="false"
      class="workflow-canvas"
      @init="onInit"
    >
      <Background pattern-color="#ddd" :gap="16" />
 
      <template #node-workflow="nodeProps">
        <!-- State Node -->
        <div
          v-if="!nodeProps.data.isTransition"
          class="viewer-node"
          :class="{
            'viewer-node--start': nodeProps.data.isStart,
            'viewer-node--current': nodeProps.data.stateId === currentStateId
          }"
        >
          <Handle id="left" type="target" :position="Position.Left" class="viewer-handle" />
          <Handle id="right" type="target" :position="Position.Right" class="viewer-handle" />
          <Handle id="top" type="target" :position="Position.Top" class="viewer-handle" />
          <Handle id="bottom" type="target" :position="Position.Bottom" class="viewer-handle" />
          <Handle id="left" type="source" :position="Position.Left" class="viewer-handle" />
          <Handle id="right" type="source" :position="Position.Right" class="viewer-handle" />
          <Handle id="top" type="source" :position="Position.Top" class="viewer-handle" />
          <Handle id="bottom" type="source" :position="Position.Bottom" class="viewer-handle" />
          <div class="viewer-node__label">{{ getTranslation(nodeProps.data.rawLabel) }}</div>
        </div>
 
        <!-- Transition Node -->
        <div v-else class="viewer-transition">
          <Handle id="left" type="target" :position="Position.Left" class="viewer-handle" />
          <Handle id="right" type="target" :position="Position.Right" class="viewer-handle" />
          <Handle id="top" type="target" :position="Position.Top" class="viewer-handle" />
          <Handle id="bottom" type="target" :position="Position.Bottom" class="viewer-handle" />
          <Handle id="left" type="source" :position="Position.Left" class="viewer-handle" />
          <Handle id="right" type="source" :position="Position.Right" class="viewer-handle" />
          <Handle id="top" type="source" :position="Position.Top" class="viewer-handle" />
          <Handle id="bottom" type="source" :position="Position.Bottom" class="viewer-handle" />
          <div class="viewer-transition__label">
            {{ getTranslation(nodeProps.data.rawLabel) || '…' }}
          </div>
        </div>
      </template>
    </VueFlow>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-WORKFLOW-003.1
import { ref, watch, nextTick } from 'vue'
import { VueFlow, Handle, Position } from '@vue-flow/core'
import { Background } from '@vue-flow/background'
import type { Node, Edge, VueFlowStore } from '@vue-flow/core'
import type { WorkflowDefinition } from '@/schemas/entity-schema'
 
interface Props {
  workflowDefinition?: WorkflowDefinition
  currentStateId?: string
  getTranslation: (value: string | Record<string, string> | null | undefined) => string
}
 
const props = withDefaults(defineProps<Props>(), {
  workflowDefinition: undefined,
  currentStateId: undefined
})
 
const flowId = `workflow-viewer-${Math.random().toString(36).slice(2, 9)}`
const vueFlowInstance = ref<VueFlowStore | null>(null)
 
const nodes = ref<Node[]>([])
const edges = ref<Edge[]>([])
 
// ── Geometry helpers (same logic as WorkflowEditor) ─────────────────────────
 
function isConnectionHorizontal(fromX: number, fromY: number, toX: number, toY: number): boolean {
  return Math.abs(toX - fromX) >= Math.abs(toY - fromY)
}
 
function getSourceHandle(fx: number, fy: number, tx: number, ty: number): string {
  if (isConnectionHorizontal(fx, fy, tx, ty)) return 'right'
  return fy < ty ? 'bottom' : 'top'
}
 
function getTargetHandle(fx: number, fy: number, tx: number, ty: number): string {
  if (isConnectionHorizontal(fx, fy, tx, ty)) return 'left'
  return fy < ty ? 'top' : 'bottom'
}
 
// ── Graph builders ───────────────────────────────────────────────────────────
 
function buildNodes() {
  const def = props.workflowDefinition
  if (!def?.states?.length) {
    nodes.value = []
    return
  }
 
  const SPACING_X = 320
  const STATE_Y = 150
  const TRANSITION_Y_ALIGN = 8
  const BACK_TRANSITION_Y_OFFSET = -100
 
  const stateNodes: Node[] = def.states.map((state, index) => {
    const position = def.positions?.[state.id] ?? { x: 50 + index * SPACING_X, y: STATE_Y }
    return {
      id: state.id,
      type: 'workflow',
      position,
      data: {
        rawLabel: state.label,
        stateId: state.id,
        isTransition: false,
        isStart: state.id === def.startStateId
      }
    }
  })
 
  const transitionNodes: Node[] = def.transitions.map((transition, index) => {
    const transitionId = `t${index}`
    const fromNode = stateNodes.find((n) => n.id === transition.from)
    const toNode = stateNodes.find((n) => n.id === transition.to)
 
    let position = def.positions?.[transitionId]
    Eif (!position && fromNode && toNode) {
      const midX = (fromNode.position.x + toNode.position.x) / 2
      const isBackward = fromNode.position.x > toNode.position.x
      position = {
        x: midX,
        y: isBackward
          ? STATE_Y + BACK_TRANSITION_Y_OFFSET + TRANSITION_Y_ALIGN
          : STATE_Y + TRANSITION_Y_ALIGN
      }
    }
 
    return {
      id: transitionId,
      type: 'workflow',
      position: position ?? { x: 50, y: STATE_Y + TRANSITION_Y_ALIGN },
      data: {
        rawLabel: transition.label ?? '',
        transitionIndex: index,
        isTransition: true,
        from: transition.from,
        to: transition.to
      }
    }
  })
 
  nodes.value = [...stateNodes, ...transitionNodes]
}
 
function buildEdges() {
  const def = props.workflowDefinition
  if (!def?.transitions?.length) {
    edges.value = []
    return
  }
 
  const newEdges: Edge[] = []
 
  def.transitions.forEach((transition, index) => {
    const transitionId = `t${index}`
    const fromNode = nodes.value.find((n) => n.id === transition.from)
    const toNode = nodes.value.find((n) => n.id === transition.to)
    const transitionNode = nodes.value.find((n) => n.id === transitionId)
    Iif (!fromNode || !toNode || !transitionNode) return
 
    const isH = isConnectionHorizontal(
      fromNode.position.x,
      fromNode.position.y,
      toNode.position.x,
      toNode.position.y
    )
 
    const tTargetHandle = isH
      ? fromNode.position.x < toNode.position.x
        ? 'left'
        : 'right'
      : fromNode.position.y < toNode.position.y
        ? 'top'
        : 'bottom'
 
    const tSourceHandle = isH
      ? fromNode.position.x < toNode.position.x
        ? 'right'
        : 'left'
      : fromNode.position.y < toNode.position.y
        ? 'bottom'
        : 'top'
 
    newEdges.push({
      id: `e${index}-from`,
      source: transition.from,
      sourceHandle: getSourceHandle(
        fromNode.position.x,
        fromNode.position.y,
        toNode.position.x,
        toNode.position.y
      ),
      target: transitionId,
      targetHandle: tTargetHandle,
      style: { strokeWidth: 2 }
    })
 
    newEdges.push({
      id: `e${index}-to`,
      source: transitionId,
      sourceHandle: tSourceHandle,
      target: transition.to,
      targetHandle: getTargetHandle(
        fromNode.position.x,
        fromNode.position.y,
        toNode.position.x,
        toNode.position.y
      ),
      markerEnd: 'arrowclosed',
      style: { strokeWidth: 2 }
    })
  })
 
  edges.value = newEdges
}
 
function buildGraph() {
  buildNodes()
  buildEdges()
  nextTick(() => {
    vueFlowInstance.value?.fitView({ padding: 0.25, duration: 150 })
  })
}
 
// ── Init & watchers ──────────────────────────────────────────────────────────
 
function onInit(instance: VueFlowStore) {
  vueFlowInstance.value = instance
  nextTick(() => instance.fitView({ padding: 0.25, duration: 150 }))
}
 
// Rebuild graph when the workflow structure changes (nodes/edges geometry).
watch(() => props.workflowDefinition, buildGraph, { deep: true, immediate: true })
 
// Re-render state nodes when currentStateId changes so the --current
// class updates.  No full fitView needed — just swap nodes/edges.
watch(
  () => props.currentStateId,
  () => {
    buildNodes()
    buildEdges()
  }
)
</script>
 
<style>
/* VueFlow global styles — must not be scoped */
@import '@vue-flow/core/dist/style.css';
@import '@vue-flow/core/dist/theme-default.css';
</style>
 
<style scoped>
.workflow-canvas {
  height: 260px;
  border-radius: 0.375rem;
  overflow: hidden;
}
 
/* Hide handles — they exist only to anchor edges */
.viewer-handle {
  opacity: 0 !important;
  pointer-events: none !important;
  width: 8px !important;
  height: 8px !important;
}
 
/* State node */
.viewer-node {
  padding: 10px 18px;
  border-radius: 8px;
  background: #fff;
  border: 2px solid #0d6efd;
  font-weight: 500;
  min-width: 110px;
  text-align: center;
  position: relative;
  pointer-events: none;
}
 
.viewer-node--start {
  border-color: #198754;
}
 
.viewer-node__label {
  font-size: 13px;
  color: #0d6efd;
}
 
.viewer-node--start .viewer-node__label {
  color: #198754;
}
 
/* Current state: gold highlight */
.viewer-node--current {
  border-color: #fd7e14;
  background: #fff8f0;
  box-shadow: 0 0 0 3px rgba(253, 126, 20, 0.25);
}
 
.viewer-node--current .viewer-node__label {
  color: #e8590c;
  font-weight: 700;
}
 
/* Transition node */
.viewer-transition {
  padding: 5px 12px;
  border-radius: 6px;
  background: #f8f9fa;
  border: 1px solid #adb5bd;
  text-align: center;
  pointer-events: none;
}
 
.viewer-transition__label {
  font-size: 11px;
  color: #6c757d;
  user-select: none;
}
</style>