2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-12-27 15:13:01 +01:00
|
|
|
<template>
|
2023-03-09 11:59:11 +01:00
|
|
|
<div data-cy-mkw-button class="mkw-button">
|
2022-01-08 12:30:01 +01:00
|
|
|
<MkButton :primary="widgetProps.colored" full @click="run">
|
|
|
|
{{ widgetProps.label }}
|
2020-12-27 15:13:01 +01:00
|
|
|
</MkButton>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-08 12:30:01 +01:00
|
|
|
<script lang="ts" setup>
|
2023-02-16 15:09:41 +01:00
|
|
|
import { Interpreter, Parser } from '@syuilo/aiscript';
|
2023-09-29 04:22:59 +02:00
|
|
|
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget.js';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { GetFormResultType } from '@/scripts/form.js';
|
|
|
|
import * as os from '@/os.js';
|
|
|
|
import { createAiScriptEnv } from '@/scripts/aiscript/api.js';
|
|
|
|
import { $i } from '@/account.js';
|
2022-09-06 11:21:49 +02:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2020-12-27 15:13:01 +01:00
|
|
|
|
2022-01-08 12:30:01 +01:00
|
|
|
const name = 'button';
|
2020-12-27 15:13:01 +01:00
|
|
|
|
2022-01-08 12:30:01 +01:00
|
|
|
const widgetPropsDef = {
|
|
|
|
label: {
|
|
|
|
type: 'string' as const,
|
|
|
|
default: 'BUTTON',
|
2020-12-27 15:13:01 +01:00
|
|
|
},
|
2022-01-08 12:30:01 +01:00
|
|
|
colored: {
|
|
|
|
type: 'boolean' as const,
|
|
|
|
default: true,
|
2020-12-27 15:13:01 +01:00
|
|
|
},
|
2022-01-08 12:30:01 +01:00
|
|
|
script: {
|
|
|
|
type: 'string' as const,
|
|
|
|
multiline: true,
|
|
|
|
default: 'Mk:dialog("hello" "world")',
|
|
|
|
},
|
|
|
|
};
|
2020-12-27 15:13:01 +01:00
|
|
|
|
2022-01-08 12:30:01 +01:00
|
|
|
type WidgetProps = GetFormResultType<typeof widgetPropsDef>;
|
|
|
|
|
2023-05-19 09:20:53 +02:00
|
|
|
const props = defineProps<WidgetComponentProps<WidgetProps>>();
|
|
|
|
const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
|
2022-01-08 12:30:01 +01:00
|
|
|
|
|
|
|
const { widgetProps, configure } = useWidgetPropsManager(name,
|
|
|
|
widgetPropsDef,
|
|
|
|
props,
|
|
|
|
emit,
|
|
|
|
);
|
|
|
|
|
2023-01-03 07:51:49 +01:00
|
|
|
const parser = new Parser();
|
|
|
|
|
2022-01-08 12:30:01 +01:00
|
|
|
const run = async () => {
|
2023-01-03 07:51:49 +01:00
|
|
|
const aiscript = new Interpreter(createAiScriptEnv({
|
2022-01-08 12:30:01 +01:00
|
|
|
storageKey: 'widget',
|
|
|
|
token: $i?.token,
|
|
|
|
}), {
|
|
|
|
in: (q) => {
|
|
|
|
return new Promise(ok => {
|
|
|
|
os.inputText({
|
|
|
|
title: q,
|
|
|
|
}).then(({ canceled, result: a }) => {
|
2023-02-05 12:29:10 +01:00
|
|
|
if (canceled) {
|
|
|
|
ok('');
|
|
|
|
} else {
|
|
|
|
ok(a);
|
|
|
|
}
|
2020-12-27 15:13:01 +01:00
|
|
|
});
|
2022-01-08 12:30:01 +01:00
|
|
|
});
|
2020-12-27 15:13:01 +01:00
|
|
|
},
|
2022-01-08 12:30:01 +01:00
|
|
|
out: (value) => {
|
|
|
|
// nop
|
|
|
|
},
|
|
|
|
log: (type, params) => {
|
|
|
|
// nop
|
2022-09-06 11:21:49 +02:00
|
|
|
},
|
2022-01-08 12:30:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
let ast;
|
|
|
|
try {
|
2023-01-03 07:51:49 +01:00
|
|
|
ast = parser.parse(widgetProps.script);
|
2022-05-25 09:43:12 +02:00
|
|
|
} catch (err) {
|
2022-01-08 12:30:01 +01:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: 'Syntax error :(',
|
|
|
|
});
|
|
|
|
return;
|
2020-12-27 15:13:01 +01:00
|
|
|
}
|
2022-01-08 12:30:01 +01:00
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
2022-05-25 09:43:12 +02:00
|
|
|
} catch (err) {
|
2022-01-08 12:30:01 +01:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-05-25 09:43:12 +02:00
|
|
|
text: err,
|
2022-01-08 12:30:01 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
defineExpose<WidgetComponentExpose>({
|
|
|
|
name,
|
|
|
|
configure,
|
|
|
|
id: props.widget ? props.widget.id : null,
|
2020-12-27 15:13:01 +01:00
|
|
|
});
|
|
|
|
</script>
|