Skip to content

Commit

Permalink
fix(editor): pass language mode to editors
Browse files Browse the repository at this point in the history
  • Loading branch information
pdanpdan committed Dec 4, 2023
1 parent baf2ffe commit 75a29d8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/editor/CodeMirrorEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const onChange = (code: string, fileName?: string, save?: boolean) => {
const modes: Record<string, Props['mode']> = {
css: 'css',
sass: 'sass',
scss: 'css',
less: 'css',
md: 'gfm',
html: 'htmlmixed',
js: {
name: 'javascript',
Expand Down
14 changes: 12 additions & 2 deletions src/editor/MonacoEditor.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script setup lang="ts">
import { computed } from 'vue'
import Monaco from '../monaco/Monaco.vue'
import type { EditorEmits, EditorProps } from './types'
defineProps<EditorProps>()
const props = defineProps<EditorProps>()
const emit = defineEmits<EditorEmits>()
defineOptions({
Expand All @@ -12,6 +13,15 @@ defineOptions({
const onChange = (code: string, fileName?: string, save?: boolean) => {
emit('change', code, fileName, save)
}
const activeMode = computed(() => {
const { mode: forcedMode, filename } = props
return forcedMode
? forcedMode
: ['css', 'sass', 'scss', 'less'].includes(filename.split('.').pop()!)
? 'css'
: 'js'
})
</script>

<template>
Expand All @@ -20,6 +30,6 @@ const onChange = (code: string, fileName?: string, save?: boolean) => {
:filename="filename"
:value="value"
:readonly="readonly"
:mode="mode"
:mode="activeMode"
/>
</template>
11 changes: 2 additions & 9 deletions src/monaco/Monaco.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,9 @@ import { getOrCreateModel } from './utils'
import { loadGrammars, loadTheme } from 'monaco-volar'
import { debounce } from '../utils'
import type { Store } from '../store'
import type { EditorEmits, PreviewMode } from '../editor/types'
import type { EditorProps, EditorEmits } from '../editor/types'
export interface Props {
filename: string
value?: string
readonly?: boolean
mode?: PreviewMode
}
const props = withDefaults(defineProps<Props>(), {
const props = withDefaults(defineProps<EditorProps>(), {
readonly: false,
})
Expand Down
10 changes: 6 additions & 4 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ export class File {
if (this.filename.endsWith('.html')) {
return 'html'
}
if (this.filename.endsWith('.css')) {
if (
this.filename.endsWith('.css') ||
this.filename.endsWith('.sass') ||
this.filename.endsWith('.scss') ||
this.filename.endsWith('.less')
) {
return 'css'
}
if (this.filename.endsWith('.ts')) {
Expand All @@ -67,9 +72,6 @@ export class File {
if (this.filename.endsWith('.md')) {
return 'markdown'
}
if (this.filename.endsWith('.sass') || this.filename.endsWith('.scss')) {
return 'scss'
}
return 'javascript'
}
}
Expand Down

0 comments on commit 75a29d8

Please sign in to comment.