Browse Source

feat: add method in the regex variable block

Ahmad Kholid 3 years ago
parent
commit
7bfd50fb26

+ 12 - 4
src/background/workflowEngine/blocksHandler/handlerRegexVariable.js

@@ -7,16 +7,24 @@ export async function regexVariable({ id, data }) {
     throw new Error(`Cant find "${data.variableName}" variable`);
   }
 
-  const currentVar = refVariables[data.variableName];
-  if (typeof currentVar !== 'string') {
+  const str = refVariables[data.variableName];
+  if (typeof str !== 'string') {
     throw new Error(
       `The value of the "${data.variableName}" variable is not a string/text`
     );
   }
 
+  const method = data.method || 'match';
   const regex = new RegExp(data.expression, data.flag.join(''));
-  const matches = currentVar.match(regex);
-  const newValue = matches && !data.flag.includes('g') ? matches[0] : matches;
+
+  let newValue = '';
+
+  if (method === 'match') {
+    const matches = str.match(regex);
+    newValue = matches && !data.flag.includes('g') ? matches[0] : matches;
+  } else if (method === 'replace') {
+    newValue = str.replace(regex, data.replaceVal ?? '');
+  }
 
   refVariables[data.variableName] = newValue;
 

+ 24 - 2
src/components/newtab/workflow/edit/EditRegexVariable.vue

@@ -13,7 +13,24 @@
       class="mt-2 w-full"
       @change="updateData({ variableName: $event })"
     />
-    <div class="flex items-end mt-2">
+    <ui-select
+      :model-value="data.method"
+      label="Method"
+      class="mt-2 w-full"
+      @change="updateData({ method: $event })"
+    >
+      <option v-for="method in methods" :key="method.id" :value="method.id">
+        {{ method.name }}
+      </option>
+    </ui-select>
+    <ui-input
+      :model-value="data.replaceVal"
+      label="Replace with"
+      placeholder="(empty)"
+      class="mt-2 w-full"
+      @change="updateData({ replaceVal: $event })"
+    />
+    <div class="flex items-end mt-3">
       <div class="flex-1 mr-2">
         <label
           class="ml-1 block text-gray-600 dark:text-gray-200 text-sm"
@@ -67,13 +84,18 @@ const props = defineProps({
 });
 const emit = defineEmits(['update:data']);
 
-const { t } = useI18n();
+const methods = [
+  { id: 'match', name: 'Match value' },
+  { id: 'replace', name: 'Replace value' },
+];
 const flags = [
   { id: 'g', name: 'global' },
   { id: 'i', name: 'ignore case' },
   { id: 'm', name: 'multiline' },
 ];
 
+const { t } = useI18n();
+
 function updateData(value) {
   emit('update:data', { ...props.data, ...value });
 }

+ 3 - 0
src/utils/shared.js

@@ -1124,8 +1124,11 @@ export const tasks = {
     outputs: 1,
     allowedInputs: true,
     maxConnection: 1,
+    refDataKeys: ['replaceVal'],
     data: {
       disableBlock: false,
+      method: 'match',
+      replaceVal: '',
       description: '',
       expression: '',
       flag: [],