Skip to content

Commit e266b78

Browse files
committed
refactor: javascript engine of shiki
1 parent 21c61d0 commit e266b78

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

src/monaco/Monaco.vue

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
import {
33
computed,
44
inject,
5-
nextTick,
65
onBeforeUnmount,
76
onMounted,
87
onWatcherCleanup,
9-
ref,
108
shallowRef,
119
useTemplateRef,
1210
watch,
@@ -15,6 +13,7 @@ import * as monaco from 'monaco-editor-core'
1513
import { initMonaco } from './env'
1614
import { getOrCreateModel } from './utils'
1715
import { type EditorMode, injectKeyProps } from '../types'
16+
import { registerHighlighter } from './highlight'
1817
1918
const props = withDefaults(
2019
defineProps<{
@@ -35,7 +34,6 @@ const emit = defineEmits<{
3534
}>()
3635
3736
const containerRef = useTemplateRef('container')
38-
const ready = ref(false)
3937
const editor = shallowRef<monaco.editor.IStandaloneCodeEditor>()
4038
const {
4139
store,
@@ -53,11 +51,8 @@ function emitChangeEvent() {
5351
emit('change', editorInstance.getValue())
5452
}
5553
56-
onMounted(async () => {
57-
const theme = await import('./highlight').then((r) => r.registerHighlighter())
58-
ready.value = true
59-
await nextTick()
60-
54+
onMounted(() => {
55+
const theme = registerHighlighter()
6156
if (!containerRef.value) {
6257
throw new Error('Cannot find containerRef')
6358
}

src/monaco/highlight.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import * as monaco from 'monaco-editor-core'
2-
import { createHighlighterCore } from 'shiki/core'
2+
import { createHighlighterCoreSync } from 'shiki/core'
3+
import { createJavaScriptRegexEngine } from 'shiki/engine-javascript.mjs'
34
import { shikiToMonaco } from '@shikijs/monaco'
45

56
import langVue from 'shiki/langs/vue.mjs'
7+
import langTsx from 'shiki/langs/tsx.mjs'
8+
import langJsx from 'shiki/langs/jsx.mjs'
69
import themeDark from 'shiki/themes/dark-plus.mjs'
710
import themeLight from 'shiki/themes/light-plus.mjs'
811

9-
export async function registerHighlighter() {
10-
const highlighter = await createHighlighterCore({
11-
themes: [themeDark, themeLight],
12-
langs: [langVue],
13-
loadWasm: import('shiki/wasm'),
14-
})
15-
monaco.languages.register({ id: 'vue' })
16-
shikiToMonaco(highlighter, monaco)
12+
let registered = false
13+
export function registerHighlighter() {
14+
if (!registered) {
15+
const highlighter = createHighlighterCoreSync({
16+
themes: [themeDark, themeLight],
17+
langs: [langVue, langTsx, langJsx],
18+
engine: createJavaScriptRegexEngine(),
19+
})
20+
monaco.languages.register({ id: 'vue' })
21+
shikiToMonaco(highlighter, monaco)
22+
registered = true
23+
}
24+
1725
return {
1826
light: themeLight.name!,
1927
dark: themeDark.name!,

0 commit comments

Comments
 (0)