WorkflowBuilder.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <div
  3. id="drawflow"
  4. class="parent-drawflow relative"
  5. @drop="dropHandler"
  6. @dragover.prevent
  7. >
  8. <slot></slot>
  9. <div class="absolute z-10 p-4 bottom-0 left-0">
  10. <button
  11. v-tooltip.group="t('workflow.editor.resetZoom')"
  12. class="p-2 rounded-lg bg-white mr-2"
  13. @click="editor.zoom_reset()"
  14. >
  15. <v-remixicon name="riFullscreenLine" />
  16. </button>
  17. <div class="rounded-lg bg-white inline-block">
  18. <button
  19. v-tooltip.group="t('workflow.editor.zoomOut')"
  20. class="p-2 rounded-lg relative z-10"
  21. @click="editor.zoom_out()"
  22. >
  23. <v-remixicon name="riSubtractLine" />
  24. </button>
  25. <hr class="h-6 border-r inline-block" />
  26. <button
  27. v-tooltip.group="t('workflow.editor.zoomIn')"
  28. class="p-2 rounded-lg"
  29. @click="editor.zoom_in()"
  30. >
  31. <v-remixicon name="riAddLine" />
  32. </button>
  33. </div>
  34. </div>
  35. <ui-popover
  36. v-model="contextMenu.show"
  37. :options="contextMenu.position"
  38. padding="p-3"
  39. >
  40. <ui-list class="w-36 space-y-1">
  41. <ui-list-item
  42. v-for="item in contextMenu.items"
  43. :key="item.id"
  44. v-close-popover
  45. class="cursor-pointer"
  46. @click="contextMenuHandler[item.event]"
  47. >
  48. <v-remixicon :name="item.icon" class="mr-2 -ml-1" />
  49. <span>{{ item.name }}</span>
  50. </ui-list-item>
  51. </ui-list>
  52. </ui-popover>
  53. </div>
  54. </template>
  55. <script>
  56. /* eslint-disable camelcase */
  57. import { onMounted, shallowRef, reactive, getCurrentInstance } from 'vue';
  58. import emitter from 'tiny-emitter/instance';
  59. import { useI18n } from 'vue-i18n';
  60. import { tasks } from '@/utils/shared';
  61. import { parseJSON } from '@/utils/helper';
  62. import { useGroupTooltip } from '@/composable/groupTooltip';
  63. import drawflow from '@/lib/drawflow';
  64. export default {
  65. props: {
  66. data: {
  67. type: [Object, String],
  68. default: null,
  69. },
  70. },
  71. emits: ['load', 'deleteBlock'],
  72. setup(props, { emit }) {
  73. useGroupTooltip();
  74. const { t } = useI18n();
  75. const contextMenuItems = {
  76. block: [
  77. {
  78. id: 'duplicate',
  79. name: t('workflow.editor.duplicate'),
  80. icon: 'riFileCopyLine',
  81. event: 'duplicateBlock',
  82. },
  83. {
  84. id: 'delete',
  85. name: t('common.delete'),
  86. icon: 'riDeleteBin7Line',
  87. event: 'deleteBlock',
  88. },
  89. ],
  90. };
  91. const editor = shallowRef(null);
  92. const contextMenu = reactive({
  93. items: [],
  94. data: null,
  95. show: false,
  96. position: {},
  97. });
  98. function dropHandler({ dataTransfer, clientX, clientY }) {
  99. const block = JSON.parse(dataTransfer.getData('block') || null);
  100. if (!block) return;
  101. const isTriggerExists =
  102. block.id === 'trigger' &&
  103. editor.value.getNodesFromName('trigger').length !== 0;
  104. if (!block || isTriggerExists) return;
  105. const xPosition =
  106. clientX *
  107. (editor.value.precanvas.clientWidth /
  108. (editor.value.precanvas.clientWidth * editor.value.zoom)) -
  109. editor.value.precanvas.getBoundingClientRect().x *
  110. (editor.value.precanvas.clientWidth /
  111. (editor.value.precanvas.clientWidth * editor.value.zoom));
  112. const yPosition =
  113. clientY *
  114. (editor.value.precanvas.clientHeight /
  115. (editor.value.precanvas.clientHeight * editor.value.zoom)) -
  116. editor.value.precanvas.getBoundingClientRect().y *
  117. (editor.value.precanvas.clientHeight /
  118. (editor.value.precanvas.clientHeight * editor.value.zoom));
  119. editor.value.addNode(
  120. block.id,
  121. block.inputs,
  122. block.outputs,
  123. xPosition,
  124. yPosition,
  125. block.id,
  126. block.data,
  127. block.component,
  128. 'vue'
  129. );
  130. emitter.emit('editor:data-changed');
  131. }
  132. function isInputAllowed(allowedInputs, input) {
  133. if (typeof allowedInputs === 'boolean') return allowedInputs;
  134. return allowedInputs.some((item) => {
  135. if (item.startsWith('#')) {
  136. return tasks[input].category === item.substr(1);
  137. }
  138. return item === input;
  139. });
  140. }
  141. function deleteBlock() {
  142. editor.value.removeNodeId(contextMenu.data);
  143. }
  144. function duplicateBlock() {
  145. const { name, pos_x, pos_y, data, html } = editor.value.getNodeFromId(
  146. contextMenu.data.substr(5)
  147. );
  148. if (name === 'trigger') return;
  149. const { outputs, inputs } = tasks[name];
  150. editor.value.addNode(
  151. name,
  152. inputs,
  153. outputs,
  154. pos_x + 50,
  155. pos_y + 100,
  156. name,
  157. data,
  158. html,
  159. 'vue'
  160. );
  161. }
  162. onMounted(() => {
  163. const context = getCurrentInstance().appContext.app._context;
  164. const element = document.querySelector('#drawflow');
  165. editor.value = drawflow(element, { context, options: { reroute: true } });
  166. editor.value.start();
  167. emit('load', editor.value);
  168. if (props.data) {
  169. const data =
  170. typeof props.data === 'string'
  171. ? parseJSON(props.data.replace(/BlockNewTab/g, 'BlockBasic'), null)
  172. : props.data;
  173. if (!data) return;
  174. editor.value.import(data);
  175. } else {
  176. editor.value.addNode(
  177. 'trigger',
  178. 0,
  179. 1,
  180. 50,
  181. 300,
  182. 'trigger',
  183. tasks.trigger.data,
  184. 'BlockBasic',
  185. 'vue'
  186. );
  187. }
  188. editor.value.on('nodeRemoved', (id) => {
  189. emit('deleteBlock', id);
  190. });
  191. editor.value.on(
  192. 'connectionCreated',
  193. ({ output_id, input_id, output_class, input_class }) => {
  194. const { outputs } = editor.value.getNodeFromId(output_id);
  195. const { name: inputName } = editor.value.getNodeFromId(input_id);
  196. const { allowedInputs, maxConnection } = tasks[inputName];
  197. const isAllowed = isInputAllowed(allowedInputs, inputName);
  198. const isMaxConnections =
  199. outputs[output_class]?.connections.length > maxConnection;
  200. if (!isAllowed || isMaxConnections) {
  201. editor.value.removeSingleConnection(
  202. output_id,
  203. input_id,
  204. output_class,
  205. input_class
  206. );
  207. }
  208. emitter.emit('editor:data-changed');
  209. }
  210. );
  211. editor.value.on('connectionRemoved', () => {
  212. emitter.emit('editor:data-changed');
  213. });
  214. editor.value.on('contextmenu', ({ clientY, clientX, target }) => {
  215. const isBlock = target.closest('.drawflow .drawflow-node');
  216. if (isBlock) {
  217. const virtualEl = {
  218. getReferenceClientRect: () => ({
  219. width: 0,
  220. height: 0,
  221. top: clientY,
  222. right: clientX,
  223. bottom: clientY,
  224. left: clientX,
  225. }),
  226. };
  227. contextMenu.data = isBlock.id;
  228. contextMenu.position = virtualEl;
  229. contextMenu.items = contextMenuItems.block;
  230. contextMenu.show = true;
  231. }
  232. });
  233. setTimeout(() => {
  234. editor.value.zoom_refresh();
  235. }, 500);
  236. });
  237. return {
  238. t,
  239. editor,
  240. contextMenu,
  241. dropHandler,
  242. contextMenuHandler: {
  243. deleteBlock,
  244. duplicateBlock,
  245. },
  246. };
  247. },
  248. };
  249. </script>
  250. <style>
  251. #drawflow {
  252. background-image: url('@/assets/images/tile.png');
  253. background-size: 35px;
  254. }
  255. </style>