2023-07-27 05:31:52 +00:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-04-11 13:44:32 +00:00
|
|
|
<template>
|
2023-10-29 05:12:40 +00:00
|
|
|
<MkStickyContainer>
|
|
|
|
<template #header><MkPageHeader/></template>
|
|
|
|
|
|
|
|
<MkSpacer :contentMax="800">
|
|
|
|
<div :class="$style.root">
|
|
|
|
<div class="_gaps_s">
|
|
|
|
<div :class="$style.editor" class="_panel">
|
|
|
|
<MkCodeEditor v-model="code" lang="aiscript"/>
|
|
|
|
</div>
|
2023-10-31 18:33:24 +00:00
|
|
|
<MkButton primary @click="run()"><i class="ph-play ph-bold ph-lg"></i></MkButton>
|
2023-01-05 04:59:48 +00:00
|
|
|
</div>
|
|
|
|
|
2023-10-29 05:12:40 +00:00
|
|
|
<MkContainer v-if="root && components.length > 1" :key="uiKey" :foldable="true">
|
|
|
|
<template #header>UI</template>
|
|
|
|
<div :class="$style.ui">
|
|
|
|
<MkAsUi :component="root" :components="components" size="small"/>
|
|
|
|
</div>
|
|
|
|
</MkContainer>
|
|
|
|
|
|
|
|
<MkContainer :foldable="true" class="">
|
|
|
|
<template #header>{{ i18n.ts.output }}</template>
|
|
|
|
<div :class="$style.logs">
|
|
|
|
<div v-for="log in logs" :key="log.id" class="log" :class="{ print: log.print }">{{ log.text }}</div>
|
|
|
|
</div>
|
|
|
|
</MkContainer>
|
|
|
|
|
|
|
|
<div class="">
|
|
|
|
{{ i18n.ts.scratchpadDescription }}
|
2023-01-05 04:59:48 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-10-29 05:12:40 +00:00
|
|
|
</MkSpacer>
|
|
|
|
</MkStickyContainer>
|
2020-04-11 13:44:32 +00:00
|
|
|
</template>
|
|
|
|
|
2022-04-29 03:26:24 +00:00
|
|
|
<script lang="ts" setup>
|
2023-12-07 05:42:09 +00:00
|
|
|
import { onDeactivated, onUnmounted, Ref, ref, watch, computed } from 'vue';
|
2023-01-03 06:51:49 +00:00
|
|
|
import { Interpreter, Parser, utils } from '@syuilo/aiscript';
|
2022-09-06 09:21:49 +00:00
|
|
|
import MkContainer from '@/components/MkContainer.vue';
|
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2023-10-29 05:12:40 +00:00
|
|
|
import MkCodeEditor from '@/components/MkCodeEditor.vue';
|
2023-09-19 07:37:43 +00:00
|
|
|
import { createAiScriptEnv } from '@/scripts/aiscript/api.js';
|
|
|
|
import * as os from '@/os.js';
|
|
|
|
import { $i } from '@/account.js';
|
|
|
|
import { i18n } from '@/i18n.js';
|
|
|
|
import { definePageMetadata } from '@/scripts/page-metadata.js';
|
|
|
|
import { AsUiComponent, AsUiRoot, registerAsUiLib } from '@/scripts/aiscript/ui.js';
|
2023-01-05 04:59:48 +00:00
|
|
|
import MkAsUi from '@/components/MkAsUi.vue';
|
2023-09-19 07:37:43 +00:00
|
|
|
import { miLocalStorage } from '@/local-storage.js';
|
|
|
|
import { claimAchievement } from '@/scripts/achievements.js';
|
2020-04-11 13:44:32 +00:00
|
|
|
|
2023-01-03 06:51:49 +00:00
|
|
|
const parser = new Parser();
|
2023-01-05 04:59:48 +00:00
|
|
|
let aiscript: Interpreter;
|
2022-04-29 03:26:24 +00:00
|
|
|
const code = ref('');
|
|
|
|
const logs = ref<any[]>([]);
|
2023-01-05 04:59:48 +00:00
|
|
|
const root = ref<AsUiRoot>();
|
2023-12-07 05:42:09 +00:00
|
|
|
const components = ref<Ref<AsUiComponent>[]>([]);
|
|
|
|
const uiKey = ref(0);
|
2020-04-11 13:44:32 +00:00
|
|
|
|
2023-01-07 01:13:02 +00:00
|
|
|
const saved = miLocalStorage.getItem('scratchpad');
|
2022-04-29 03:26:24 +00:00
|
|
|
if (saved) {
|
|
|
|
code.value = saved;
|
|
|
|
}
|
2020-04-11 13:44:32 +00:00
|
|
|
|
2022-04-29 03:26:24 +00:00
|
|
|
watch(code, () => {
|
2023-01-07 01:13:02 +00:00
|
|
|
miLocalStorage.setItem('scratchpad', code.value);
|
2022-04-29 03:26:24 +00:00
|
|
|
});
|
2020-04-11 13:44:32 +00:00
|
|
|
|
2022-04-29 03:26:24 +00:00
|
|
|
async function run() {
|
2023-01-05 04:59:48 +00:00
|
|
|
if (aiscript) aiscript.abort();
|
|
|
|
root.value = undefined;
|
2023-12-07 05:42:09 +00:00
|
|
|
components.value = [];
|
|
|
|
uiKey.value++;
|
2022-04-29 03:26:24 +00:00
|
|
|
logs.value = [];
|
2023-01-05 04:59:48 +00:00
|
|
|
aiscript = new Interpreter(({
|
|
|
|
...createAiScriptEnv({
|
|
|
|
storageKey: 'widget',
|
|
|
|
token: $i?.token,
|
|
|
|
}),
|
2023-12-07 05:42:09 +00:00
|
|
|
...registerAsUiLib(components.value, (_root) => {
|
2023-01-05 04:59:48 +00:00
|
|
|
root.value = _root.value;
|
|
|
|
}),
|
2022-04-29 03:26:24 +00:00
|
|
|
}), {
|
|
|
|
in: (q) => {
|
|
|
|
return new Promise(ok => {
|
|
|
|
os.inputText({
|
|
|
|
title: q,
|
|
|
|
}).then(({ canceled, result: a }) => {
|
2023-02-05 11:29:10 +00:00
|
|
|
if (canceled) {
|
|
|
|
ok('');
|
|
|
|
} else {
|
|
|
|
ok(a);
|
|
|
|
}
|
2020-04-11 13:44:32 +00:00
|
|
|
});
|
2022-04-29 03:26:24 +00:00
|
|
|
});
|
2020-08-21 23:03:11 +00:00
|
|
|
},
|
2022-04-29 03:26:24 +00:00
|
|
|
out: (value) => {
|
2023-01-21 06:30:29 +00:00
|
|
|
if (value.type === 'str' && value.value.toLowerCase().replace(',', '').includes('hello world')) {
|
|
|
|
claimAchievement('outputHelloWorldOnScratchpad');
|
|
|
|
}
|
2022-04-29 03:26:24 +00:00
|
|
|
logs.value.push({
|
|
|
|
id: Math.random(),
|
|
|
|
text: value.type === 'str' ? value.value : utils.valToString(value),
|
2022-06-20 08:38:49 +00:00
|
|
|
print: true,
|
2022-04-29 03:26:24 +00:00
|
|
|
});
|
2020-08-21 23:03:11 +00:00
|
|
|
},
|
2023-09-19 07:53:43 +00:00
|
|
|
err: (err) => {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
title: 'AiScript Error',
|
|
|
|
text: err.toString(),
|
|
|
|
});
|
|
|
|
},
|
2022-04-29 03:26:24 +00:00
|
|
|
log: (type, params) => {
|
|
|
|
switch (type) {
|
|
|
|
case 'end': logs.value.push({
|
|
|
|
id: Math.random(),
|
|
|
|
text: utils.valToString(params.val, true),
|
2022-06-20 08:38:49 +00:00
|
|
|
print: false,
|
2022-04-29 03:26:24 +00:00
|
|
|
}); break;
|
|
|
|
default: break;
|
|
|
|
}
|
2022-06-20 08:38:49 +00:00
|
|
|
},
|
2022-04-29 03:26:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let ast;
|
|
|
|
try {
|
2023-01-03 06:51:49 +00:00
|
|
|
ast = parser.parse(code.value);
|
2023-09-19 07:53:43 +00:00
|
|
|
} catch (err: any) {
|
2022-04-29 03:26:24 +00:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2023-09-19 07:53:43 +00:00
|
|
|
title: 'Syntax Error',
|
|
|
|
text: err.toString(),
|
2022-04-29 03:26:24 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
2023-01-05 04:59:48 +00:00
|
|
|
} catch (err: any) {
|
2023-09-19 07:53:43 +00:00
|
|
|
// AiScript runtime errors should be processed by error callback function
|
|
|
|
// so errors caught here are AiScript's internal errors.
|
2022-04-29 03:26:24 +00:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2023-09-19 07:53:43 +00:00
|
|
|
title: 'Internal Error',
|
|
|
|
text: err.toString(),
|
2022-04-29 03:26:24 +00:00
|
|
|
});
|
2020-04-11 13:44:32 +00:00
|
|
|
}
|
2022-06-10 05:36:55 +00:00
|
|
|
}
|
2022-04-29 03:26:24 +00:00
|
|
|
|
2023-01-05 04:59:48 +00:00
|
|
|
onDeactivated(() => {
|
|
|
|
if (aiscript) aiscript.abort();
|
|
|
|
});
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
if (aiscript) aiscript.abort();
|
|
|
|
});
|
|
|
|
|
2023-12-07 05:42:09 +00:00
|
|
|
const headerActions = computed(() => []);
|
2022-06-20 08:38:49 +00:00
|
|
|
|
2023-12-07 05:42:09 +00:00
|
|
|
const headerTabs = computed(() => []);
|
2022-06-20 08:38:49 +00:00
|
|
|
|
|
|
|
definePageMetadata({
|
|
|
|
title: i18n.ts.scratchpad,
|
2023-09-30 22:46:42 +00:00
|
|
|
icon: 'ph-terminal-window ph-bold ph-lg-2',
|
2020-04-11 13:44:32 +00:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-01-05 04:59:48 +00:00
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
gap: var(--margin);
|
|
|
|
}
|
2020-11-29 09:22:35 +00:00
|
|
|
|
2023-01-05 04:59:48 +00:00
|
|
|
.editor {
|
|
|
|
position: relative;
|
2020-11-29 09:22:35 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 09:25:31 +00:00
|
|
|
.code {
|
|
|
|
background: #2d2d2d;
|
|
|
|
color: #ccc;
|
|
|
|
font-size: 14px;
|
|
|
|
line-height: 1.5;
|
|
|
|
padding: 5px;
|
|
|
|
}
|
|
|
|
|
2023-01-05 04:59:48 +00:00
|
|
|
.ui {
|
|
|
|
padding: 32px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.logs {
|
2020-04-11 13:44:32 +00:00
|
|
|
padding: 16px;
|
|
|
|
|
2023-01-05 04:59:48 +00:00
|
|
|
&:global {
|
|
|
|
> .log {
|
|
|
|
&:not(.print) {
|
|
|
|
opacity: 0.7;
|
|
|
|
}
|
2020-04-11 13:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|