vue-codemirror6 1.1.16 → 1.1.19
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/CHANGELOG.md +8 -0
- package/README.md +31 -11
- package/dist/index.es.js +97 -66
- package/dist/index.iife.js +97 -65
- package/dist/index.umd.js +99 -67
- package/dist/src/components/CodeMirror.d.ts +474 -16
- package/dist/src/helpers/h-demi.d.ts +6 -6
- package/package.json +19 -19
- package/src/components/CodeMirror.ts +144 -117
|
@@ -8,68 +8,46 @@ import {
|
|
|
8
8
|
shallowRef,
|
|
9
9
|
watch,
|
|
10
10
|
type ComputedRef,
|
|
11
|
-
type ObjectEmitsOptions,
|
|
12
11
|
type PropType,
|
|
13
12
|
type Ref,
|
|
14
|
-
type SetupContext,
|
|
15
13
|
type ShallowRef,
|
|
16
14
|
type WritableComputedRef,
|
|
17
15
|
} from 'vue-demi';
|
|
18
16
|
|
|
19
17
|
// Helpers
|
|
20
18
|
import h, { slot } from '@/helpers/h-demi';
|
|
21
|
-
import { compact, trim } from 'lodash';
|
|
22
19
|
|
|
23
20
|
// CodeMirror
|
|
24
21
|
import { basicSetup, minimalSetup } from 'codemirror';
|
|
22
|
+
import {
|
|
23
|
+
EditorView,
|
|
24
|
+
keymap,
|
|
25
|
+
placeholder,
|
|
26
|
+
type ViewUpdate,
|
|
27
|
+
} from '@codemirror/view';
|
|
25
28
|
import {
|
|
26
29
|
Compartment,
|
|
27
30
|
EditorSelection,
|
|
28
31
|
EditorState,
|
|
29
32
|
StateEffect,
|
|
33
|
+
type Transaction,
|
|
30
34
|
type Extension,
|
|
31
35
|
type SelectionRange,
|
|
36
|
+
type StateField,
|
|
32
37
|
type Text,
|
|
33
38
|
} from '@codemirror/state';
|
|
34
39
|
import {
|
|
35
|
-
|
|
36
|
-
keymap,
|
|
37
|
-
placeholder,
|
|
38
|
-
type ViewUpdate,
|
|
39
|
-
} from '@codemirror/view';
|
|
40
|
-
import { indentWithTab } from '@codemirror/commands';
|
|
41
|
-
import {
|
|
40
|
+
diagnosticCount as linterDagnosticCount,
|
|
42
41
|
forceLinting,
|
|
43
42
|
linter,
|
|
44
43
|
lintGutter,
|
|
44
|
+
type Diagnostic,
|
|
45
45
|
type LintSource,
|
|
46
46
|
} from '@codemirror/lint';
|
|
47
|
+
import { indentWithTab } from '@codemirror/commands';
|
|
47
48
|
import type { LanguageSupport } from '@codemirror/language';
|
|
48
49
|
import type { StyleSpec } from 'style-mod';
|
|
49
50
|
|
|
50
|
-
/** Emit Interface */
|
|
51
|
-
export interface CodeMirrorEmitsOptions extends ObjectEmitsOptions {
|
|
52
|
-
/** Model Update */
|
|
53
|
-
(e: 'update:modelValue', value: string | Text): void;
|
|
54
|
-
/** CodeMirror ViewUpdate */
|
|
55
|
-
(e: 'update', value: ViewUpdate): void;
|
|
56
|
-
/** CodeMirror onFocus */
|
|
57
|
-
(e: 'focus', value: boolean): void;
|
|
58
|
-
/** CodeMirror onReady */
|
|
59
|
-
(
|
|
60
|
-
e: 'ready',
|
|
61
|
-
value: {
|
|
62
|
-
view: EditorView;
|
|
63
|
-
state: EditorState;
|
|
64
|
-
container: HTMLElement;
|
|
65
|
-
}
|
|
66
|
-
): void;
|
|
67
|
-
/** CodeMirror onDestroy */
|
|
68
|
-
(e: 'destroy'): void;
|
|
69
|
-
/** State Changed */
|
|
70
|
-
(e: 'change', value: EditorState): void;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
51
|
/** CodeMirror Component */
|
|
74
52
|
export default defineComponent({
|
|
75
53
|
/** Component Name */
|
|
@@ -240,7 +218,9 @@ export default defineComponent({
|
|
|
240
218
|
*/
|
|
241
219
|
linterConfig: {
|
|
242
220
|
type: Object,
|
|
243
|
-
default: () =>
|
|
221
|
+
default: () => {
|
|
222
|
+
return { needsRefresh: true };
|
|
223
|
+
},
|
|
244
224
|
},
|
|
245
225
|
/**
|
|
246
226
|
* Forces any linters configured to run when the editor is idle to run right away.
|
|
@@ -281,15 +261,31 @@ export default defineComponent({
|
|
|
281
261
|
},
|
|
282
262
|
},
|
|
283
263
|
/** Emits */
|
|
284
|
-
emits:
|
|
264
|
+
emits: {
|
|
265
|
+
/** Model Update */
|
|
266
|
+
'update:modelValue': (value: string | Text) => true,
|
|
267
|
+
/** CodeMirror ViewUpdate */
|
|
268
|
+
update: (value: ViewUpdate) => true,
|
|
269
|
+
/** CodeMirror onReady */
|
|
270
|
+
ready: (value: {
|
|
271
|
+
view: EditorView;
|
|
272
|
+
state: EditorState;
|
|
273
|
+
container: HTMLElement;
|
|
274
|
+
}) => true,
|
|
275
|
+
/** CodeMirror onFocus */
|
|
276
|
+
focus: (value: boolean) => true,
|
|
277
|
+
/** State Changed */
|
|
278
|
+
change: (value: EditorState) => true,
|
|
279
|
+
/** CodeMirror onDestroy */
|
|
280
|
+
destroy: () => true,
|
|
281
|
+
},
|
|
285
282
|
/**
|
|
286
283
|
* Setup
|
|
287
284
|
*
|
|
288
285
|
* @param props - Props
|
|
289
286
|
* @param context - Context
|
|
290
287
|
*/
|
|
291
|
-
|
|
292
|
-
setup(props, context: SetupContext<CodeMirrorEmitsOptions>) {
|
|
288
|
+
setup(props, context) {
|
|
293
289
|
/** Editor DOM */
|
|
294
290
|
const editor: Ref<HTMLElement | undefined> = ref();
|
|
295
291
|
|
|
@@ -304,23 +300,27 @@ export default defineComponent({
|
|
|
304
300
|
const view: ShallowRef<EditorView> = shallowRef(new EditorView());
|
|
305
301
|
|
|
306
302
|
/**
|
|
307
|
-
*
|
|
303
|
+
* Focus
|
|
308
304
|
*
|
|
309
|
-
* @see {@link https://codemirror.net/docs/ref/#
|
|
305
|
+
* @see {@link https://codemirror.net/docs/ref/#view.EditorView.hasFocus}
|
|
310
306
|
*/
|
|
311
|
-
const
|
|
312
|
-
get: () => view.value.
|
|
313
|
-
set:
|
|
307
|
+
const focus: WritableComputedRef<boolean> = computed({
|
|
308
|
+
get: () => view.value.hasFocus,
|
|
309
|
+
set: f => {
|
|
310
|
+
if (f) {
|
|
311
|
+
view.value.focus();
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
314
|
});
|
|
315
315
|
|
|
316
316
|
/**
|
|
317
|
-
* Editor
|
|
317
|
+
* Editor Selection
|
|
318
318
|
*
|
|
319
|
-
* @see {@link https://codemirror.net/docs/ref/#state.
|
|
319
|
+
* @see {@link https://codemirror.net/docs/ref/#state.EditorSelection}
|
|
320
320
|
*/
|
|
321
|
-
const
|
|
322
|
-
get: () => view.value.state,
|
|
323
|
-
set: s => view.value.
|
|
321
|
+
const selection: WritableComputedRef<EditorSelection> = computed({
|
|
322
|
+
get: () => view.value.state.selection,
|
|
323
|
+
set: s => view.value.dispatch({ selection: s }),
|
|
324
324
|
});
|
|
325
325
|
|
|
326
326
|
/** Cursor Position */
|
|
@@ -329,24 +329,23 @@ export default defineComponent({
|
|
|
329
329
|
set: a => view.value.dispatch({ selection: { anchor: a } }),
|
|
330
330
|
});
|
|
331
331
|
|
|
332
|
+
/** JSON */
|
|
333
|
+
const json: WritableComputedRef<Record<string, StateField<any>>> = computed(
|
|
334
|
+
{
|
|
335
|
+
get: () => view.value.state.toJSON(),
|
|
336
|
+
set: j => view.value.setState(EditorState.fromJSON(j)),
|
|
337
|
+
}
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
/** Text length */
|
|
341
|
+
const length: Ref<number> = ref(0);
|
|
342
|
+
|
|
332
343
|
/**
|
|
333
|
-
*
|
|
344
|
+
* Returns the number of active lint diagnostics in the given state.
|
|
334
345
|
*
|
|
335
|
-
* @see {@link https://codemirror.net/docs/ref/#
|
|
346
|
+
* @see {@link https://codemirror.net/docs/ref/#lint.diagnosticCount}
|
|
336
347
|
*/
|
|
337
|
-
const
|
|
338
|
-
get: () => view.value.hasFocus,
|
|
339
|
-
set: f => {
|
|
340
|
-
if (f) {
|
|
341
|
-
view.value.focus();
|
|
342
|
-
}
|
|
343
|
-
},
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
/** Text length */
|
|
347
|
-
const length: ComputedRef<number> = computed(
|
|
348
|
-
() => view.value.state.doc.length
|
|
349
|
-
);
|
|
348
|
+
const diagnosticCount: Ref<number> = ref(0);
|
|
350
349
|
|
|
351
350
|
// Synamic Reconfiguration
|
|
352
351
|
// @see https://codemirror.net/examples/config/
|
|
@@ -357,15 +356,31 @@ export default defineComponent({
|
|
|
357
356
|
/** Get CodeMirror Extension */
|
|
358
357
|
const extensions: ComputedRef<Extension[]> = computed(() =>
|
|
359
358
|
// TODO: Ignore previous prop was not changed.
|
|
360
|
-
|
|
359
|
+
[
|
|
361
360
|
// Toggle basic setup
|
|
362
361
|
props.basic ? basicSetup : undefined,
|
|
363
362
|
// Toggle minimal setup
|
|
364
363
|
props.minimal && !props.basic ? minimalSetup : undefined,
|
|
365
364
|
// ViewUpdate event listener
|
|
366
|
-
EditorView.updateListener.of((update: ViewUpdate) =>
|
|
367
|
-
|
|
368
|
-
|
|
365
|
+
EditorView.updateListener.of((update: ViewUpdate): void => {
|
|
366
|
+
if (update.changes.empty || !update.docChanged) {
|
|
367
|
+
// Suppress event firing if no change
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (props.linter) {
|
|
371
|
+
// Linter process
|
|
372
|
+
if (props.forceLinting) {
|
|
373
|
+
// If forceLinting enabled, first liting.
|
|
374
|
+
forceLinting(view.value);
|
|
375
|
+
}
|
|
376
|
+
// Count diagnostics.
|
|
377
|
+
diagnosticCount.value = (
|
|
378
|
+
props.linter(view.value) as readonly Diagnostic[]
|
|
379
|
+
).length;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
context.emit('update', update);
|
|
383
|
+
}),
|
|
369
384
|
// Toggle light/dark mode.
|
|
370
385
|
EditorView.theme(props.theme, { dark: props.dark }),
|
|
371
386
|
// Toggle line wrapping
|
|
@@ -400,43 +415,41 @@ export default defineComponent({
|
|
|
400
415
|
props.placeholder ? placeholder(props.placeholder) : undefined,
|
|
401
416
|
// Append Extensions
|
|
402
417
|
...props.extensions,
|
|
403
|
-
])
|
|
418
|
+
].filter((extension): extension is Extension => !!extension)
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
// Extension (mostly props) Changed
|
|
422
|
+
watch(
|
|
423
|
+
extensions,
|
|
424
|
+
exts => {
|
|
425
|
+
view.value?.dispatch({
|
|
426
|
+
effects: StateEffect.reconfigure.of(exts),
|
|
427
|
+
});
|
|
428
|
+
},
|
|
429
|
+
{ immediate: true }
|
|
404
430
|
);
|
|
405
431
|
|
|
406
432
|
// for parent-to-child binding.
|
|
407
433
|
watch(
|
|
408
434
|
() => props.modelValue,
|
|
409
|
-
value => {
|
|
435
|
+
async value => {
|
|
410
436
|
if (view.value.composing) {
|
|
411
437
|
// IME fix
|
|
412
438
|
return;
|
|
413
439
|
}
|
|
414
440
|
|
|
415
|
-
// Enable Force Linting
|
|
416
|
-
if (props.linter && props.forceLinting) {
|
|
417
|
-
forceLinting(view.value);
|
|
418
|
-
}
|
|
419
|
-
|
|
420
441
|
// Update
|
|
421
442
|
view.value.dispatch({
|
|
422
443
|
changes: { from: 0, to: view.value.state.doc.length, insert: value },
|
|
423
444
|
selection: view.value.state.selection,
|
|
424
445
|
scrollIntoView: true,
|
|
425
446
|
});
|
|
447
|
+
// Update count
|
|
448
|
+
length.value = view.value.state.doc.length;
|
|
426
449
|
},
|
|
427
450
|
{ immediate: true }
|
|
428
451
|
);
|
|
429
452
|
|
|
430
|
-
// Extension (mostly props) Changed
|
|
431
|
-
watch(
|
|
432
|
-
() => extensions.value,
|
|
433
|
-
exts => {
|
|
434
|
-
view.value.dispatch({
|
|
435
|
-
effects: StateEffect.reconfigure.of(exts),
|
|
436
|
-
});
|
|
437
|
-
}
|
|
438
|
-
);
|
|
439
|
-
|
|
440
453
|
// focus changed
|
|
441
454
|
watch(focus, isFocus => context.emit('focus', isFocus));
|
|
442
455
|
|
|
@@ -444,37 +457,43 @@ export default defineComponent({
|
|
|
444
457
|
onMounted(async () => {
|
|
445
458
|
/** Initial value */
|
|
446
459
|
let value: string | Text = doc.value;
|
|
447
|
-
if (editor.value
|
|
460
|
+
if (!editor.value) {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if (editor.value.childNodes[0]) {
|
|
448
464
|
// when slot mode, overwrite initial value
|
|
449
465
|
if (doc.value !== '') {
|
|
450
466
|
console.warn(
|
|
451
467
|
'[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values.'
|
|
452
468
|
);
|
|
453
469
|
}
|
|
454
|
-
value =
|
|
470
|
+
value = (editor.value.childNodes[0] as HTMLElement).innerText.trim();
|
|
455
471
|
}
|
|
456
472
|
|
|
457
473
|
// Register Codemirror
|
|
458
474
|
view.value = new EditorView({
|
|
459
|
-
doc: value,
|
|
460
|
-
extensions: extensions.value,
|
|
461
475
|
parent: editor.value,
|
|
462
|
-
|
|
476
|
+
state: EditorState.create({ doc: value, extensions: extensions.value }),
|
|
477
|
+
dispatch: (tr: Transaction) => {
|
|
463
478
|
view.value.update([tr]);
|
|
464
|
-
|
|
479
|
+
|
|
480
|
+
if (tr.changes.empty || !tr.docChanged) {
|
|
481
|
+
// if not change value, no fire emit event
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
|
|
465
485
|
// console.log(view.state.doc.toString(), tr);
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
//
|
|
469
|
-
|
|
470
|
-
context.emit('update:modelValue', v);
|
|
471
|
-
context.emit('change', view.value.state);
|
|
486
|
+
// state.toString() is not defined, so use toJSON and toText function to convert string.
|
|
487
|
+
context.emit('update:modelValue', (tr.state.doc as any).toString());
|
|
488
|
+
// Emit EditorState
|
|
489
|
+
context.emit('change', tr.state);
|
|
472
490
|
},
|
|
473
491
|
});
|
|
474
492
|
|
|
475
493
|
await nextTick();
|
|
494
|
+
|
|
476
495
|
context.emit('ready', {
|
|
477
|
-
view,
|
|
496
|
+
view: view.value,
|
|
478
497
|
state: view.value.state,
|
|
479
498
|
container: editor.value,
|
|
480
499
|
});
|
|
@@ -492,9 +511,13 @@ export default defineComponent({
|
|
|
492
511
|
* @see {@link https://codemirror.net/docs/ref/#lint.forceLinting}
|
|
493
512
|
*/
|
|
494
513
|
const lint = (): void => {
|
|
495
|
-
if (props.linter) {
|
|
514
|
+
if (!props.linter || !view.value) {
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
if (props.forceLinting) {
|
|
496
518
|
forceLinting(view.value);
|
|
497
519
|
}
|
|
520
|
+
diagnosticCount.value = linterDagnosticCount(view.value.state);
|
|
498
521
|
};
|
|
499
522
|
|
|
500
523
|
/**
|
|
@@ -504,11 +527,11 @@ export default defineComponent({
|
|
|
504
527
|
*/
|
|
505
528
|
const forceReconfigure = (): void => {
|
|
506
529
|
// Deconfigure all Extensions
|
|
507
|
-
view.value
|
|
530
|
+
view.value?.dispatch({
|
|
508
531
|
effects: StateEffect.reconfigure.of([]),
|
|
509
532
|
});
|
|
510
533
|
// Register extensions
|
|
511
|
-
view.value
|
|
534
|
+
view.value?.dispatch({
|
|
512
535
|
effects: StateEffect.appendConfig.of(extensions.value),
|
|
513
536
|
});
|
|
514
537
|
};
|
|
@@ -521,7 +544,7 @@ export default defineComponent({
|
|
|
521
544
|
* @param from - start line number
|
|
522
545
|
* @param to - end line number
|
|
523
546
|
*/
|
|
524
|
-
const getRange = (from?: number, to?: number): string =>
|
|
547
|
+
const getRange = (from?: number, to?: number): string | undefined =>
|
|
525
548
|
view.value.state.sliceDoc(from, to);
|
|
526
549
|
/**
|
|
527
550
|
* Get the content of line.
|
|
@@ -533,24 +556,30 @@ export default defineComponent({
|
|
|
533
556
|
/** Get the number of lines in the editor. */
|
|
534
557
|
const lineCount = (): number => view.value.state.doc.lines;
|
|
535
558
|
/** Retrieve one end of the primary selection. */
|
|
536
|
-
const getCursor = (): number =>
|
|
559
|
+
const getCursor = (): number => view.value.state.selection.main.head;
|
|
537
560
|
/** Retrieves a list of all current selections. */
|
|
538
561
|
const listSelections = (): readonly SelectionRange[] =>
|
|
539
|
-
view.value.state.selection.ranges;
|
|
562
|
+
view.value.state.selection.ranges ?? [];
|
|
540
563
|
/** Get the currently selected code. */
|
|
541
564
|
const getSelection = (): string =>
|
|
542
565
|
view.value.state.sliceDoc(
|
|
543
566
|
view.value.state.selection.main.from,
|
|
544
567
|
view.value.state.selection.main.to
|
|
545
|
-
);
|
|
568
|
+
) ?? '';
|
|
546
569
|
/**
|
|
547
570
|
* The length of the given array should be the same as the number of active selections.
|
|
548
571
|
* Replaces the content of the selections with the strings in the array.
|
|
549
572
|
*/
|
|
550
|
-
const getSelections = (): string[] =>
|
|
551
|
-
view.value.state
|
|
552
|
-
|
|
573
|
+
const getSelections = (): string[] => {
|
|
574
|
+
const s = view.value.state;
|
|
575
|
+
if (!s) {
|
|
576
|
+
return [];
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
return s.selection.ranges.map((r: { from: number; to: number }) =>
|
|
580
|
+
s.sliceDoc(r.from, r.to)
|
|
553
581
|
);
|
|
582
|
+
};
|
|
554
583
|
/** Return true if any text is selected. */
|
|
555
584
|
const somethingSelected = (): boolean =>
|
|
556
585
|
view.value.state.selection.ranges.some(
|
|
@@ -585,7 +614,8 @@ export default defineComponent({
|
|
|
585
614
|
*
|
|
586
615
|
* @param position - position.
|
|
587
616
|
*/
|
|
588
|
-
const setCursor = (position: number):
|
|
617
|
+
const setCursor = (position: number): void =>
|
|
618
|
+
view.value.dispatch({ selection: { anchor: position } });
|
|
589
619
|
/**
|
|
590
620
|
* Set a single selection range.
|
|
591
621
|
*
|
|
@@ -619,15 +649,15 @@ export default defineComponent({
|
|
|
619
649
|
),
|
|
620
650
|
});
|
|
621
651
|
|
|
622
|
-
|
|
623
|
-
context.expose({
|
|
652
|
+
const exposed = {
|
|
624
653
|
editor,
|
|
625
654
|
view,
|
|
626
655
|
cursor,
|
|
627
656
|
selection,
|
|
628
|
-
state,
|
|
629
657
|
focus,
|
|
630
658
|
length,
|
|
659
|
+
json,
|
|
660
|
+
diagnosticCount,
|
|
631
661
|
lint,
|
|
632
662
|
forceReconfigure,
|
|
633
663
|
// Bellow is CodeMirror5's function
|
|
@@ -645,11 +675,11 @@ export default defineComponent({
|
|
|
645
675
|
setSelection,
|
|
646
676
|
setSelections,
|
|
647
677
|
extendSelectionsBy,
|
|
648
|
-
});
|
|
649
|
-
|
|
650
|
-
return {
|
|
651
|
-
editor,
|
|
652
678
|
};
|
|
679
|
+
|
|
680
|
+
/** Export properties and functions */
|
|
681
|
+
context.expose(exposed);
|
|
682
|
+
return exposed;
|
|
653
683
|
},
|
|
654
684
|
render() {
|
|
655
685
|
// <template>
|
|
@@ -658,19 +688,16 @@ export default defineComponent({
|
|
|
658
688
|
// </div>
|
|
659
689
|
// </template>
|
|
660
690
|
return h(
|
|
661
|
-
// @ts-expect-error
|
|
662
691
|
this.$props.tag,
|
|
663
692
|
{
|
|
664
693
|
ref: 'editor',
|
|
665
694
|
class: 'vue-codemirror',
|
|
666
695
|
},
|
|
667
|
-
// @ts-expect-error
|
|
668
696
|
this.$slots.default
|
|
669
697
|
? // Hide original content
|
|
670
698
|
h(
|
|
671
699
|
'aside',
|
|
672
700
|
{ style: 'display: none;', 'aria-hidden': 'true' },
|
|
673
|
-
// @ts-expect-error
|
|
674
701
|
slot(this.$slots.default)
|
|
675
702
|
)
|
|
676
703
|
: undefined
|