vue-codemirror6 1.3.22 → 1.4.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.
@@ -1,816 +0,0 @@
1
- import { indentWithTab } from '@codemirror/commands';
2
- import { indentUnit, type LanguageSupport } from '@codemirror/language';
3
- import {
4
- diagnosticCount as linterDiagnosticCount,
5
- forceLinting,
6
- linter,
7
- lintGutter,
8
- type Diagnostic,
9
- type LintSource,
10
- } from '@codemirror/lint';
11
- import {
12
- Compartment,
13
- EditorSelection,
14
- EditorState,
15
- StateEffect,
16
- type Transaction,
17
- type Extension,
18
- type SelectionRange,
19
- type StateField,
20
- type Text,
21
- } from '@codemirror/state';
22
- import {
23
- EditorView,
24
- keymap,
25
- placeholder,
26
- type KeyBinding,
27
- type ViewUpdate,
28
- } from '@codemirror/view';
29
- import { basicSetup, minimalSetup } from 'codemirror';
30
- import {
31
- computed,
32
- defineComponent,
33
- nextTick,
34
- onMounted,
35
- onUnmounted,
36
- ref,
37
- shallowRef,
38
- watch,
39
- type ComputedRef,
40
- type PropType,
41
- type Ref,
42
- type ShallowRef,
43
- type WritableComputedRef,
44
- } from 'vue-demi';
45
-
46
- import type { StyleSpec } from 'style-mod';
47
-
48
- import h, { slot } from '@/helpers/h-demi';
49
-
50
- /** CodeMirror Component */
51
- export default defineComponent({
52
- /** Component Name */
53
- name: 'CodeMirror',
54
- /** Model Definition */
55
- model: {
56
- prop: 'modelValue',
57
- event: 'update:modelValue',
58
- },
59
- /** Props Definition */
60
- props: {
61
- /** Model value */
62
- modelValue: {
63
- type: String as PropType<string | Text>,
64
- default: '',
65
- },
66
- /**
67
- * Theme
68
- *
69
- * @see {@link https://codemirror.net/docs/ref/#view.EditorView^theme}
70
- */
71
- theme: {
72
- type: Object as PropType<Record<string, StyleSpec>>,
73
- default: () => {
74
- return {};
75
- },
76
- },
77
- /** Dark Mode */
78
- dark: {
79
- type: Boolean,
80
- default: false,
81
- },
82
- /**
83
- * Use Basic Setup
84
- *
85
- * @see {@link https://codemirror.net/docs/ref/#codemirror.basicSetup}
86
- */
87
- basic: {
88
- type: Boolean,
89
- default: false,
90
- },
91
- /**
92
- * Use Minimal Setup (The basic setting has priority.)
93
- *
94
- * @see {@link https://codemirror.net/docs/ref/#codemirror.minimalSetup}
95
- */
96
- minimal: {
97
- type: Boolean,
98
- default: false,
99
- },
100
- /**
101
- * Placeholder
102
- *
103
- * @see {@link https://codemirror.net/docs/ref/#view.placeholder}
104
- */
105
- placeholder: {
106
- type: String as PropType<string | HTMLElement>,
107
- default: undefined,
108
- },
109
- /**
110
- * Line wrapping
111
- *
112
- * An extension that enables line wrapping in the editor (by setting CSS white-space to pre-wrap in the content).
113
- *
114
- * @see {@link https://codemirror.net/docs/ref/#view.EditorView%5ElineWrapping}
115
- */
116
- wrap: {
117
- type: Boolean,
118
- default: false,
119
- },
120
- /**
121
- * Allow tab key indent.
122
- *
123
- * @see {@link https://codemirror.net/examples/tab/}
124
- */
125
- tab: {
126
- type: Boolean,
127
- default: false,
128
- },
129
- /**
130
- * Tab character
131
- * This is the unit of indentation used when the editor is configured to indent with tabs.
132
- * It is also used to determine the size of the tab character when the editor is configured to use tabs for indentation..
133
- *
134
- * @see {@link https://codemirror.net/docs/ref/#state.EditorState^indentUnit}
135
- */
136
- indentUnit: {
137
- type: String,
138
- default: undefined,
139
- },
140
- /**
141
- * Allow Multiple Selection.
142
- * This allows the editor to have multiple selections at the same time.
143
- * This is useful for editing multiple parts of the document at once.
144
- * If this is set to true, the editor will allow multiple selections.
145
- * If this is set to false, the editor will only allow a single selection.
146
- *
147
- * @see {@link https://codemirror.net/docs/ref/#state.EditorState^allowMultipleSelections}
148
- */
149
- allowMultipleSelections: {
150
- type: Boolean,
151
- default: false,
152
- },
153
- /**
154
- * Tab size
155
- * This is the number of spaces that a tab character represents in the editor.
156
- * It is used to determine the size of the tab character when the editor is configured to use tabs for indentation.
157
- * If this is set to a number, the editor will use that number of spaces for each tab character.
158
- *
159
- * @see {@link https://codemirror.net/docs/ref/#state.EditorState^tabSize}
160
- */
161
- tabSize: {
162
- type: Number,
163
- default: undefined,
164
- },
165
- /**
166
- * Set line break (separetor) char.
167
- *
168
- * This is the character that is used to separate lines in the editor.
169
- * It is used to determine the line break character when the editor is configured to use a specific line break character.
170
- *
171
- * @see {@link https://codemirror.net/docs/ref/#state.EditorState^lineSeparator}
172
- */
173
- lineSeparator: {
174
- type: String,
175
- default: undefined,
176
- },
177
- /**
178
- * Readonly
179
- *
180
- * This is a CodeMirror Facet that allows you to set the editor to read-only mode.
181
- * When this is set to true, the editor will not allow any changes to be made to the document.
182
- * This is useful for displaying code that should not be edited, such as documentation or examples.
183
- * If this is set to false, the editor will allow changes to be made to the document.
184
- *
185
- * @see {@link https://codemirror.net/docs/ref/#state.EditorState^readOnly}
186
- */
187
- readonly: {
188
- type: Boolean,
189
- default: false,
190
- },
191
- /**
192
- * Disable input.
193
- *
194
- * This is the reversed value of the CodeMirror editable.
195
- * Similar to `readonly`, but setting this value to true disables dragging.
196
- *
197
- * @see {@link https://codemirror.net/docs/ref/#view.EditorView^editable}
198
- */
199
- disabled: {
200
- type: Boolean,
201
- default: false,
202
- },
203
- /**
204
- * Additional Extension
205
- *
206
- * You can use this to add any additional extensions that you want to use in the editor.
207
- *
208
- * @see {@link https://codemirror.net/docs/ref/#state.Extension}
209
- */
210
- extensions: {
211
- type: Array as PropType<Extension[]>,
212
- default: () => {
213
- return [];
214
- },
215
- },
216
- /**
217
- * Language Phreses
218
- *
219
- * This is a CodeMirror Facet that allows you to define custom phrases for the editor.
220
- * It can be used to override default phrases or add new ones.
221
- * This is useful for translating the editor to different languages or for customizing the editor's UI.
222
- *
223
- * @see {@link https://codemirror.net/examples/translate/}
224
- */
225
- phrases: {
226
- type: Object as PropType<Record<string, string>>,
227
- default: undefined,
228
- },
229
- /**
230
- * CodeMirror Language
231
- *
232
- * This is a CodeMirror Facet that allows you to define the language of the editor.
233
- * It can be used to enable syntax highlighting and other language-specific features.
234
- * It is useful for displaying code in a specific language, such as JavaScript, Python, or HTML.
235
- *
236
- * @see {@link https://codemirror.net/docs/ref/#language}
237
- */
238
- lang: {
239
- type: Object as PropType<LanguageSupport>,
240
- default: undefined,
241
- },
242
- /**
243
- * CodeMirror Linter
244
- *
245
- * This is a CodeMirror Facet that allows you to define a linter for the editor.
246
- * It can be used to check the code for errors and warnings, and to provide feedback to the user.
247
- * It is useful for displaying code in a specific language, such as JavaScript, Python, or HTML.
248
- * This is useful for providing feedback to the user about the code they are writing.
249
- *
250
- * @see {@link https://codemirror.net/docs/ref/#lint.linter}
251
- */
252
- linter: {
253
- type: Function as PropType<LintSource | any>,
254
- default: undefined,
255
- },
256
- /**
257
- * Linter Config
258
- *
259
- * This is a CodeMirror Facet that allows you to define the configuration for the linter.
260
- * It can be used to specify options for the linter, such as the severity of errors and warnings, and to customize the behavior of the linter.
261
- * This is useful for providing feedback to the user about the code they are writing.
262
- *
263
- * @see {@link https://codemirror.net/docs/ref/#lint.linter^config}
264
- */
265
- linterConfig: {
266
- type: Object,
267
- default: () => {
268
- return {};
269
- },
270
- },
271
- /**
272
- * Forces any linters configured to run when the editor is idle to run right away.
273
- *
274
- * This is useful for running linters on the initial load of the editor, or when the user has made changes to the code and wants to see the results immediately.
275
- *
276
- * @see {@link https://codemirror.net/docs/ref/#lint.forceLinting}
277
- */
278
- forceLinting: {
279
- type: Boolean,
280
- default: false,
281
- },
282
- /**
283
- * Show Linter Gutter
284
- *
285
- * An area to 🔴 the lines with errors will be displayed.
286
- * This feature is not enabled if `linter` is not specified.
287
- *
288
- * @see {@link https://codemirror.net/docs/ref/#lint.lintGutter}
289
- */
290
- gutter: {
291
- type: Boolean,
292
- default: false,
293
- },
294
- /**
295
- * Gutter Config
296
- *
297
- * This is a CodeMirror Facet that allows you to define the configuration for the gutter.
298
- * It can be used to specify options for the gutter, such as the size of the gutter, the position of the gutter, and to customize the behavior of the gutter.
299
- * This is useful for providing feedback to the user about the code they are writing.
300
- *
301
- * @see {@link https://codemirror.net/docs/ref/#lint.lintGutter^config}
302
- */
303
- gutterConfig: {
304
- type: Object,
305
- default: undefined,
306
- },
307
- /**
308
- * Using tag
309
- */
310
- tag: {
311
- type: String,
312
- default: 'div',
313
- },
314
- /**
315
- * Allows an external update to scroll the form.
316
- *
317
- * This is useful for scrolling the editor to a specific position when the user has made changes to the code and wants to see the results immediately.
318
- * If this is set to true, the editor will scroll to the position specified in the transaction.
319
- * If this is set to false, the editor will not scroll to the position specified in the transaction.
320
- *
321
- * @see {@link https://codemirror.net/docs/ref/#state.TransactionSpec.scrollIntoView}
322
- */
323
- scrollIntoView: {
324
- type: Boolean,
325
- default: true,
326
- },
327
- /**
328
- * Key map
329
- * This is a CodeMirror Facet that allows you to define custom key bindings.
330
- * It can be used to override default key bindings or add new ones.
331
- *
332
- * @see {@link https://codemirror.net/docs/ref/#view.keymap}
333
- */
334
- keymap: {
335
- type: Array as PropType<KeyBinding[]>,
336
- default: () => [],
337
- },
338
- },
339
- /** Emits */
340
- emits: {
341
- /** Model Update */
342
- 'update:modelValue': (_value: string | Text = '') => true,
343
- /** CodeMirror ViewUpdate */
344
- update: (_value: ViewUpdate) => true,
345
- /** CodeMirror onReady */
346
- ready: (_value: {
347
- view: EditorView;
348
- state: EditorState;
349
- container: HTMLElement;
350
- }) => true,
351
- /** CodeMirror onFocus */
352
- focus: (_value: boolean) => true,
353
- /** State Changed */
354
- change: (_value: EditorState) => true,
355
- /** CodeMirror onDestroy */
356
- destroy: () => true,
357
- },
358
- /**
359
- * Setup
360
- *
361
- * @param props - Props
362
- * @param context - Context
363
- */
364
- setup(props, context) {
365
- /** Editor DOM */
366
- const editor: Ref<HTMLElement | undefined> = ref();
367
-
368
- /** Internal value */
369
- const doc: Ref<string | Text> = ref(props.modelValue);
370
-
371
- /**
372
- * CodeMirror Editor View
373
- *
374
- * @see {@link https://codemirror.net/docs/ref/#view.EditorView}
375
- */
376
- const view: ShallowRef<EditorView> = shallowRef(new EditorView());
377
-
378
- /**
379
- * Focus
380
- *
381
- * @see {@link https://codemirror.net/docs/ref/#view.EditorView.hasFocus}
382
- */
383
- const focus: WritableComputedRef<boolean> = computed({
384
- get: () => view.value.hasFocus,
385
- set: f => {
386
- if (f) {
387
- view.value.focus();
388
- }
389
- },
390
- });
391
-
392
- /**
393
- * Editor Selection
394
- *
395
- * @see {@link https://codemirror.net/docs/ref/#state.EditorSelection}
396
- */
397
- const selection: WritableComputedRef<EditorSelection> = computed({
398
- get: () => view.value.state.selection,
399
- set: s => view.value.dispatch({ selection: s }),
400
- });
401
-
402
- /** Cursor Position */
403
- const cursor: WritableComputedRef<number> = computed({
404
- get: () => view.value.state.selection.main.head,
405
- set: a => view.value.dispatch({ selection: { anchor: a } }),
406
- });
407
-
408
- /** JSON */
409
- const json: WritableComputedRef<Record<string, StateField<any>>> = computed(
410
- {
411
- get: () => view.value.state.toJSON(),
412
- set: j => view.value.setState(EditorState.fromJSON(j)),
413
- }
414
- );
415
-
416
- /** Text length */
417
- const length: Ref<number> = ref(0);
418
-
419
- /**
420
- * Returns the number of active lint diagnostics in the given state.
421
- *
422
- * @see {@link https://codemirror.net/docs/ref/#lint.diagnosticCount}
423
- */
424
- const diagnosticCount: Ref<number> = ref(0);
425
-
426
- /** Get CodeMirror Extension */
427
- const extensions: ComputedRef<Extension[]> = computed(() => {
428
- // Synamic Reconfiguration
429
- // @see https://codemirror.net/examples/config/
430
- const language = new Compartment();
431
- const tabSize = new Compartment();
432
- if (props.basic && props.minimal) {
433
- throw new Error(
434
- '[Vue CodeMirror] Both basic and minimal cannot be specified.'
435
- );
436
- }
437
- /** Keymap */
438
- let keymaps: KeyBinding[] = [];
439
- if (props.keymap && props.keymap.length > 0) {
440
- // If keymap is specified, use it.
441
- keymaps = props.keymap;
442
- }
443
- if (props.tab) {
444
- // If tab is enabled, add indentWithTab to keymap.
445
- keymaps.push(indentWithTab);
446
- }
447
-
448
- // TODO: Ignore previous prop was not changed.
449
- return [
450
- // Toggle basic setup
451
- props.basic && !props.minimal ? basicSetup : undefined,
452
- // Toggle minimal setup
453
- props.minimal && !props.basic ? minimalSetup : undefined,
454
- // ViewUpdate event listener
455
- EditorView.updateListener.of((update: ViewUpdate): void => {
456
- // Emit focus status
457
- context.emit('focus', view.value.hasFocus);
458
-
459
- // Update count
460
- length.value = view.value.state.doc?.length;
461
-
462
- if (update.changes.empty || !update.docChanged) {
463
- // Suppress event firing if no change
464
- return;
465
- }
466
- if (props.linter) {
467
- // Linter process
468
- if (props.forceLinting) {
469
- // If forceLinting enabled, first liting.
470
- forceLinting(view.value);
471
- }
472
- // Count diagnostics.
473
- diagnosticCount.value = (
474
- props.linter(view.value) as readonly Diagnostic[]
475
- ).length;
476
- }
477
- context.emit('update', update);
478
- }),
479
- // Toggle light/dark mode.
480
- EditorView.theme(props.theme, { dark: props.dark }),
481
- // Toggle line wrapping
482
- props.wrap ? EditorView.lineWrapping : undefined,
483
- // Tab character
484
- props.indentUnit ? indentUnit.of(props.indentUnit) : undefined,
485
- // Allow Multiple Selections
486
- EditorState.allowMultipleSelections.of(props.allowMultipleSelections),
487
- // Indent tab size
488
- props.tabSize
489
- ? tabSize.of(EditorState.tabSize.of(props.tabSize))
490
- : undefined,
491
- // locale settings
492
- props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
493
- // Readonly option
494
- EditorState.readOnly.of(props.readonly),
495
- // Editable option
496
- EditorView.editable.of(!props.disabled),
497
- // Set Line break char
498
- props.lineSeparator
499
- ? EditorState.lineSeparator.of(props.lineSeparator)
500
- : undefined,
501
- // Lang
502
- props.lang ? language.of(props.lang) : undefined,
503
- // Append Linter settings
504
- props.linter ? linter(props.linter, props.linterConfig) : undefined,
505
- // Show 🔴 to error line when linter enabled.
506
- props.linter && props.gutter
507
- ? lintGutter(props.gutterConfig)
508
- : undefined,
509
- // Placeholder
510
- props.placeholder ? placeholder(props.placeholder) : undefined,
511
- // Keymap and Indent with Tab
512
- keymaps.length !== 0 ? keymap.of(keymaps) : undefined,
513
- // Append Extensions
514
- ...props.extensions,
515
- ].filter((extension): extension is Extension => !!extension); // Filter undefined
516
- });
517
-
518
- // Extension (mostly props) Changed
519
- watch(
520
- extensions,
521
- exts =>
522
- view.value?.dispatch({ effects: StateEffect.reconfigure.of(exts) }),
523
- { immediate: true }
524
- );
525
-
526
- // for parent-to-child binding.
527
- watch(
528
- () => props.modelValue,
529
- async value => {
530
- if (
531
- view.value.composing || // IME fix
532
- view.value.state.doc.toJSON().join(props.lineSeparator ?? '\n') ===
533
- value // don't need to update
534
- ) {
535
- // Do not commit CodeMirror's store.
536
- return;
537
- }
538
-
539
- // Range Fix ?
540
- // https://github.com/logue/vue-codemirror6/issues/27
541
- const isSelectionOutOfRange = !view.value.state.selection.ranges.every(
542
- range => range.anchor < value.length && range.head < value.length
543
- );
544
-
545
- // Update
546
- view.value.dispatch({
547
- changes: { from: 0, to: view.value.state.doc.length, insert: value },
548
- selection: isSelectionOutOfRange
549
- ? { anchor: 0, head: 0 }
550
- : view.value.state.selection,
551
- scrollIntoView: props.scrollIntoView,
552
- });
553
- },
554
- { immediate: true }
555
- );
556
-
557
- /** When loaded */
558
- onMounted(async () => {
559
- /** Initial value */
560
- let value: string | Text = doc.value;
561
- if (!editor.value) {
562
- return;
563
- }
564
- if (editor.value.childNodes[0]) {
565
- // when slot mode, overwrite initial value
566
- if (doc.value !== '') {
567
- console.warn(
568
- '[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values.'
569
- );
570
- }
571
- value = (editor.value.childNodes[0] as HTMLElement).innerText.trim();
572
- }
573
-
574
- // Register Codemirror
575
- view.value = new EditorView({
576
- parent: editor.value,
577
- state: EditorState.create({ doc: value, extensions: extensions.value }),
578
- dispatch: (tr: Transaction) => {
579
- view.value.update([tr]);
580
- if (tr.changes.empty || !tr.docChanged) {
581
- // if not change value, no fire emit event
582
- return;
583
- }
584
-
585
- // console.log(view.state.doc.toString(), tr);
586
- // state.toString() is not defined, so use toJSON and toText function to convert string.
587
- context.emit('update:modelValue', tr.state.doc.toString());
588
- // Emit EditorState
589
- context.emit('change', tr.state);
590
- },
591
- });
592
-
593
- await nextTick();
594
-
595
- context.emit('ready', {
596
- view: view.value,
597
- state: view.value.state,
598
- container: editor.value,
599
- });
600
- });
601
-
602
- /** Destroy */
603
- onUnmounted(() => {
604
- view.value.destroy();
605
- context.emit('destroy');
606
- });
607
-
608
- /**
609
- * Forces any linters configured to run when the editor is idle to run right away.
610
- *
611
- * @see {@link https://codemirror.net/docs/ref/#lint.forceLinting}
612
- */
613
- const lint = (): void => {
614
- if (!props.linter || !view.value) {
615
- return;
616
- }
617
- if (props.forceLinting) {
618
- forceLinting(view.value);
619
- }
620
- diagnosticCount.value = linterDiagnosticCount(view.value.state);
621
- };
622
-
623
- /**
624
- * Force Reconfigure Extension
625
- *
626
- * @see {@link https://codemirror.net/examples/config/#top-level-reconfiguration}
627
- */
628
- const forceReconfigure = (): void => {
629
- // Deconfigure all Extensions
630
- view.value?.dispatch({
631
- effects: StateEffect.reconfigure.of([]),
632
- });
633
- // Register extensions
634
- view.value?.dispatch({
635
- effects: StateEffect.appendConfig.of(extensions.value),
636
- });
637
- };
638
-
639
- /* ----- Bellow is experimental. ------ */
640
-
641
- /**
642
- * Get the text between the given points in the editor.
643
- *
644
- * @param from - start line number
645
- * @param to - end line number
646
- */
647
- const getRange = (from?: number, to?: number): string | undefined =>
648
- view.value.state.sliceDoc(from, to);
649
- /**
650
- * Get the content of line.
651
- *
652
- * @param number - line number
653
- */
654
- const getLine = (number: number): string =>
655
- view.value.state.doc.line(number + 1).text;
656
- /** Get the number of lines in the editor. */
657
- const lineCount = (): number => view.value.state.doc.lines;
658
- /** Retrieve one end of the primary selection. */
659
- const getCursor = (): number => view.value.state.selection.main.head;
660
- /** Retrieves a list of all current selections. */
661
- const listSelections = (): readonly SelectionRange[] => {
662
- let _view$value$state$sel;
663
- return (_view$value$state$sel = view.value.state.selection.ranges) !==
664
- null && _view$value$state$sel !== undefined
665
- ? _view$value$state$sel
666
- : [];
667
- };
668
- /** Get the currently selected code. */
669
- const getSelection = (): string => {
670
- let _view$value$state$sli;
671
- return (_view$value$state$sli = view.value.state.sliceDoc(
672
- view.value.state.selection.main.from,
673
- view.value.state.selection.main.to
674
- )) !== null && _view$value$state$sli !== undefined
675
- ? _view$value$state$sli
676
- : '';
677
- };
678
- /**
679
- * The length of the given array should be the same as the number of active selections.
680
- * Replaces the content of the selections with the strings in the array.
681
- */
682
- const getSelections = (): string[] => {
683
- const s = view.value.state;
684
- if (!s) {
685
- return [];
686
- }
687
-
688
- return s.selection.ranges.map((r: { from: number; to: number }) =>
689
- s.sliceDoc(r.from, r.to)
690
- );
691
- };
692
- /** Return true if any text is selected. */
693
- const somethingSelected = (): boolean =>
694
- view.value.state.selection.ranges.some(
695
- (r: { empty: boolean }) => !r.empty
696
- );
697
-
698
- /**
699
- * Replace the part of the document between from and to with the given string.
700
- *
701
- * @param replacement - replacement text
702
- * @param from - start string at position
703
- * @param to - insert the string at position
704
- */
705
- const replaceRange = (
706
- replacement: string | Text,
707
- from: number,
708
- to: number
709
- ): void =>
710
- view.value.dispatch({
711
- changes: { from, to, insert: replacement },
712
- });
713
- /**
714
- * Replace the selection(s) with the given string.
715
- * By default, the new selection ends up after the inserted text.
716
- *
717
- * @param replacement - replacement text
718
- */
719
- const replaceSelection = (replacement: string | Text): void =>
720
- view.value.dispatch(view.value.state.replaceSelection(replacement));
721
- /**
722
- * Set the cursor position.
723
- *
724
- * @param position - position.
725
- */
726
- const setCursor = (position: number): void =>
727
- view.value.dispatch({ selection: { anchor: position } });
728
- /**
729
- * Set a single selection range.
730
- *
731
- * @param anchor - anchor position
732
- * @param head -
733
- */
734
- const setSelection = (anchor: number, head?: number): void =>
735
- view.value.dispatch({ selection: { anchor, head } });
736
- /**
737
- * Sets a new set of selections. There must be at least one selection in the given array.
738
- *
739
- * @param ranges - Selection range
740
- * @param primary -
741
- */
742
- const setSelections = (
743
- ranges: readonly SelectionRange[],
744
- primary?: number
745
- ): void =>
746
- view.value.dispatch({
747
- selection: EditorSelection.create(ranges, primary),
748
- });
749
- /**
750
- * Applies the given function to all existing selections, and calls extendSelections on the result.
751
- *
752
- * @param f - function
753
- */
754
- const extendSelectionsBy = (f: any): void =>
755
- view.value.dispatch({
756
- selection: EditorSelection.create(
757
- selection.value.ranges.map((r: SelectionRange) => r.extend(f(r)))
758
- ),
759
- });
760
-
761
- const exposed = {
762
- editor,
763
- view,
764
- cursor,
765
- selection,
766
- focus,
767
- length,
768
- json,
769
- diagnosticCount,
770
- dom: view.value.contentDOM,
771
- lint,
772
- forceReconfigure,
773
- // Bellow is CodeMirror5's function
774
- getRange,
775
- getLine,
776
- lineCount,
777
- getCursor,
778
- listSelections,
779
- getSelection,
780
- getSelections,
781
- somethingSelected,
782
- replaceRange,
783
- replaceSelection,
784
- setCursor,
785
- setSelection,
786
- setSelections,
787
- extendSelectionsBy,
788
- };
789
-
790
- /** Export properties and functions */
791
- context.expose(exposed);
792
- return exposed;
793
- },
794
- render() {
795
- // <template>
796
- // <div ref="editor" class="vue-codemirror">
797
- // <aside v-show="!context.slots.default" aria-hidden><slot /></aside>
798
- // </div>
799
- // </template>
800
- return h(
801
- this.$props.tag,
802
- {
803
- ref: 'editor',
804
- class: 'vue-codemirror',
805
- },
806
- this.$slots.default
807
- ? // Hide original content
808
- h(
809
- 'aside',
810
- { style: 'display: none;', 'aria-hidden': 'true' },
811
- slot(this.$slots.default)
812
- )
813
- : undefined
814
- );
815
- },
816
- });