Skip to content

Commit 8b68a79

Browse files
authored
Routing (#210)
* wip * tinkering * vue hash router
1 parent 51eacb5 commit 8b68a79

File tree

7 files changed

+178
-54
lines changed

7 files changed

+178
-54
lines changed

package-lock.json

Lines changed: 116 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"prettier": "npx prettier . --write"
1010
},
1111
"dependencies": {
12+
"@vueuse/core": "^11.2.0",
1213
"aiscript_develop": "npm:@syuilo/aiscript@dev",
1314
"aiscript_next": "npm:@syuilo/aiscript@next",
1415
"aiscript0_14_1": "npm:@syuilo/[email protected]",
@@ -19,7 +20,8 @@
1920
"aiscript0_19_0": "npm:@syuilo/[email protected]",
2021
"prismjs": "^1.29.0",
2122
"vue": "^3.4.31",
22-
"vue-prism-editor": "^2.0.0-alpha.2"
23+
"vue-prism-editor": "^2.0.0-alpha.2",
24+
"vue-router": "^4.4.5"
2325
},
2426
"devDependencies": {
2527
"@babel/types": "^7.24.7",

src/App.vue

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@
33
<h1>
44
AiScript (
55
<MenuButton id="version" :options="menu" @select="onVersionSelect">{{
6-
version
6+
router.currentRoute.value.params.ver
77
}}</MenuButton>
88
) Playground
99
</h1>
10-
<div v-for="v in versions">
11-
<MainArea :ver="v" v-if="v == version" />
12-
</div>
10+
<RouterView />
1311
</div>
1412
</template>
1513

1614
<script setup lang="ts">
1715
import { ref } from "vue";
18-
import MainArea, { versions, latest } from "./MainArea.vue";
16+
import { useRouter, useRoute } from 'vue-router'
17+
import { versions, latest } from "./MainArea.vue";
1918
import MenuButton from "@common/MenuButton.vue";
2019
21-
const version = ref(window.localStorage.getItem("version") ?? latest);
20+
const router = useRouter();
2221
const menu = Object.fromEntries(
2322
versions.map((v) => [v, v + (v == latest ? "(latest)" : "")]),
2423
);
2524
function onVersionSelect(v: string) {
26-
version.value = v;
27-
window.localStorage.setItem("version", version.value);
25+
window.localStorage.setItem("version", v);
26+
router.push({ path: v });
2827
}
2928
</script>
3029

src/MainArea.vue

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div id="grid1">
44
<Editor
55
v-model="script"
6-
:samples="samples"
6+
:samples="v.samples"
77
:parseError="syntaxErrorMessage"
88
@run="run"
99
/>
@@ -55,7 +55,8 @@ export type Log = {
5555
};
5656
</script>
5757
<script setup lang="ts">
58-
import { ref, watch } from "vue";
58+
import { ref, computed, watch } from "vue";
59+
import { useStorage } from '@vueuse/core';
5960
import Editor from "@common/Editor.vue";
6061
import Container from "@common/Container.vue";
6162
// import * as Next from "@/versions/next/index.ts";
@@ -70,7 +71,8 @@ import * as V0_14_1 from "@/versions/0.14.1/index.ts";
7071
const props = defineProps<{
7172
ver: (typeof versions)[number];
7273
}>();
73-
const { parse, exec, version, samples } = {
74+
75+
const vmodules = {
7476
// next: Next,
7577
develop: Develop,
7678
"0.19.0": V0_19_0,
@@ -79,36 +81,34 @@ const { parse, exec, version, samples } = {
7981
"0.16.0": V0_16_0,
8082
"0.15.0": V0_15_0,
8183
"0.14.1": V0_14_1,
82-
}[props.ver];
83-
84-
const script = ref(
85-
window.localStorage.getItem(version) ?? '<: "Hello, AiScript!"',
86-
);
84+
} as const;
85+
const v = computed(() => vmodules[props.ver]);
8786
87+
const vscripts = Object.fromEntries(versions.map(
88+
(_v) => [_v, useStorage<string>(_v, '<: "Hello, AiScript!"')]
89+
));
90+
const script = computed({
91+
get(): string { return vscripts[props.ver]!.value },
92+
set(newVal: string): void { vscripts[props.ver]!.value = newVal },
93+
});
8894
const logs = ref<Log[]>([]);
89-
const ast = ref<string>("");
9095
const syntaxErrorMessage = ref<string | null>(null);
91-
92-
watch(
93-
script,
94-
() => {
95-
window.localStorage.setItem(version, script.value);
96-
try {
97-
ast.value = parse(script.value);
98-
syntaxErrorMessage.value = null;
99-
} catch (e) {
100-
const err = e as Error;
101-
syntaxErrorMessage.value = err.message;
102-
console.error("info" in err ? err.info : err);
103-
return;
104-
}
105-
},
106-
{ immediate: true },
107-
);
96+
const ast = computed<string>(() => {
97+
try {
98+
const _ast = v.value.parse(script.value);
99+
syntaxErrorMessage.value = null;
100+
return _ast;
101+
} catch (e) {
102+
const err = e as Error;
103+
syntaxErrorMessage.value = err.message;
104+
console.error("info" in err ? err.info : err);
105+
return `${err}`;
106+
}
107+
});
108108
109109
function run() {
110110
logs.value = [];
111-
exec({
111+
v.value.exec({
112112
in: (q: string) =>
113113
new Promise((ok) => {
114114
const res = window.prompt(q);

src/common/Editor.vue

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</template>
1313
<PrismEditor
1414
:class="$style.code"
15-
v-model="modelValueInter"
15+
v-model="code"
1616
:highlight="highlighter"
1717
:line-numbers="false"
1818
/>
@@ -28,7 +28,7 @@
2828
</template>
2929

3030
<script setup lang="ts">
31-
import { ref, watch } from "vue";
31+
import { ref, computed, watch } from "vue";
3232
import { PrismEditor } from "vue-prism-editor";
3333
import "vue-prism-editor/dist/prismeditor.min.css";
3434
import "prismjs";
@@ -55,28 +55,16 @@ declare function highlight(
5555
): string;
5656
5757
const props = defineProps<{
58-
modelValue: string;
5958
samples?: Record<string, string>;
6059
parseError?: string | null;
6160
}>();
6261
const emit = defineEmits<{
6362
(e: "run"): void;
64-
(e: "update:modelValue", value: string): void;
6563
}>();
66-
67-
const modelValueInter = ref<string>(props.modelValue);
68-
watch(
69-
modelValueInter,
70-
() => {
71-
emit("update:modelValue", modelValueInter.value);
72-
},
73-
{
74-
immediate: true,
75-
},
76-
);
64+
const code = defineModel<string>();
7765
7866
function onSelectSample(chosen: string) {
79-
modelValueInter.value = props.samples![chosen] as string;
67+
code.value = props.samples![chosen] as string;
8068
}
8169
8270
const highlighter = (code: string) => {

src/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { createApp } from "vue";
22
import App from "./App.vue";
3+
import { router } from "./router.js";
34

4-
createApp(App).mount("#app");
5+
const app = createApp(App);
6+
app.use(router);
7+
app.mount("#app");

src/router.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { createRouter, createWebHashHistory } from 'vue-router';
2+
import MainArea, { latest } from "./MainArea.vue";
3+
4+
export const router = createRouter({
5+
history: createWebHashHistory(),
6+
routes: [
7+
{
8+
path: '/',
9+
redirect: () => window.localStorage.getItem("version") ?? latest,
10+
},
11+
{
12+
path: '/:ver',
13+
component: MainArea,
14+
props: true,
15+
},
16+
],
17+
});

0 commit comments

Comments
 (0)