vue-codemirror6 0.0.5 → 0.3.0
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/README.md +186 -85
- package/dist/components/CodeMirror.vue.d.ts +176 -0
- package/dist/components/CodeMirror.vue.d.ts.map +1 -0
- package/dist/index.d.ts +107 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.esm.d.ts +98 -0
- package/dist/index.esm.d.ts.map +1 -0
- package/dist/interfaces/CodeMirrorEmitsInterface.d.ts +9 -0
- package/dist/interfaces/CodeMirrorEmitsInterface.d.ts.map +1 -0
- package/dist/interfaces/CodeMirrorPropsInterface.d.ts +56 -0
- package/dist/interfaces/CodeMirrorPropsInterface.d.ts.map +1 -0
- package/dist/vue-codemirror6.es.js +141 -5579
- package/dist/vue-codemirror6.umd.js +8 -28
- package/package.json +34 -22
- package/src/components/CodeMirror.vue +219 -161
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<div ref="editor">
|
|
3
|
-
<
|
|
2
|
+
<div ref="editor" class="vue-codemirror">
|
|
3
|
+
<div v-show="context.slots.defatlt"><slot /></div>
|
|
4
4
|
</div>
|
|
5
5
|
</template>
|
|
6
6
|
|
|
7
7
|
<script lang="ts">
|
|
8
|
-
import {
|
|
9
|
-
|
|
8
|
+
import {
|
|
9
|
+
computed,
|
|
10
|
+
type ComputedRef,
|
|
11
|
+
ref,
|
|
12
|
+
watch,
|
|
13
|
+
onMounted,
|
|
14
|
+
onUnmounted,
|
|
15
|
+
type Ref,
|
|
16
|
+
defineComponent,
|
|
17
|
+
type PropType,
|
|
18
|
+
type SetupContext,
|
|
19
|
+
EmitsOptions,
|
|
20
|
+
} from 'vue-demi';
|
|
21
|
+
|
|
22
|
+
import type CodeMirrorPropsInterface from '@/interfaces/CodeMirrorPropsInterface';
|
|
23
|
+
import type CodeMirrorEmitsInterface from '@/interfaces/CodeMirrorEmitsInterface';
|
|
10
24
|
|
|
11
25
|
import {
|
|
12
26
|
EditorState,
|
|
@@ -17,171 +31,215 @@ import { EditorView, type ViewUpdate } from '@codemirror/view';
|
|
|
17
31
|
import type { LanguageSupport } from '@codemirror/language';
|
|
18
32
|
import type { Diagnostic } from '@codemirror/lint';
|
|
19
33
|
import type { StyleSpec } from 'style-mod';
|
|
20
|
-
|
|
21
34
|
import { compact, merge } from 'lodash';
|
|
22
35
|
|
|
23
|
-
@Component({ name: 'CodeMirror' })
|
|
24
36
|
/** CodeMirror Component */
|
|
25
|
-
export default
|
|
26
|
-
/**
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
export default defineComponent({
|
|
38
|
+
/** Component Name */
|
|
39
|
+
name: 'CodeMirror',
|
|
40
|
+
/** Model Definition */
|
|
41
|
+
model: {
|
|
42
|
+
prop: 'modelValue',
|
|
43
|
+
event: 'update:modelValue',
|
|
44
|
+
},
|
|
45
|
+
/** Props Definition */
|
|
46
|
+
props: {
|
|
47
|
+
/** Model value */
|
|
48
|
+
modelValue: {
|
|
49
|
+
type: String,
|
|
50
|
+
default: '',
|
|
51
|
+
},
|
|
52
|
+
/**
|
|
53
|
+
* Theme
|
|
54
|
+
*
|
|
55
|
+
* @see {@link https://codemirror.net/6/examples/styling/ | Example: Styling}
|
|
56
|
+
*/
|
|
57
|
+
theme: {
|
|
58
|
+
type: Object as PropType<{ [selector: string]: StyleSpec }>,
|
|
59
|
+
default: undefined,
|
|
60
|
+
},
|
|
61
|
+
/** Dark Mode */
|
|
62
|
+
dark: {
|
|
63
|
+
type: Boolean,
|
|
64
|
+
default: false,
|
|
65
|
+
},
|
|
66
|
+
/**
|
|
67
|
+
* Readonly
|
|
68
|
+
*
|
|
69
|
+
* @see {@link https://codemirror.net/6/docs/ref/#state.EditorState%5EreadOnly | readOnly}
|
|
70
|
+
*/
|
|
71
|
+
readonly: {
|
|
72
|
+
type: Boolean,
|
|
73
|
+
default: false,
|
|
74
|
+
},
|
|
75
|
+
/**
|
|
76
|
+
* Editable
|
|
77
|
+
*
|
|
78
|
+
* @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5Eeditable | editable}
|
|
79
|
+
*/
|
|
80
|
+
editable: {
|
|
81
|
+
type: Boolean,
|
|
82
|
+
default: true,
|
|
83
|
+
},
|
|
84
|
+
/**
|
|
85
|
+
* Additional Extension
|
|
86
|
+
*
|
|
87
|
+
* @see {@link https://codemirror.net/6/docs/ref/#state.Extension | Extending Editor State}
|
|
88
|
+
*/
|
|
89
|
+
extensions: {
|
|
90
|
+
type: Array as PropType<Extension[]>,
|
|
91
|
+
default: undefined,
|
|
92
|
+
},
|
|
93
|
+
/**
|
|
94
|
+
* Language Phreses
|
|
95
|
+
*
|
|
96
|
+
* @see {@link https://codemirror.net/6/examples/translate/ | Example: Internationalization}
|
|
97
|
+
*/
|
|
98
|
+
phrases: {
|
|
99
|
+
type: Object as PropType<Record<string, string>>,
|
|
100
|
+
default: undefined,
|
|
101
|
+
},
|
|
102
|
+
/**
|
|
103
|
+
* CodeMirror Language
|
|
104
|
+
*
|
|
105
|
+
* @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
|
|
106
|
+
*/
|
|
107
|
+
lang: {
|
|
108
|
+
type: Object as PropType<LanguageSupport>,
|
|
109
|
+
default: undefined,
|
|
110
|
+
},
|
|
111
|
+
/**
|
|
112
|
+
* CodeMirror Linter
|
|
113
|
+
*
|
|
114
|
+
* @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
|
|
115
|
+
*/
|
|
116
|
+
linter: {
|
|
117
|
+
type: Array as PropType<Diagnostic[]>,
|
|
118
|
+
default: undefined,
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
/** Emits */
|
|
122
|
+
emits: ['update:modelValue', 'update'],
|
|
29
123
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* @
|
|
124
|
+
* Setup
|
|
125
|
+
* @param props - Props
|
|
126
|
+
* @param context - Context
|
|
33
127
|
*/
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
128
|
+
setup(props: CodeMirrorPropsInterface, context: SetupContext<EmitsOptions>) {
|
|
129
|
+
/** Editor DOM */
|
|
130
|
+
const editor: Ref<Element | undefined> = ref<Element>();
|
|
131
|
+
|
|
132
|
+
/** Model */
|
|
133
|
+
const modelValue: Ref<string> = ref(props.modelValue);
|
|
134
|
+
|
|
135
|
+
/** Dark mode */
|
|
136
|
+
const dark: Ref<boolean | undefined> = ref(props.dark);
|
|
137
|
+
|
|
138
|
+
/** Emits */
|
|
139
|
+
const emit = context.emit as CodeMirrorEmitsInterface;
|
|
140
|
+
|
|
141
|
+
/** CodeMirror Extension */
|
|
142
|
+
const extensions: ComputedRef<Extension[]> = computed(() => {
|
|
143
|
+
/** Default extension */
|
|
144
|
+
const ext = [
|
|
145
|
+
// ViewUpdate event listener
|
|
146
|
+
EditorView.updateListener.of((update: ViewUpdate) => {
|
|
147
|
+
emit('update', update);
|
|
148
|
+
}),
|
|
149
|
+
// Toggle light/dark mode.
|
|
150
|
+
EditorView.theme(props.theme || {}, { dark: props.dark }),
|
|
151
|
+
// locale settings
|
|
152
|
+
props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
|
|
153
|
+
// Parser language setting
|
|
154
|
+
props.lang,
|
|
155
|
+
// Readonly option
|
|
156
|
+
props.readonly ? EditorState.readOnly.of(props.readonly) : undefined,
|
|
157
|
+
// Editable option
|
|
158
|
+
props.editable ? EditorView.editable.of(props.editable) : undefined,
|
|
159
|
+
];
|
|
160
|
+
|
|
161
|
+
if (props.linter) {
|
|
162
|
+
// Append Linter settings
|
|
163
|
+
merge(ext, props.linter);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (props.extensions) {
|
|
167
|
+
// Append Extensions (such as basic-setup)
|
|
168
|
+
merge(ext, props.extensions);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// console.debug('[CodeMirror.vue] Loaded extensions:', ext, compact(ext));
|
|
172
|
+
return compact(ext);
|
|
173
|
+
});
|
|
48
174
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
175
|
+
/** CodeMirror Editor View */
|
|
176
|
+
let view!: EditorView;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Input value changed
|
|
180
|
+
*
|
|
181
|
+
* @see {@link https://codemirror.net/6/docs/migration/#making-changes | Making Changes}
|
|
182
|
+
*/
|
|
183
|
+
watch(modelValue, current => {
|
|
184
|
+
if (view.composing) {
|
|
185
|
+
// IME fix
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Previous cursor location */
|
|
190
|
+
const previous = view.state.selection;
|
|
191
|
+
view.setState(
|
|
192
|
+
EditorState.create({
|
|
193
|
+
doc: current,
|
|
194
|
+
extensions: extensions.value,
|
|
195
|
+
selection: previous,
|
|
196
|
+
})
|
|
197
|
+
);
|
|
198
|
+
});
|
|
56
199
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
200
|
+
/** Toggle Dark mode */
|
|
201
|
+
watch(dark, () => {
|
|
202
|
+
view.setState(
|
|
203
|
+
EditorState.create({
|
|
204
|
+
doc: modelValue.value,
|
|
205
|
+
extensions: extensions.value,
|
|
206
|
+
})
|
|
207
|
+
);
|
|
208
|
+
});
|
|
64
209
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
210
|
+
/** When loaded */
|
|
211
|
+
onMounted(() => {
|
|
212
|
+
/** Value */
|
|
213
|
+
const value =
|
|
214
|
+
!modelValue.value && editor.value
|
|
215
|
+
? (editor.value.childNodes[0] as HTMLDivElement).innerText
|
|
216
|
+
: modelValue.value;
|
|
217
|
+
|
|
218
|
+
// Register Codemirror
|
|
219
|
+
view = new EditorView({
|
|
220
|
+
state: EditorState.create({
|
|
221
|
+
doc: value,
|
|
222
|
+
extensions: extensions.value,
|
|
223
|
+
}),
|
|
224
|
+
parent: editor.value,
|
|
225
|
+
dispatch: (tr: Transaction) => {
|
|
226
|
+
view.update([tr]);
|
|
227
|
+
|
|
228
|
+
if (tr.changes.empty) return;
|
|
229
|
+
// to parent binding
|
|
230
|
+
modelValue.value = view.state.doc.toString();
|
|
231
|
+
emit('update:modelValue', modelValue.value);
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
});
|
|
72
235
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
readonly value!: string;
|
|
236
|
+
/** Destroy */
|
|
237
|
+
onUnmounted(() => view.destroy());
|
|
76
238
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
onValueChanged() {
|
|
84
|
-
/** Previous cursor location */
|
|
85
|
-
const previous = this.editor.state.selection;
|
|
86
|
-
/*
|
|
87
|
-
this.editor.dispatch({
|
|
88
|
-
changes: {
|
|
89
|
-
from: 0,
|
|
90
|
-
to: this.editor.state.doc.length,
|
|
91
|
-
insert: this.value,
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
|
-
*/
|
|
95
|
-
this.editor.setState(
|
|
96
|
-
EditorState.create({
|
|
97
|
-
doc: this.value,
|
|
98
|
-
extensions: this.extension,
|
|
99
|
-
selection: previous,
|
|
100
|
-
})
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** Darkmode */
|
|
105
|
-
@Watch('dark')
|
|
106
|
-
onToggleDarkMode() {
|
|
107
|
-
this.editor.setState(
|
|
108
|
-
EditorState.create({
|
|
109
|
-
doc: this.value,
|
|
110
|
-
extensions: this.extension,
|
|
111
|
-
})
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** CodeMirror Extension */
|
|
116
|
-
get extension(): Extension[] {
|
|
117
|
-
/** Default extension */
|
|
118
|
-
const ext: Extension[] = compact([
|
|
119
|
-
// ViewUpdate event listener
|
|
120
|
-
EditorView.updateListener.of((update: ViewUpdate) =>
|
|
121
|
-
this.$emit('update', update)
|
|
122
|
-
),
|
|
123
|
-
// Toggle light/dark mode.
|
|
124
|
-
EditorView.theme(this.theme, { dark: this.dark }),
|
|
125
|
-
// locale settings
|
|
126
|
-
this.phrases ? EditorState.phrases.of(this.phrases) : undefined,
|
|
127
|
-
// Parser language setting
|
|
128
|
-
this.lang ? this.lang : undefined,
|
|
129
|
-
]);
|
|
130
|
-
|
|
131
|
-
if (this.linter.length !== 0) {
|
|
132
|
-
// Append Linter settings
|
|
133
|
-
merge(ext, this.linter);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (this.extensions.length !== 0) {
|
|
137
|
-
// Append Extensions (such as basic-setup)
|
|
138
|
-
merge(ext, this.extensions);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
console.debug('[CodeMirror.vue] Loaded extensions:', ext);
|
|
142
|
-
|
|
143
|
-
return ext;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** When loaded */
|
|
147
|
-
mounted() {
|
|
148
|
-
let value = this.value;
|
|
149
|
-
if (!this.value && this.$slots.default !== undefined) {
|
|
150
|
-
// override text node
|
|
151
|
-
value = this.getChildrenTextContent(this.$slots.default);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// Register Codemirror
|
|
155
|
-
this.editor = new EditorView({
|
|
156
|
-
state: EditorState.create({
|
|
157
|
-
doc: value,
|
|
158
|
-
extensions: this.extension,
|
|
159
|
-
}),
|
|
160
|
-
parent: this.$refs.editor as Element,
|
|
161
|
-
dispatch: (tr: Transaction) => {
|
|
162
|
-
this.editor.update([tr]);
|
|
163
|
-
|
|
164
|
-
if (tr.changes.empty) return;
|
|
165
|
-
// to parent binding
|
|
166
|
-
this.$emit('input', this.editor.state.doc.toString());
|
|
167
|
-
},
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/** Destroy */
|
|
172
|
-
beforeDestroy() {
|
|
173
|
-
this.editor.destroy();
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/** Vue node to plain text */
|
|
177
|
-
private getChildrenTextContent(children: VNode[]): string {
|
|
178
|
-
return children
|
|
179
|
-
.map((node: VNode) =>
|
|
180
|
-
node.children
|
|
181
|
-
? this.getChildrenTextContent(node.children)
|
|
182
|
-
: node.text || '\n'
|
|
183
|
-
)
|
|
184
|
-
.join('');
|
|
185
|
-
}
|
|
186
|
-
}
|
|
239
|
+
return {
|
|
240
|
+
context,
|
|
241
|
+
editor,
|
|
242
|
+
};
|
|
243
|
+
},
|
|
244
|
+
});
|
|
187
245
|
</script>
|