Skip to content

Commit 5947e53

Browse files
committed
加强变量校验,重复和非法时自动重命名,优化变量管理,实时获取最新变量,不再进行增删操作
1 parent 25be7f4 commit 5947e53

File tree

10 files changed

+268
-77
lines changed

10 files changed

+268
-77
lines changed

src/components/composer/CommandComposer.vue

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
findCommandByValue,
3030
} from "js/composer/composerConfig";
3131
import { generateCode } from "js/composer/generateCode";
32+
import { parseVariables } from "js/composer/variableManager";
3233
3334
export default defineComponent({
3435
name: "CommandComposer",
@@ -37,37 +38,32 @@ export default defineComponent({
3738
ComposerFlow,
3839
},
3940
setup() {
40-
const variables = ref([]);
41+
const commandFlow = ref([]);
4142
42-
const addVariable = (name, command) => {
43-
if (!variables.value.find((v) => v.name === name)) {
44-
variables.value.push({
45-
name,
46-
sourceCommand: command,
47-
});
48-
}
49-
};
50-
51-
const removeVariable = (name) => {
52-
const index = variables.value.findIndex((v) => v.name === name);
53-
if (index !== -1) {
54-
variables.value.splice(index, 1);
43+
// 提供获取当前变量的函数,直接返回解析后的变量列表
44+
const getCurrentVariables = () => {
45+
const variables = [];
46+
for (const cmd of commandFlow.value) {
47+
if (cmd.saveOutput && cmd.outputVariable) {
48+
variables.push(
49+
...parseVariables(cmd.outputVariable).map((variable) => ({
50+
name: variable,
51+
sourceCommand: cmd,
52+
}))
53+
);
54+
}
5555
}
56+
return variables;
5657
};
5758
58-
provide("composerVariables", variables);
59-
provide("addVariable", addVariable);
60-
provide("removeVariable", removeVariable);
59+
provide("getCurrentVariables", getCurrentVariables);
6160
6261
return {
63-
variables,
64-
addVariable,
65-
removeVariable,
62+
commandFlow,
6663
};
6764
},
6865
data() {
6966
return {
70-
commandFlow: [],
7167
availableCommands,
7268
};
7369
},

src/components/composer/ComposerCard.vue

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@
6666

6767
<script>
6868
import { defineComponent, inject } from "vue";
69-
import { validateVariableName } from "js/common/variableValidator";
7069
import VariableInput from "components/composer/common/VariableInput.vue";
7170
import MultiParams from "components/composer/MultiParams.vue";
7271
import CommandHead from "components/composer/card/CommandHead.vue";
7372
import * as CardComponents from "js/composer/cardComponents";
73+
import { processVariable } from "js/composer/variableManager";
7474
7575
export default defineComponent({
7676
name: "ComposerCard",
@@ -110,46 +110,27 @@ export default defineComponent({
110110
},
111111
},
112112
setup() {
113-
const addVariable = inject("addVariable");
114-
const removeVariable = inject("removeVariable");
115-
const variables = inject("composerVariables", []);
116-
117-
return {
118-
addVariable,
119-
removeVariable,
120-
variables,
121-
};
113+
const getCurrentVariables = inject("getCurrentVariables");
114+
return { getCurrentVariables };
122115
},
123116
methods: {
124117
handleOutputVariableUpdate(value) {
125-
// 检查变量名是否合法
126-
const validation = validateVariableName(value);
127-
if (!validation.isValid) {
128-
quickcommand.showMessageBox(validation.error, "warning");
129-
return;
130-
}
118+
const result = processVariable({
119+
value,
120+
existingVars: this.getCurrentVariables().map((v) => v.name),
121+
});
131122
132-
// 检查变量名是否重复
133-
if (this.variables.some((v) => v.name === value)) {
134-
quickcommand.showMessageBox(`变量名 "${value}" 已经存在`, "warning");
135-
return;
123+
if (result.warning) {
124+
quickcommand.showMessageBox(result.warning, "info");
136125
}
137126
138-
// 处理变量管理
139-
if (this.localCommand.outputVariable) {
140-
this.removeVariable(this.localCommand.outputVariable);
141-
}
142-
if (value) {
143-
this.addVariable(value, this.localCommand);
144-
}
145-
this.localCommand.outputVariable = value;
127+
this.localCommand.outputVariable = result.processedValue;
146128
},
147129
handleToggleOutput() {
148130
this.localCommand.saveOutput = !this.localCommand.saveOutput;
149131
150132
// 如果关闭输出,清空变量名
151-
if (!this.localCommand.saveOutput && this.localCommand.outputVariable) {
152-
this.removeVariable(this.localCommand.outputVariable);
133+
if (!this.localCommand.saveOutput) {
153134
this.localCommand.outputVariable = null;
154135
}
155136
},

src/components/composer/ComposerFlow.vue

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import ChainStyles from "./flow/ChainStyles.vue";
7474
import EmptyFlow from "./flow/EmptyFlow.vue";
7575
import DropArea from "./flow/DropArea.vue";
7676
import { findCommandByValue } from "js/composer/composerConfig";
77+
import { processVariable } from "js/composer/variableManager";
7778
7879
export default defineComponent({
7980
name: "ComposerFlow",
@@ -96,11 +97,11 @@ export default defineComponent({
9697
required: true,
9798
},
9899
},
99-
emits: ["update:modelValue", "add-command", "action"],
100100
setup() {
101-
const removeVariable = inject("removeVariable");
102-
return { removeVariable };
101+
const getCurrentVariables = inject("getCurrentVariables");
102+
return { getCurrentVariables };
103103
},
104+
emits: ["update:modelValue", "add-command", "action"],
104105
data() {
105106
return {
106107
dragIndex: -1,
@@ -261,13 +262,17 @@ export default defineComponent({
261262
} catch (error) {}
262263
},
263264
createNewCommand(parsedAction) {
264-
return {
265+
const newCommand = {
265266
...parsedAction,
266267
id: this.getUniqueId(),
267-
saveOutput: false,
268-
useOutput: null,
269-
outputVariable: null,
270268
};
269+
if (newCommand.saveOutput && newCommand.outputVariable) {
270+
newCommand.outputVariable = processVariable({
271+
value: newCommand.outputVariable,
272+
existingVars: this.getCurrentVariables().map((v) => v.name),
273+
}).processedValue;
274+
}
275+
return newCommand;
271276
},
272277
getUniqueId() {
273278
return this.$root.getUniqueId();
@@ -284,9 +289,6 @@ export default defineComponent({
284289
const cmd = newCommands[i];
285290
// 如果chainId不为空,则只删除指定chainId的命令
286291
if (chainId && cmd.chainId !== chainId) continue;
287-
if (cmd.outputVariable) {
288-
this.removeVariable(cmd.outputVariable);
289-
}
290292
newCommands.splice(i, 1);
291293
}
292294
this.$emit("update:modelValue", newCommands);
@@ -326,7 +328,7 @@ export default defineComponent({
326328
command,
327329
{
328330
//没有输出,则不打印
329-
code: `${command.outputVariable} && console.log(${command.outputVariable})`,
331+
code: `if(${command.outputVariable}!==undefined){console.log(${command.outputVariable})}`,
330332
},
331333
];
332334
// 触发运行事件

src/components/composer/MultiParams.vue

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,13 @@ export default defineComponent({
179179
)?.label;
180180
const subFeature = funcNameLabel ? `${funcNameLabel} ` : "";
181181
const allArgvs = argvs
182+
.filter((item) => item != null && item != "")
182183
.map((item) =>
183-
item?.hasOwnProperty("__varInputVal__")
184-
? window.lodashM.truncate(item.value, {
185-
length: 30,
186-
omission: "...",
187-
})
188-
: item
189-
)
190-
.filter((item) => item != null && item != "");
184+
window.lodashM.truncate(stringifyArgv(item).toString(), {
185+
length: 30,
186+
omission: "...",
187+
})
188+
);
191189
return `${subFeature}${allArgvs.join(",")}`;
192190
},
193191
updateModelValue(funcName, argvs) {

src/components/composer/card/CommandButtons.vue

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
<!-- 变量输入框 -->
1010
<q-input
1111
v-if="command.saveOutput"
12-
:model-value="command.outputVariable"
13-
@update:model-value="$emit('update:outputVariable', $event)"
12+
v-model="inputValue"
13+
@focus="sourceValue = inputValue"
14+
@blur="handleBlur"
1415
outlined
1516
placeholder="变量名"
1617
class="variable-input"
@@ -91,13 +92,35 @@ export default {
9192
default: false,
9293
},
9394
},
95+
data() {
96+
return {
97+
inputValue: this.command.outputVariable || "",
98+
sourceValue: "",
99+
};
100+
},
101+
watch: {
102+
"command.outputVariable"(newVal) {
103+
this.inputValue = newVal || "";
104+
},
105+
},
94106
emits: [
95107
"update:outputVariable",
96108
"toggle-output",
97109
"run",
98110
"remove",
99111
"toggle-collapse",
100112
],
113+
methods: {
114+
handleBlur() {
115+
// 如果输入框的值和源值相同,则不更新
116+
if (
117+
this.inputValue.replace(/[ ]/g, "") ===
118+
this.sourceValue.replace(/[ ]/g, "")
119+
)
120+
return;
121+
this.$emit("update:outputVariable", this.inputValue);
122+
},
123+
},
101124
};
102125
</script>
103126

@@ -115,7 +138,7 @@ export default {
115138
}
116139
117140
.variable-input {
118-
width: 100px;
141+
width: 120px;
119142
}
120143
121144
.output-section :deep(.q-field) {

src/components/composer/common/VariableInput.vue

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
}"
8080
class="variable-dropdown prepend-btn"
8181
size="sm"
82-
v-if="variables.length"
82+
@click="variables = getCurrentVariables()"
8383
>
8484
<q-list class="variable-list">
8585
<q-item-label header class="variable-label">
@@ -108,6 +108,15 @@
108108
</q-item-section>
109109
</q-item>
110110
</template>
111+
<template v-else>
112+
<q-item>
113+
<q-item-section>
114+
<q-item-label
115+
>无可用变量,请先点击命令卡片输出变量按钮设置变量</q-item-label
116+
>
117+
</q-item-section>
118+
</q-item>
119+
</template>
111120
</q-list>
112121
</q-btn-dropdown>
113122
</template>
@@ -176,13 +185,14 @@ export default defineComponent({
176185
177186
emits: ["update:modelValue"],
178187
setup() {
179-
const variables = inject("composerVariables", []);
180-
return { variables };
188+
const getCurrentVariables = inject("getCurrentVariables");
189+
return { getCurrentVariables };
181190
},
182191
183192
data() {
184193
return {
185194
selectedVariable: null,
195+
variables: [],
186196
};
187197
},
188198

src/components/composer/ubrowser/UBrowserOperations.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ export default defineComponent({
136136
id: this.$root.getUniqueId(),
137137
argv: "",
138138
saveOutput: false,
139-
useOutput: null,
140139
cmd: action.value || action.cmd,
141140
value: action.value || action.cmd,
142141
};

src/js/common/variableValidator.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ const reservedWords = [
117117
* @returns {object} - 包含验证结果和错误信息的对象
118118
*/
119119
export function validateVariableName(name) {
120+
// 去除空格
121+
name = name.trim();
122+
120123
// 检查是否为空
121124
if (!name) {
122125
return {

src/js/composer/commands/uiCommand.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export const uiCommands = {
77
value: "quickcommand.showButtonBox",
88
label: "按钮组弹窗",
99
isAsync: true,
10+
outputVariable: "{id,text}",
11+
saveOutput: true,
1012
config: [
1113
{
1214
label: "按钮组",

0 commit comments

Comments
 (0)