Pārlūkot izejas kodu

feat: execute workflow from a specific block

Ahmad Kholid 2 gadi atpakaļ
vecāks
revīzija
bad4269e11

+ 16 - 4
src/components/newtab/workflow/editor/EditorLocalCtxMenu.vue

@@ -50,6 +50,7 @@ const emit = defineEmits([
   'saveBlock',
   'duplicate',
   'packageIo',
+  'execute:block',
 ]);
 
 const { t } = useI18n();
@@ -126,6 +127,11 @@ const menuItems = {
     name: 'Set as block output',
     event: () => emit('packageIo', { type: 'outputs', ...ctxData }),
   },
+  executeBlock: {
+    id: 'executeBlock',
+    name: 'Execute from here',
+    event: () => emit('execute:block', ctxData),
+  },
 };
 
 /* eslint-disable-next-line */
@@ -160,11 +166,17 @@ function clearContextMenu() {
 
 onMounted(() => {
   props.editor.onNodeContextMenu(({ event, node }) => {
-    const items = ['copy', 'duplicate', 'saveToFolder', 'delete'];
+    const items = [
+      'copy',
+      'duplicate',
+      'executeBlock',
+      'saveToFolder',
+      'delete',
+    ];
     if (node.label === 'blocks-group') {
-      items.splice(3, 0, 'ungroup');
+      items.splice(items.indexOf('saveToFolder'), 0, 'ungroup');
     } else if (!excludeGroupBlocks.includes(node.label)) {
-      items.splice(3, 0, 'group');
+      items.splice(items.indexOf('saveToFolder'), 0, 'group');
     }
 
     const currCtxData = {
@@ -184,7 +196,7 @@ onMounted(() => {
       if (props.isPackage && props.packageIo) {
         items.unshift(isOutput ? 'setAsOutput' : 'setAsInput');
       } else if (isOutput) {
-        items.splice(2, 0, 'startRecording');
+        items.splice(3, 0, 'startRecording');
       }
     }
 

+ 21 - 0
src/newtab/pages/workflows/[id].vue

@@ -231,6 +231,7 @@
             @paste="pasteCopiedElements"
             @saveBlock="initBlockFolder"
             @duplicate="duplicateElements"
+            @execute:block="executeFromBlock"
           />
         </ui-tab-panel>
         <ui-tab-panel value="logs" class="mt-24 container">
@@ -320,6 +321,7 @@ import { functions } from '@/utils/referenceData/mustacheReplacer';
 import { useGroupTooltip } from '@/composable/groupTooltip';
 import { useCommandManager } from '@/composable/commandManager';
 import { debounce, parseJSON, throttle } from '@/utils/helper';
+import { executeWorkflow } from '@/newtab/utils/workflowEngine';
 import { registerWorkflowTrigger } from '@/utils/workflowTrigger';
 import browser from 'webextension-polyfill';
 import dbStorage from '@/db/storage';
@@ -677,6 +679,25 @@ const onEdgesChange = debounce((changes) => {
 
 let nodeTargetHandle = null;
 
+async function executeFromBlock({ nodes }) {
+  try {
+    if (nodes.length === 0) return;
+
+    const [node] = nodes;
+    const workflowOptions = {
+      blockId: node.id,
+    };
+
+    const [tab] = await browser.tabs.query({ active: true, url: '*://*/*' });
+    if (tab) {
+      workflowOptions.tabId = tab.id;
+    }
+
+    executeWorkflow(workflow.value, workflowOptions);
+  } catch (error) {
+    console.error(error);
+  }
+}
 function startRecording({ nodeId, handleId }) {
   if (state.dataChanged) {
     alert('Make sure to save the workflow before starting recording');

+ 8 - 3
src/newtab/utils/workflowEngine/WorkflowEngine.js

@@ -107,7 +107,11 @@ class WorkflowEngine {
         return;
       }
 
-      const triggerBlock = nodes.find((node) => node.label === 'trigger');
+      const triggerBlock = nodes.find((node) => {
+        if (this.options?.blockId) return node.id === this.options.blockId;
+
+        return node.label === 'trigger';
+      });
       if (!triggerBlock) {
         console.error(`${this.workflow.name} doesn't have a trigger block`);
         return;
@@ -116,8 +120,9 @@ class WorkflowEngine {
       blocks = getBlocks();
 
       const checkParams = this.options?.checkParams ?? true;
-      const hasParams = triggerBlock.data.parameters?.length > 0;
-      if (checkParams && hasParams) {
+      const hasParams =
+        checkParams && triggerBlock.data?.parameters?.length > 0;
+      if (hasParams) {
         this.eventListeners = {};
 
         const paramUrl = browser.runtime.getURL('params.html');

+ 1 - 1
src/newtab/utils/workflowEngine/WorkflowWorker.js

@@ -296,7 +296,7 @@ class WorkflowWorker {
         this.reset();
 
         const triggerBlock = this.engine.blocks[this.engine.triggerBlockId];
-        this.executeBlock(triggerBlock, execParam);
+        if (triggerBlock) this.executeBlock(triggerBlock, execParam);
 
         localStorage.setItem(restartKey, restartCount + 1);
       } else {