2020-12-27 23:13:01 +09:00
|
|
|
<template>
|
|
|
|
<div class="mkw-button">
|
|
|
|
<MkButton :primary="props.colored" full @click="run">
|
|
|
|
{{ props.label }}
|
|
|
|
</MkButton>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import { defineComponent } from 'vue';
|
|
|
|
import define from './define';
|
2021-03-23 17:30:14 +09:00
|
|
|
import MkButton from '@client/components/ui/button.vue';
|
|
|
|
import * as os from '@client/os';
|
2020-12-27 23:13:01 +09:00
|
|
|
import { AiScript, parse, utils } from '@syuilo/aiscript';
|
2021-03-23 17:30:14 +09:00
|
|
|
import { createAiScriptEnv } from '@client/scripts/aiscript/api';
|
2020-12-27 23:13:01 +09:00
|
|
|
|
|
|
|
const widget = define({
|
|
|
|
name: 'button',
|
|
|
|
props: () => ({
|
|
|
|
label: {
|
|
|
|
type: 'string',
|
|
|
|
default: 'BUTTON',
|
|
|
|
},
|
|
|
|
colored: {
|
|
|
|
type: 'boolean',
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
script: {
|
|
|
|
type: 'string',
|
|
|
|
multiline: true,
|
|
|
|
default: 'Mk:dialog("hello" "world")',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
components: {
|
|
|
|
MkButton
|
|
|
|
},
|
|
|
|
extends: widget,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
async run() {
|
|
|
|
const aiscript = new AiScript(createAiScriptEnv({
|
2021-02-11 16:16:04 +09:00
|
|
|
storageKey: 'widget',
|
|
|
|
token: this.$i?.token,
|
2020-12-27 23:13:01 +09:00
|
|
|
}), {
|
|
|
|
in: (q) => {
|
|
|
|
return new Promise(ok => {
|
|
|
|
os.dialog({
|
|
|
|
title: q,
|
|
|
|
input: {}
|
|
|
|
}).then(({ canceled, result: a }) => {
|
|
|
|
ok(a);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
out: (value) => {
|
|
|
|
// nop
|
|
|
|
},
|
|
|
|
log: (type, params) => {
|
|
|
|
// nop
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let ast;
|
|
|
|
try {
|
|
|
|
ast = parse(this.props.script);
|
|
|
|
} catch (e) {
|
|
|
|
os.dialog({
|
|
|
|
type: 'error',
|
|
|
|
text: 'Syntax error :('
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
|
|
|
} catch (e) {
|
|
|
|
os.dialog({
|
|
|
|
type: 'error',
|
|
|
|
text: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.mkw-button {
|
|
|
|
}
|
|
|
|
</style>
|