2020-04-11 13:44:32 +00:00
|
|
|
<template>
|
|
|
|
<div class="">
|
|
|
|
<div class="_panel">
|
2020-10-17 11:12:00 +00:00
|
|
|
<prism-editor class="_code" v-model:value="code" :highlight="highlighter" :line-numbers="false"/>
|
|
|
|
<MkButton style="position: absolute; top: 8px; right: 8px;" @click="run()" primary><Fa :icon="faPlay"/></MkButton>
|
2020-04-11 13:44:32 +00:00
|
|
|
</div>
|
|
|
|
|
2020-10-17 11:12:00 +00:00
|
|
|
<MkContainer :body-togglable="true">
|
|
|
|
<template #header><Fa fixed-width/>{{ $t('output') }}</template>
|
2020-04-11 13:44:32 +00: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 11:12:00 +00:00
|
|
|
</MkContainer>
|
2020-04-11 13:44:32 +00:00
|
|
|
|
2020-10-17 11:12:00 +00:00
|
|
|
<section class="_section" style="margin-top: var(--margin);">
|
2020-07-24 16:56:52 +00:00
|
|
|
<div class="_content">{{ $t('scratchpadDescription') }}</div>
|
2020-04-11 13:44:32 +00:00
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2020-10-17 11:12:00 +00:00
|
|
|
import { defineComponent } from 'vue';
|
2020-04-12 18:23:23 +00:00
|
|
|
import { faTerminal, faPlay } from '@fortawesome/free-solid-svg-icons';
|
2020-08-21 23:03:11 +00:00
|
|
|
import 'prismjs';
|
|
|
|
import { highlight, languages } from 'prismjs/components/prism-core';
|
|
|
|
import 'prismjs/components/prism-clike';
|
|
|
|
import 'prismjs/components/prism-javascript';
|
2020-04-17 11:36:51 +00:00
|
|
|
import 'prismjs/themes/prism-okaidia.css';
|
2020-08-21 23:03:11 +00:00
|
|
|
import { PrismEditor } from 'vue-prism-editor';
|
|
|
|
import 'vue-prism-editor/dist/prismeditor.min.css';
|
2020-04-11 13:44:32 +00:00
|
|
|
import { AiScript, parse, utils, values } from '@syuilo/aiscript';
|
2020-10-17 11:12:00 +00: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';
|
2020-04-11 13:44:32 +00:00
|
|
|
|
2020-10-17 11:12:00 +00:00
|
|
|
export default defineComponent({
|
2020-04-11 13:44:32 +00:00
|
|
|
components: {
|
|
|
|
MkContainer,
|
|
|
|
MkButton,
|
|
|
|
PrismEditor,
|
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
2020-10-17 11:12:00 +00:00
|
|
|
INFO: {
|
2020-11-03 11:36:12 +00:00
|
|
|
title: this.$t('scratchpad'),
|
|
|
|
icon: faTerminal,
|
2020-10-17 11:12:00 +00:00
|
|
|
},
|
2020-04-11 13:44:32 +00:00
|
|
|
code: '',
|
|
|
|
logs: [],
|
|
|
|
faTerminal, faPlay
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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 11:12:00 +00:00
|
|
|
const aiscript = new AiScript(createAiScriptEnv({
|
2020-04-12 10:57:18 +00:00
|
|
|
storageKey: 'scratchpad'
|
|
|
|
}), {
|
2020-04-11 13:44:32 +00:00
|
|
|
in: (q) => {
|
|
|
|
return new Promise(ok => {
|
2020-10-17 11:12:00 +00:00
|
|
|
os.dialog({
|
2020-04-11 13:44:32 +00:00
|
|
|
title: q,
|
|
|
|
input: {}
|
|
|
|
}).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) {
|
2020-10-17 11:12:00 +00:00
|
|
|
os.dialog({
|
2020-04-11 13:44:32 +00:00
|
|
|
type: 'error',
|
|
|
|
text: 'Syntax error :('
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
|
|
|
} catch (e) {
|
2020-10-17 11:12:00 +00:00
|
|
|
os.dialog({
|
2020-04-11 13:44:32 +00:00
|
|
|
type: 'error',
|
|
|
|
text: e
|
|
|
|
});
|
|
|
|
}
|
2020-08-21 23:03:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
highlighter(code) {
|
|
|
|
return highlight(code, languages.js, 'javascript');
|
|
|
|
},
|
2020-04-11 13:44:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.bepmlvbi {
|
|
|
|
padding: 16px;
|
|
|
|
|
|
|
|
> .log {
|
|
|
|
&:not(.print) {
|
|
|
|
opacity: 0.7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|