Browse Source

feat: execute workflow through JS custom event

Ahmad Kholid 3 years ago
parent
commit
5e34c056e7

+ 3 - 1
src/background/collection-engine/flow-handler.js

@@ -35,7 +35,9 @@ export function workflow(flow) {
         isCollection: true,
         isCollection: true,
         name: this.collection.name,
         name: this.collection.name,
       },
       },
-      globalData: globalData.trim() === '' ? null : globalData,
+      data: {
+        globalData: globalData.trim() === '' ? null : globalData,
+      },
     });
     });
 
 
     this.executedWorkflow.data = {
     this.executedWorkflow.data = {

+ 1 - 1
src/background/index.js

@@ -370,7 +370,7 @@ message.on('collection:execute', (collection) => {
 });
 });
 
 
 message.on('workflow:execute', (workflowData) => {
 message.on('workflow:execute', (workflowData) => {
-  workflow.execute(workflowData);
+  workflow.execute(workflowData, workflowData?.options || {});
 });
 });
 message.on('workflow:stop', async (id) => {
 message.on('workflow:stop', async (id) => {
   await workflow.states.stop(id);
   await workflow.states.stop(id);

+ 3 - 1
src/background/workflow-engine/blocks-handler/handler-execute-workflow.js

@@ -77,7 +77,9 @@ async function executeWorkflow({ outputs, data }) {
       states: this.states,
       states: this.states,
       logger: this.logger,
       logger: this.logger,
       blocksHandler: this.blocksHandler,
       blocksHandler: this.blocksHandler,
-      globalData: isWhitespace(data.globalData) ? null : data.globalData,
+      data: {
+        globalData: isWhitespace(data.globalData) ? null : data.globalData,
+      },
     };
     };
 
 
     if (workflow.drawflow.includes(this.workflow.id)) {
     if (workflow.drawflow.includes(this.workflow.id)) {

+ 5 - 4
src/background/workflow-engine/engine.js

@@ -9,7 +9,7 @@ import executeContentScript from './execute-content-script';
 class WorkflowEngine {
 class WorkflowEngine {
   constructor(
   constructor(
     workflow,
     workflow,
-    { states, logger, blocksHandler, tabId, parentWorkflow, globalData }
+    { states, logger, blocksHandler, tabId, parentWorkflow, data }
   ) {
   ) {
     this.id = nanoid();
     this.id = nanoid();
     this.states = states;
     this.states = states;
@@ -34,7 +34,8 @@ class WorkflowEngine {
     this.eventListeners = {};
     this.eventListeners = {};
     this.columns = { column: { index: 0, type: 'any' } };
     this.columns = { column: { index: 0, type: 'any' } };
 
 
-    const globalDataValue = globalData || workflow.globalData;
+    const globalData = data?.globalData || workflow.globalData;
+    const variables = isObject(data?.variables) ? data.variables : {};
 
 
     this.activeTab = {
     this.activeTab = {
       url: '',
       url: '',
@@ -44,12 +45,12 @@ class WorkflowEngine {
       groupId: null,
       groupId: null,
     };
     };
     this.referenceData = {
     this.referenceData = {
+      variables,
       table: [],
       table: [],
       loopData: {},
       loopData: {},
       workflow: {},
       workflow: {},
-      variables: {},
       googleSheets: {},
       googleSheets: {},
-      globalData: parseJSON(globalDataValue, globalDataValue),
+      globalData: parseJSON(globalData, globalData),
     };
     };
 
 
     this.onWorkflowStopped = (id) => {
     this.onWorkflowStopped = (id) => {

+ 6 - 4
src/components/newtab/workflow/edit/EditExecuteWorkflow.vue

@@ -85,10 +85,12 @@ const state = shallowReactive({
 
 
 const workflows = computed(() =>
 const workflows = computed(() =>
   Workflow.query()
   Workflow.query()
-    .where(
-      ({ id, drawflow }) =>
-        id !== route.params.id && !drawflow.includes(route.params.id)
-    )
+    .where(({ id, drawflow }) => {
+      const flow =
+        typeof drawflow === 'string' ? drawflow : JSON.stringify(drawflow);
+
+      return id !== route.params.id && !flow.includes(route.params.id);
+    })
     .orderBy('name', 'asc')
     .orderBy('name', 'asc')
     .get()
     .get()
 );
 );

+ 55 - 26
src/content/services/shortcut-listener.js

@@ -6,6 +6,56 @@ Mousetrap.prototype.stopCallback = function () {
   return false;
   return false;
 };
 };
 
 
+function automaCustomEventListener(findWorkflow) {
+  window.addEventListener(
+    'automa:execute-workflow',
+    ({ detail }) => {
+      if (!detail || !detail.id) return;
+
+      const workflow = findWorkflow(detail.id);
+
+      if (!workflow) return;
+
+      workflow.options = {
+        data: detail.data || {},
+      };
+      sendMessage('workflow:execute', workflow, 'background');
+    },
+    true
+  );
+}
+function workflowShortcutsListener(findWorkflow, shortcutsObj) {
+  const shortcuts = Object.entries(shortcutsObj);
+
+  if (shortcuts.length === 0) return;
+
+  const keyboardShortcuts = shortcuts.reduce((acc, [id, value]) => {
+    const workflow = findWorkflow(id);
+
+    (acc[value] = acc[value] || []).push({
+      id,
+      workflow,
+      activeInInput: workflow.trigger?.activeInInput || false,
+    });
+
+    return acc;
+  }, {});
+
+  Mousetrap.bind(Object.keys(keyboardShortcuts), ({ target }, command) => {
+    const isInputElement =
+      ['INPUT', 'SELECT', 'TEXTAREA'].includes(target.tagName) ||
+      target?.contentEditable === 'true';
+
+    keyboardShortcuts[command].forEach((item) => {
+      if (!item.activeInInput && isInputElement) return;
+
+      sendMessage('workflow:execute', item.workflow, 'background');
+    });
+
+    return true;
+  });
+}
+
 (async () => {
 (async () => {
   try {
   try {
     const { shortcuts, workflows, workflowHosts } =
     const { shortcuts, workflows, workflowHosts } =
@@ -14,11 +64,7 @@ Mousetrap.prototype.stopCallback = function () {
         'workflows',
         'workflows',
         'workflowHosts',
         'workflowHosts',
       ]);
       ]);
-    const shortcutsArr = Object.entries(shortcuts || {});
-
-    if (shortcutsArr.length === 0) return;
-
-    const keyboardShortcuts = shortcutsArr.reduce((acc, [id, value]) => {
+    const findWorkflow = (id) => {
       let workflow = workflows.find((item) => item.id === id);
       let workflow = workflows.find((item) => item.id === id);
 
 
       if (!workflow) {
       if (!workflow) {
@@ -29,28 +75,11 @@ Mousetrap.prototype.stopCallback = function () {
         if (workflow) workflow.id = workflow.hostId;
         if (workflow) workflow.id = workflow.hostId;
       }
       }
 
 
-      (acc[value] = acc[value] || []).push({
-        id,
-        workflow,
-        activeInInput: workflow.trigger?.activeInInput || false,
-      });
+      return workflow;
+    };
 
 
-      return acc;
-    }, {});
-
-    Mousetrap.bind(Object.keys(keyboardShortcuts), ({ target }, command) => {
-      const isInputElement =
-        ['INPUT', 'SELECT', 'TEXTAREA'].includes(target.tagName) ||
-        target?.contentEditable === 'true';
-
-      keyboardShortcuts[command].forEach((item) => {
-        if (!item.activeInInput && isInputElement) return;
-
-        sendMessage('workflow:execute', item.workflow, 'background');
-      });
-
-      return true;
-    });
+    automaCustomEventListener(findWorkflow);
+    workflowShortcutsListener(findWorkflow, shortcuts || {});
   } catch (error) {
   } catch (error) {
     console.error(error);
     console.error(error);
   }
   }