mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-08 22:51:25 +08:00
添加函数返回、完善注入JS功能
This commit is contained in:
parent
0094cb6c29
commit
d69c473fe7
@ -102,6 +102,9 @@ export default defineComponent({
|
||||
this.updateModelValue(this.funcName, newArgvs);
|
||||
},
|
||||
generateCode(funcName, argvs) {
|
||||
if (this.localCommand.isExpression) {
|
||||
return argvs.join("");
|
||||
}
|
||||
/**
|
||||
* 字符串模式stringfiy后,null会变成'"null"', ''变成'""'
|
||||
* 变量模式stringify后,null变成'null', ''保持''
|
||||
@ -140,6 +143,10 @@ export default defineComponent({
|
||||
let argvs = window.lodashM.cloneDeep(this.defaultArgvs);
|
||||
if (!code) return argvs;
|
||||
|
||||
if (this.localCommand.isExpression) {
|
||||
return [code];
|
||||
}
|
||||
|
||||
const variableFormatPaths = [];
|
||||
|
||||
const addVariableFormatPath = (prefix, config) => {
|
||||
|
@ -21,6 +21,7 @@
|
||||
<!-- 保存变量按钮 -->
|
||||
<q-icon
|
||||
:name="command.saveOutput ? 'data_object' : 'output'"
|
||||
v-if="!command.neverHasOutput"
|
||||
class="output-btn"
|
||||
@click="$emit('toggle-output')"
|
||||
>
|
||||
|
92
src/components/composer/script/ReturnEditor.vue
Normal file
92
src/components/composer/script/ReturnEditor.vue
Normal 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>
|
@ -50,3 +50,8 @@ export const SystemCommandEditor = defineAsyncComponent(() =>
|
||||
export const SelectListEditor = defineAsyncComponent(() =>
|
||||
import("components/composer/ui/SelectListEditor.vue")
|
||||
);
|
||||
|
||||
// 编程组件
|
||||
export const ReturnEditor = defineAsyncComponent(() =>
|
||||
import("components/composer/script/ReturnEditor.vue")
|
||||
);
|
||||
|
@ -17,9 +17,11 @@ export const scriptCommands = {
|
||||
],
|
||||
},
|
||||
{
|
||||
value: "(function(code){new Function(code)()})",
|
||||
value: "injectJs",
|
||||
label: "注入JS脚本",
|
||||
icon: "script",
|
||||
neverHasOutput: true,
|
||||
isExpression: true,
|
||||
config: [
|
||||
{
|
||||
label: "JS脚本",
|
||||
@ -29,11 +31,10 @@ export const scriptCommands = {
|
||||
],
|
||||
},
|
||||
{
|
||||
value: "quickcommand.runAppleScript",
|
||||
label: "执行 AppleScript",
|
||||
value: "quickcommand.runCode",
|
||||
label: "执行代码",
|
||||
icon: "script",
|
||||
outputVariable: "result",
|
||||
saveOutput: true,
|
||||
config: [
|
||||
{
|
||||
label: "脚本",
|
||||
@ -42,5 +43,11 @@ export const scriptCommands = {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
value: "return",
|
||||
label: "函数返回",
|
||||
neverHasOutput: true,
|
||||
component: "ReturnEditor",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user