统一VarInput变量的管理

This commit is contained in:
fofolee
2025-01-08 14:39:08 +08:00
parent b528cfa97d
commit 7a43695e2d
24 changed files with 190 additions and 516 deletions

View File

@@ -94,35 +94,30 @@
* @example
* // 基础数组
* [
* {
* value: "张三",
* isString: true,
* __varInputVal__: true
* }
* newVarInputVal("str", "张三")
* ]
*
* // 多键对象数组
* options.keys = ['name', 'age', 'email']
* [
* {
* name: { value: "张三", isString: true, __varInputVal__: true },
* age: { value: "18", isString: false, __varInputVal__: true },
* email: { value: "zhangsan@example.com", isString: true, __varInputVal__: true }
* name: newVarInputVal("str", "张三"),
* age: newVarInputVal("str", "18"),
* email: newVarInputVal("str", "zhangsan@example.com")
* }
* ]
*
* // 下拉选择模式
* options.items = ['选项1', '选项2', '选项3']
* [
* {
* value: "选项1",
* isString: true,
* __varInputVal__: true
* }
* newVarInputVal("str", "选项1"),
* newVarInputVal("str", "选项2"),
* newVarInputVal("str", "选项3")
* ]
*/
import { defineComponent } from "vue";
import VariableInput from "components/composer/common/VariableInput.vue";
import { newVarInputVal } from "js/composer/varInputValManager";
export default defineComponent({
name: "ArrayEditor",
@@ -169,22 +164,12 @@ export default defineComponent({
if (this.optionsKeys?.length) {
const item = {};
this.optionsKeys.forEach((key) => {
item[key] = {
value: "",
isString: true,
__varInputVal__: true,
};
item[key] = newVarInputVal("str");
});
return [item];
}
return [
{
value: "",
isString: true,
__varInputVal__: true,
},
];
return [newVarInputVal("str")];
},
/**
* 添加新的数组项
@@ -195,22 +180,11 @@ export default defineComponent({
if (this.options.keys) {
const newItem = {};
this.options.keys.forEach((key) => {
newItem[key] = {
value: "",
isString: true,
__varInputVal__: true,
};
newItem[key] = newVarInputVal("str");
});
newItems = [...this.items, newItem];
} else {
newItems = [
...this.items,
{
value: "",
isString: true,
__varInputVal__: true,
},
];
newItems = [...this.items, newVarInputVal("str")];
}
this.$emit("update:modelValue", newItems);
},
@@ -225,19 +199,11 @@ export default defineComponent({
if (this.options.keys) {
const newItem = {};
this.options.keys.forEach((key) => {
newItem[key] = {
value: "",
isString: true,
__varInputVal__: true,
};
newItem[key] = newVarInputVal("str");
});
newItems.push(newItem);
} else {
newItems.push({
value: "",
isString: true,
__varInputVal__: true,
});
newItems.push(newVarInputVal("str"));
}
}
this.$emit("update:modelValue", newItems);

View File

@@ -95,6 +95,7 @@
<script>
import { defineComponent } from "vue";
import { newVarInputVal } from "js/composer/varInputValManager";
import VariableInput from "components/composer/common/VariableInput.vue";
/**
@@ -108,21 +109,15 @@ import VariableInput from "components/composer/common/VariableInput.vue";
* @example
* // 基础字典对象
* {
* key: {
* value: "", // 输入框的值
* isString: true, // 是否是字符串
* __varInputVal__: true // 用于标识是变量输入框
* }
* key: newVarInputVal("str"),
* }
*
* // 下拉选择模式
* options.items = ['User-Agent', 'Content-Type', 'Accept']
* {
* "User-Agent": {
* value: "Mozilla/5.0",
* isString: true,
* __varInputVal__: true
* }
* "User-Agent": newVarInputVal("str", "Mozilla/5.0"),
* "Content-Type": newVarInputVal("str", "text/html"),
* "Accept": newVarInputVal("str", "text/html")
* }
*/
export default defineComponent({
@@ -149,11 +144,7 @@ export default defineComponent({
: [
{
key: "",
value: {
value: "",
isString: true,
__varInputVal__: true,
},
value: newVarInputVal("str"),
},
],
filterOptions: this.options?.items || [],
@@ -183,7 +174,7 @@ export default defineComponent({
...this.items,
{
key: "",
value: { value: "", isString: true, __varInputVal__: true },
value: newVarInputVal("str"),
},
];
},
@@ -193,7 +184,7 @@ export default defineComponent({
if (newItems.length === 0) {
newItems.push({
key: "",
value: { value: "", isString: true, __varInputVal__: true },
value: newVarInputVal("str"),
});
}
this.items = newItems;

View File

@@ -138,7 +138,7 @@
<script>
import { defineComponent, inject } from "vue";
import { newVarInputVal } from "js/composer/varInputValManager";
/**
* 变量输入框组件
* @description 支持变量选择和字符串输入的输入框组件
@@ -179,11 +179,7 @@ export default defineComponent({
modelValue: {
type: Object,
required: true,
default: () => ({
value: "",
isString: true,
__varInputVal__: true,
}),
default: () => newVarInputVal("str"),
},
label: String,
icon: String,
@@ -265,22 +261,14 @@ export default defineComponent({
insertVariable(variable) {
this.selectedVariable = variable;
this.isString = false; // 变量模式下不需要字符串处理
this.$emit("update:modelValue", {
isString: false,
value: variable.name,
__varInputVal__: true,
});
this.$emit("update:modelValue", newVarInputVal("var", variable.name));
},
// 清除变量时的处理
clearVariable() {
this.selectedVariable = null;
this.isString = true; // 恢复到字符串模式
this.$emit("update:modelValue", {
isString: true,
value: "",
__varInputVal__: true,
});
this.$emit("update:modelValue", newVarInputVal("str"));
},
// 切换类型
@@ -301,11 +289,7 @@ export default defineComponent({
selectItem(option) {
const value = this.getItemValue(option);
this.$emit("update:modelValue", {
value,
isString: true,
__varInputVal__: true,
});
this.$emit("update:modelValue", newVarInputVal("str", value));
},
escapePath(paths) {
if (!paths) return null;
@@ -319,26 +303,14 @@ export default defineComponent({
const files = this.escapePath(utools.showOpenDialog(options));
if (!files) return;
if (files.length > 1) {
this.$emit("update:modelValue", {
value: files,
isString: false,
__varInputVal__: true,
});
this.$emit("update:modelValue", newVarInputVal("var", files));
} else if (files.length === 1) {
this.$emit("update:modelValue", {
value: files[0],
isString: true,
__varInputVal__: true,
});
this.$emit("update:modelValue", newVarInputVal("str", files[0]));
}
} else {
const file = this.escapePath(utools.showSaveDialog(options));
if (!file) return;
this.$emit("update:modelValue", {
value: file,
isString: true,
__varInputVal__: true,
});
this.$emit("update:modelValue", newVarInputVal("str", file));
}
},
},