WorkflowEditor.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <vue-flow
  3. :id="props.id"
  4. :class="{ disabled: options.disabled }"
  5. :default-edge-options="{
  6. type: settings.lineType,
  7. updatable: !options.disabled,
  8. selectable: !options.disabled,
  9. markerEnd: settings.arrow ? MarkerType.ArrowClosed : '',
  10. }"
  11. >
  12. <Background />
  13. <MiniMap v-if="minimap" :node-class-name="minimapNodeClassName" />
  14. <div
  15. v-if="editorControls"
  16. class="flex items-end absolute p-4 left-0 bottom-0 z-10"
  17. >
  18. <slot name="controls-prepend" />
  19. <button
  20. v-tooltip.group="t('workflow.editor.resetZoom')"
  21. class="control-button mr-2"
  22. @click="editor.fitView()"
  23. >
  24. <v-remixicon name="riFullscreenLine" />
  25. </button>
  26. <div class="rounded-lg bg-white dark:bg-gray-800 inline-block">
  27. <button
  28. v-tooltip.group="t('workflow.editor.zoomOut')"
  29. class="p-2 rounded-lg relative z-10"
  30. @click="editor.zoomOut()"
  31. >
  32. <v-remixicon name="riSubtractLine" />
  33. </button>
  34. <hr class="h-6 border-r inline-block" />
  35. <button
  36. v-tooltip.group="t('workflow.editor.zoomIn')"
  37. class="p-2 rounded-lg"
  38. @click="editor.zoomIn()"
  39. >
  40. <v-remixicon name="riAddLine" />
  41. </button>
  42. </div>
  43. <editor-search-blocks :editor="editor" />
  44. <slot name="controls-append" />
  45. </div>
  46. <template v-for="(node, name) in nodeTypes" :key="name" #[name]="nodeProps">
  47. <component
  48. :is="node"
  49. v-bind="nodeProps"
  50. @delete="deleteBlock"
  51. @edit="editBlock(nodeProps, $event)"
  52. @update="updateBlockData(nodeProps.id, $event)"
  53. />
  54. </template>
  55. </vue-flow>
  56. </template>
  57. <script setup>
  58. import { onMounted, onBeforeUnmount } from 'vue';
  59. import { useI18n } from 'vue-i18n';
  60. import {
  61. VueFlow,
  62. MiniMap,
  63. Background,
  64. useVueFlow,
  65. MarkerType,
  66. } from '@braks/vue-flow';
  67. import cloneDeep from 'lodash.clonedeep';
  68. import { useStore } from '@/stores/main';
  69. import { tasks, categories } from '@/utils/shared';
  70. import EditorSearchBlocks from './editor/EditorSearchBlocks.vue';
  71. const props = defineProps({
  72. id: {
  73. type: String,
  74. default: 'editor',
  75. },
  76. data: {
  77. type: Object,
  78. default: () => ({
  79. x: 0,
  80. y: 0,
  81. zoom: 0,
  82. nodes: [],
  83. edges: [],
  84. }),
  85. },
  86. options: {
  87. type: Object,
  88. default: () => ({}),
  89. },
  90. editorControls: {
  91. type: Boolean,
  92. default: true,
  93. },
  94. minimap: {
  95. type: Boolean,
  96. default: true,
  97. },
  98. });
  99. const emit = defineEmits(['edit', 'init', 'update:node', 'delete:node']);
  100. const fallbackBlocks = {
  101. BlockBasic: ['BlockExportData'],
  102. BlockBasicWithFallback: ['BlockWebhook'],
  103. };
  104. const isMac = navigator.appVersion.indexOf('Mac') !== -1;
  105. const blockComponents = require.context('@/components/block', false, /\.vue$/);
  106. const nodeTypes = blockComponents.keys().reduce((acc, key) => {
  107. const name = key.replace(/(.\/)|\.vue$/g, '');
  108. const component = blockComponents(key).default;
  109. if (fallbackBlocks[name]) {
  110. fallbackBlocks[name].forEach((fallbackBlock) => {
  111. acc[`node-${fallbackBlock}`] = component;
  112. });
  113. }
  114. acc[`node-${name}`] = component;
  115. return acc;
  116. }, {});
  117. const getPosition = (position) => (Array.isArray(position) ? position : [0, 0]);
  118. const { t } = useI18n();
  119. const store = useStore();
  120. const editor = useVueFlow({
  121. id: props.id,
  122. minZoom: 0.4,
  123. edgeUpdaterRadius: 20,
  124. deleteKeyCode: 'Delete',
  125. elevateEdgesOnSelect: true,
  126. defaultZoom: props.data?.zoom ?? 0.7,
  127. multiSelectionKeyCode: isMac ? 'Meta' : 'Control',
  128. defaultPosition: getPosition(props.data?.position),
  129. ...props.options,
  130. });
  131. editor.onConnect((params) => {
  132. params.class = `source-${params.sourceHandle} target-${params.targetHandle}`;
  133. editor.addEdges([params]);
  134. });
  135. editor.onEdgeUpdate(({ edge, connection }) => {
  136. const isBothOutput =
  137. connection.sourceHandle.includes('output') &&
  138. connection.targetHandle.includes('output');
  139. if (isBothOutput) return;
  140. Object.assign(edge, connection);
  141. });
  142. const settings = store.settings.editor;
  143. function minimapNodeClassName({ label }) {
  144. const { category } = tasks[label];
  145. const { color } = categories[category];
  146. return color;
  147. }
  148. function updateBlockData(nodeId, data = {}) {
  149. if (props.options.disabled) return;
  150. const node = editor.getNode.value(nodeId);
  151. node.data = { ...node.data, ...data };
  152. emit('update:node', node);
  153. }
  154. function editBlock({ id, label, data }, additionalData = {}) {
  155. if (props.options.disabled) return;
  156. emit('edit', {
  157. id: label,
  158. blockId: id,
  159. data: cloneDeep(data),
  160. ...additionalData,
  161. });
  162. }
  163. function deleteBlock(nodeId) {
  164. if (props.options.disabled) return;
  165. editor.removeNodes([nodeId]);
  166. emit('delete:node', nodeId);
  167. }
  168. function onMousedown(event) {
  169. if (props.options.disabled && event.shiftKey) {
  170. event.stopPropagation();
  171. event.preventDefault();
  172. }
  173. }
  174. function applyFlowData() {
  175. if (settings.snapToGrid) {
  176. editor.snapToGrid.value = true;
  177. editor.snapGrid.value = Object.values(settings.snapGrid);
  178. }
  179. editor.setNodes(props.data.nodes || []);
  180. editor.setEdges(props.data.edges || []);
  181. editor.setTransform({
  182. x: props.data.x || 0,
  183. y: props.data.y || 0,
  184. zoom: props.data.zoom || 1,
  185. });
  186. }
  187. onMounted(() => {
  188. applyFlowData();
  189. window.addEventListener('mousedown', onMousedown, true);
  190. emit('init', editor);
  191. });
  192. onBeforeUnmount(() => {
  193. window.removeEventListener('mousedown', onMousedown, true);
  194. });
  195. </script>
  196. <style>
  197. @import '@braks/vue-flow/dist/style.css';
  198. @import '@braks/vue-flow/dist/theme-default.css';
  199. .control-button {
  200. @apply p-2 rounded-lg bg-white dark:bg-gray-800 transition-colors;
  201. }
  202. </style>