Skip to content

fix: add ts and json mode #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/codemirror/CodeMirror.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
</template>

<script setup lang="ts">
import type { ModeSpec, ModeSpecOptions } from 'codemirror'
import { ref, onMounted, watchEffect, inject } from 'vue'
import { debounce } from '../utils'
import CodeMirror from './codemirror'

export interface Props {
mode?: string
mode?: string | ModeSpec<ModeSpecOptions>
value?: string
readonly?: boolean
}
Expand Down
30 changes: 24 additions & 6 deletions src/editor/Editor.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
<script setup lang="ts">
import FileSelector from './FileSelector.vue'
import CodeMirror from '../codemirror/CodeMirror.vue'
import CodeMirror, { type Props } from '../codemirror/CodeMirror.vue'
import Message from '../Message.vue'
import { debounce } from '../utils'
import { computed, inject } from 'vue'
import { Store } from '../store'

const store = inject('store') as Store

const modes: Record<string, Props['mode']> = {
css: 'css',
html: 'htmlmixed',
js: {
name: 'javascript',
},
json: {
name: 'javascript',
json: true,
},
ts: {
name: 'javascript',
typescript: true,
},
vue: 'htmlmixed',
}

const onChange = debounce((code: string) => {
store.state.activeFile.code = code
}, 250)

const activeMode = computed(() => {
const { filename } = store.state.activeFile
return filename.endsWith('.vue') || filename.endsWith('.html')
? 'htmlmixed'
: filename.endsWith('.css')
? 'css'
: 'javascript'

const mode = modes[filename.split('.').pop()!]

return filename.lastIndexOf('.') !== -1 && mode
? mode
: modes.js
})
</script>

Expand Down