-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
192 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Plugin, resolvePackageData } from 'vite' | ||
|
||
export function createMonacoLoader(): Plugin { | ||
return { | ||
name: 'monaco-types-loader', | ||
|
||
resolveId(id) { | ||
if (id.startsWith('/@monaco-types/')) | ||
return id | ||
return null | ||
}, | ||
|
||
load(id) { | ||
const match = id.match(/^\/\@monaco-types\/(.*)$/) | ||
if (match) { | ||
const pkg = match[1] | ||
const info = resolvePackageData(pkg, process.cwd()) | ||
if (!info) | ||
return | ||
|
||
if (!info.data.types) | ||
return '' | ||
|
||
return [ | ||
'import * as monaco from \'monaco-editor\'', | ||
`import Type from "${info.dir}/${info.data.types}?raw"`, | ||
...Object.keys(info.data.dependencies || {}).map(i => `import "/@monaco-types/${i}"`), | ||
`monaco.languages.typescript.typescriptDefaults.addExtraLib(\`declare module "${pkg}" { \$\{Type\} }\`)`, | ||
].join('\n') | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<template> | ||
<div ref="el" class="monaco-editor text-base" :style="{ height, width }"></div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { ref, onMounted, onUnmounted, defineProps, watch } from 'vue' | ||
import { monaco } from './MonacoEnv' | ||
import { isDark, useNavigateControls } from '~/logic' | ||
const props = defineProps({ | ||
code: { | ||
default: | ||
` | ||
import { ref, computed } from 'vue' | ||
const counter = ref(0) | ||
const doubled = computed(() => counter.value * 2) | ||
`.trim(), | ||
}, | ||
fontSize: { | ||
default: 20, | ||
}, | ||
lang: { | ||
default: 'typescript', | ||
}, | ||
lineNumbers: { | ||
default: 'off', | ||
}, | ||
width: { | ||
default: '800px', | ||
}, | ||
height: { | ||
default: '600px', | ||
}, | ||
}) | ||
const el = ref<HTMLElement>() | ||
const controls = useNavigateControls() | ||
let editor: monaco.editor.IStandaloneCodeEditor | ||
onMounted(() => { | ||
editor = monaco.editor.create(el.value!, { | ||
language: props.lang, | ||
value: props.code, | ||
tabSize: 2, | ||
insertSpaces: true, | ||
detectIndentation: false, | ||
folding: false, | ||
lineDecorationsWidth: 4, | ||
lineNumbersMinChars: 0, | ||
theme: isDark.value ? 'vitesse-dark' : 'vitesse-light', | ||
lineNumbers: props.lineNumbers as any, | ||
fontSize: props.fontSize, | ||
glyphMargin: false, | ||
scrollbar: { | ||
useShadows: false, | ||
vertical: 'hidden', | ||
horizontal: 'hidden', | ||
}, | ||
overviewRulerLanes: 0, | ||
hideCursorInOverviewRuler: true, | ||
minimap: { enabled: false }, | ||
}) | ||
editor.onDidFocusEditorText(() => controls.paused.value = true) | ||
editor.onDidBlurEditorText(() => controls.paused.value = false) | ||
// @ts-expect-error | ||
editor._themeService._theme.getTokenStyleMetadata = (type, modifiers) => { | ||
console.log(type, modifiers) | ||
if (type === 'keyword') { | ||
return { | ||
foreground: 5, // color id 5 | ||
bold: true, | ||
underline: true, | ||
italic: true, | ||
} | ||
} | ||
} | ||
}) | ||
watch(isDark, () => monaco.editor.setTheme(isDark.value ? 'vitesse-dark' : 'vitesse-light')) | ||
onUnmounted(() => { | ||
editor.dispose() | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as monaco from 'monaco-editor' | ||
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker' | ||
import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker' | ||
import CssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker' | ||
import HtmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker' | ||
import TsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker' | ||
|
||
import '/@monaco-types/vue' | ||
import '/@monaco-types/@vueuse/core' | ||
|
||
import dark from 'theme-vitesse/themes/vitesse-dark.json' | ||
import light from 'theme-vitesse/themes/vitesse-light.json' | ||
|
||
monaco.editor.defineTheme('vitesse-light', light as any) | ||
monaco.editor.defineTheme('vitesse-dark', dark as any) | ||
|
||
// @ts-expect-error | ||
self.MonacoEnvironment = { | ||
getWorker(_: any, label: string) { | ||
if (label === 'json') | ||
return new JsonWorker() | ||
if (label === 'css' || label === 'scss' || label === 'less') | ||
return new CssWorker() | ||
if (label === 'html' || label === 'handlebars' || label === 'razor') | ||
return new HtmlWorker() | ||
if (label === 'typescript' || label === 'javascript') | ||
return new TsWorker() | ||
return new EditorWorker() | ||
}, | ||
} | ||
|
||
export { monaco } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters