/
Codemirror.vue
114 lines (93 loc) · 3.37 KB
/
Codemirror.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<template>
<div class="vue-codemirror">
<div ref="codemirror" v-observe-visibility="visibilityChanged"></div>
</div>
</template>
<script lang="ts">
// Inspired by these repo: https://github.com/surmon-china/vue-codemirror
import { Component, Mixins, Prop, Watch } from 'vue-property-decorator'
import BaseMixin from '../mixins/base'
import { basicSetup } from 'codemirror'
import { EditorView, keymap } from '@codemirror/view'
import { EditorState } from '@codemirror/state'
import { vscodeDark } from '@uiw/codemirror-theme-vscode'
import { StreamLanguage } from '@codemirror/language'
import { klipper_config } from '@/plugins/StreamParserKlipperConfig'
import { gcode } from '@/plugins/StreamParserGcode'
import { indentWithTab } from '@codemirror/commands'
import { json } from '@codemirror/lang-json'
import { css } from '@codemirror/lang-css'
import { indentUnit } from '@codemirror/language'
@Component
export default class Codemirror extends Mixins(BaseMixin) {
private content = ''
private codemirror: null | EditorView = null
private cminstance: null | EditorView = null
declare $refs: {
codemirror: HTMLElement
}
@Prop({ required: false, default: '' })
declare readonly code: string
@Prop({ required: false, default: '' })
declare value: string
@Prop({ required: false, default: 'codemirror' })
declare readonly name: string
@Prop({ required: false, default: '' })
declare readonly fileExtension: string
@Watch('value')
valueChanged(newVal: string) {
const cm_value = this.cminstance?.state?.doc.toString()
if (newVal !== cm_value) {
this.setCmValue(newVal)
}
}
mounted(): void {
this.initialize()
}
beforeDestroy() {
this.destroy()
}
destroy() {
this.cminstance?.destroy()
}
initialize() {
this.codemirror = new EditorView({
parent: this.$refs.codemirror,
})
this.cminstance = this.codemirror
this.$nextTick(() => {
this.setCmValue(this.code || this.value || this.content)
this.$emit('ready', this.codemirror)
})
}
setCmValue(content: string) {
this.cminstance?.setState(EditorState.create({ doc: content, extensions: this.cmExtensions }))
}
get cmExtensions() {
const extensions = [
EditorView.theme({}, { dark: true }),
basicSetup,
vscodeDark,
indentUnit.of(' '.repeat(this.tabSize)),
keymap.of([indentWithTab]),
EditorView.updateListener.of((update) => {
this.content = update.state?.doc.toString()
if (this.$emit) {
this.$emit('input', this.content)
}
}),
]
if (['cfg', 'conf'].includes(this.fileExtension)) extensions.push(StreamLanguage.define(klipper_config))
else if (['gcode'].includes(this.fileExtension)) extensions.push(StreamLanguage.define(gcode))
else if (['json'].includes(this.fileExtension)) extensions.push(json())
else if (['css', 'scss', 'sass'].includes(this.fileExtension)) extensions.push(css())
return extensions
}
visibilityChanged(isVisible: boolean) {
if (isVisible) this.cminstance?.focus()
}
get tabSize() {
return this.$store.state.gui.editor.tabSize || 2
}
}
</script>