2020-04-11 08:44:32 -05:00
|
|
|
<template>
|
2020-12-27 20:56:42 -06:00
|
|
|
<div class="iltifgqe">
|
2021-04-10 04:17:42 -05:00
|
|
|
<div class="editor _panel _gap">
|
2021-11-19 04:36:12 -06:00
|
|
|
<PrismEditor v-model="code" class="_code code" :highlight="highlighter" :line-numbers="false"/>
|
2022-12-19 04:01:30 -06:00
|
|
|
<MkButton style="position: absolute; top: 8px; right: 8px;" primary @click="run()"><i class="ti ti-player-play"></i></MkButton>
|
2020-04-11 08:44:32 -05:00
|
|
|
</div>
|
|
|
|
|
2021-04-15 22:17:22 -05:00
|
|
|
<MkContainer :foldable="true" class="_gap">
|
2022-04-28 22:26:24 -05:00
|
|
|
<template #header>{{ i18n.ts.output }}</template>
|
2020-04-11 08:44:32 -05:00
|
|
|
<div class="bepmlvbi">
|
2021-11-19 04:36:12 -06:00
|
|
|
<div v-for="log in logs" :key="log.id" class="log" :class="{ print: log.print }">{{ log.text }}</div>
|
2020-04-11 08:44:32 -05:00
|
|
|
</div>
|
2020-10-17 06:12:00 -05:00
|
|
|
</MkContainer>
|
2020-04-11 08:44:32 -05:00
|
|
|
|
2021-04-10 04:17:42 -05:00
|
|
|
<div class="_gap">
|
2022-04-28 22:26:24 -05:00
|
|
|
{{ i18n.ts.scratchpadDescription }}
|
2020-12-27 20:56:42 -06:00
|
|
|
</div>
|
2020-04-11 08:44:32 -05:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-04-28 22:26:24 -05:00
|
|
|
<script lang="ts" setup>
|
2022-06-20 03:38:49 -05:00
|
|
|
import { ref, watch } from 'vue';
|
2020-08-21 18:03:11 -05:00
|
|
|
import 'prismjs';
|
|
|
|
import { highlight, languages } from 'prismjs/components/prism-core';
|
|
|
|
import 'prismjs/components/prism-clike';
|
|
|
|
import 'prismjs/components/prism-javascript';
|
2020-04-17 06:36:51 -05:00
|
|
|
import 'prismjs/themes/prism-okaidia.css';
|
2020-08-21 18:03:11 -05:00
|
|
|
import { PrismEditor } from 'vue-prism-editor';
|
|
|
|
import 'vue-prism-editor/dist/prismeditor.min.css';
|
2023-01-03 00:51:49 -06:00
|
|
|
import { Interpreter, Parser, utils } from '@syuilo/aiscript';
|
2022-09-06 04:21:49 -05:00
|
|
|
import MkContainer from '@/components/MkContainer.vue';
|
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2021-11-11 11:02:25 -06:00
|
|
|
import { createAiScriptEnv } from '@/scripts/aiscript/api';
|
|
|
|
import * as os from '@/os';
|
2022-04-28 22:26:24 -05:00
|
|
|
import { $i } from '@/account';
|
|
|
|
import { i18n } from '@/i18n';
|
2022-06-20 03:38:49 -05:00
|
|
|
import { definePageMetadata } from '@/scripts/page-metadata';
|
2020-04-11 08:44:32 -05:00
|
|
|
|
2023-01-03 00:51:49 -06:00
|
|
|
const parser = new Parser();
|
|
|
|
|
2022-04-28 22:26:24 -05:00
|
|
|
const code = ref('');
|
|
|
|
const logs = ref<any[]>([]);
|
2020-04-11 08:44:32 -05:00
|
|
|
|
2022-04-28 22:26:24 -05:00
|
|
|
const saved = localStorage.getItem('scratchpad');
|
|
|
|
if (saved) {
|
|
|
|
code.value = saved;
|
|
|
|
}
|
2020-04-11 08:44:32 -05:00
|
|
|
|
2022-04-28 22:26:24 -05:00
|
|
|
watch(code, () => {
|
|
|
|
localStorage.setItem('scratchpad', code.value);
|
|
|
|
});
|
2020-04-11 08:44:32 -05:00
|
|
|
|
2022-04-28 22:26:24 -05:00
|
|
|
async function run() {
|
|
|
|
logs.value = [];
|
2023-01-03 00:51:49 -06:00
|
|
|
const aiscript = new Interpreter(createAiScriptEnv({
|
2022-04-28 22:26:24 -05:00
|
|
|
storageKey: 'scratchpad',
|
|
|
|
token: $i?.token,
|
|
|
|
}), {
|
|
|
|
in: (q) => {
|
|
|
|
return new Promise(ok => {
|
|
|
|
os.inputText({
|
|
|
|
title: q,
|
|
|
|
}).then(({ canceled, result: a }) => {
|
|
|
|
ok(a);
|
2020-04-11 08:44:32 -05:00
|
|
|
});
|
2022-04-28 22:26:24 -05:00
|
|
|
});
|
2020-08-21 18:03:11 -05:00
|
|
|
},
|
2022-04-28 22:26:24 -05:00
|
|
|
out: (value) => {
|
|
|
|
logs.value.push({
|
|
|
|
id: Math.random(),
|
|
|
|
text: value.type === 'str' ? value.value : utils.valToString(value),
|
2022-06-20 03:38:49 -05:00
|
|
|
print: true,
|
2022-04-28 22:26:24 -05:00
|
|
|
});
|
2020-08-21 18:03:11 -05:00
|
|
|
},
|
2022-04-28 22:26:24 -05:00
|
|
|
log: (type, params) => {
|
|
|
|
switch (type) {
|
|
|
|
case 'end': logs.value.push({
|
|
|
|
id: Math.random(),
|
|
|
|
text: utils.valToString(params.val, true),
|
2022-06-20 03:38:49 -05:00
|
|
|
print: false,
|
2022-04-28 22:26:24 -05:00
|
|
|
}); break;
|
|
|
|
default: break;
|
|
|
|
}
|
2022-06-20 03:38:49 -05:00
|
|
|
},
|
2022-04-28 22:26:24 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
let ast;
|
|
|
|
try {
|
2023-01-03 00:51:49 -06:00
|
|
|
ast = parser.parse(code.value);
|
2022-04-28 22:26:24 -05:00
|
|
|
} catch (error) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 03:38:49 -05:00
|
|
|
text: 'Syntax error :(',
|
2022-04-28 22:26:24 -05:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
|
|
|
} catch (error: any) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 03:38:49 -05:00
|
|
|
text: error.message,
|
2022-04-28 22:26:24 -05:00
|
|
|
});
|
2020-04-11 08:44:32 -05:00
|
|
|
}
|
2022-06-10 00:36:55 -05:00
|
|
|
}
|
2022-04-28 22:26:24 -05:00
|
|
|
|
|
|
|
function highlighter(code) {
|
|
|
|
return highlight(code, languages.js, 'javascript');
|
|
|
|
}
|
|
|
|
|
2022-06-20 03:38:49 -05:00
|
|
|
const headerActions = $computed(() => []);
|
|
|
|
|
|
|
|
const headerTabs = $computed(() => []);
|
|
|
|
|
|
|
|
definePageMetadata({
|
|
|
|
title: i18n.ts.scratchpad,
|
2022-12-19 04:01:30 -06:00
|
|
|
icon: 'ti ti-terminal-2',
|
2020-04-11 08:44:32 -05:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2022-12-27 03:29:39 -06:00
|
|
|
<style lang="scss" scoped>
|
2020-11-29 03:22:35 -06:00
|
|
|
.iltifgqe {
|
2020-12-27 20:56:42 -06:00
|
|
|
padding: 16px;
|
2020-11-29 03:22:35 -06:00
|
|
|
|
2020-12-27 20:56:42 -06:00
|
|
|
> .editor {
|
|
|
|
position: relative;
|
2020-11-29 03:22:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 08:44:32 -05:00
|
|
|
.bepmlvbi {
|
|
|
|
padding: 16px;
|
|
|
|
|
|
|
|
> .log {
|
|
|
|
&:not(.print) {
|
|
|
|
opacity: 0.7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|