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