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