calckey/packages/client/src/widgets/button.vue

114 lines
2.2 KiB
Vue
Raw Normal View History

2020-12-27 14:13:01 +00:00
<template>
2023-04-08 00:01:42 +00:00
<div class="mkw-button">
<MkButton :primary="widgetProps.colored" full @click="run">
{{ widgetProps.label }}
</MkButton>
</div>
2020-12-27 14:13:01 +00:00
</template>
<script lang="ts" setup>
2023-04-08 00:01:42 +00:00
import { onMounted, onUnmounted, ref, watch } from "vue";
import { AiScript, parse, utils } from "@syuilo/aiscript";
import {
useWidgetPropsManager,
Widget,
WidgetComponentEmits,
WidgetComponentExpose,
WidgetComponentProps,
} from "./widget";
import { GetFormResultType } from "@/scripts/form";
import * as os from "@/os";
import { createAiScriptEnv } from "@/scripts/aiscript/api";
import { $i } from "@/account";
import MkButton from "@/components/MkButton.vue";
2020-12-27 14:13:01 +00:00
2023-04-08 00:01:42 +00:00
const name = "button";
2020-12-27 14:13:01 +00:00
const widgetPropsDef = {
label: {
2023-04-08 00:01:42 +00:00
type: "string" as const,
default: "BUTTON",
2020-12-27 14:13:01 +00:00
},
colored: {
2023-04-08 00:01:42 +00:00
type: "boolean" as const,
default: true,
2020-12-27 14:13:01 +00:00
},
script: {
2023-04-08 00:01:42 +00:00
type: "string" as const,
multiline: true,
default: 'Mk:dialog("hello" "world")',
},
};
2020-12-27 14:13:01 +00:00
type WidgetProps = GetFormResultType<typeof widgetPropsDef>;
// 現時点ではvueの制限によりimportしたtypeをジェネリックに渡せない
//const props = defineProps<WidgetComponentProps<WidgetProps>>();
//const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
2023-04-08 00:01:42 +00:00
const props = defineProps<{ widget?: Widget<WidgetProps> }>();
const emit = defineEmits<{ (ev: "updateProps", props: WidgetProps) }>();
2023-04-08 00:01:42 +00:00
const { widgetProps, configure } = useWidgetPropsManager(
name,
widgetPropsDef,
props,
2023-04-08 00:01:42 +00:00
emit
);
const run = async () => {
2023-04-08 00:01:42 +00:00
const aiscript = new AiScript(
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 14:13:01 +00:00
});
2023-04-08 00:01:42 +00:00
},
out: (value) => {
// nop
},
log: (type, params) => {
// nop
},
}
);
let ast;
try {
ast = parse(widgetProps.script);
} catch (err) {
os.alert({
2023-04-08 00:01:42 +00:00
type: "error",
text: "Syntax error :(",
});
return;
2020-12-27 14:13:01 +00:00
}
try {
await aiscript.exec(ast);
} catch (err) {
os.alert({
2023-04-08 00:01:42 +00:00
type: "error",
text: err,
});
}
};
defineExpose<WidgetComponentExpose>({
name,
configure,
id: props.widget ? props.widget.id : null,
2020-12-27 14:13:01 +00:00
});
</script>
<style lang="scss" scoped>
.mkw-button {
}
</style>