2020-04-11 08:44:32 -05:00
|
|
|
<template>
|
|
|
|
<div class="">
|
|
|
|
<portal to="icon"><fa :icon="faTerminal"/></portal>
|
2020-07-24 11:56:52 -05:00
|
|
|
<portal to="title">{{ $t('scratchpad') }}</portal>
|
2020-04-11 08:44:32 -05:00
|
|
|
|
|
|
|
<div class="_panel">
|
2020-08-21 18:03:11 -05:00
|
|
|
<prism-editor class="_code" v-model="code" :highlight="highlighter" :line-numbers="false"/>
|
2020-04-11 08:44:32 -05:00
|
|
|
<mk-button style="position: absolute; top: 8px; right: 8px;" @click="run()" primary><fa :icon="faPlay"/></mk-button>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<mk-container :body-togglable="true">
|
|
|
|
<template #header><fa fixed-width/>{{ $t('output') }}</template>
|
|
|
|
<div class="bepmlvbi">
|
|
|
|
<div v-for="log in logs" class="log" :key="log.id" :class="{ print: log.print }">{{ log.text }}</div>
|
|
|
|
</div>
|
|
|
|
</mk-container>
|
|
|
|
|
|
|
|
<section class="_card" style="margin-top: var(--margin);">
|
2020-07-24 11:56:52 -05:00
|
|
|
<div class="_content">{{ $t('scratchpadDescription') }}</div>
|
2020-04-11 08:44:32 -05:00
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import Vue from 'vue';
|
2020-04-12 13:23:23 -05:00
|
|
|
import { faTerminal, faPlay } from '@fortawesome/free-solid-svg-icons';
|
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';
|
2020-04-11 08:44:32 -05:00
|
|
|
import { AiScript, parse, utils, values } from '@syuilo/aiscript';
|
|
|
|
import MkContainer from '../components/ui/container.vue';
|
|
|
|
import MkButton from '../components/ui/button.vue';
|
2020-07-11 10:38:55 -05:00
|
|
|
import { createAiScriptEnv } from '../scripts/aiscript/api';
|
2020-04-11 08:44:32 -05:00
|
|
|
|
|
|
|
export default Vue.extend({
|
|
|
|
metaInfo() {
|
|
|
|
return {
|
|
|
|
title: this.$t('scratchpad') as string
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
components: {
|
|
|
|
MkContainer,
|
|
|
|
MkButton,
|
|
|
|
PrismEditor,
|
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
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-04-12 05:57:18 -05:00
|
|
|
const aiscript = new AiScript(createAiScriptEnv(this, {
|
|
|
|
storageKey: 'scratchpad'
|
|
|
|
}), {
|
2020-04-11 08:44:32 -05:00
|
|
|
in: (q) => {
|
|
|
|
return new Promise(ok => {
|
|
|
|
this.$root.dialog({
|
|
|
|
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) {
|
|
|
|
this.$root.dialog({
|
|
|
|
type: 'error',
|
|
|
|
text: 'Syntax error :('
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await aiscript.exec(ast);
|
|
|
|
} catch (e) {
|
|
|
|
this.$root.dialog({
|
|
|
|
type: 'error',
|
|
|
|
text: e
|
|
|
|
});
|
|
|
|
}
|
2020-08-21 18:03:11 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
highlighter(code) {
|
|
|
|
return highlight(code, languages.js, 'javascript');
|
|
|
|
},
|
2020-04-11 08:44:32 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.bepmlvbi {
|
|
|
|
padding: 16px;
|
|
|
|
|
|
|
|
> .log {
|
|
|
|
&:not(.print) {
|
|
|
|
opacity: 0.7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|