2020-04-11 06:44:32 -07:00
|
|
|
<template>
|
2023-01-04 20:59:48 -08:00
|
|
|
<MkSpacer :content-max="800">
|
|
|
|
<div :class="$style.root">
|
|
|
|
<div :class="$style.editor" class="_panel">
|
|
|
|
<PrismEditor v-model="code" class="_code code" :highlight="highlighter" :line-numbers="false"/>
|
|
|
|
<MkButton style="position: absolute; top: 8px; right: 8px;" primary @click="run()"><i class="ti ti-player-play"></i></MkButton>
|
2020-04-11 06:44:32 -07:00
|
|
|
</div>
|
|
|
|
|
2023-01-05 00:30:21 -08:00
|
|
|
<MkContainer v-if="root && components.length > 1" :key="uiKey" :foldable="true">
|
2023-01-04 20:59:48 -08:00
|
|
|
<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 }}
|
|
|
|
</div>
|
2020-12-27 18:56:42 -08:00
|
|
|
</div>
|
2023-01-04 20:59:48 -08:00
|
|
|
</MkSpacer>
|
2020-04-11 06:44:32 -07:00
|
|
|
</template>
|
|
|
|
|
2022-04-28 20:26:24 -07:00
|
|
|
<script lang="ts" setup>
|
2023-01-04 20:59:48 -08:00
|
|
|
import { onDeactivated, onUnmounted, Ref, ref, watch } from 'vue';
|
2020-08-21 16:03:11 -07:00
|
|
|
import 'prismjs';
|
|
|
|
import { highlight, languages } from 'prismjs/components/prism-core';
|
|
|
|
import 'prismjs/components/prism-clike';
|
|
|
|
import 'prismjs/components/prism-javascript';
|
2020-04-17 04:36:51 -07:00
|
|
|
import 'prismjs/themes/prism-okaidia.css';
|
2020-08-21 16:03:11 -07:00
|
|
|
import { PrismEditor } from 'vue-prism-editor';
|
|
|
|
import 'vue-prism-editor/dist/prismeditor.min.css';
|
2023-01-02 22:51:49 -08:00
|
|
|
import { Interpreter, Parser, utils } from '@syuilo/aiscript';
|
2022-09-06 02:21:49 -07:00
|
|
|
import MkContainer from '@/components/MkContainer.vue';
|
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2021-11-11 09:02:25 -08:00
|
|
|
import { createAiScriptEnv } from '@/scripts/aiscript/api';
|
|
|
|
import * as os from '@/os';
|
2022-04-28 20:26:24 -07:00
|
|
|
import { $i } from '@/account';
|
|
|
|
import { i18n } from '@/i18n';
|
2022-06-20 01:38:49 -07:00
|
|
|
import { definePageMetadata } from '@/scripts/page-metadata';
|
2023-02-16 06:09:41 -08:00
|
|
|
import { AsUiComponent, AsUiRoot, registerAsUiLib } from '@/scripts/aiscript/ui';
|
2023-01-04 20:59:48 -08:00
|
|
|
import MkAsUi from '@/components/MkAsUi.vue';
|
2023-01-06 17:13:02 -08:00
|
|
|
import { miLocalStorage } from '@/local-storage';
|
2023-01-20 22:30:29 -08:00
|
|
|
import { claimAchievement } from '@/scripts/achievements';
|
2020-04-11 06:44:32 -07:00
|
|
|
|
2023-01-02 22:51:49 -08:00
|
|
|
const parser = new Parser();
|
2023-01-04 20:59:48 -08:00
|
|
|
let aiscript: Interpreter;
|
2022-04-28 20:26:24 -07:00
|
|
|
const code = ref('');
|
|
|
|
const logs = ref<any[]>([]);
|
2023-01-04 20:59:48 -08:00
|
|
|
const root = ref<AsUiRoot>();
|
2023-01-15 17:04:10 -08:00
|
|
|
let components: Ref<AsUiComponent>[] = $ref([]);
|
2023-01-04 20:59:48 -08:00
|
|
|
let uiKey = $ref(0);
|
2020-04-11 06:44:32 -07:00
|
|
|
|
2023-01-06 17:13:02 -08:00
|
|
|
const saved = miLocalStorage.getItem('scratchpad');
|
2022-04-28 20:26:24 -07:00
|
|
|
if (saved) {
|
|
|
|
code.value = saved;
|
|
|
|
}
|
2020-04-11 06:44:32 -07:00
|
|
|
|
2022-04-28 20:26:24 -07:00
|
|
|
watch(code, () => {
|
2023-01-06 17:13:02 -08:00
|
|
|
miLocalStorage.setItem('scratchpad', code.value);
|
2022-04-28 20:26:24 -07:00
|
|
|
});
|
2020-04-11 06:44:32 -07:00
|
|
|
|
2022-04-28 20:26:24 -07:00
|
|
|
async function run() {
|
2023-01-04 20:59:48 -08:00
|
|
|
if (aiscript) aiscript.abort();
|
|
|
|
root.value = undefined;
|
|
|
|
components = [];
|
|
|
|
uiKey++;
|
2022-04-28 20:26:24 -07:00
|
|
|
logs.value = [];
|
2023-01-04 20:59:48 -08:00
|
|
|
aiscript = new Interpreter(({
|
|
|
|
...createAiScriptEnv({
|
|
|
|
storageKey: 'widget',
|
|
|
|
token: $i?.token,
|
|
|
|
}),
|
|
|
|
...registerAsUiLib(components, (_root) => {
|
|
|
|
root.value = _root.value;
|
|
|
|
}),
|
2022-04-28 20:26:24 -07:00
|
|
|
}), {
|
|
|
|
in: (q) => {
|
|
|
|
return new Promise(ok => {
|
|
|
|
os.inputText({
|
|
|
|
title: q,
|
|
|
|
}).then(({ canceled, result: a }) => {
|
2023-02-05 03:29:10 -08:00
|
|
|
if (canceled) {
|
|
|
|
ok('');
|
|
|
|
} else {
|
|
|
|
ok(a);
|
|
|
|
}
|
2020-04-11 06:44:32 -07:00
|
|
|
});
|
2022-04-28 20:26:24 -07:00
|
|
|
});
|
2020-08-21 16:03:11 -07:00
|
|
|
},
|
2022-04-28 20:26:24 -07:00
|
|
|
out: (value) => {
|
2023-01-20 22:30:29 -08:00
|
|
|
if (value.type === 'str' && value.value.toLowerCase().replace(',', '').includes('hello world')) {
|
|
|
|
claimAchievement('outputHelloWorldOnScratchpad');
|
|
|
|
}
|
2022-04-28 20:26:24 -07:00
|
|
|
logs.value.push({
|
|
|
|
id: Math.random(),
|
|
|
|
text: value.type === 'str' ? value.value : utils.valToString(value),
|
2022-06-20 01:38:49 -07:00
|
|
|
print: true,
|
2022-04-28 20:26:24 -07:00
|
|
|
});
|
2020-08-21 16:03:11 -07:00
|
|
|
},
|
2022-04-28 20:26:24 -07:00
|
|
|
log: (type, params) => {
|
|
|
|
switch (type) {
|
|
|
|
case 'end': logs.value.push({
|
|
|
|
id: Math.random(),
|
|
|
|
text: utils.valToString(params.val, true),
|
2022-06-20 01:38:49 -07:00
|
|
|
print: false,
|
2022-04-28 20:26:24 -07:00
|
|
|
}); break;
|
|
|
|
default: break;
|
|
|
|
}
|
2022-06-20 01:38:49 -07:00
|
|
|
},
|
2022-04-28 20:26:24 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
let ast;
|
|
|
|
try {
|
2023-01-02 22:51:49 -08:00
|
|
|
ast = parser.parse(code.value);
|
2022-04-28 20:26:24 -07:00
|
|
|
} catch (error) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 01:38:49 -07:00
|
|
|
text: 'Syntax error :(',
|
2022-04-28 20:26:24 -07:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
2023-01-04 20:59:48 -08:00
|
|
|
} catch (err: any) {
|
2022-04-28 20:26:24 -07:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2023-01-04 20:59:48 -08:00
|
|
|
title: 'AiScript Error',
|
|
|
|
text: err.message,
|
2022-04-28 20:26:24 -07:00
|
|
|
});
|
2020-04-11 06:44:32 -07:00
|
|
|
}
|
2022-06-09 22:36:55 -07:00
|
|
|
}
|
2022-04-28 20:26:24 -07:00
|
|
|
|
|
|
|
function highlighter(code) {
|
|
|
|
return highlight(code, languages.js, 'javascript');
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:59:48 -08:00
|
|
|
onDeactivated(() => {
|
|
|
|
if (aiscript) aiscript.abort();
|
|
|
|
});
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
if (aiscript) aiscript.abort();
|
|
|
|
});
|
|
|
|
|
2022-06-20 01:38:49 -07:00
|
|
|
const headerActions = $computed(() => []);
|
|
|
|
|
|
|
|
const headerTabs = $computed(() => []);
|
|
|
|
|
|
|
|
definePageMetadata({
|
|
|
|
title: i18n.ts.scratchpad,
|
2022-12-19 02:01:30 -08:00
|
|
|
icon: 'ti ti-terminal-2',
|
2020-04-11 06:44:32 -07:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-01-04 20:59:48 -08:00
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
gap: var(--margin);
|
|
|
|
}
|
2020-11-29 01:22:35 -08:00
|
|
|
|
2023-01-04 20:59:48 -08:00
|
|
|
.editor {
|
|
|
|
position: relative;
|
2020-11-29 01:22:35 -08:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:59:48 -08:00
|
|
|
.ui {
|
|
|
|
padding: 32px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.logs {
|
2020-04-11 06:44:32 -07:00
|
|
|
padding: 16px;
|
|
|
|
|
2023-01-04 20:59:48 -08:00
|
|
|
&:global {
|
|
|
|
> .log {
|
|
|
|
&:not(.print) {
|
|
|
|
opacity: 0.7;
|
|
|
|
}
|
2020-04-11 06:44:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|