Skip to content

Commit ada510e

Browse files
committed
📘feat: use shiki in editor
1 parent 6b69c7e commit ada510e

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

‎components/midori/editor.vue

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
<script setup lang="ts">
2-
import { onMounted, ref } from 'vue'
3-
import Prism from 'vue-prism-component'
4-
import PrismJS from 'prismjs';
5-
const { highlight, languages } = PrismJS
6-
72
import { Elysia } from 'elysia'
3+
import { onMounted, ref, watch } from 'vue'
4+
import { getHighlighterCore } from 'shiki/core'
5+
import getWasm from 'shiki/wasm'
6+
import javascript from 'shiki/langs/javascript.mjs'
7+
import githubDark from 'shiki/themes/github-dark.mjs'
8+
import githubLight from 'shiki/themes/github-light.mjs'
9+
import useDark from './use-dark'
10+
11+
const isDark = useDark()
12+
13+
const highlighter = await getHighlighterCore({
14+
themes: [githubDark, githubLight],
15+
langs: [javascript],
16+
loadWasm: getWasm
17+
})
18+
819
9-
let code = `const app = new Elysia()
10-
.get('/', () => 'Hello!')
20+
let code = /* js */ `const app = new Elysia()
21+
.get('/', () => ({ some: "json" }))
1122
// Try edit this line
1223
.get('/hello', () => 'Hello from Elysia!')
1324
.listen(80)
@@ -73,7 +84,7 @@ let url = ref('/hello')
7384
let response = ref('Hello from Elysia!')
7485
7586
let instance = new Elysia()
76-
.get('/', () => 'Hello!')
87+
.get('/', () => ({ some: "json" }))
7788
.get('/hello', () => 'Hello from Elysia!') as Elysia<any, any>
7889
7990
let editorError = ref(undefined as Error | undefined)
@@ -97,22 +108,37 @@ const execute = async () => {
97108
response.value = x.headers
98109
.get('Content-Type')
99110
.includes('application/json')
100-
? JSON.stringify(await x.json())
111+
? JSON.stringify(await x.json(), null, 4)
101112
: await x.text()
102113
})
103114
}
104115
116+
watch(isDark, (isDark) => {
117+
const editor = document.querySelector<HTMLElement>('pre.elysia-editor');
118+
119+
editor.innerHTML = highlighter.codeToHtml(
120+
editor.innerText, {
121+
theme: isDark ? 'github-dark' : 'github-light',
122+
lang: 'javascript',
123+
})
124+
})
125+
105126
onMounted(() => {
106127
const editor = document.querySelector<HTMLElement>('pre.elysia-editor')
107128
129+
editor.innerHTML = highlighter.codeToHtml(code, {
130+
theme: isDark ? 'github-dark' : 'github-light',
131+
lang: 'javascript',
132+
})
133+
108134
function rehighlight(event) {
109135
const restore = saveCaretPosition(this)
110136
111-
editor.innerHTML = highlight(
112-
event.currentTarget.innerText,
113-
languages.typescript,
114-
'typescript'
115-
)
137+
editor.innerHTML = highlighter.codeToHtml(
138+
event.currentTarget.innerText, {
139+
theme: isDark ? 'github-dark' : 'github-light',
140+
lang: 'javascript',
141+
})
116142
117143
restore()
118144
}
@@ -170,11 +196,9 @@ onMounted(() => {
170196
<aside class="flex flex-col md:flex-row justify-center items-center w-full max-w-6xl gap-8 my-8">
171197
<section class="flex flex-col w-full h-96 border dark:border-slate-700 bg-white dark:bg-slate-800 rounded-2xl">
172198
<div class="mockup-window flex relative w-full h-full shadow-xl">
173-
<Prism
174-
class="elysia-editor block !bg-transparent !text-base !font-mono rounded-xl w-full max-w-xl h-full !pt-0 !px-2 outline-none"
175-
language="typescript" contenteditable="true">
176-
{{ code }}
177-
</Prism>
199+
<pre class="elysia-editor block !bg-transparent !text-base !font-mono rounded-xl w-full max-w-xl h-full !pt-0 !px-2 outline-none"
200+
contenteditable="true">
201+
</pre>
178202

179203
<footer v-if="editorError"
180204
class="absolute bottom-0 flex flex-col w-full max-h-40 overflow-y-auto text-white font-medium px-4 py-2 bg-red-500">
@@ -202,3 +226,14 @@ onMounted(() => {
202226
</aside>
203227
</article>
204228
</template>
229+
230+
<style>
231+
.elysia-editor>pre {
232+
padding: 0 10px;
233+
background-color: rgba(0, 0, 0, 0) !important;
234+
}
235+
236+
.elysia-editor>pre:focus-visible {
237+
outline: none;
238+
}
239+
</style>

0 commit comments

Comments
 (0)