vue-codemirror6 0.3.2 → 0.3.6
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 +254 -219
- package/dist/components/CodeMirror.vue.d.ts +231 -9
- package/dist/components/CodeMirror.vue.d.ts.map +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/vue-codemirror6.es.js +51 -27
- package/dist/vue-codemirror6.umd.js +2 -2
- package/package.json +40 -30
- package/src/components/CodeMirror.vue +92 -37
|
@@ -7,21 +7,30 @@
|
|
|
7
7
|
<script lang="ts">
|
|
8
8
|
import {
|
|
9
9
|
computed,
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
defineComponent,
|
|
11
|
+
nextTick,
|
|
12
12
|
onMounted,
|
|
13
13
|
onUnmounted,
|
|
14
|
-
|
|
14
|
+
ref,
|
|
15
|
+
toRaw,
|
|
16
|
+
toRefs,
|
|
17
|
+
watch,
|
|
18
|
+
type ComputedRef,
|
|
19
|
+
type PropType,
|
|
20
|
+
type Ref,
|
|
21
|
+
type SetupContext,
|
|
15
22
|
} from 'vue-demi';
|
|
16
|
-
import { EditorState } from '@codemirror/state';
|
|
17
|
-
import { EditorView } from '@codemirror/view';
|
|
18
|
-
import { compact, merge } from 'lodash';
|
|
19
23
|
|
|
20
|
-
import
|
|
24
|
+
import { EditorSelection, EditorState } from '@codemirror/state';
|
|
25
|
+
import { EditorView, keymap } from '@codemirror/view';
|
|
26
|
+
import { basicSetup } from '@codemirror/basic-setup';
|
|
27
|
+
import { indentWithTab } from '@codemirror/commands';
|
|
28
|
+
|
|
29
|
+
import { compact, merge, trim } from 'lodash';
|
|
21
30
|
|
|
22
31
|
import type CodeMirrorEmitsInterface from '@/interfaces/CodeMirrorEmitsInterface';
|
|
23
32
|
|
|
24
|
-
import type { Extension, Transaction } from '@codemirror/state';
|
|
33
|
+
import type { Extension, Text, Transaction } from '@codemirror/state';
|
|
25
34
|
import type { LanguageSupport } from '@codemirror/language';
|
|
26
35
|
import type { ViewUpdate } from '@codemirror/view';
|
|
27
36
|
import type { Diagnostic } from '@codemirror/lint';
|
|
@@ -40,9 +49,20 @@ export default defineComponent({
|
|
|
40
49
|
props: {
|
|
41
50
|
/** Model value */
|
|
42
51
|
modelValue: {
|
|
43
|
-
type: String
|
|
52
|
+
type: String as PropType<string | Text>,
|
|
44
53
|
default: '',
|
|
45
54
|
},
|
|
55
|
+
/**
|
|
56
|
+
* Selection
|
|
57
|
+
*
|
|
58
|
+
* @see {@link https://codemirror.net/6/docs/ref/#state.EditorSelection | EditorSelection}
|
|
59
|
+
*/
|
|
60
|
+
selection: {
|
|
61
|
+
type: Object as PropType<
|
|
62
|
+
EditorSelection | { anchor: number; head?: number }
|
|
63
|
+
>,
|
|
64
|
+
default: undefined,
|
|
65
|
+
},
|
|
46
66
|
/**
|
|
47
67
|
* Theme
|
|
48
68
|
*
|
|
@@ -50,13 +70,42 @@ export default defineComponent({
|
|
|
50
70
|
*/
|
|
51
71
|
theme: {
|
|
52
72
|
type: Object as PropType<{ [selector: string]: StyleSpec }>,
|
|
53
|
-
default:
|
|
73
|
+
default: () => {
|
|
74
|
+
return {};
|
|
75
|
+
},
|
|
54
76
|
},
|
|
55
77
|
/** Dark Mode */
|
|
56
78
|
dark: {
|
|
57
79
|
type: Boolean,
|
|
58
80
|
default: false,
|
|
59
81
|
},
|
|
82
|
+
/**
|
|
83
|
+
* Use Basic Setup
|
|
84
|
+
*
|
|
85
|
+
* @see {@link https://codemirror.net/6/docs/ref/#basic-setup | basic-setup}
|
|
86
|
+
*/
|
|
87
|
+
basic: {
|
|
88
|
+
type: Boolean,
|
|
89
|
+
default: false,
|
|
90
|
+
},
|
|
91
|
+
/**
|
|
92
|
+
* Line wrapping
|
|
93
|
+
*
|
|
94
|
+
* @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5ElineWrapping | LineWrapping}
|
|
95
|
+
*/
|
|
96
|
+
wrap: {
|
|
97
|
+
type: Boolean,
|
|
98
|
+
default: false,
|
|
99
|
+
},
|
|
100
|
+
/**
|
|
101
|
+
* Tab handling
|
|
102
|
+
*
|
|
103
|
+
* @see {@link https://codemirror.net/6/examples/tab/ | Tab Handling}
|
|
104
|
+
*/
|
|
105
|
+
tab: {
|
|
106
|
+
type: Boolean,
|
|
107
|
+
default: false,
|
|
108
|
+
},
|
|
60
109
|
/**
|
|
61
110
|
* Readonly
|
|
62
111
|
*
|
|
@@ -121,35 +170,41 @@ export default defineComponent({
|
|
|
121
170
|
*/
|
|
122
171
|
setup(props, context: SetupContext) {
|
|
123
172
|
/** Editor DOM */
|
|
124
|
-
const editor: Ref<Element | undefined> = ref
|
|
173
|
+
const editor: Ref<Element | undefined> = ref();
|
|
125
174
|
|
|
126
|
-
/**
|
|
127
|
-
const
|
|
175
|
+
/** Internal value */
|
|
176
|
+
const doc: Ref<string | Text | undefined> = ref(props.modelValue);
|
|
128
177
|
|
|
129
178
|
/** Dark mode */
|
|
130
|
-
const dark
|
|
179
|
+
const { dark } = toRefs(props);
|
|
131
180
|
|
|
132
181
|
/** Emits */
|
|
133
182
|
const emit = context.emit as CodeMirrorEmitsInterface;
|
|
134
183
|
|
|
135
184
|
/** CodeMirror Extension */
|
|
136
|
-
const
|
|
185
|
+
const exts: ComputedRef<Extension[]> = computed(() => {
|
|
137
186
|
/** Default extension */
|
|
138
187
|
const ext = [
|
|
188
|
+
// Toggle basic setup
|
|
189
|
+
props.basic ? basicSetup : undefined,
|
|
139
190
|
// ViewUpdate event listener
|
|
140
|
-
EditorView.updateListener.of((update: ViewUpdate) =>
|
|
141
|
-
emit('update', update)
|
|
142
|
-
|
|
191
|
+
EditorView.updateListener.of((update: ViewUpdate) =>
|
|
192
|
+
emit('update', update)
|
|
193
|
+
),
|
|
143
194
|
// Toggle light/dark mode.
|
|
144
|
-
EditorView.theme(props.theme
|
|
195
|
+
EditorView.theme(props.theme, { dark: dark.value }),
|
|
196
|
+
// Toggle line wrapping
|
|
197
|
+
props.wrap ? EditorView.lineWrapping : undefined,
|
|
198
|
+
// Indent with tab
|
|
199
|
+
props.tab ? keymap.of([indentWithTab]) : undefined,
|
|
145
200
|
// locale settings
|
|
146
201
|
props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
|
|
147
|
-
// Parser language setting
|
|
148
|
-
props.lang,
|
|
149
202
|
// Readonly option
|
|
150
203
|
props.readonly ? EditorState.readOnly.of(props.readonly) : undefined,
|
|
151
204
|
// Editable option
|
|
152
205
|
props.editable ? EditorView.editable.of(props.editable) : undefined,
|
|
206
|
+
// Lang
|
|
207
|
+
props.lang ? toRaw(props.lang) : undefined,
|
|
153
208
|
];
|
|
154
209
|
|
|
155
210
|
if (props.linter) {
|
|
@@ -162,19 +217,19 @@ export default defineComponent({
|
|
|
162
217
|
merge(ext, props.extensions);
|
|
163
218
|
}
|
|
164
219
|
|
|
165
|
-
// console.debug('[CodeMirror.vue] Loaded extensions:',
|
|
220
|
+
// console.debug('[CodeMirror.vue] Loaded extensions:', compact(ext));
|
|
166
221
|
return compact(ext);
|
|
167
222
|
});
|
|
168
223
|
|
|
169
224
|
/** CodeMirror Editor View */
|
|
170
|
-
let view
|
|
225
|
+
let view: EditorView;
|
|
171
226
|
|
|
172
227
|
/**
|
|
173
228
|
* Input value changed
|
|
174
229
|
*
|
|
175
230
|
* @see {@link https://codemirror.net/6/docs/migration/#making-changes | Making Changes}
|
|
176
231
|
*/
|
|
177
|
-
watch(
|
|
232
|
+
watch(doc, current => {
|
|
178
233
|
if (view.composing) {
|
|
179
234
|
// IME fix
|
|
180
235
|
return;
|
|
@@ -185,35 +240,34 @@ export default defineComponent({
|
|
|
185
240
|
view.setState(
|
|
186
241
|
EditorState.create({
|
|
187
242
|
doc: current,
|
|
188
|
-
extensions:
|
|
243
|
+
extensions: exts.value,
|
|
189
244
|
selection: previous,
|
|
190
245
|
})
|
|
191
246
|
);
|
|
192
247
|
});
|
|
193
248
|
|
|
194
|
-
/** Toggle Dark mode */
|
|
195
249
|
watch(dark, () => {
|
|
196
250
|
view.setState(
|
|
197
251
|
EditorState.create({
|
|
198
|
-
doc:
|
|
199
|
-
extensions:
|
|
252
|
+
doc: doc.value,
|
|
253
|
+
extensions: exts.value,
|
|
200
254
|
})
|
|
201
255
|
);
|
|
202
256
|
});
|
|
203
257
|
|
|
204
258
|
/** When loaded */
|
|
205
|
-
onMounted(() => {
|
|
259
|
+
onMounted(async () => {
|
|
206
260
|
/** Initial Value */
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
: modelValue.value;
|
|
261
|
+
if (doc.value == '' && editor.value) {
|
|
262
|
+
doc.value = trim((editor.value.childNodes[0] as HTMLElement).innerText);
|
|
263
|
+
}
|
|
211
264
|
|
|
212
265
|
// Register Codemirror
|
|
213
266
|
view = new EditorView({
|
|
214
267
|
state: EditorState.create({
|
|
215
|
-
doc: value,
|
|
216
|
-
|
|
268
|
+
doc: doc.value,
|
|
269
|
+
selection: props.selection,
|
|
270
|
+
extensions: exts.value,
|
|
217
271
|
}),
|
|
218
272
|
parent: editor.value,
|
|
219
273
|
dispatch: (tr: Transaction) => {
|
|
@@ -221,10 +275,11 @@ export default defineComponent({
|
|
|
221
275
|
|
|
222
276
|
if (tr.changes.empty) return;
|
|
223
277
|
// to parent binding
|
|
224
|
-
|
|
225
|
-
emit('update:modelValue',
|
|
278
|
+
doc.value = view.state.doc.toString();
|
|
279
|
+
emit('update:modelValue', doc.value);
|
|
226
280
|
},
|
|
227
281
|
});
|
|
282
|
+
await nextTick();
|
|
228
283
|
});
|
|
229
284
|
|
|
230
285
|
/** Destroy */
|