Browse Source

feat: add more operators in condition builder

Ahmad Kholid 3 years ago
parent
commit
63b42cfd71

+ 1 - 1
src/background/workflowEngine/blocksHandler/handlerConditions.js

@@ -33,7 +33,7 @@ async function conditions({ data, outputs }, { prevBlockData, refData }) {
       );
 
       Object.assign(replacedValue, result?.replacedValue || {});
-
+      console.log(result);
       if (result.isMatch) {
         isConditionMatch = true;
         outputIndex = index + 1;

+ 16 - 6
src/components/newtab/shared/SharedConditionBuilder/ConditionBuilderInputs.vue

@@ -56,13 +56,15 @@
       :model-value="inputsData[index].type"
       @change="updateCompareType($event, index)"
     >
-      <option
-        v-for="type in conditionBuilder.compareTypes"
-        :key="type.id"
-        :value="type.id"
+      <optgroup
+        v-for="(types, category) in conditionOperators"
+        :key="category"
+        :label="category"
       >
-        {{ type.name }}
-      </option>
+        <option v-for="type in types" :key="type.id" :value="type.id">
+          {{ type.name }}
+        </option>
+      </optgroup>
     </ui-select>
   </div>
 </template>
@@ -90,6 +92,14 @@ const props = defineProps({
 });
 const emit = defineEmits(['update']);
 
+const conditionOperators = conditionBuilder.compareTypes.reduce((acc, type) => {
+  if (!acc[type.category]) acc[type.category] = [];
+
+  acc[type.category].push(type);
+
+  return acc;
+}, {});
+
 const { t } = useI18n();
 const inputsData = ref(cloneDeep(props.data));
 

+ 1 - 1
src/components/newtab/shared/SharedConditionBuilder/index.vue

@@ -132,7 +132,7 @@ function getConditionText({ category, type, data }) {
 
   if (type === 'value') {
     text = data.value || 'Empty';
-  } else if (type === 'code') {
+  } else if (type.startsWith('code')) {
     text = 'JS Code';
   } else if (type.startsWith('element')) {
     text = type;

+ 1 - 1
src/content/handleTestCondition.js

@@ -88,7 +88,7 @@ export default async function (data) {
   if (data.type.startsWith('element')) {
     result = await handleConditionElement(data);
   }
-  if (data.type === 'code') {
+  if (data.type.startsWith('code')) {
     result = await injectJsCode(data);
   }
 

+ 23 - 10
src/utils/shared.js

@@ -1068,7 +1068,7 @@ export const conditionBuilder = {
       id: 'code',
       category: 'value',
       name: 'Code',
-      compareable: true,
+      compareable: false,
       data: { code: '\nreturn true;' },
     },
     {
@@ -1122,15 +1122,28 @@ export const conditionBuilder = {
     },
   ],
   compareTypes: [
-    { id: 'eq', name: 'Equals', needValue: true },
-    { id: 'nq', name: 'Not equals', needValue: true },
-    { id: 'gt', name: 'Greater than', needValue: true },
-    { id: 'gte', name: 'Greater than or equal', needValue: true },
-    { id: 'lt', name: 'Less than', needValue: true },
-    { id: 'lte', name: 'Less than or equal', needValue: true },
-    { id: 'cnt', name: 'Contains', needValue: true },
-    { id: 'itr', name: 'Is truthy', needValue: false },
-    { id: 'ifl', name: 'Is falsy', needValue: false },
+    { id: 'eq', name: 'Equals', needValue: true, category: 'basic' },
+    { id: 'nq', name: 'Not equals', needValue: true, category: 'basic' },
+    { id: 'gt', name: 'Greater than', needValue: true, category: 'number' },
+    {
+      id: 'gte',
+      name: 'Greater than or equal',
+      needValue: true,
+      category: 'number',
+    },
+    { id: 'lt', name: 'Less than', needValue: true, category: 'number' },
+    {
+      id: 'lte',
+      name: 'Less than or equal',
+      needValue: true,
+      category: 'number',
+    },
+    { id: 'cnt', name: 'Contains', needValue: true, category: 'text' },
+    { id: 'nct', name: 'Not contains', needValue: true, category: 'text' },
+    { id: 'stw', name: 'Starts with', needValue: true, category: 'text' },
+    { id: 'enw', name: 'Ends with', needValue: true, category: 'text' },
+    { id: 'itr', name: 'Is truthy', needValue: false, category: 'boolean' },
+    { id: 'ifl', name: 'Is falsy', needValue: false, category: 'boolean' },
   ],
   inputTypes: {
     selector: {

+ 4 - 1
src/utils/testConditions.js

@@ -17,6 +17,9 @@ const comparisons = {
   lt: (a, b) => isNumStr(a) < isNumStr(b),
   lte: (a, b) => isNumStr(a) <= isNumStr(b),
   cnt: (a, b) => a?.includes(b) ?? false,
+  nct: (a, b) => !comparisons.cnt(a, b),
+  stw: (a, b) => a?.startsWith(b) ?? false,
+  enw: (a, b) => a?.endsWith(b) ?? false,
   itr: (a) => Boolean(isBoolStr(a)),
   ifl: (a) => !isBoolStr(a),
 };
@@ -42,7 +45,7 @@ export default async function (conditionsArr, workflowData) {
 
     if (type === 'value') return copyData.value;
 
-    if (type.startsWith('element') || type === 'code') {
+    if (type.startsWith('element') || type.startsWith('code')) {
       const conditionValue = await workflowData.sendMessage({
         type: 'condition-builder',
         data: {