summaryrefslogtreecommitdiff
path: root/src/client/widgets/button.vue
blob: af6718c50792dbf627def000bf8b64330430444f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<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';
import MkButton from '@client/components/ui/button.vue';
import * as os from '@client/os';
import { AiScript, parse, utils } from '@syuilo/aiscript';
import { createAiScriptEnv } from '@client/scripts/aiscript/api';

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({
				storageKey: 'widget',
				token: this.$i?.token,
			}), {
				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>