WorkflowBuilder.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 { useI18n } from 'vue-i18n';
  59. import { compare } from 'compare-versions';
  60. import emitter from 'tiny-emitter/instance';
  61. import { tasks } from '@/utils/shared';
  62. import { parseJSON } from '@/utils/helper';
  63. import { useGroupTooltip } from '@/composable/groupTooltip';
  64. import drawflow from '@/lib/drawflow';
  65. export default {
  66. props: {
  67. data: {
  68. type: [Object, String],
  69. default: null,
  70. },
  71. version: {
  72. type: String,
  73. default: '',
  74. },
  75. },
  76. emits: ['load', 'deleteBlock', 'update'],
  77. setup(props, { emit }) {
  78. useGroupTooltip();
  79. const { t } = useI18n();
  80. const contextMenuItems = {
  81. block: [
  82. {
  83. id: 'duplicate',
  84. name: t('workflow.editor.duplicate'),
  85. icon: 'riFileCopyLine',
  86. event: 'duplicateBlock',
  87. },
  88. {
  89. id: 'delete',
  90. name: t('common.delete'),
  91. icon: 'riDeleteBin7Line',
  92. event: 'deleteBlock',
  93. },
  94. ],
  95. };
  96. const editor = shallowRef(null);
  97. const contextMenu = reactive({
  98. items: [],
  99. data: null,
  100. show: false,
  101. position: {},
  102. });
  103. function dropHandler({ dataTransfer, clientX, clientY }) {
  104. const block = JSON.parse(dataTransfer.getData('block') || null);
  105. if (!block) return;
  106. const isTriggerExists =
  107. block.id === 'trigger' &&
  108. editor.value.getNodesFromName('trigger').length !== 0;
  109. if (!block || isTriggerExists) return;
  110. const xPosition =
  111. clientX *
  112. (editor.value.precanvas.clientWidth /
  113. (editor.value.precanvas.clientWidth * editor.value.zoom)) -
  114. editor.value.precanvas.getBoundingClientRect().x *
  115. (editor.value.precanvas.clientWidth /
  116. (editor.value.precanvas.clientWidth * editor.value.zoom));
  117. const yPosition =
  118. clientY *
  119. (editor.value.precanvas.clientHeight /
  120. (editor.value.precanvas.clientHeight * editor.value.zoom)) -
  121. editor.value.precanvas.getBoundingClientRect().y *
  122. (editor.value.precanvas.clientHeight /
  123. (editor.value.precanvas.clientHeight * editor.value.zoom));
  124. editor.value.addNode(
  125. block.id,
  126. block.inputs,
  127. block.outputs,
  128. xPosition,
  129. yPosition,
  130. block.id,
  131. block.data,
  132. block.component,
  133. 'vue'
  134. );
  135. emitter.emit('editor:data-changed');
  136. }
  137. function isInputAllowed(allowedInputs, input) {
  138. if (typeof allowedInputs === 'boolean') return allowedInputs;
  139. return allowedInputs.some((item) => {
  140. if (item.startsWith('#')) {
  141. return tasks[input].category === item.substr(1);
  142. }
  143. return item === input;
  144. });
  145. }
  146. function deleteBlock() {
  147. editor.value.removeNodeId(contextMenu.data);
  148. }
  149. function duplicateBlock() {
  150. const { name, pos_x, pos_y, data, html } = editor.value.getNodeFromId(
  151. contextMenu.data.substr(5)
  152. );
  153. if (name === 'trigger') return;
  154. const { outputs, inputs } = tasks[name];
  155. editor.value.addNode(
  156. name,
  157. inputs,
  158. outputs,
  159. pos_x + 50,
  160. pos_y + 100,
  161. name,
  162. data,
  163. html,
  164. 'vue'
  165. );
  166. }
  167. onMounted(() => {
  168. const context = getCurrentInstance().appContext.app._context;
  169. const element = document.querySelector('#drawflow');
  170. editor.value = drawflow(element, { context, options: { reroute: true } });
  171. editor.value.start();
  172. emit('load', editor.value);
  173. if (props.data) {
  174. let data =
  175. typeof props.data === 'string'
  176. ? parseJSON(props.data.replace(/BlockNewTab/g, 'BlockBasic'), null)
  177. : props.data;
  178. if (!data) return;
  179. const currentExtVersion = chrome.runtime.getManifest().version;
  180. const isOldWorkflow = compare(
  181. currentExtVersion,
  182. props.version || '0.0.0',
  183. '>'
  184. );
  185. if (isOldWorkflow) {
  186. const newDrawflowData = Object.entries(
  187. data.drawflow.Home.data
  188. ).reduce((obj, [key, value]) => {
  189. const newBlockData = {
  190. ...tasks[value.name],
  191. ...value,
  192. data: {
  193. ...tasks[value.name].data,
  194. ...value.data,
  195. },
  196. };
  197. obj[key] = newBlockData;
  198. return obj;
  199. }, {});
  200. const drawflowData = {
  201. drawflow: { Home: { data: newDrawflowData } },
  202. };
  203. data = drawflowData;
  204. emit('update', {
  205. version: currentExtVersion,
  206. drawflow: JSON.stringify(drawflowData),
  207. });
  208. }
  209. editor.value.import(data);
  210. } else {
  211. editor.value.addNode(
  212. 'trigger',
  213. 0,
  214. 1,
  215. 50,
  216. 300,
  217. 'trigger',
  218. tasks.trigger.data,
  219. 'BlockBasic',
  220. 'vue'
  221. );
  222. }
  223. editor.value.on('nodeRemoved', (id) => {
  224. emit('deleteBlock', id);
  225. });
  226. editor.value.on(
  227. 'connectionCreated',
  228. ({ output_id, input_id, output_class, input_class }) => {
  229. const { outputs } = editor.value.getNodeFromId(output_id);
  230. const { name: inputName } = editor.value.getNodeFromId(input_id);
  231. const { allowedInputs, maxConnection } = tasks[inputName];
  232. const isAllowed = isInputAllowed(allowedInputs, inputName);
  233. const isMaxConnections =
  234. outputs[output_class]?.connections.length > maxConnection;
  235. if (!isAllowed || isMaxConnections) {
  236. editor.value.removeSingleConnection(
  237. output_id,
  238. input_id,
  239. output_class,
  240. input_class
  241. );
  242. }
  243. emitter.emit('editor:data-changed');
  244. }
  245. );
  246. editor.value.on('connectionRemoved', () => {
  247. emitter.emit('editor:data-changed');
  248. });
  249. editor.value.on('contextmenu', ({ clientY, clientX, target }) => {
  250. const isBlock = target.closest('.drawflow .drawflow-node');
  251. if (isBlock) {
  252. const virtualEl = {
  253. getReferenceClientRect: () => ({
  254. width: 0,
  255. height: 0,
  256. top: clientY,
  257. right: clientX,
  258. bottom: clientY,
  259. left: clientX,
  260. }),
  261. };
  262. contextMenu.data = isBlock.id;
  263. contextMenu.position = virtualEl;
  264. contextMenu.items = contextMenuItems.block;
  265. contextMenu.show = true;
  266. }
  267. });
  268. setTimeout(() => {
  269. editor.value.zoom_refresh();
  270. }, 500);
  271. });
  272. return {
  273. t,
  274. editor,
  275. contextMenu,
  276. dropHandler,
  277. contextMenuHandler: {
  278. deleteBlock,
  279. duplicateBlock,
  280. },
  281. };
  282. },
  283. };
  284. </script>
  285. <style>
  286. #drawflow {
  287. background-image: url('@/assets/images/tile.png');
  288. background-size: 35px;
  289. }
  290. </style>