添加函数返回、完善注入JS功能

This commit is contained in:
fofolee 2025-01-22 00:52:53 +08:00
parent 0094cb6c29
commit d69c473fe7
5 changed files with 116 additions and 4 deletions

View File

@ -102,6 +102,9 @@ export default defineComponent({
this.updateModelValue(this.funcName, newArgvs); this.updateModelValue(this.funcName, newArgvs);
}, },
generateCode(funcName, argvs) { generateCode(funcName, argvs) {
if (this.localCommand.isExpression) {
return argvs.join("");
}
/** /**
* 字符串模式stringfiy后null会变成'"null"', ''变成'""' * 字符串模式stringfiy后null会变成'"null"', ''变成'""'
* 变量模式stringify后null变成'null', ''保持'' * 变量模式stringify后null变成'null', ''保持''
@ -140,6 +143,10 @@ export default defineComponent({
let argvs = window.lodashM.cloneDeep(this.defaultArgvs); let argvs = window.lodashM.cloneDeep(this.defaultArgvs);
if (!code) return argvs; if (!code) return argvs;
if (this.localCommand.isExpression) {
return [code];
}
const variableFormatPaths = []; const variableFormatPaths = [];
const addVariableFormatPath = (prefix, config) => { const addVariableFormatPath = (prefix, config) => {

View File

@ -21,6 +21,7 @@
<!-- 保存变量按钮 --> <!-- 保存变量按钮 -->
<q-icon <q-icon
:name="command.saveOutput ? 'data_object' : 'output'" :name="command.saveOutput ? 'data_object' : 'output'"
v-if="!command.neverHasOutput"
class="output-btn" class="output-btn"
@click="$emit('toggle-output')" @click="$emit('toggle-output')"
> >

View File

@ -0,0 +1,92 @@
<template>
<div class="row items-center">
<div class="col-2 return-label text-h6">return</div>
<div class="col-10">
<VariableInput
:model-value="argvs.returnValue"
@update:modelValue="updateArgvs('returnValue', $event)"
/>
</div>
</div>
</template>
<script>
import { defineComponent } from "vue";
import { newVarInputVal } from "js/composer/varInputValManager";
import VariableInput from "components/composer/common/VariableInput.vue";
import { parseFunction, stringifyArgv } from "js/composer/formatString";
export default defineComponent({
name: "ReturnEditor",
props: {
modelValue: Object,
},
components: {
VariableInput,
},
emits: ["update:modelValue"],
data() {
return {
defaultArgvs: {
returnValue: newVarInputVal("var"),
},
};
},
computed: {
argvs() {
return (
this.modelValue.argvs || this.parseCodeToArgvs(this.modelValue.code)
);
},
},
methods: {
parseCodeToArgvs(code) {
const argvs = window.lodashM.cloneDeep(this.defaultArgvs);
if (!code) return argvs;
code = code.trim().replace(/^return\s(.*)/, "tempFunc($1)");
try {
const variableFormatPaths = ["arg0"];
const params = parseFunction(code, { variableFormatPaths });
return {
returnValue: params.argvs[0],
};
} catch (e) {
console.error("解析参数失败:", e);
}
return argvs;
},
generateCode(argvs = this.argvs) {
return `${this.modelValue.value} ${stringifyArgv(argvs.returnValue)}`;
},
getSummary(argvs) {
return "返回" + " " + argvs.returnValue.value;
},
updateArgvs(key, newValue) {
this.argvs[key] = newValue;
this.updateModelValue(this.argvs);
},
updateModelValue(argvs) {
this.$emit("update:modelValue", {
...this.modelValue,
summary: this.getSummary(argvs),
argvs,
code: this.generateCode(argvs),
});
},
},
mounted() {
const argvs = this.modelValue.argvs || this.defaultArgvs;
if (!this.modelValue.code) {
this.updateModelValue(argvs);
}
},
});
</script>
<style scoped>
.return-label {
display: flex;
align-items: center;
justify-content: center;
}
</style>

View File

@ -50,3 +50,8 @@ export const SystemCommandEditor = defineAsyncComponent(() =>
export const SelectListEditor = defineAsyncComponent(() => export const SelectListEditor = defineAsyncComponent(() =>
import("components/composer/ui/SelectListEditor.vue") import("components/composer/ui/SelectListEditor.vue")
); );
// 编程组件
export const ReturnEditor = defineAsyncComponent(() =>
import("components/composer/script/ReturnEditor.vue")
);

View File

@ -17,9 +17,11 @@ export const scriptCommands = {
], ],
}, },
{ {
value: "(function(code){new Function(code)()})", value: "injectJs",
label: "注入JS脚本", label: "注入JS脚本",
icon: "script", icon: "script",
neverHasOutput: true,
isExpression: true,
config: [ config: [
{ {
label: "JS脚本", label: "JS脚本",
@ -29,11 +31,10 @@ export const scriptCommands = {
], ],
}, },
{ {
value: "quickcommand.runAppleScript", value: "quickcommand.runCode",
label: "执行 AppleScript", label: "执行代码",
icon: "script", icon: "script",
outputVariable: "result", outputVariable: "result",
saveOutput: true,
config: [ config: [
{ {
label: "脚本", label: "脚本",
@ -42,5 +43,11 @@ export const scriptCommands = {
}, },
], ],
}, },
{
value: "return",
label: "函数返回",
neverHasOutput: true,
component: "ReturnEditor",
},
], ],
}; };