Files

227 lines
5.9 KiB
Vue

<template>
<VueFlow
class="workflow-editor"
:nodes="vueFlowNodes"
:edges="vueFlowEdges"
:zoom-on-scroll="false"
:nodes-draggable="false"
:zoom-on-drag="zoomOnScroll"
:pan-on-scroll="panOnScroll"
:node-drag-threshold="2000"
:zoom-on-double-click="zoomOnDoubleClick"
fit-view-on-init
:max-zoom="1"
:min-zoom="0.5"
>
<Controls :show-interactive="false" />
<Background pattern-color="#ededed" :size="3" :gap="15" />
<template #node-workflow-node>
<div v-if="trigger">
<WorkflowNode
:key="updateKey"
:node="trigger"
:debug="workflowDebug"
:read-only="readOnly"
:selected-node-id="selectedNodeId"
@add-node="emit('add-node', $event)"
@add-recipe="emit('add-recipe', $event)"
@remove-node="emit('remove-node', $event)"
@replace-node="emit('replace-node', $event)"
@select-node="emit('update:modelValue', $event.id)"
@move-node="emit('move-node', $event)"
@duplicate-node="emit('duplicate-node', $event)"
/>
</div>
<template v-else>
<!-- `nowheel` stops Vue Flow from panning the graph on wheel events,
so the trigger list scrolls instead. -->
<div class="workflow-editor__trigger-selector nowheel">
<WorkflowAddNodeMenu
:only-trigger="true"
@change="emit('add-node', { type: $event })"
/>
</div>
</template>
</template>
</VueFlow>
</template>
<script setup>
import { VueFlow, useVueFlow } from '@vue-flow/core'
import { Background } from '@vue-flow/background'
import { Controls } from '@vue-flow/controls'
import { ref, computed, watch, toRef, inject, provide, onMounted } from 'vue'
import debounce from 'lodash/debounce'
import WorkflowNode from '@baserow/modules/automation/components/workflow/WorkflowNode'
import WorkflowAddNodeMenu from '@baserow/modules/automation/components/workflow/WorkflowAddNodeMenu'
import NodeGraphHandler from '@baserow/modules/automation/utils/nodeGraphHandler'
import { assignGotoMarkers } from '@baserow/modules/automation/utils/gotoMarkers'
const props = defineProps({
nodes: {
type: Array,
required: true,
},
modelValue: {
type: [String, Number],
default: null,
},
isAddingNode: {
type: Boolean,
default: false,
},
readOnly: {
type: Boolean,
default: false,
},
})
const emit = defineEmits([
'update:modelValue',
'add-node',
'add-recipe',
'remove-node',
'replace-node',
'move-node',
'duplicate-node',
])
// Injected dependencies
const { $registry } = useNuxtApp()
const workflow = inject('workflow')
const workflowDebug = inject('workflowDebug')
// Vue Flow setup
const { onPaneClick } = useVueFlow()
const vueFlowEdges = []
// Local state
const zoomOnScroll = ref(false)
const panOnScroll = ref(true)
const zoomOnDoubleClick = ref(false)
const updateKey = ref(1)
// Computed properties
const selectedNodeId = toRef(props, 'modelValue')
// The goto connectors compare against numeric node ids, while `modelValue`
// can arrive as a string from the editor.
const selectedNodeIdNumber = computed(() =>
props.modelValue == null ? null : Number(props.modelValue)
)
// Every valid jump (e.g. a "Go to node" and its destination) gets a stable
// marker letter. Rather than draw a line between the two ends, both node cards
// show the same marker so the reader can pair them by eye.
const gotoJumps = computed(() => {
if (!workflow.value) {
return []
}
return assignGotoMarkers({
nodes: props.nodes,
getConnectionsFor: (node) =>
$registry
.get('node', node.type)
.getConnections({ workflow: workflow.value, node }),
})
})
// Per-node lookup of the markers entering ("in") and leaving ("out") it,
// provided to every node card. A marker is "active" while either end of its
// jump is the selected node, so selecting one end highlights its pair.
const gotoMarkers = computed(() => {
const selected = selectedNodeIdNumber.value
const byNode = {}
const push = (nodeId, entry) => {
;(byNode[nodeId] = byNode[nodeId] || []).push(entry)
}
for (const { sourceId, destinationId, marker } of gotoJumps.value) {
const active = sourceId === selected || destinationId === selected
// Each end's arrow points toward its pair. Only backward jumps are allowed
// (the destination runs before the Go to node), so the Go to node always
// points up to its destination and the destination points down.
push(sourceId, {
marker,
direction: 'out',
arrow: 'up',
active,
})
push(destinationId, {
marker,
direction: 'in',
arrow: 'down',
active,
})
}
return byNode
})
provide('gotoMarkers', gotoMarkers)
const trigger = computed(() => {
if (!workflow.value?.graph) {
return null
}
return new NodeGraphHandler(workflow.value).getFirstNode()
})
const vueFlowNodes = computed(() => {
return [
{
id: '1',
type: 'workflow-node',
selectable: false,
position: { x: 0, y: 0 },
},
]
})
const computedNodes = computed(() => props.nodes)
const currentGraph = computed(() => workflow.value?.graph)
// Debounced update function
const triggerUpdate = debounce(() => {
updateKey.value += 1
}, 500)
/**
* These watchers are used to force the update the workflow graph when nodes are updated.
* Vue-flow prevents the natural update.
*/
watch(
computedNodes,
() => {
triggerUpdate()
},
{ deep: true }
)
watch(
currentGraph,
() => {
updateKey.value += 1
},
{ deep: false }
)
/**
* When the component is mounted, we emit the first node's ID. This is
* to ensure that the first node (the trigger) is selected by default.
*/
onMounted(() => {
if (props.nodes.length) {
emit('update:modelValue', props.nodes[0].id)
}
})
/**
* When the pane is clicked, we emit `null` which
* clears the selected node in the node store.
*/
onPaneClick(() => {
emit('update:modelValue', null)
})
</script>