refactor(client): refactor scratchpad to use Composition API (#8565)

This commit is contained in:
Andreas Nedbal 2022-04-29 05:26:24 +02:00 committed by GitHub
parent 8f32064fea
commit 5ad42d1d85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,20 +6,20 @@
</div> </div>
<MkContainer :foldable="true" class="_gap"> <MkContainer :foldable="true" class="_gap">
<template #header>{{ $ts.output }}</template> <template #header>{{ i18n.ts.output }}</template>
<div class="bepmlvbi"> <div class="bepmlvbi">
<div v-for="log in logs" :key="log.id" class="log" :class="{ print: log.print }">{{ log.text }}</div> <div v-for="log in logs" :key="log.id" class="log" :class="{ print: log.print }">{{ log.text }}</div>
</div> </div>
</MkContainer> </MkContainer>
<div class="_gap"> <div class="_gap">
{{ $ts.scratchpadDescription }} {{ i18n.ts.scratchpadDescription }}
</div> </div>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from 'vue'; import { defineExpose, ref, watch } from 'vue';
import 'prismjs'; import 'prismjs';
import { highlight, languages } from 'prismjs/components/prism-core'; import { highlight, languages } from 'prismjs/components/prism-core';
import 'prismjs/components/prism-clike'; import 'prismjs/components/prism-clike';
@ -27,103 +27,90 @@ import 'prismjs/components/prism-javascript';
import 'prismjs/themes/prism-okaidia.css'; import 'prismjs/themes/prism-okaidia.css';
import { PrismEditor } from 'vue-prism-editor'; import { PrismEditor } from 'vue-prism-editor';
import 'vue-prism-editor/dist/prismeditor.min.css'; import 'vue-prism-editor/dist/prismeditor.min.css';
import { AiScript, parse, utils, values } from '@syuilo/aiscript'; import { AiScript, parse, utils } from '@syuilo/aiscript';
import MkContainer from '@/components/ui/container.vue'; import MkContainer from '@/components/ui/container.vue';
import MkButton from '@/components/ui/button.vue'; import MkButton from '@/components/ui/button.vue';
import { createAiScriptEnv } from '@/scripts/aiscript/api'; import { createAiScriptEnv } from '@/scripts/aiscript/api';
import * as os from '@/os'; import * as os from '@/os';
import * as symbols from '@/symbols'; import * as symbols from '@/symbols';
import { $i } from '@/account';
import { i18n } from '@/i18n';
export default defineComponent({ const code = ref('');
components: { const logs = ref<any[]>([]);
MkContainer,
MkButton,
PrismEditor,
},
data() { const saved = localStorage.getItem('scratchpad');
return { if (saved) {
[symbols.PAGE_INFO]: { code.value = saved;
title: this.$ts.scratchpad, }
icon: 'fas fa-terminal',
},
code: '',
logs: [],
}
},
watch: { watch(code, () => {
code() { localStorage.setItem('scratchpad', code.value);
localStorage.setItem('scratchpad', this.code); });
}
},
created() { async function run() {
const saved = localStorage.getItem('scratchpad'); logs.value = [];
if (saved) { const aiscript = new AiScript(createAiScriptEnv({
this.code = saved; storageKey: 'scratchpad',
} token: $i?.token,
}, }), {
in: (q) => {
methods: { return new Promise(ok => {
async run() { os.inputText({
this.logs = []; title: q,
const aiscript = new AiScript(createAiScriptEnv({ }).then(({ canceled, result: a }) => {
storageKey: 'scratchpad', ok(a);
token: this.$i?.token, });
}), {
in: (q) => {
return new Promise(ok => {
os.inputText({
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) {
os.alert({
type: 'error',
text: 'Syntax error :('
});
return;
}
try {
await aiscript.exec(ast);
} catch (e) {
os.alert({
type: 'error',
text: e
});
}
}, },
out: (value) => {
highlighter(code) { logs.value.push({
return highlight(code, languages.js, 'javascript'); id: Math.random(),
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;
}
}
});
let ast;
try {
ast = parse(code.value);
} catch (error) {
os.alert({
type: 'error',
text: 'Syntax error :('
});
return;
} }
try {
await aiscript.exec(ast);
} catch (error: any) {
os.alert({
type: 'error',
text: error.message
});
}
};
function highlighter(code) {
return highlight(code, languages.js, 'javascript');
}
defineExpose({
[symbols.PAGE_INFO]: {
title: i18n.ts.scratchpad,
icon: 'fas fa-terminal',
},
}); });
</script> </script>