vue-codemirror6 1.1.1 → 1.1.2

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.
@@ -1 +1 @@
1
- {"version":3,"file":"CodeMirror.d.ts","sourceRoot":"","sources":["../../src/components/CodeMirror.ts"],"names":[],"mappings":"AAAA,OAAO,EASL,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,KAAK,QAAQ,EACb,KAAK,GAAG,EACR,KAAK,YAAY,EAEjB,KAAK,mBAAmB,EACzB,MAAM,UAAU,CAAC;AAQlB,OAAO,EAAE,eAAe,EAAE,WAAW,EAAe,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,UAAU,EAAuB,MAAM,kBAAkB,CAAC;AAInE,OAAO,KAAK,EACV,SAAS,EACT,cAAc,EACd,IAAI,EAEL,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,qBAAqB;AACrB,MAAM,WAAW,sBAAuB,SAAQ,kBAAkB;IAChE,mBAAmB;IACnB,CAAC,CAAC,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;IACrD,4BAA4B;IAC5B,CAAC,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;IACvC,yBAAyB;IACzB,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IACnC,yBAAyB;IACzB,CACE,CAAC,EAAE,OAAO,EACV,KAAK,EAAE;QACL,IAAI,EAAE,UAAU,CAAC;QACjB,KAAK,EAAE,WAAW,CAAC;QACnB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,CAAC;IACrB,oBAAoB;IACpB,CAAC,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,WAAW,GAAG,IAAI,CAAC;CACzC;AAED,2BAA2B;;IAWvB,kBAAkB;;;;;IAKlB;;;;OAIG;;;;;;;IAKH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;;;OAMG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;;;;OAOG;;;;;IAKH;;;;OAIG;;;;;IAOH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;;;;OAOG;;;;;IAKH;;;;OAIG;;;;;IAKH;;OAEG;;;;;;;;IA/KH,kBAAkB;;;;;IAKlB;;;;OAIG;;;;;;;IAKH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;;;OAMG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;;;;OAOG;;;;;IAKH;;;;OAIG;;;;;IAOH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;;;;OAOG;;;;;IAKH;;;;OAIG;;;;;IAKH;;OAEG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAzLP,wBAoiBG"}
@@ -0,0 +1,494 @@
1
+ import { computed, defineComponent, nextTick, onMounted, onUnmounted, ref, shallowRef, watch, } from 'vue-demi';
2
+ // Helpers
3
+ import h, { slot } from '@/helpers/h-demi';
4
+ import compact from 'lodash/compact';
5
+ import trim from 'lodash/trim';
6
+ // CodeMirror
7
+ import { basicSetup, minimalSetup } from 'codemirror';
8
+ import { EditorSelection, EditorState, StateEffect, } from '@codemirror/state';
9
+ import { EditorView, keymap, placeholder, } from '@codemirror/view';
10
+ import { indentWithTab } from '@codemirror/commands';
11
+ import { forceLinting, linter, lintGutter, } from '@codemirror/lint';
12
+ /** CodeMirror Component */
13
+ export default defineComponent({
14
+ /** Component Name */
15
+ name: 'CodeMirror',
16
+ /** Model Definition */
17
+ model: {
18
+ prop: 'modelValue',
19
+ event: 'update:modelValue',
20
+ },
21
+ /** Props Definition */
22
+ props: {
23
+ /** Model value */
24
+ modelValue: {
25
+ type: String,
26
+ default: '',
27
+ },
28
+ /**
29
+ * Theme
30
+ *
31
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView^theme}
32
+ */
33
+ theme: {
34
+ type: Object,
35
+ default: () => { },
36
+ },
37
+ /** Dark Mode */
38
+ dark: {
39
+ type: Boolean,
40
+ default: false,
41
+ },
42
+ /**
43
+ * Use Basic Setup
44
+ *
45
+ * @see {@link https://codemirror.net/docs/ref/#codemirror.basicSetup}
46
+ */
47
+ basic: {
48
+ type: Boolean,
49
+ default: false,
50
+ },
51
+ /**
52
+ * Use Minimal Setup (The basic setting has priority.)
53
+ *
54
+ * @see {@link https://codemirror.net/docs/ref/#codemirror.minimalSetup}
55
+ */
56
+ minimal: {
57
+ type: Boolean,
58
+ default: false,
59
+ },
60
+ /**
61
+ * Placeholder
62
+ *
63
+ * @see {@link https://codemirror.net/docs/ref/#view.placeholder}
64
+ */
65
+ placeholder: {
66
+ type: String,
67
+ default: undefined,
68
+ },
69
+ /**
70
+ * Line wrapping
71
+ *
72
+ * An extension that enables line wrapping in the editor (by setting CSS white-space to pre-wrap in the content).
73
+ *
74
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView%5ElineWrapping}
75
+ */
76
+ wrap: {
77
+ type: Boolean,
78
+ default: false,
79
+ },
80
+ /**
81
+ * Allow tab key indent.
82
+ *
83
+ * @see {@link https://codemirror.net/examples/tab/}
84
+ */
85
+ tab: {
86
+ type: Boolean,
87
+ default: false,
88
+ },
89
+ /**
90
+ * Tab size
91
+ *
92
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState^tabSize}
93
+ */
94
+ tabSize: {
95
+ type: Number,
96
+ default: undefined,
97
+ },
98
+ /**
99
+ * Set line break (separetor) char.
100
+ *
101
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState^lineSeparator}
102
+ */
103
+ lineSeparator: {
104
+ type: String,
105
+ default: undefined,
106
+ },
107
+ /**
108
+ * Readonly
109
+ *
110
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState^readOnly}
111
+ */
112
+ readonly: {
113
+ type: Boolean,
114
+ default: false,
115
+ },
116
+ /**
117
+ * Disable input.
118
+ *
119
+ * This is the reversed value of the CodeMirror editable.
120
+ * Similar to `readonly`, but setting this value to true disables dragging.
121
+ *
122
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView^editable}
123
+ */
124
+ disabled: {
125
+ type: Boolean,
126
+ default: false,
127
+ },
128
+ /**
129
+ * Additional Extension
130
+ *
131
+ * @see {@link https://codemirror.net/docs/ref/#state.Extension}
132
+ */
133
+ extensions: {
134
+ type: Array,
135
+ default: () => {
136
+ return [];
137
+ },
138
+ },
139
+ /**
140
+ * Language Phreses
141
+ *
142
+ * @see {@link https://codemirror.net/examples/translate/}
143
+ */
144
+ phrases: {
145
+ type: Object,
146
+ default: () => undefined,
147
+ },
148
+ /**
149
+ * CodeMirror Language
150
+ *
151
+ * @see {@link https://codemirror.net/docs/ref/#language}
152
+ */
153
+ lang: {
154
+ type: Object,
155
+ default: () => undefined,
156
+ },
157
+ /**
158
+ * CodeMirror Linter
159
+ *
160
+ * @see {@link https://codemirror.net/docs/ref/#lint.linter}
161
+ */
162
+ linter: {
163
+ type: Function,
164
+ default: undefined,
165
+ },
166
+ /**
167
+ * Linter Config
168
+ *
169
+ * @see {@link https://codemirror.net/docs/ref/#lint.linter^config}
170
+ */
171
+ linterConfig: {
172
+ type: Object,
173
+ default: () => { },
174
+ },
175
+ /**
176
+ * Show Linter Gutter
177
+ *
178
+ * An area to 🔴 the lines with errors will be displayed.
179
+ * This feature is not enabled if `linter` is not specified.
180
+ *
181
+ * @see {@link https://codemirror.net/docs/ref/#lint.lintGutter}
182
+ */
183
+ gutter: {
184
+ type: Boolean,
185
+ defalt: false,
186
+ },
187
+ /**
188
+ * Gutter Config
189
+ *
190
+ * @see {@link https://codemirror.net/docs/ref/#lint.lintGutter^config}
191
+ */
192
+ gutterConfig: {
193
+ type: Object,
194
+ default: () => { },
195
+ },
196
+ /**
197
+ * Using tag
198
+ */
199
+ tag: {
200
+ type: String,
201
+ default: 'div',
202
+ },
203
+ },
204
+ /** Emits */
205
+ emits: ['update:modelValue', 'update', 'ready', 'focus', 'change', 'destroy'],
206
+ /**
207
+ * Setup
208
+ *
209
+ * @param props - Props
210
+ * @param context - Context
211
+ */
212
+ setup(props, context) {
213
+ /** Editor DOM */
214
+ const editor = ref();
215
+ /** Internal value */
216
+ const doc = ref(props.modelValue);
217
+ /**
218
+ * CodeMirror Editor View
219
+ *
220
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView}
221
+ */
222
+ const view = shallowRef(new EditorView());
223
+ /**
224
+ * Editor Selection
225
+ *
226
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorSelection}
227
+ */
228
+ const selection = computed({
229
+ get: () => view.value.state.selection,
230
+ set: s => view.value.dispatch({ selection: s }),
231
+ });
232
+ /**
233
+ * Editor State
234
+ *
235
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState}
236
+ */
237
+ const state = computed({
238
+ get: () => view.value.state,
239
+ set: s => view.value.setState(s),
240
+ });
241
+ /** Cursor Position */
242
+ const cursor = computed({
243
+ get: () => view.value.state.selection.main.head || 0,
244
+ set: a => view.value.dispatch({ selection: { anchor: a } }),
245
+ });
246
+ /**
247
+ * Focus
248
+ *
249
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView.hasFocus}
250
+ */
251
+ const focus = computed({
252
+ get: () => view.value.hasFocus,
253
+ set: f => {
254
+ if (f) {
255
+ view.value.focus();
256
+ }
257
+ },
258
+ });
259
+ /** Text length */
260
+ const length = computed(() => view.value.state.doc.length);
261
+ /** Get CodeMirror Extension */
262
+ const extensions = computed(() =>
263
+ // TODO: Ignore previous prop was not changed.
264
+ compact([
265
+ // Toggle basic setup
266
+ props.basic ? basicSetup : undefined,
267
+ // Toggle minimal setup
268
+ props.minimal && !props.basic ? minimalSetup : undefined,
269
+ // ViewUpdate event listener
270
+ EditorView.updateListener.of((update) => context.emit('update', update)),
271
+ // Toggle light/dark mode.
272
+ EditorView.theme(props.theme, { dark: props.dark }),
273
+ // Toggle line wrapping
274
+ props.wrap ? EditorView.lineWrapping : undefined,
275
+ // Indent with tab
276
+ props.tab ? keymap.of([indentWithTab]) : undefined,
277
+ // Indent tab size
278
+ props.tabSize ? EditorState.tabSize.of(props.tabSize) : undefined,
279
+ // locale settings
280
+ props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
281
+ // Readonly option
282
+ EditorState.readOnly.of(props.readonly),
283
+ // Editable option
284
+ EditorView.editable.of(!props.disabled),
285
+ // Set Line break char
286
+ props.lineSeparator
287
+ ? EditorState.lineSeparator.of(props.lineSeparator)
288
+ : undefined,
289
+ // Lang
290
+ props.lang ? props.lang : undefined,
291
+ // Append Linter settings
292
+ props.linter ? linter(props.linter, props.linterConfig) : undefined,
293
+ // Show 🔴 to error line when linter enabled.
294
+ props.linter && props.gutter
295
+ ? lintGutter(props.gutterConfig)
296
+ : undefined,
297
+ // Placeholder
298
+ props.placeholder ? placeholder(props.placeholder) : undefined,
299
+ // Append Extensions (such as basic-setup)
300
+ ...props.extensions,
301
+ ]));
302
+ // for parent-to-child binding.
303
+ watch(() => props.modelValue, value => {
304
+ if (view.value.composing) {
305
+ // IME fix
306
+ return;
307
+ }
308
+ // Update
309
+ view.value.dispatch({
310
+ changes: { from: 0, to: view.value.state.doc.length, insert: value },
311
+ selection: view.value.state.selection,
312
+ scrollIntoView: true,
313
+ });
314
+ }, { immediate: true });
315
+ // Extension (mostly props) Changed
316
+ watch(extensions, exts =>
317
+ // TODO: Reduce unchanched value
318
+ view.value.dispatch({
319
+ effects: StateEffect.reconfigure.of(exts),
320
+ }));
321
+ // focus changed
322
+ watch(focus, isFocus => context.emit('focus', isFocus));
323
+ /** When loaded */
324
+ onMounted(async () => {
325
+ /** Initial value */
326
+ let value = doc.value;
327
+ if (editor.value && editor.value.childNodes[0]) {
328
+ // when slot mode, overwrite initial value
329
+ if (doc.value !== '') {
330
+ console.warn('[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values.');
331
+ }
332
+ value = trim(editor.value.childNodes[0].innerText);
333
+ }
334
+ // Register Codemirror
335
+ view.value = new EditorView({
336
+ doc: value,
337
+ extensions: extensions.value,
338
+ parent: editor.value,
339
+ dispatch: tr => {
340
+ view.value.update([tr]);
341
+ // TODO: Emit lint error event
342
+ // console.log(view.state.doc.toString(), tr);
343
+ if (tr.changes.empty)
344
+ return;
345
+ // child-to-parent binding
346
+ const v = view.value.state.doc.toString();
347
+ context.emit('update:modelValue', v);
348
+ context.emit('change', view.value.state);
349
+ },
350
+ });
351
+ await nextTick();
352
+ context.emit('ready', {
353
+ view: view,
354
+ state: view.value.state,
355
+ container: editor.value,
356
+ });
357
+ });
358
+ /** Destroy */
359
+ onUnmounted(() => {
360
+ if (view.value) {
361
+ view.value.destroy();
362
+ context.emit('destroy');
363
+ }
364
+ });
365
+ /**
366
+ * Forces any linters configured to run when the editor is idle to run right away.
367
+ *
368
+ * @see {@link https://codemirror.net/docs/ref/#lint.forceLinting}
369
+ */
370
+ const lint = () => {
371
+ if (props.linter) {
372
+ forceLinting(view.value);
373
+ }
374
+ };
375
+ /* ----- Bellow is experimental. ------ */
376
+ /**
377
+ * Get the text between the given points in the editor.
378
+ *
379
+ * @param from - start line number
380
+ * @param to - end line number
381
+ */
382
+ const getRange = (from, to) => view.value.state.sliceDoc(from, to);
383
+ /**
384
+ * Get the content of line.
385
+ *
386
+ * @param number - line number
387
+ */
388
+ const getLine = (number) => view.value.state.doc.line(number + 1).text;
389
+ /** Get the number of lines in the editor. */
390
+ const lineCount = () => view.value.state.doc.lines;
391
+ /** Retrieve one end of the primary selection. */
392
+ const getCursor = () => cursor.value;
393
+ /** Retrieves a list of all current selections. */
394
+ const listSelections = () => view.value.state.selection.ranges;
395
+ /** Get the currently selected code. */
396
+ const getSelection = () => view.value.state.sliceDoc(view.value.state.selection.main.from, view.value.state.selection.main.to);
397
+ /**
398
+ * The length of the given array should be the same as the number of active selections.
399
+ * Replaces the content of the selections with the strings in the array.
400
+ */
401
+ const getSelections = () => view.value.state.selection.ranges.map((r) => view.value.state.sliceDoc(r.from, r.to));
402
+ /** Return true if any text is selected. */
403
+ const somethingSelected = () => view.value.state.selection.ranges.some((r) => !r.empty);
404
+ /**
405
+ * Replace the part of the document between from and to with the given string.
406
+ *
407
+ * @param replacement - replacement text
408
+ * @param from - start string at position
409
+ * @param to - insert the string at position
410
+ */
411
+ const replaceRange = (replacement, from, to) => view.value.dispatch({
412
+ changes: { from, to, insert: replacement },
413
+ });
414
+ /**
415
+ * Replace the selection(s) with the given string.
416
+ * By default, the new selection ends up after the inserted text.
417
+ *
418
+ * @param replacement - replacement text
419
+ */
420
+ const replaceSelection = (replacement) => view.value.dispatch(view.value.state.replaceSelection(replacement));
421
+ /**
422
+ * Set the cursor position.
423
+ *
424
+ * @param position - position.
425
+ */
426
+ const setCursor = (position) => (cursor.value = position);
427
+ /**
428
+ * Set a single selection range.
429
+ *
430
+ * @param anchor - anchor position
431
+ * @param head -
432
+ */
433
+ const setSelection = (anchor, head) => view.value.dispatch({ selection: { anchor, head } });
434
+ /**
435
+ * Sets a new set of selections. There must be at least one selection in the given array.
436
+ *
437
+ * @param ranges - Selection range
438
+ * @param primary -
439
+ */
440
+ const setSelections = (ranges, primary) => view.value.dispatch({
441
+ selection: EditorSelection.create(ranges, primary),
442
+ });
443
+ /**
444
+ * Applies the given function to all existing selections, and calls extendSelections on the result.
445
+ *
446
+ * @param f - function
447
+ */
448
+ const extendSelectionsBy = (f) => view.value.dispatch({
449
+ selection: EditorSelection.create(selection.value.ranges.map((r) => r.extend(f(r)))),
450
+ });
451
+ /** Export properties and functions */
452
+ context.expose({
453
+ editor,
454
+ cursor,
455
+ selection,
456
+ state,
457
+ focus,
458
+ length,
459
+ lint,
460
+ // Bellow is CodeMirror5's function
461
+ getRange,
462
+ getLine,
463
+ lineCount,
464
+ getCursor,
465
+ listSelections,
466
+ getSelection,
467
+ getSelections,
468
+ somethingSelected,
469
+ replaceRange,
470
+ replaceSelection,
471
+ setCursor,
472
+ setSelection,
473
+ setSelections,
474
+ extendSelectionsBy,
475
+ });
476
+ return {
477
+ editor,
478
+ };
479
+ },
480
+ render() {
481
+ // <template>
482
+ // <div ref="editor" class="vue-codemirror">
483
+ // <aside v-show="!context.slots.default" aria-hidden><slot /></aside>
484
+ // </div>
485
+ // </template>
486
+ return h(this.$props.tag, {
487
+ ref: 'editor',
488
+ class: 'vue-codemirror',
489
+ }, this.$slots.default
490
+ ? // Hide original content
491
+ h('aside', { style: 'display: none;', 'aria-hidden': 'true' }, slot(this.$slots.default))
492
+ : undefined);
493
+ },
494
+ });
@@ -1,19 +1,19 @@
1
- /**
2
- * h-demi - h function for Vue 2 and 3
3
- *
4
- * @see {@link https://github.com/vueuse/vue-demi/issues/65}
5
- */
6
- import { type VNode } from 'vue-demi';
7
- interface Options {
8
- class?: string;
9
- domProps?: Record<any, any>;
10
- on?: Record<any, any>;
11
- props?: Record<any, any>;
12
- ref?: string;
13
- style?: string;
14
- 'aria-hidden'?: string;
15
- }
16
- declare const h: (type: string | Record<any, any>, options?: Options, chidren?: any) => VNode;
17
- declare const slot: (defaultSlots: any) => any;
18
- export { slot, h as default };
1
+ /**
2
+ * h-demi - h function for Vue 2 and 3
3
+ *
4
+ * @see {@link https://github.com/vueuse/vue-demi/issues/65}
5
+ */
6
+ import { type VNode } from 'vue-demi';
7
+ interface Options {
8
+ class?: string;
9
+ domProps?: Record<any, any>;
10
+ on?: Record<any, any>;
11
+ props?: Record<any, any>;
12
+ ref?: string;
13
+ style?: string;
14
+ 'aria-hidden'?: string;
15
+ }
16
+ declare const h: (type: string | Record<any, any>, options?: Options, chidren?: any) => VNode;
17
+ declare const slot: (defaultSlots: any) => any;
18
+ export { slot, h as default };
19
19
  //# sourceMappingURL=h-demi.d.ts.map
@@ -0,0 +1,27 @@
1
+ /**
2
+ * h-demi - h function for Vue 2 and 3
3
+ *
4
+ * @see {@link https://github.com/vueuse/vue-demi/issues/65}
5
+ */
6
+ import { h as hDemi, isVue2, Vue2 } from 'vue-demi';
7
+ const adaptOnsV3 = (ons) => {
8
+ if (!ons)
9
+ return {};
10
+ return Object.entries(ons).reduce((ret, [key, handler]) => {
11
+ key = key.charAt(0).toUpperCase() + key.slice(1);
12
+ key = `on${key}`;
13
+ return { ...ret, [key]: handler };
14
+ }, {});
15
+ };
16
+ const h = (type, options = {}, chidren) => {
17
+ if (isVue2 && parseInt(Vue2.version) < 2.7) {
18
+ // Makeshift support :(
19
+ // Since Vue2.7 includes the Composition API, the functions in vue-demi are not used.
20
+ return hDemi(type, options, chidren);
21
+ }
22
+ const { props, domProps, on, ...extraOptions } = options;
23
+ const ons = on ? adaptOnsV3(on) : {};
24
+ return hDemi(type, { ...extraOptions, ...props, ...domProps, ...ons }, chidren);
25
+ };
26
+ const slot = (defaultSlots) => typeof defaultSlots == 'function' ? defaultSlots() : defaultSlots;
27
+ export { slot, h as default };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import CodeMirror from './components/CodeMirror';
2
- import Meta from './Meta';
3
- declare const installCodeMirror: (app: any) => any;
4
- export { CodeMirror as default, installCodeMirror as install, Meta };
1
+ import CodeMirror from './components/CodeMirror';
2
+ import Meta from './Meta';
3
+ declare const installCodeMirror: (app: any) => any;
4
+ export { CodeMirror as default, installCodeMirror as install, Meta };
5
5
  //# sourceMappingURL=index.d.ts.map
package/dist/index.es.js CHANGED
@@ -5,12 +5,13 @@
5
5
  * @author Logue <logue@hotmail.co.jp>
6
6
  * @copyright 2022 By Masashi Yoshikawa All rights reserved.
7
7
  * @license MIT
8
- * @version 1.1.1
8
+ * @version 1.1.2
9
9
  * @see {@link https://github.com/logue/vue-codemirror6}
10
10
  */
11
11
 
12
12
  import { isVue2, Vue2, h as h$1, defineComponent, ref, shallowRef, computed, watch, onMounted, nextTick, onUnmounted } from 'vue-demi';
13
- import { compact, trim } from 'lodash';
13
+ import compact from 'lodash/compact';
14
+ import trim from 'lodash/trim';
14
15
  import { basicSetup, minimalSetup } from 'codemirror';
15
16
  import { EditorState, StateEffect, EditorSelection } from '@codemirror/state';
16
17
  import { EditorView, keymap, placeholder } from '@codemirror/view';
@@ -40,7 +41,7 @@ const h = (type, options = {}, chidren) => {
40
41
  };
41
42
  const slot = (defaultSlots) => typeof defaultSlots == "function" ? defaultSlots() : defaultSlots;
42
43
 
43
- const CodeMirror = defineComponent({
44
+ var CodeMirror = defineComponent({
44
45
  name: "CodeMirror",
45
46
  model: {
46
47
  prop: "modelValue",
@@ -133,7 +134,7 @@ const CodeMirror = defineComponent({
133
134
  default: "div"
134
135
  }
135
136
  },
136
- emits: ["update:modelValue", "update", "ready", "focus", "change"],
137
+ emits: ["update:modelValue", "update", "ready", "focus", "change", "destroy"],
137
138
  setup(props, context) {
138
139
  const editor = ref();
139
140
  const doc = ref(props.modelValue);
@@ -207,7 +208,11 @@ const CodeMirror = defineComponent({
207
208
  onMounted(async () => {
208
209
  let value = doc.value;
209
210
  if (editor.value && editor.value.childNodes[0]) {
210
- if (doc.value !== "") ;
211
+ if (doc.value !== "") {
212
+ console.warn(
213
+ "[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."
214
+ );
215
+ }
211
216
  value = trim(editor.value.childNodes[0].innerText);
212
217
  }
213
218
  view.value = new EditorView({
@@ -220,20 +225,27 @@ const CodeMirror = defineComponent({
220
225
  return;
221
226
  const v = view.value.state.doc.toString();
222
227
  context.emit("update:modelValue", v);
223
- context.emit("change", v);
228
+ context.emit("change", view.value.state);
224
229
  }
225
230
  });
226
231
  await nextTick();
227
232
  context.emit("ready", {
228
233
  view,
229
- state: state.value,
234
+ state: view.value.state,
230
235
  container: editor.value
231
236
  });
232
237
  });
233
238
  onUnmounted(() => {
234
- view.value.destroy();
239
+ if (view.value) {
240
+ view.value.destroy();
241
+ context.emit("destroy");
242
+ }
235
243
  });
236
- const lint = () => forceLinting(view.value);
244
+ const lint = () => {
245
+ if (props.linter) {
246
+ forceLinting(view.value);
247
+ }
248
+ };
237
249
  const getRange = (from, to) => view.value.state.sliceDoc(from, to);
238
250
  const getLine = (number) => view.value.state.doc.line(number + 1).text;
239
251
  const lineCount = () => view.value.state.doc.lines;
@@ -263,8 +275,7 @@ const CodeMirror = defineComponent({
263
275
  selection.value.ranges.map((r) => r.extend(f(r)))
264
276
  )
265
277
  });
266
- return {
267
- context,
278
+ context.expose({
268
279
  editor,
269
280
  cursor,
270
281
  selection,
@@ -286,6 +297,9 @@ const CodeMirror = defineComponent({
286
297
  setSelection,
287
298
  setSelections,
288
299
  extendSelectionsBy
300
+ });
301
+ return {
302
+ editor
289
303
  };
290
304
  },
291
305
  render() {
@@ -305,8 +319,8 @@ const CodeMirror = defineComponent({
305
319
  });
306
320
 
307
321
  const meta = {
308
- version: "1.1.1",
309
- date: "2022-11-07T00:40:00.830Z"
322
+ version: "1.1.2",
323
+ date: "2022-12-23T18:38:01.605Z"
310
324
  };
311
325
 
312
326
  const installCodeMirror = (app) => app.component("CodeMirror", CodeMirror);