Skip to content

Commit 81455ec

Browse files
improve playground
1 parent a045d4a commit 81455ec

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

.vitepress/pages/Playground.vue

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</div>
99
<div :class="$style.playgroundEditorPane">
1010
<div :class="$style.playgroundEditorScroller">
11-
<div :class="$style.playgroundEditorHighlight" v-html="html"></div>
11+
<div :class="[$style.highlight, $style.playgroundEditorHighlight]" v-html="editorHtml"></div>
1212
<textarea
1313
ref="inputEl"
1414
v-model="code"
@@ -34,7 +34,7 @@
3434
<button :class="[$style.playgroundButton, $style.playgroundButtonPrimary]" @click="run">Run</button>
3535
</div>
3636
</div>
37-
<div v-if="resultTab === 'output'" :class="[$style.playgroundResultContent, $style.playgroundResultLogs]">
37+
<div ref="logEl" v-if="resultTab === 'output'" :class="[$style.playgroundResultContent, $style.playgroundResultLogs]">
3838
<div
3939
v-for="log in logs"
4040
:key="log.text"
@@ -48,15 +48,15 @@
4848
>{{ log.text }}</div>
4949
</div>
5050
<div v-else-if="resultTab === 'ast'" :class="$style.playgroundResultContent">
51-
<pre>{{ JSON.stringify(ast, null, 2) }}</pre>
51+
<div :class="$style.highlight" v-html="astHtml"></div>
5252
</div>
5353
</div>
5454
</div>
5555
</template>
5656

5757
<script setup lang="ts">
5858
import { AISCRIPT_VERSION, Parser, Interpreter, utils, errors, type Ast } from '@syuilo/aiscript';
59-
import { ref, useTemplateRef, nextTick, onMounted, watch, computed } from 'vue';
59+
import { ref, useTemplateRef, nextTick, onMounted, watch } from 'vue';
6060
import { createHighlighterCore } from 'shiki/core';
6161
import type { HighlighterCore, LanguageRegistration } from 'shiki/core';
6262
import { createOnigurumaEngine } from 'shiki/engine/oniguruma';
@@ -71,13 +71,14 @@ const code = ref(`for (let i, 100) {
7171
\t\telif (i % 5 == 0) "Buzz"
7272
\t\telse i
7373
}`);
74-
const html = ref('');
74+
const editorHtml = ref('');
7575
7676
let highlighter: HighlighterCore | null = null;
7777
7878
async function init() {
7979
highlighter = await createHighlighterCore({
8080
langs: [
81+
import('shiki/langs/json.mjs'),
8182
import('aiscript-vscode/aiscript/syntaxes/aiscript.tmLanguage.json') as unknown as LanguageRegistration,
8283
],
8384
themes: [
@@ -127,7 +128,7 @@ onMounted(async () => {
127128
128129
watch(code, async (newCode) => {
129130
if (highlighter) {
130-
html.value = highlighter.codeToHtml(newCode, {
131+
editorHtml.value = highlighter.codeToHtml(newCode, {
131132
lang: 'aiscript',
132133
themes: {
133134
light: 'github-light',
@@ -137,6 +138,19 @@ onMounted(async () => {
137138
});
138139
}
139140
}, { immediate: true });
141+
142+
watch(ast, async (newAst) => {
143+
if (highlighter && newAst != null) {
144+
astHtml.value = highlighter.codeToHtml(JSON.stringify(newAst, null, 2), {
145+
lang: 'json',
146+
themes: {
147+
light: 'github-light',
148+
dark: 'github-dark',
149+
},
150+
defaultColor: false,
151+
});
152+
}
153+
});
140154
});
141155
//#endregion
142156
@@ -148,7 +162,10 @@ const logs = ref<{
148162
type?: 'info' | 'error';
149163
text: string;
150164
}[]>([]);
165+
const logEl = useTemplateRef('logEl');
166+
151167
const ast = ref<Ast.Node[] | null>(null);
168+
const astHtml = ref('');
152169
153170
function parse() {
154171
if (parser != null) {
@@ -211,6 +228,11 @@ async function run() {
211228
text: `[Playground] Execution Completed in ${Math.round(endTime - startTime)}ms`,
212229
type: 'info',
213230
});
231+
if (resultTab.value === 'output' && logEl.value != null) {
232+
logEl.value.scrollTo({
233+
top: logEl.value.scrollHeight,
234+
});
235+
}
214236
} catch (err) {
215237
if (err instanceof errors.AiScriptError) {
216238
let errorName = 'AiScriptError';
@@ -256,14 +278,20 @@ function clearLog() {
256278
grid-template-columns: 1fr 1fr;
257279
}
258280
281+
@media (max-width: 768px) {
282+
.playgroundRoot {
283+
grid-template-columns: 1fr;
284+
grid-template-rows: auto 1fr 1fr;
285+
}
286+
}
287+
259288
.playgroundHeader {
260289
grid-column: 1 / -1;
261290
border-bottom: 1px solid var(--vp-c-divider);
262291
}
263292
264293
.playgroundHeaderInner {
265294
margin: 0 auto;
266-
max-width: var(--vp-layout-max-width);
267295
padding: 0.5em 32px;
268296
display: flex;
269297
}
@@ -322,20 +350,20 @@ function clearLog() {
322350
font-family: Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace;
323351
}
324352
325-
.playgroundEditorHighlight pre {
353+
.highlight pre {
326354
margin: 0;
327355
}
328356
329-
.playgroundEditorHighlight span:global(.line) {
357+
.highlight span:global(.line) {
330358
display: inline-block;
331359
min-height: 1em;
332360
}
333361
334-
:global(html.dark) .playgroundEditorHighlight span {
362+
:global(html.dark) .highlight span {
335363
color: var(--shiki-dark, inherit);
336364
}
337365
338-
:global(html:not(.dark)) .playgroundEditorHighlight span {
366+
:global(html:not(.dark)) .highlight span {
339367
color: var(--shiki-light, inherit);
340368
}
341369

0 commit comments

Comments
 (0)