浏览代码

feat: add increase variable block

Ahmad Kholid 2 年之前
父节点
当前提交
adf89dd0bd

+ 25 - 0
src/background/workflowEngine/blocksHandler/handlerIncreaseVariable.js

@@ -0,0 +1,25 @@
+import { objectHasKey } from '@/utils/helper';
+
+export async function logData({ id, data }) {
+  const refVariables = this.engine.referenceData.variables;
+
+  if (!objectHasKey(refVariables, data.variableName)) {
+    throw new Error(`Cant find "${data.variableName}" variable`);
+  }
+
+  const currentVar = refVariables[data.variableName];
+  if (typeof currentVar !== 'number') {
+    throw new Error(
+      `The "${data.variableName}" variable value is not a number`
+    );
+  }
+
+  refVariables[data.variableName] += data.increaseBy;
+
+  return {
+    data: refVariables[data.variableName],
+    nextBlockId: this.getBlockConnections(id),
+  };
+}
+
+export default logData;

+ 47 - 0
src/components/newtab/workflow/edit/EditIncreaseVariable.vue

@@ -0,0 +1,47 @@
+<template>
+  <div>
+    <ui-textarea
+      :model-value="data.description"
+      :placeholder="t('common.description')"
+      class="w-full"
+      @change="updateData({ description: $event })"
+    />
+    <ui-input
+      :model-value="data.variableName"
+      :label="t('workflow.variables.name')"
+      :title="t('workflow.variables.name')"
+      class="mt-2 w-full"
+      @change="updateData({ variableName: $event })"
+    />
+    <ui-input
+      :model-value="data.increaseBy"
+      :label="t('workflow.blocks.increase-variable.increase')"
+      placeholder="0"
+      type="number"
+      class="w-full mt-2"
+      @change="updateData({ increaseBy: +$event })"
+    />
+  </div>
+</template>
+<script setup>
+import { useI18n } from 'vue-i18n';
+
+const props = defineProps({
+  data: {
+    type: Object,
+    default: () => ({}),
+  },
+});
+const emit = defineEmits(['update:data']);
+
+const { t } = useI18n();
+
+function updateData(value) {
+  emit('update:data', { ...props.data, ...value });
+}
+</script>
+<style>
+.log-data .block-variable {
+  margin-top: 4px;
+}
+</style>

+ 2 - 0
src/lib/vRemixicon.js

@@ -120,6 +120,7 @@ import {
   riArrowGoForwardLine,
   riCheckboxCircleLine,
   riLightbulbFlashLine,
+  riIncreaseDecreaseLine,
 } from 'v-remixicon/icons';
 
 export const icons = {
@@ -243,6 +244,7 @@ export const icons = {
   riArrowGoForwardLine,
   riCheckboxCircleLine,
   riLightbulbFlashLine,
+  riIncreaseDecreaseLine,
   mdiEqual: 'M19,10H5V8H19V10M19,16H5V14H19V16Z',
   mdiVariable:
     'M20.41,3C21.8,5.71 22.35,8.84 22,12C21.8,15.16 20.7,18.29 18.83,21L17.3,20C18.91,17.57 19.85,14.8 20,12C20.34,9.2 19.89,6.43 18.7,4L20.41,3M5.17,3L6.7,4C5.09,6.43 4.15,9.2 4,12C3.66,14.8 4.12,17.57 5.3,20L3.61,21C2.21,18.29 1.65,15.17 2,12C2.2,8.84 3.3,5.71 5.17,3M12.08,10.68L14.4,7.45H16.93L13.15,12.45L15.35,17.37H13.09L11.71,14L9.28,17.33H6.76L10.66,12.21L8.53,7.45H10.8L12.08,10.68Z',

+ 5 - 0
src/locales/en/blocks.json

@@ -101,6 +101,11 @@
         "start": "Start index",
         "end": "End index"
       },
+      "increase-variable": {
+        "name": "Increase variable",
+        "description": "Increase the value of a variable by specific amount",
+        "increase": "Increase by"
+      },
       "notification": {
         "name": "notification",
         "description": "Display a notification",

+ 18 - 0
src/utils/shared.js

@@ -1070,6 +1070,24 @@ export const tasks = {
       variableName: '',
     },
   },
+  'increase-variable': {
+    name: 'Increase variable',
+    description: 'Increase the value of a variable by specific amount',
+    icon: 'riIncreaseDecreaseLine',
+    editComponent: 'EditIncreaseVariable',
+    component: 'BlockBasic',
+    category: 'data',
+    inputs: 1,
+    outputs: 1,
+    allowedInputs: true,
+    maxConnection: 1,
+    data: {
+      disableBlock: false,
+      description: '',
+      increaseBy: 1,
+      variableName: '',
+    },
+  },
 };
 
 export const categories = {