vue-codemirror6 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +93 -0
- package/dist/vue-codemirror.es.js +9726 -0
- package/dist/vue-codemirror.umd.js +12 -0
- package/package.json +89 -0
- package/src/components/CodeMirror.vue +121 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div ref="editor" />
|
|
3
|
+
</template>
|
|
4
|
+
|
|
5
|
+
<script lang="ts">
|
|
6
|
+
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
|
|
7
|
+
|
|
8
|
+
import { EditorState, Extension, Transaction } from '@codemirror/state';
|
|
9
|
+
import { EditorView, ViewUpdate } from '@codemirror/view';
|
|
10
|
+
import type { Diagnostic } from '@codemirror/lint';
|
|
11
|
+
import type { LanguageSupport } from '@codemirror/language';
|
|
12
|
+
|
|
13
|
+
import merge from 'lodash/merge';
|
|
14
|
+
|
|
15
|
+
@Component
|
|
16
|
+
/** CodeMirror Component */
|
|
17
|
+
export default class CodeMirror extends Vue {
|
|
18
|
+
/** Editor */
|
|
19
|
+
editor!: EditorView;
|
|
20
|
+
|
|
21
|
+
/** Theme */
|
|
22
|
+
@Prop({ type: Object })
|
|
23
|
+
readonly theme;
|
|
24
|
+
|
|
25
|
+
/** Dark Mode */
|
|
26
|
+
@Prop({ type: Boolean, default: false })
|
|
27
|
+
readonly dark!: boolean;
|
|
28
|
+
|
|
29
|
+
/** Language Phreses */
|
|
30
|
+
@Prop({ type: Object })
|
|
31
|
+
readonly phrases!: Record<string, string>;
|
|
32
|
+
|
|
33
|
+
/** Additional Extension */
|
|
34
|
+
@Prop({ type: Array, default: () => [] })
|
|
35
|
+
readonly extensions!: Extension[];
|
|
36
|
+
|
|
37
|
+
/** CodeMirror Language */
|
|
38
|
+
@Prop({ type: Object })
|
|
39
|
+
readonly lang!: LanguageSupport;
|
|
40
|
+
|
|
41
|
+
/** CodeMirror Linter */
|
|
42
|
+
@Prop({ type: Array, default: () => [] })
|
|
43
|
+
readonly linter!: Diagnostic[];
|
|
44
|
+
|
|
45
|
+
/** Input */
|
|
46
|
+
@Prop({ type: String, required: true })
|
|
47
|
+
readonly value!: string;
|
|
48
|
+
|
|
49
|
+
/** Input value changed */
|
|
50
|
+
@Watch('value')
|
|
51
|
+
onValueChanged() {
|
|
52
|
+
this.editor.dispatch({
|
|
53
|
+
changes: {
|
|
54
|
+
from: 0,
|
|
55
|
+
to: this.editor.state.doc.length,
|
|
56
|
+
insert: this.value,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Darkmode */
|
|
62
|
+
@Watch('dark')
|
|
63
|
+
onToggleDarkMode() {
|
|
64
|
+
this.editor.setState(
|
|
65
|
+
EditorState.create({
|
|
66
|
+
doc: this.value,
|
|
67
|
+
extensions: this.extension,
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** CodeMirror Extension */
|
|
73
|
+
get extension(): Extension[] {
|
|
74
|
+
/** Default extension */
|
|
75
|
+
const ext = [
|
|
76
|
+
EditorState.phrases.of(this.phrases),
|
|
77
|
+
EditorView.updateListener.of((update: ViewUpdate) =>
|
|
78
|
+
this.$emit('update', update)
|
|
79
|
+
),
|
|
80
|
+
EditorView.theme(this.theme, { dark: this.dark }),
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
if (this.lang) {
|
|
84
|
+
ext.push(this.lang);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (this.linter.length !== 0) {
|
|
88
|
+
merge(ext, this.linter);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (this.extensions.length !== 0) {
|
|
92
|
+
merge(ext, this.extensions);
|
|
93
|
+
}
|
|
94
|
+
return ext;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** When loaded */
|
|
98
|
+
mounted() {
|
|
99
|
+
// Register Codemirror
|
|
100
|
+
this.editor = new EditorView({
|
|
101
|
+
state: EditorState.create({
|
|
102
|
+
doc: this.value,
|
|
103
|
+
extensions: this.extension,
|
|
104
|
+
}),
|
|
105
|
+
parent: this.$refs.editor as Element,
|
|
106
|
+
dispatch: (tr: Transaction) => {
|
|
107
|
+
this.editor.update([tr]);
|
|
108
|
+
|
|
109
|
+
if (tr.changes.empty) return;
|
|
110
|
+
// Binding
|
|
111
|
+
this.$emit('input', this.editor.state.doc.toString());
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Destroy */
|
|
117
|
+
beforeDestroy() {
|
|
118
|
+
this.editor.destroy();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
</script>
|