2020-04-11 22:44:32 +09:00
|
|
|
<template>
|
2020-12-28 11:56:42 +09:00
|
|
|
<div class="iltifgqe">
|
2021-04-10 18:17:42 +09:00
|
|
|
<div class="editor _panel _gap">
|
2020-11-29 18:22:35 +09:00
|
|
|
<PrismEditor class="_code code" v-model="code" :highlight="highlighter" :line-numbers="false"/>
|
2021-04-20 23:22:59 +09:00
|
|
|
<MkButton style="position: absolute; top: 8px; right: 8px;" @click="run()" primary><i class="fas fa-play"></i></MkButton>
|
2020-04-11 22:44:32 +09:00
|
|
|
</div>
|
|
|
|
|
2021-04-16 12:17:22 +09:00
|
|
|
<MkContainer :foldable="true" class="_gap">
|
2021-04-20 23:22:59 +09:00
|
|
|
<template #header>{{ $ts.output }}</template>
|
2020-04-11 22:44:32 +09:00
|
|
|
<div class="bepmlvbi">
|
|
|
|
<div v-for="log in logs" class="log" :key="log.id" :class="{ print: log.print }">{{ log.text }}</div>
|
|
|
|
</div>
|
2020-10-17 20:12:00 +09:00
|
|
|
</MkContainer>
|
2020-04-11 22:44:32 +09:00
|
|
|
|
2021-04-10 18:17:42 +09:00
|
|
|
<div class="_gap">
|
2020-12-28 11:56:42 +09:00
|
|
|
{{ $ts.scratchpadDescription }}
|
|
|
|
</div>
|
2020-04-11 22:44:32 +09:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2020-10-17 20:12:00 +09:00
|
|
|
import { defineComponent } from 'vue';
|
2020-08-22 08:03:11 +09:00
|
|
|
import 'prismjs';
|
|
|
|
import { highlight, languages } from 'prismjs/components/prism-core';
|
|
|
|
import 'prismjs/components/prism-clike';
|
|
|
|
import 'prismjs/components/prism-javascript';
|
2020-04-17 20:36:51 +09:00
|
|
|
import 'prismjs/themes/prism-okaidia.css';
|
2020-08-22 08:03:11 +09:00
|
|
|
import { PrismEditor } from 'vue-prism-editor';
|
|
|
|
import 'vue-prism-editor/dist/prismeditor.min.css';
|
2020-04-11 22:44:32 +09:00
|
|
|
import { AiScript, parse, utils, values } from '@syuilo/aiscript';
|
2021-11-12 02:02:25 +09:00
|
|
|
import MkContainer from '@/components/ui/container.vue';
|
|
|
|
import MkButton from '@/components/ui/button.vue';
|
|
|
|
import { createAiScriptEnv } from '@/scripts/aiscript/api';
|
|
|
|
import * as os from '@/os';
|
|
|
|
import * as symbols from '@/symbols';
|
2020-04-11 22:44:32 +09:00
|
|
|
|
2020-10-17 20:12:00 +09:00
|
|
|
export default defineComponent({
|
2020-04-11 22:44:32 +09:00
|
|
|
components: {
|
|
|
|
MkContainer,
|
|
|
|
MkButton,
|
|
|
|
PrismEditor,
|
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
2021-04-10 12:54:12 +09:00
|
|
|
[symbols.PAGE_INFO]: {
|
2020-12-26 10:47:36 +09:00
|
|
|
title: this.$ts.scratchpad,
|
2021-04-20 23:22:59 +09:00
|
|
|
icon: 'fas fa-terminal',
|
2020-10-17 20:12:00 +09:00
|
|
|
},
|
2020-04-11 22:44:32 +09:00
|
|
|
code: '',
|
|
|
|
logs: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
code() {
|
|
|
|
localStorage.setItem('scratchpad', this.code);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
|
|
|
const saved = localStorage.getItem('scratchpad');
|
|
|
|
if (saved) {
|
|
|
|
this.code = saved;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
async run() {
|
|
|
|
this.logs = [];
|
2020-10-17 20:12:00 +09:00
|
|
|
const aiscript = new AiScript(createAiScriptEnv({
|
2021-02-11 16:16:04 +09:00
|
|
|
storageKey: 'scratchpad',
|
|
|
|
token: this.$i?.token,
|
2020-04-12 19:57:18 +09:00
|
|
|
}), {
|
2020-04-11 22:44:32 +09:00
|
|
|
in: (q) => {
|
|
|
|
return new Promise(ok => {
|
2021-11-18 18:45:58 +09:00
|
|
|
os.inputText({
|
2020-04-11 22:44:32 +09:00
|
|
|
title: q,
|
|
|
|
}).then(({ canceled, result: a }) => {
|
|
|
|
ok(a);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
out: (value) => {
|
|
|
|
this.logs.push({
|
|
|
|
id: Math.random(),
|
|
|
|
text: value.type === 'str' ? value.value : utils.valToString(value),
|
|
|
|
print: true
|
|
|
|
});
|
|
|
|
},
|
|
|
|
log: (type, params) => {
|
|
|
|
switch (type) {
|
|
|
|
case 'end': this.logs.push({
|
|
|
|
id: Math.random(),
|
|
|
|
text: utils.valToString(params.val, true),
|
|
|
|
print: false
|
|
|
|
}); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let ast;
|
|
|
|
try {
|
|
|
|
ast = parse(this.code);
|
|
|
|
} catch (e) {
|
2021-11-18 18:45:58 +09:00
|
|
|
os.alert({
|
2020-04-11 22:44:32 +09:00
|
|
|
type: 'error',
|
|
|
|
text: 'Syntax error :('
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
|
|
|
} catch (e) {
|
2021-11-18 18:45:58 +09:00
|
|
|
os.alert({
|
2020-04-11 22:44:32 +09:00
|
|
|
type: 'error',
|
|
|
|
text: e
|
|
|
|
});
|
|
|
|
}
|
2020-08-22 08:03:11 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
highlighter(code) {
|
|
|
|
return highlight(code, languages.js, 'javascript');
|
|
|
|
},
|
2020-04-11 22:44:32 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2020-11-29 18:22:35 +09:00
|
|
|
.iltifgqe {
|
2020-12-28 11:56:42 +09:00
|
|
|
padding: 16px;
|
2020-11-29 18:22:35 +09:00
|
|
|
|
2020-12-28 11:56:42 +09:00
|
|
|
> .editor {
|
|
|
|
position: relative;
|
2020-11-29 18:22:35 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 22:44:32 +09:00
|
|
|
.bepmlvbi {
|
|
|
|
padding: 16px;
|
|
|
|
|
|
|
|
> .log {
|
|
|
|
&:not(.print) {
|
|
|
|
opacity: 0.7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|