From b528cfa97d67106d6592e948d06c5c1b05f96797 Mon Sep 17 00:00:00 2001 From: fofolee Date: Wed, 8 Jan 2025 11:52:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=80=89=E9=A1=B9=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=88=87=E6=8D=A2=E4=B8=8D=E5=90=8C=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E9=80=89=E9=A1=B9=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../composer/common/ArrayEditor.vue | 8 +- .../composer/common/VariableInput.vue | 3 +- .../composer/ui/SelectListEditor.vue | 115 ++++++++++++------ src/js/composer/formatString.js | 34 +++++- 4 files changed, 117 insertions(+), 43 deletions(-) diff --git a/src/components/composer/common/ArrayEditor.vue b/src/components/composer/common/ArrayEditor.vue index 71683a6..a11b71e 100644 --- a/src/components/composer/common/ArrayEditor.vue +++ b/src/components/composer/common/ArrayEditor.vue @@ -5,12 +5,15 @@
@@ -187,6 +187,7 @@ export default defineComponent({ }, label: String, icon: String, + noIcon: Boolean, options: { type: Object, default: () => ({}), diff --git a/src/components/composer/ui/SelectListEditor.vue b/src/components/composer/ui/SelectListEditor.vue index 2d4f0eb..15a2ab5 100644 --- a/src/components/composer/ui/SelectListEditor.vue +++ b/src/components/composer/ui/SelectListEditor.vue @@ -6,7 +6,7 @@
@@ -22,7 +22,7 @@
{ + if (typeof val !== "string") val = JSON.stringify(val); + return { + value: val, + isString: type === "str", + __varInputVal__: true, + }; }; -const textDefaultSelects = { - selects: new Array(3).fill({ - value: "", - isString: true, - __varInputVal__: true, - }), -}; +const jsonDefaultSelects = new Array(3).fill().map((_, index) => ({ + id: newVarInputVal("var", index), + title: newVarInputVal("str"), + description: newVarInputVal("str"), +})); + +const textDefaultSelects = new Array(3).fill(newVarInputVal("str")); const defaultSelects = { json: jsonDefaultSelects, @@ -248,12 +247,25 @@ export default defineComponent({ }, }, methods: { - updateArgvs(key, value) { + /** + * 更新参数 + * @param {string|string[]} keys - 键名 + * @param {string|string[]} values - 值 + */ + updateArgvs(keys, values) { + if (typeof keys === "string") { + keys = [keys]; + values = [values]; + } const argvs = { ...this.argvs }; - const keys = key.split("."); - const lastKey = keys.pop(); - const target = keys.reduce((obj, key) => obj[key], argvs); - target[lastKey] = value; + // 更新每一个键 + keys.forEach((key, index) => { + // 支持嵌套对象的键值 + const subKeys = key.split("."); + const lastKey = subKeys.pop(); + const target = subKeys.reduce((obj, key) => obj[key], argvs); + target[lastKey] = values[index]; + }); this.updateModelValue(argvs); }, generateCode(argvs = this.argvs) { @@ -285,13 +297,16 @@ export default defineComponent({ try { const result = parseFunction(code, { - variableFormatPaths: ["arg0", "arg1.placeholder"], + variableFormatPaths: ["arg0", "arg0[*]", "arg1.placeholder"], }); + if (!result) return this.defaultArgvs; const [selects, options = {}] = result.argvs; + const inputMode = selects.__varInputVal__ ? "variable" : "manual"; return { ...this.defaultArgvs, + inputMode, selects, ...options, }; @@ -318,20 +333,46 @@ export default defineComponent({ argvs, }); }, + handleOptionTypeChange(newOptionType) { + const oldOptionType = this.argvs.optionType; + // 原地点击不处理 + if (oldOptionType === newOptionType) return; + // 变量输入模式不需要处理 selects + if (this.argvs.inputMode === "variable") { + this.updateArgvs("optionType", newOptionType); + return; + } + const oldSelects = this.argvs.selects; + let newSelects = oldSelects; + // 从JSON转换为非JSON时,取title或description + if (oldOptionType === "json") { + newSelects = oldSelects.map( + (item) => item.title || item.description || newVarInputVal("str") + ); + } else if (newOptionType === "json") { + // 从非JSON转换为JSON时,添加title和description + newSelects = oldSelects.map((item) => ({ + title: item, + description: item, + })); + } + this.updateArgvs(["optionType", "selects"], [newOptionType, newSelects]); + }, + handleInputModeChange(newInputMode) { + let newSelects = this.argvs.selects; + if (newInputMode === "variable") { + newSelects = newVarInputVal("var"); + } else { + newSelects = defaultSelects[this.argvs.optionType]; + } + this.updateArgvs(["inputMode", "selects"], [newInputMode, newSelects]); + }, }, mounted() { if (!this.modelValue.argvs && !this.modelValue.code) { this.updateModelValue(this.defaultArgvs); } }, - watch: { - "argvs.optionType": { - immediate: true, - handler(newVal) { - this.argvs.selects = defaultSelects[newVal]; - }, - }, - }, }); diff --git a/src/js/composer/formatString.js b/src/js/composer/formatString.js index 6360239..1f531cd 100644 --- a/src/js/composer/formatString.js +++ b/src/js/composer/formatString.js @@ -306,9 +306,37 @@ export const parseFunction = (functionStr, options = {}) => { return obj; }, {}); case "ArrayExpression": - return node.elements.map((element, index) => - processNode(element, `${currentPath}[${index}]`) - ); + return node.elements.map((element, index) => { + const elementPath = `${currentPath}[${index}]`; + const processedElement = processNode(element, elementPath); + + if ( + shouldUseVariableFormat && + typeof processedElement === "object" + ) { + return Object.entries(processedElement).reduce( + (acc, [key, value]) => { + // 如果值已经是 varInputVal 格式,直接使用 + if (value?.__varInputVal__) { + acc[key] = value; + } else { + // 否则转换为 varInputVal 格式 + acc[key] = { + value: + typeof value === "string" + ? value + : JSON.stringify(value), + isString: typeof value === "string", + __varInputVal__: true, + }; + } + return acc; + }, + {} + ); + } + return processedElement; + }); case "ObjectProperty": return processNode(node.value, currentPath); case "MemberExpression":