2020-12-27 15:13:01 +01:00
|
|
|
<template>
|
2023-04-08 02:01:42 +02:00
|
|
|
<div class="mkw-button">
|
|
|
|
<MkButton :primary="widgetProps.colored" full @click="run">
|
|
|
|
{{ widgetProps.label }}
|
|
|
|
</MkButton>
|
|
|
|
</div>
|
2020-12-27 15:13:01 +01:00
|
|
|
</template>
|
|
|
|
|
2022-01-08 12:30:01 +01:00
|
|
|
<script lang="ts" setup>
|
2023-11-16 21:04:05 +01:00
|
|
|
import { onMounted, onUnmounted, ref, watch } from "vue";
|
|
|
|
import { Interpreter, Parser } from "@syuilo/aiscript";
|
2023-11-16 21:18:19 +01:00
|
|
|
import { useWidgetPropsManager } from "./widget";
|
|
|
|
import type {
|
2023-11-16 21:04:05 +01:00
|
|
|
WidgetComponentEmits,
|
2023-11-16 21:18:19 +01:00
|
|
|
WidgetComponentExpose,
|
2023-11-16 21:04:05 +01:00
|
|
|
WidgetComponentProps,
|
|
|
|
} from "./widget";
|
2023-11-16 21:18:19 +01:00
|
|
|
import { createAiScriptEnv } from "@/scripts/aiscript/api";
|
2023-07-17 00:32:32 +02:00
|
|
|
import type { GetFormResultType } from "@/scripts/form";
|
2023-04-08 02:01:42 +02:00
|
|
|
import * as os from "@/os";
|
2023-10-22 23:29:46 +02:00
|
|
|
import { $i } from "@/reactiveAccount";
|
2023-04-08 02:01:42 +02:00
|
|
|
import MkButton from "@/components/MkButton.vue";
|
2020-12-27 15:13:01 +01:00
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
const name = "button";
|
2020-12-27 15:13:01 +01:00
|
|
|
|
2022-01-08 12:30:01 +01:00
|
|
|
const widgetPropsDef = {
|
|
|
|
label: {
|
2023-04-08 02:01:42 +02:00
|
|
|
type: "string" as const,
|
|
|
|
default: "BUTTON",
|
2020-12-27 15:13:01 +01:00
|
|
|
},
|
2022-01-08 12:30:01 +01:00
|
|
|
colored: {
|
2023-04-08 02:01:42 +02:00
|
|
|
type: "boolean" as const,
|
2022-01-08 12:30:01 +01:00
|
|
|
default: true,
|
2020-12-27 15:13:01 +01:00
|
|
|
},
|
2022-01-08 12:30:01 +01:00
|
|
|
script: {
|
2023-04-08 02:01:42 +02:00
|
|
|
type: "string" as const,
|
2022-01-08 12:30:01 +01:00
|
|
|
multiline: true,
|
|
|
|
default: 'Mk:dialog("hello" "world")',
|
|
|
|
},
|
|
|
|
};
|
2020-12-27 15:13:01 +01:00
|
|
|
|
2022-01-08 12:30:01 +01:00
|
|
|
type WidgetProps = GetFormResultType<typeof widgetPropsDef>;
|
|
|
|
|
2023-11-16 21:04:05 +01:00
|
|
|
const props = defineProps<WidgetComponentProps<WidgetProps>>();
|
|
|
|
const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
|
2022-01-08 12:30:01 +01:00
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
const { widgetProps, configure } = useWidgetPropsManager(
|
|
|
|
name,
|
2022-01-08 12:30:01 +01:00
|
|
|
widgetPropsDef,
|
|
|
|
props,
|
2023-07-06 03:28:27 +02:00
|
|
|
emit,
|
2022-01-08 12:30:01 +01:00
|
|
|
);
|
|
|
|
|
2023-11-16 21:04:05 +01:00
|
|
|
const parser = new Parser();
|
|
|
|
|
2022-01-08 12:30:01 +01:00
|
|
|
const run = async () => {
|
2023-11-16 21:04:05 +01:00
|
|
|
const aiscript = new Interpreter(
|
2023-04-08 02:01:42 +02:00
|
|
|
createAiScriptEnv({
|
|
|
|
storageKey: "widget",
|
|
|
|
token: $i?.token,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
in: (q) => {
|
|
|
|
return new Promise((ok) => {
|
|
|
|
os.inputText({
|
|
|
|
title: q,
|
|
|
|
}).then(({ canceled, result: a }) => {
|
|
|
|
ok(a);
|
|
|
|
});
|
2020-12-27 15:13:01 +01:00
|
|
|
});
|
2023-04-08 02:01:42 +02:00
|
|
|
},
|
|
|
|
out: (value) => {
|
|
|
|
// nop
|
|
|
|
},
|
|
|
|
log: (type, params) => {
|
|
|
|
// nop
|
|
|
|
},
|
2023-07-06 03:28:27 +02:00
|
|
|
},
|
2023-04-08 02:01:42 +02:00
|
|
|
);
|
2022-01-08 12:30:01 +01:00
|
|
|
|
|
|
|
let ast;
|
|
|
|
try {
|
2023-11-16 21:04:05 +01:00
|
|
|
ast = parser.parse(widgetProps.script);
|
2022-05-25 09:43:12 +02:00
|
|
|
} catch (err) {
|
2022-01-08 12:30:01 +01:00
|
|
|
os.alert({
|
2023-04-08 02:01:42 +02:00
|
|
|
type: "error",
|
2023-11-16 21:04:05 +01:00
|
|
|
text: `Syntax error: ${err}`,
|
2022-01-08 12:30:01 +01:00
|
|
|
});
|
|
|
|
return;
|
2020-12-27 15:13:01 +01:00
|
|
|
}
|
2022-01-08 12:30:01 +01:00
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
2022-05-25 09:43:12 +02:00
|
|
|
} catch (err) {
|
2022-01-08 12:30:01 +01:00
|
|
|
os.alert({
|
2023-04-08 02:01:42 +02:00
|
|
|
type: "error",
|
2023-11-16 21:04:05 +01:00
|
|
|
text: String(err),
|
2022-01-08 12:30:01 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
defineExpose<WidgetComponentExpose>({
|
|
|
|
name,
|
|
|
|
configure,
|
|
|
|
id: props.widget ? props.widget.id : null,
|
2020-12-27 15:13:01 +01:00
|
|
|
});
|
|
|
|
</script>
|