2020-12-27 16:13:01 +02:00
|
|
|
<template>
|
2023-01-15 13:55:09 +02:00
|
|
|
<div class="mkw-button data-cy-mkw-button">
|
2022-01-08 13:30:01 +02:00
|
|
|
<MkButton :primary="widgetProps.colored" full @click="run">
|
|
|
|
{{ widgetProps.label }}
|
2020-12-27 16:13:01 +02:00
|
|
|
</MkButton>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-08 13:30:01 +02:00
|
|
|
<script lang="ts" setup>
|
2023-02-16 16:09:41 +02:00
|
|
|
import { Interpreter, Parser } from '@syuilo/aiscript';
|
|
|
|
import { useWidgetPropsManager, Widget, WidgetComponentExpose } from './widget';
|
2022-09-06 12:21:49 +03:00
|
|
|
import { GetFormResultType } from '@/scripts/form';
|
2021-11-11 19:02:25 +02:00
|
|
|
import * as os from '@/os';
|
|
|
|
import { createAiScriptEnv } from '@/scripts/aiscript/api';
|
2022-01-08 13:30:01 +02:00
|
|
|
import { $i } from '@/account';
|
2022-09-06 12:21:49 +03:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2020-12-27 16:13:01 +02:00
|
|
|
|
2022-01-08 13:30:01 +02:00
|
|
|
const name = 'button';
|
2020-12-27 16:13:01 +02:00
|
|
|
|
2022-01-08 13:30:01 +02:00
|
|
|
const widgetPropsDef = {
|
|
|
|
label: {
|
|
|
|
type: 'string' as const,
|
|
|
|
default: 'BUTTON',
|
2020-12-27 16:13:01 +02:00
|
|
|
},
|
2022-01-08 13:30:01 +02:00
|
|
|
colored: {
|
|
|
|
type: 'boolean' as const,
|
|
|
|
default: true,
|
2020-12-27 16:13:01 +02:00
|
|
|
},
|
2022-01-08 13:30:01 +02:00
|
|
|
script: {
|
|
|
|
type: 'string' as const,
|
|
|
|
multiline: true,
|
|
|
|
default: 'Mk:dialog("hello" "world")',
|
|
|
|
},
|
|
|
|
};
|
2020-12-27 16:13:01 +02:00
|
|
|
|
2022-01-08 13:30:01 +02: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 10:43:12 +03:00
|
|
|
const emit = defineEmits<{ (ev: 'updateProps', props: WidgetProps); }>();
|
2022-01-08 13:30:01 +02:00
|
|
|
|
|
|
|
const { widgetProps, configure } = useWidgetPropsManager(name,
|
|
|
|
widgetPropsDef,
|
|
|
|
props,
|
|
|
|
emit,
|
|
|
|
);
|
|
|
|
|
2023-01-03 08:51:49 +02:00
|
|
|
const parser = new Parser();
|
|
|
|
|
2022-01-08 13:30:01 +02:00
|
|
|
const run = async () => {
|
2023-01-03 08:51:49 +02:00
|
|
|
const aiscript = new Interpreter(createAiScriptEnv({
|
2022-01-08 13:30:01 +02:00
|
|
|
storageKey: 'widget',
|
|
|
|
token: $i?.token,
|
|
|
|
}), {
|
|
|
|
in: (q) => {
|
|
|
|
return new Promise(ok => {
|
|
|
|
os.inputText({
|
|
|
|
title: q,
|
|
|
|
}).then(({ canceled, result: a }) => {
|
2023-02-05 13:29:10 +02:00
|
|
|
if (canceled) {
|
|
|
|
ok('');
|
|
|
|
} else {
|
|
|
|
ok(a);
|
|
|
|
}
|
2020-12-27 16:13:01 +02:00
|
|
|
});
|
2022-01-08 13:30:01 +02:00
|
|
|
});
|
2020-12-27 16:13:01 +02:00
|
|
|
},
|
2022-01-08 13:30:01 +02:00
|
|
|
out: (value) => {
|
|
|
|
// nop
|
|
|
|
},
|
|
|
|
log: (type, params) => {
|
|
|
|
// nop
|
2022-09-06 12:21:49 +03:00
|
|
|
},
|
2022-01-08 13:30:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let ast;
|
|
|
|
try {
|
2023-01-03 08:51:49 +02:00
|
|
|
ast = parser.parse(widgetProps.script);
|
2022-05-25 10:43:12 +03:00
|
|
|
} catch (err) {
|
2022-01-08 13:30:01 +02:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: 'Syntax error :(',
|
|
|
|
});
|
|
|
|
return;
|
2020-12-27 16:13:01 +02:00
|
|
|
}
|
2022-01-08 13:30:01 +02:00
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
2022-05-25 10:43:12 +03:00
|
|
|
} catch (err) {
|
2022-01-08 13:30:01 +02:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-05-25 10:43:12 +03:00
|
|
|
text: err,
|
2022-01-08 13:30:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
defineExpose<WidgetComponentExpose>({
|
|
|
|
name,
|
|
|
|
configure,
|
|
|
|
id: props.widget ? props.widget.id : null,
|
2020-12-27 16:13:01 +02:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2022-12-27 11:29:39 +02:00
|
|
|
<style lang="scss" scoped>
|
2020-12-27 16:13:01 +02:00
|
|
|
.mkw-button {
|
|
|
|
}
|
|
|
|
</style>
|