Skip to content

Commit

Permalink
separate editor
Browse files Browse the repository at this point in the history
  • Loading branch information
FineArchs committed Nov 4, 2023
1 parent c449f67 commit 300a5ac
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 120 deletions.
79 changes: 7 additions & 72 deletions src/versions/0.15.0/index.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
<template>
<div id="root">
<div id="grid1">
<Container id='editor'>
<template #header>
Input<div class="actions"><button @click="setCode">FizzBuzz</button></div>
</template>
<PrismEditor class="code" v-model="script" :highlight="highlighter" :line-numbers="false"/>
<template #footer>
<span v-if="syntaxErrorMessage" class="syntaxError">{{ syntaxErrorMessage }}</span>
<div class="actions"><button @click="run">RUN</button></div>
</template>
</Container>
<Editor v-model='script' :samples='samples' :parseError='syntaxErrorMessage' @run='run'/>
<Container id='logs'>
<template #header>Output</template>
<div v-for="log in logs" class="log" :key="log.id" :class="[{ print: log.print }, log.type]"><span class="type">{{ log.type }}</span> {{ log.text }}</div>
Expand All @@ -36,27 +27,10 @@
import { ref, watch } from 'vue';
import { Interpreter, Parser, values, utils, Ast } from 'aiscript0_15_0';
import { PrismEditor } from 'vue-prism-editor';
import 'vue-prism-editor/dist/prismeditor.min.css';
import 'prismjs';
/**
* @types/prismjsの更新が止まっているためとりあえずts-ignoreする(declare moduleしようとしたがうまくいかなかった)
*/
// @ts-ignore
import { highlight, languages } from 'prismjs/components/prism-core';
import 'prismjs/components/prism-clike';
import 'prismjs/components/prism-javascript';
import 'prismjs/themes/prism-okaidia.css';
import Container from '@common/Container.vue';
// 使う場所はそんなにないしとりあえずunknownで
type Grammer = unknown;
declare var languages: {
js: Grammer;
};
declare function highlight(test: string, grammer: Grammer, language: string): string;
const script = ref(window.localStorage.getItem('script') || '<: "Hello, AiScript!"');
const script = ref(window.localStorage.getItem('script') ?? '<: "Hello, AiScript!"');
const ast = ref<Ast.Node[]|null>(null);
const logs = ref<{
Expand All @@ -82,13 +56,14 @@ watch(script, () => {
immediate: true
});
const setCode = () => {
script.value = `for (let i, 100) {
const samples = {
['Hello AiScript']: '<: "Hello, AiScript!"',
FizzBazz: `for (let i, 100) {
<: if (i % 15 == 0) "FizzBuzz"
elif (i % 3 == 0) "Fizz"
elif (i % 5 == 0) "Buzz"
else i
}`;
}`,
};
let interpreter: Interpreter|null = null;
Expand Down Expand Up @@ -130,10 +105,6 @@ const run = async () => {
window.alert(`{e}`);
}
}
const highlighter = (code: string) => {
return highlight(code, languages.js, 'javascript');
};
</script>

<style scoped>
Expand Down Expand Up @@ -174,16 +145,6 @@ pre {
min-height: 0;
}
#editor {
}
#editor > .code {
box-sizing: border-box;
padding: 16px;
}
#editor .syntaxError {
color: #f00;
}
#logs .log .type {
opacity: 0.5;
color: #fff;
Expand All @@ -202,33 +163,7 @@ pre {
}
.container {
display: flex;
flex-direction: column;
border: solid var(--borderThickness) #555;
border-radius: 8px;
background: #202020;
}
.container > header {
display: flex;
padding: 8px 16px;
border-bottom: dashed var(--borderThickness) #555;
font-weight: bold;
}
.container > header > .actions {
margin-left: auto;
}
.container > div {
flex: 1;
overflow: auto;
padding: 16px;
}
.container > footer {
display: flex;
padding: 8px 16px;
border-top: dashed var(--borderThickness) #555;
}
.container > footer > .actions {
.actions {
margin-left: auto;
}
</style>
53 changes: 7 additions & 46 deletions src/versions/0.16.0/index.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
<template>
<div id="root">
<div id="grid1">
<Container id='editor'>
<template #header>
Input<div class="actions"><button @click="setCode">FizzBuzz</button></div>
</template>
<PrismEditor class="code" v-model="script" :highlight="highlighter" :line-numbers="false"/>
<template #footer>
<span v-if="syntaxErrorMessage" class="syntaxError">{{ syntaxErrorMessage }}</span>
<div class="actions"><button @click="run">RUN</button></div>
</template>
</Container>
<Editor v-model='script' :samples='samples' :parseError='syntaxErrorMessage' @run='run'/>
<Container id='logs'>
<template #header>Output</template>
<div v-for="log in logs" class="log" :key="log.id" :class="[{ print: log.print }, log.type]"><span class="type">{{ log.type }}</span> {{ log.text }}</div>
Expand All @@ -35,28 +26,11 @@
<script setup lang='ts'>
import { ref, watch } from 'vue';
import { Interpreter, Parser, utils, Ast } from 'aiscript0_16_0/index.js';
import { PrismEditor } from 'vue-prism-editor';
import 'vue-prism-editor/dist/prismeditor.min.css';
import 'prismjs';
/**
* @types/prismjsの更新が止まっているためとりあえずts-ignoreする(declare moduleしようとしたがうまくいかなかった)
*/
// @ts-ignore
import { highlight, languages } from 'prismjs/components/prism-core';
import 'prismjs/components/prism-clike';
import 'prismjs/components/prism-javascript';
import 'prismjs/themes/prism-okaidia.css';
import Editor from '@common/Editor.vue';
import Container from '@common/Container.vue';
// 使う場所はそんなにないしとりあえずunknownで
type Grammer = unknown;
declare var languages: {
js: Grammer;
};
declare function highlight(test: string, grammer: Grammer, language: string): string;
const script = ref(window.localStorage.getItem('script') || '<: "Hello, AiScript!"');
const script = ref(window.localStorage.getItem('script') ?? '<: "Hello, AiScript!"');
const ast = ref<Ast.Node[]|null>(null);
const logs = ref<{
Expand All @@ -82,13 +56,14 @@ watch(script, () => {
immediate: true
});
const setCode = () => {
script.value = `for (let i, 100) {
const samples = {
['Hello AiScript']: '<: "Hello, AiScript!"',
FizzBazz: `for (let i, 100) {
<: if (i % 15 == 0) "FizzBuzz"
elif (i % 3 == 0) "Fizz"
elif (i % 5 == 0) "Buzz"
else i
}`;
}`,
};
let interpreter: Interpreter|null = null;
Expand Down Expand Up @@ -133,10 +108,6 @@ const run = async () => {
window.alert('Internal Error: ' + e);
}
}
const highlighter = (code: string) => {
return highlight(code, languages.js, 'javascript');
};
</script>

<style scoped>
Expand Down Expand Up @@ -177,16 +148,6 @@ pre {
min-height: 0;
}
#editor {
}
#editor > .code {
box-sizing: border-box;
padding: 16px;
}
#editor .syntaxError {
color: #f00;
}
#logs .log .type {
opacity: 0.5;
color: #fff;
Expand Down
2 changes: 0 additions & 2 deletions src/versions/common/Container.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export default {
background: #202020;
}
.header {
display: flex;
padding: 8px 16px;
border-bottom: dashed var(--borderThickness) #555;
font-weight: bold;
Expand All @@ -45,7 +44,6 @@ export default {
padding: 16px;
}
.footer {
display: flex;
padding: 8px 16px;
border-top: dashed var(--borderThickness) #555;
}
Expand Down
78 changes: 78 additions & 0 deletions src/versions/common/Editor.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<template>
<Container>
<template #header>
Input
<div :class="$style.actions" v-if='props.samples'>
<MenuButton :options='Object.keys(props.samples!)' @select='onSelectSample'/>
</div>
</template>
<PrismEditor :class="$style.code" v-model="modelValueInter" :highlight="highlighter" :line-numbers="false"/>
<template #footer>
<span v-if="props.parseError" :class="$style.parseError">{{ props.parseError }}</span>
<div :class="$style.actions"><button @click="emit('run')">RUN</button></div>
</template>
</Container>
</template>

<script setup lang='ts'>
import { ref, watch } from 'vue';
import { PrismEditor } from 'vue-prism-editor';
import 'vue-prism-editor/dist/prismeditor.min.css';
import 'prismjs';
/**
* @types/prismjsの更新が止まっているためとりあえずts-ignoreする(declare moduleしようとしたがうまくいかなかった)
*/
// @ts-ignore
import { highlight, languages } from 'prismjs/components/prism-core';
import 'prismjs/components/prism-clike';
import 'prismjs/components/prism-javascript';
import 'prismjs/themes/prism-okaidia.css';
import Container from './Container.vue';
import MenuButton from './MenuButton.vue';
// 使う場所はそんなにないしとりあえずunknownで
type Grammer = unknown;
declare var languages: {
js: Grammer;
};
declare function highlight(test: string, grammer: Grammer, language: string): string;
const props = defineProps<{
modelValue: string;
samples?: Record<string, string>;
parseError?: string|null;
}>();
const emit = defineEmits<{
(e: 'run'): void;
(e: 'update:modelValue', value: string): void;
}>();
const modelValueInter = ref<string>(props.modelValue);
watch(modelValueInter, () => {
emit('update:modelValue', modelValueInter.value);
}, {
immediate: true
});
function onSelectSample(chosen: string) {
modelValueInter.value = props.samples![chosen] as string;
}
const highlighter = (code: string) => {
return highlight(code, languages.js, 'javascript');
};
</script>

<style module>
.code {
box-sizing: border-box;
padding: 0;
}
.parseError {
color: #f00;
}
.actions {
margin-left: auto;
float: right;
}
</style>
41 changes: 41 additions & 0 deletions src/versions/common/MenuButton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<template>
<div>
<button @click="showMenu=!showMenu">Samples</button>
<Transition>
<div v-if='showMenu' :class="$style.menu">
<div v-if='options instanceof Array'>
<div v-for='opt in options' :class="$style.opt" @click="showMenu=false; emit('select', opt)">{{ opt }}</div>
</div>
<div v-else>
<div v-for='opt in Object.keys(options)' :class="$style.opt" @click="showMenu=false; emit('select', opt)">{{ options[opt] }}</div>
</div>
</div>
</Transition>
</div>
</template>
<script setup lang='ts'>
import { ref } from 'vue';
const showMenu = ref<boolean>(false);
const props = defineProps<{
options: string[]|Record<string, string>;
}>();
const emit = defineEmits<{
(e: 'select', value: string): void;
}>();
</script>

<style module>
.menu {
position: absolute;
border: solid var(--borderThickness) #555;
border-radius: 4px;
background: #202020;
z-index: 10;
}
.opt {
padding: 16px;
border: dashed var(--borderThickness) #555;
}
</style>

0 comments on commit 300a5ac

Please sign in to comment.