|
| 1 | +<template> |
| 2 | + <div ref="el" class="monaco-editor text-base" :style="{ height, width }"></div> |
| 3 | +</template> |
| 4 | + |
| 5 | +<script setup lang="ts"> |
| 6 | +import { ref, onMounted, onUnmounted, defineProps, watch } from 'vue' |
| 7 | +import { monaco } from './MonacoEnv' |
| 8 | +import { isDark, useNavigateControls } from '~/logic' |
| 9 | +
|
| 10 | +const props = defineProps({ |
| 11 | + code: { |
| 12 | + default: |
| 13 | +` |
| 14 | +import { ref, computed } from 'vue' |
| 15 | +
|
| 16 | +const counter = ref(0) |
| 17 | +const doubled = computed(() => counter.value * 2) |
| 18 | +`.trim(), |
| 19 | + }, |
| 20 | + fontSize: { |
| 21 | + default: 20, |
| 22 | + }, |
| 23 | + lang: { |
| 24 | + default: 'typescript', |
| 25 | + }, |
| 26 | + lineNumbers: { |
| 27 | + default: 'off', |
| 28 | + }, |
| 29 | + width: { |
| 30 | + default: '800px', |
| 31 | + }, |
| 32 | + height: { |
| 33 | + default: '600px', |
| 34 | + }, |
| 35 | +}) |
| 36 | +
|
| 37 | +const el = ref<HTMLElement>() |
| 38 | +const controls = useNavigateControls() |
| 39 | +let editor: monaco.editor.IStandaloneCodeEditor |
| 40 | +
|
| 41 | +onMounted(() => { |
| 42 | + editor = monaco.editor.create(el.value!, { |
| 43 | + language: props.lang, |
| 44 | + value: props.code, |
| 45 | + tabSize: 2, |
| 46 | + insertSpaces: true, |
| 47 | + detectIndentation: false, |
| 48 | + folding: false, |
| 49 | + lineDecorationsWidth: 4, |
| 50 | + lineNumbersMinChars: 0, |
| 51 | + theme: isDark.value ? 'vitesse-dark' : 'vitesse-light', |
| 52 | + lineNumbers: props.lineNumbers as any, |
| 53 | + fontSize: props.fontSize, |
| 54 | + glyphMargin: false, |
| 55 | + scrollbar: { |
| 56 | + useShadows: false, |
| 57 | + vertical: 'hidden', |
| 58 | + horizontal: 'hidden', |
| 59 | + }, |
| 60 | + overviewRulerLanes: 0, |
| 61 | + hideCursorInOverviewRuler: true, |
| 62 | + minimap: { enabled: false }, |
| 63 | + }) |
| 64 | + editor.onDidFocusEditorText(() => controls.paused.value = true) |
| 65 | + editor.onDidBlurEditorText(() => controls.paused.value = false) |
| 66 | +
|
| 67 | + // @ts-expect-error |
| 68 | + editor._themeService._theme.getTokenStyleMetadata = (type, modifiers) => { |
| 69 | + console.log(type, modifiers) |
| 70 | + if (type === 'keyword') { |
| 71 | + return { |
| 72 | + foreground: 5, // color id 5 |
| 73 | + bold: true, |
| 74 | + underline: true, |
| 75 | + italic: true, |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +}) |
| 80 | +
|
| 81 | +watch(isDark, () => monaco.editor.setTheme(isDark.value ? 'vitesse-dark' : 'vitesse-light')) |
| 82 | +
|
| 83 | +onUnmounted(() => { |
| 84 | + editor.dispose() |
| 85 | +}) |
| 86 | +</script> |
0 commit comments