hippofish/packages/frontend/src/widgets/WidgetAiscript.vue

180 lines
3.5 KiB
Vue
Raw Normal View History

2021-02-06 13:36:47 +01:00
<template>
2023-05-19 09:20:53 +02:00
<MkContainer :showHeader="widgetProps.showHeader" data-cy-mkw-aiscript class="mkw-aiscript">
2023-01-15 00:30:29 +01:00
<template #icon><i class="ti ti-terminal-2"></i></template>
<template #header>{{ i18n.ts._widgets.aiscript }}</template>
2021-02-06 13:36:47 +01:00
<div class="uylguesu _monospace">
<textarea v-model="widgetProps.script" placeholder="(1 + 1)"></textarea>
2021-11-19 11:36:12 +01:00
<button class="_buttonPrimary" @click="run">RUN</button>
2021-02-06 13:36:47 +01:00
<div class="logs">
2021-11-19 11:36:12 +01:00
<div v-for="log in logs" :key="log.id" class="log" :class="{ print: log.print }">{{ log.text }}</div>
2021-02-06 13:36:47 +01:00
</div>
</div>
</MkContainer>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
2023-01-03 07:51:49 +01:00
import { Interpreter, Parser, utils } from '@syuilo/aiscript';
2023-05-19 09:20:53 +02:00
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget';
2022-07-20 15:24:26 +02:00
import { GetFormResultType } from '@/scripts/form';
2021-11-11 18:02:25 +01:00
import * as os from '@/os';
import MkContainer from '@/components/MkContainer.vue';
2021-11-11 18:02:25 +01:00
import { createAiScriptEnv } from '@/scripts/aiscript/api';
import { $i } from '@/account';
2022-07-20 15:24:26 +02:00
import { i18n } from '@/i18n';
2021-02-06 13:36:47 +01:00
const name = 'aiscript';
2021-02-06 13:36:47 +01:00
const widgetPropsDef = {
showHeader: {
type: 'boolean' as const,
default: true,
2021-02-06 13:36:47 +01:00
},
script: {
type: 'string' as const,
multiline: true,
default: '(1 + 1)',
hidden: true,
2021-02-06 13:36:47 +01:00
},
};
2021-02-06 13:36:47 +01:00
type WidgetProps = GetFormResultType<typeof widgetPropsDef>;
2021-02-06 13:36:47 +01:00
2023-05-19 09:20:53 +02:00
const props = defineProps<WidgetComponentProps<WidgetProps>>();
const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
const { widgetProps, configure } = useWidgetPropsManager(name,
widgetPropsDef,
props,
emit,
);
2023-01-03 07:51:49 +01:00
const parser = new Parser();
const logs = ref<{
id: string;
text: string;
print: boolean;
}[]>([]);
const run = async () => {
logs.value = [];
2023-01-03 07:51:49 +01:00
const aiscript = new Interpreter(createAiScriptEnv({
storageKey: 'widget',
token: $i?.token,
}), {
in: (q) => {
return new Promise(ok => {
os.inputText({
title: q,
}).then(({ canceled, result: a }) => {
if (canceled) {
ok('');
} else {
ok(a);
}
2021-02-06 13:36:47 +01:00
});
});
},
out: (value) => {
logs.value.push({
id: Math.random().toString(),
text: value.type === 'str' ? value.value : utils.valToString(value),
print: true,
});
2021-02-06 13:36:47 +01:00
},
log: (type, params) => {
switch (type) {
case 'end': logs.value.push({
id: Math.random().toString(),
text: utils.valToString(params.val, true),
print: false,
}); break;
default: break;
}
2022-07-20 15:24:26 +02:00
},
});
let ast;
try {
2023-01-03 07:51:49 +01:00
ast = parser.parse(widgetProps.script);
} catch (err) {
os.alert({
type: 'error',
text: 'Syntax error :(',
});
return;
2021-02-06 13:36:47 +01:00
}
try {
await aiscript.exec(ast);
} catch (err) {
os.alert({
type: 'error',
text: err,
});
}
};
defineExpose<WidgetComponentExpose>({
name,
configure,
id: props.widget ? props.widget.id : null,
2021-02-06 13:36:47 +01:00
});
</script>
<style lang="scss" scoped>
2021-02-06 13:36:47 +01:00
.uylguesu {
text-align: right;
> textarea {
display: block;
width: 100%;
max-width: 100%;
min-width: 100%;
padding: 16px;
color: var(--fg);
background: transparent;
border: none;
border-bottom: solid 0.5px var(--divider);
2021-02-06 13:36:47 +01:00
border-radius: 0;
box-sizing: border-box;
font: inherit;
2021-10-02 19:46:58 +02:00
&:focus-visible {
2021-02-06 13:36:47 +01:00
outline: none;
}
}
> button {
display: inline-block;
margin: 8px;
padding: 0 10px;
height: 28px;
outline: none;
border-radius: 4px;
&:disabled {
opacity: 0.7;
cursor: default;
}
}
> .logs {
border-top: solid 0.5px var(--divider);
2021-02-06 13:36:47 +01:00
text-align: left;
padding: 16px;
&:empty {
display: none;
}
> .log {
&:not(.print) {
opacity: 0.7;
}
}
}
}
</style>