Skip to content

Commit 3c16978

Browse files
committed
#1 reads content from external file.
It will load the file and then listen for changes on that same path. This is helpful because amoxtli-vue will update the file at that same location from the server.js in the template
1 parent 6e88447 commit 3c16978

File tree

7 files changed

+20
-19
lines changed

7 files changed

+20
-19
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"build-only": "vite build",
1414
"type-check": "vue-tsc --build",
1515
"lint": "eslint . --fix",
16-
"format": "prettier --write src/"
16+
"format": "prettier --write src/",
17+
"install-in-amoxtli-vue": "cp -r dist/assets ../amoxtli-vue/src/templates/yehyecoa/ && cp dist/index.html ../amoxtli-vue/src/templates/yehyecoa/",
18+
"bi": "npm run build && npm run install-in-amoxtli-vue"
1719
},
1820
"dependencies": {
1921
"@vue/repl": "^4.6.3",

src/App.vue

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Repl, useStore } from '@vue/repl'
33
import Monaco from '@vue/repl/monaco-editor'
44
import '@vue/repl/style.css'
5-
import { ref } from 'vue'
5+
import { onMounted, ref } from 'vue'
66
import ReplHeader from './ReplHeader.vue'
77
import { useStorage } from '@vueuse/core'
88
@@ -21,22 +21,21 @@ window.addEventListener('resize', setVH)
2121
setVH()
2222
2323
const store = useStore()
24-
// TODO: read the contents from an external file
25-
// so it is easier to have multiple examples
26-
// within the lessons
27-
const files = {
28-
'src/App.vue': `<script setup lang="ts">
29-
import { ref } from 'vue'
3024
31-
const msg = ref('Hola Mundo!')
32-
<\/script>
25+
onMounted(async () => {
26+
// Initial load
27+
const fileContents = await fetch('/lessonFile.vue').then((r) => r.text())
28+
store.setFiles({ 'src/App.vue': fileContents })
3329
34-
<template>
35-
<h1>{{ msg }}</h1>
36-
<input v-model="msg" />
37-
</template>`,
38-
}
39-
store.setFiles(files)
30+
// Live updates via SSE
31+
const es = new EventSource('/events')
32+
es.addEventListener('fileUpdate', (event) => {
33+
const { filename, content } = JSON.parse(event.data)
34+
if (filename === 'lessonFile.vue') {
35+
store.setFiles({ 'src/App.vue': content })
36+
}
37+
})
38+
})
4039
</script>
4140

4241
<template>

src/ReplHeader.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import { onMounted } from 'vue'
3-
import Sun from '@/icons/Sun.vue'
4-
import Moon from '@/icons/Moon.vue'
3+
import Sun from '@/icons/SunIcon.vue'
4+
import Moon from '@/icons/MoonIcon.vue'
55
66
defineProps<{
77
theme: 'dark' | 'light'
File renamed without changes.
File renamed without changes.
File renamed without changes.

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { fileURLToPath, URL } from 'node:url'
22

3-
import { defineConfig } from 'vite'
43
import vue from '@vitejs/plugin-vue'
4+
import { defineConfig } from 'vite'
55
import vueDevTools from 'vite-plugin-vue-devtools'
66

77
// https://vite.dev/config/

0 commit comments

Comments
 (0)