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