支持在varInput中选择函数

This commit is contained in:
fofolee 2025-01-21 23:14:00 +08:00
parent db0ce31c7d
commit ab0ab8a7a4
3 changed files with 81 additions and 44 deletions

View File

@ -23,8 +23,8 @@
narrow-indicator narrow-indicator
outside-arrows outside-arrows
> >
<template v-for="flow in nonMainFlows" :key="flow.id"> <template v-for="flow in subFlows" :key="flow.id">
<q-tab :name="flow.id" class="flow-tab"> <q-tab :name="flow.id" class="flow-tab" no-caps>
<div class="flow-tab-content"> <div class="flow-tab-content">
<template v-if="flow.isEditing"> <template v-if="flow.isEditing">
<q-input <q-input
@ -76,7 +76,7 @@
v-model="flow.commands" v-model="flow.commands"
:generate-code="() => generateFlowCode(flow)" :generate-code="() => generateFlowCode(flow)"
:show-close-button="flows.length > 1" :show-close-button="flows.length > 1"
@action="(type, payload) => handleFlowAction(type, payload, flow)" @action="(type, payload) => handleAction(type, payload)"
ref="flowRefs" ref="flowRefs"
/> />
</div> </div>
@ -84,7 +84,7 @@
</template> </template>
<script> <script>
import { defineComponent } from "vue"; import { defineComponent, provide, reactive } from "vue";
import ComposerFlow from "./ComposerFlow.vue"; import ComposerFlow from "./ComposerFlow.vue";
import ComposerButtons from "./flow/ComposerButtons.vue"; import ComposerButtons from "./flow/ComposerButtons.vue";
import { generateCode } from "js/composer/generateCode"; import { generateCode } from "js/composer/generateCode";
@ -103,27 +103,44 @@ export default defineComponent({
default: true, default: true,
}, },
}, },
setup() {
const subFlows = reactive([]);
const getCurrentFunctions = () => {
return subFlows.map((flow) => {
return {
label: flow.label,
value: flow.name,
};
});
};
provide("getCurrentFunctions", getCurrentFunctions);
return { subFlows };
},
data() { data() {
return { return {
flows: [ activeTab: "main",
{ isAllCollapsed: false,
mainFlow: {
id: "main", id: "main",
name: "main", name: "main",
label: "主流程", label: "主流程",
commands: [], commands: [],
}, },
],
activeTab: "main",
isAllCollapsed: false,
}; };
}, },
computed: { computed: {
nonMainFlows() { flows: {
return this.flows.filter((f) => f.id !== "main"); get() {
return [this.mainFlow, ...this.subFlows];
},
set(value) {
this.mainFlow = value[0];
this.subFlows = value.slice(1);
},
}, },
}, },
methods: { methods: {
generateFlowName(baseName = "flow_") { generateFlowName(baseName = "func_") {
return ( return (
baseName + baseName +
generateUniqSuffix( generateUniqSuffix(
@ -136,18 +153,18 @@ export default defineComponent({
addFlow() { addFlow() {
const id = this.$root.getUniqueId(); const id = this.$root.getUniqueId();
const name = this.generateFlowName(); const name = this.generateFlowName();
this.flows.push({ this.subFlows.push({
id, id,
name, name,
label: name.replace("flow_", "子流程"), label: name.replace("func_", "函数"),
commands: [], commands: [],
}); });
this.activeTab = id; this.activeTab = id;
}, },
removeFlow(flow) { removeFlow(flow) {
const index = this.flows.findIndex((f) => f.id === flow.id); const index = this.flows.findIndex((f) => f.id === flow.id);
if (index > -1 && flow.id !== "main") { if (index > -1) {
this.flows.splice(index, 1); this.subFlows.splice(index, 1);
this.activeTab = this.flows[0].id; this.activeTab = this.flows[0].id;
} }
}, },
@ -156,22 +173,10 @@ export default defineComponent({
}, },
generateAllFlowCode() { generateAllFlowCode() {
// flow // flow
return this.flows return [...this.subFlows, this.mainFlow]
.reverse()
.map((flow) => this.generateFlowCode(flow)) .map((flow) => this.generateFlowCode(flow))
.join("\n\n"); .join("\n\n");
}, },
handleFlowAction(type, payload, flow) {
if (type === "close") {
const index = this.flows.findIndex((f) => f.id === flow.id);
if (index > -1 && this.flows.length > 1) {
this.flows.splice(index, 1);
this.activeTab = this.flows[0].id;
}
} else {
this.handleAction(type, payload);
}
},
handleAction(type, payload) { handleAction(type, payload) {
switch (type) { switch (type) {
case "save": case "save":
@ -269,7 +274,7 @@ export default defineComponent({
finishEdit(flow) { finishEdit(flow) {
flow.isEditing = false; flow.isEditing = false;
if (!flow.label) { if (!flow.label) {
flow.label = this.generateFlowName().replace("flow_", "子流程"); flow.label = this.generateFlowName().replace("func_", "函数");
} }
}, },
}, },

View File

@ -145,7 +145,9 @@ export default defineComponent({
}); });
}, },
typeToggleTooltip() { typeToggleTooltip() {
const currentType = this.isString ? "字符串" : "变量、数字、表达式等"; const currentType = this.isString
? "字符串"
: "变量、数字、表达式、函数等";
const toggleText = this.disableToggleType ? "" : ",点击切换"; const toggleText = this.disableToggleType ? "" : ",点击切换";
return `当前类型是:${currentType}${toggleText}`; return `当前类型是:${currentType}${toggleText}`;
}, },

View File

@ -5,23 +5,23 @@
stretch stretch
size="sm" size="sm"
class="variable-dropdown" class="variable-dropdown"
@click="variables = getAvailableVariables()" @click="({ variables, functions } = getAvailableVariablesAndFunctions())"
> >
<q-list class="variable-list"> <q-list class="variable-list">
<q-item-label header class="variable-label"> <q-item-label header class="variable-label">
<q-icon name="functions" /> <q-icon name="functions" />
<span>选择变量</span> <span>选择变量或函数</span>
</q-item-label> </q-item-label>
<q-separator class="q-my-xs" /> <q-separator class="q-my-xs" />
<template v-if="variables.length"> <template v-if="variables.length || functions.length">
<q-item <q-item
v-for="variable in variables" v-for="variable in variables"
:key="variable.name" :key="variable.name"
clickable clickable
v-close-popup v-close-popup
@click="insertVariable(variable)" @click="insertValue(variable.name)"
class="variable-item" class="variable-item"
> >
<q-item-section> <q-item-section>
@ -33,6 +33,21 @@
</q-item-label> </q-item-label>
</q-item-section> </q-item-section>
</q-item> </q-item>
<q-separator v-if="variables.length" />
<q-item
v-for="func in functions"
:key="func.value"
clickable
v-close-popup
@click="insertValue(func.value + '()')"
class="variable-item"
>
<q-item-section>
<q-item-label class="variable-name">
{{ func.label }}
</q-item-label>
</q-item-section>
</q-item>
</template> </template>
<template v-else> <template v-else>
<q-item> <q-item>
@ -41,12 +56,17 @@
<div class="q-gutter-md"> <div class="q-gutter-md">
<div class="row items-center justify-center text-grey-6"> <div class="row items-center justify-center text-grey-6">
<q-icon name="info" size="20px" class="q-mr-sm" /> <q-icon name="info" size="20px" class="q-mr-sm" />
<span>当前命令没有可用变量</span> <span>当前命令没有可用变量或函数</span>
</div> </div>
<div class="row items-center justify-center text-grey-7"> <div class="row items-center justify-center text-grey-7">
<div class="text-grey-7">点击其他命令卡片右上角</div> <div>点击其他命令右上角</div>
<q-icon name="output" size="16px" class="q-mx-xs" /> <q-icon name="output" size="16px" class="q-mx-xs" />
<div>按钮添加输出变量</div> <div>按钮添加变量</div>
</div>
<div class="row items-center justify-center text-grey-7">
<div>或点击主流程右边的</div>
<q-icon name="add" size="16px" class="q-mx-xs" />
<div>按钮添加函数</div>
</div> </div>
</div> </div>
</q-item-label> </q-item-label>
@ -73,18 +93,28 @@ export default defineComponent({
); );
}; };
const getCurrentFunctions = inject("getCurrentFunctions");
const getAvailableVariablesAndFunctions = () => {
return { return {
getAvailableVariables, variables: getAvailableVariables(),
functions: getCurrentFunctions(),
};
};
return {
getAvailableVariablesAndFunctions,
}; };
}, },
data() { data() {
return { return {
variables: [], variables: [],
functions: [],
}; };
}, },
methods: { methods: {
insertVariable(variable) { insertValue(value) {
this.$emit("emitValue", "var", variable.name); this.$emit("emitValue", "var", value);
}, },
}, },
}); });