vue-codemirror6 1.1.0 → 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,8 +1,8 @@
1
- /** Build information meta data */
2
- export default interface MetaInterface {
3
- /** Version */
4
- version: string;
5
- /** Build date */
6
- date: string;
7
- }
1
+ /** Build information meta data */
2
+ export default interface MetaInterface {
3
+ /** Version */
4
+ version: string;
5
+ /** Build date */
6
+ date: string;
7
+ }
8
8
  //# sourceMappingURL=MetaInterface.d.ts.map
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "vue-codemirror6",
4
- "version": "1.1.0",
4
+ "version": "1.1.2",
5
5
  "license": "MIT",
6
6
  "description": "CodeMirror6 Component for vue2 and vue3.",
7
7
  "keywords": [
@@ -40,7 +40,8 @@
40
40
  "exports": {
41
41
  ".": {
42
42
  "import": "./dist/index.es.js",
43
- "require": "./dist/index.umd.js"
43
+ "require": "./dist/index.umd.js",
44
+ "types": "./dist/index.d.ts"
44
45
  }
45
46
  },
46
47
  "files": [
@@ -52,30 +53,31 @@
52
53
  "./sfc": "./src/components/CodeMirror.ts"
53
54
  },
54
55
  "engines": {
55
- "node": ">=16.18.0",
56
+ "node": ">=18.12.1",
56
57
  "yarn": ">=1.22.10"
57
58
  },
58
- "packageManager": "yarn@3.2.4",
59
+ "packageManager": "yarn@3.3.1",
59
60
  "sideEffects": false,
60
61
  "scripts": {
61
62
  "dev": "vite",
62
- "clean": "rimraf node_modules/.vite && npx vue-demi-fix",
63
- "build": "vite build & vue-tsc --declaration --emitDeclarationOnly",
64
- "build:analyze": "vue-tsc --noEmit & vue-tsc --declaration --emitDeclarationOnly & vite build --mode analyze",
65
- "build:docs": "vite build --config vite.config.docs.ts",
63
+ "clean": "rimraf node_modules/.vite",
64
+ "lint": "eslint . --fix --cache --cache-location ./node_modules/.vite/vite-plugin-eslint && prettier . --write",
65
+ "type-check": "tsc --noEmit --composite false",
66
+ "build": "run-p type-check build-only build-declaration",
66
67
  "build:clean": "rimraf dist",
67
- "lint": "eslint --fix --cache --cache-location ./node_modules/.vite/vite-plugin-eslint && prettier . -w -u",
68
- "lint:markup": "vue-tsc --noEmit",
69
- "preview": "vite preview --config vite.config.docs.ts",
68
+ "build-only": "vite build",
69
+ "build-declaration": "tsc -p tsconfig.app.json --composite false --declaration --declarationMap --outDir dist",
70
70
  "prepare": "husky install",
71
71
  "version": "auto-changelog -p && git add CHANGELOG.md"
72
72
  },
73
73
  "dependencies": {
74
+ "codemirror": "^6.0.1",
75
+ "lodash.compact": "^3.0.1",
76
+ "lodash.trim": "^4.5.1",
74
77
  "vue-demi": "latest"
75
78
  },
76
79
  "peerDependencies": {
77
80
  "@vue/composition-api": "^1.7.1",
78
- "codemirror": "^6.0.1",
79
81
  "vue": "^2.6.14 || ^3.2.39"
80
82
  },
81
83
  "peerDependenciesMeta": {
@@ -84,44 +86,44 @@
84
86
  }
85
87
  },
86
88
  "devDependencies": {
87
- "@codemirror/lang-javascript": "^6.1.1",
89
+ "@codemirror/lang-javascript": "^6.1.2",
88
90
  "@codemirror/lang-json": "^6.0.1",
89
- "@codemirror/lang-markdown": "6.0.2",
90
- "@types/lodash": "^4.14.186",
91
- "@types/node": "^18.11.8",
92
- "@typescript-eslint/eslint-plugin": "^5.41.0",
93
- "@typescript-eslint/parser": "^5.41.0",
94
- "@vitejs/plugin-vue": "^3.2.0",
91
+ "@codemirror/lang-markdown": "^6.0.5",
92
+ "@types/lodash": "^4.14.191",
93
+ "@types/node": "^18.11.17",
94
+ "@typescript-eslint/eslint-plugin": "^5.47.0",
95
+ "@typescript-eslint/parser": "^5.47.0",
96
+ "@vitejs/plugin-vue": "^4.0.0",
95
97
  "@vue/eslint-config-prettier": "^7.0.0",
96
98
  "@vue/tsconfig": "^0.1.3",
97
- "codemirror": "^6.0.1",
98
- "eslint": "^8.26.0",
99
+ "eslint": "^8.30.0",
99
100
  "eslint-config-google": "^0.14.0",
100
101
  "eslint-config-prettier": "^8.5.0",
101
102
  "eslint-import-resolver-alias": "^1.1.2",
102
103
  "eslint-import-resolver-typescript": "^3.5.2",
103
- "eslint-linter-browserify": "^8.26.0",
104
+ "eslint-linter-browserify": "^8.30.0",
104
105
  "eslint-plugin-html": "^7.1.0",
105
106
  "eslint-plugin-import": "^2.26.0",
106
- "eslint-plugin-jsdoc": "^39.4.0",
107
+ "eslint-plugin-jsdoc": "^39.6.4",
107
108
  "eslint-plugin-tsdoc": "^0.2.17",
108
- "eslint-plugin-vue": "^9.7.0",
109
+ "eslint-plugin-vue": "^9.8.0",
109
110
  "eslint-plugin-vuejs-accessibility": "^2.0.0",
110
111
  "eslint-plugin-yaml": "^0.5.0",
111
- "husky": "^8.0.1",
112
- "lint-staged": "^13.0.3",
112
+ "husky": "^8.0.2",
113
+ "lint-staged": "^13.1.0",
113
114
  "lodash": "^4.17.21",
114
- "markdown-wasm": "^1.2.0",
115
- "prettier": "^2.7.1",
115
+ "npm-run-all": "^4.1.5",
116
+ "prettier": "^2.8.1",
116
117
  "rimraf": "^3.0.2",
117
118
  "rollup-plugin-visualizer": "^5.8.3",
118
- "typescript": "^4.8.4",
119
- "vite": "^3.2.2",
120
- "vite-plugin-banner": "^0.6.1",
121
- "vite-plugin-checker": "^0.5.1",
122
- "vue": "^3.2.41",
119
+ "typescript": "^4.9.4",
120
+ "vite": "^4.0.3",
121
+ "vite-plugin-banner": "^0.7.0",
122
+ "vite-plugin-checker": "^0.5.3",
123
+ "vue": "^3.2.45",
123
124
  "vue-eslint-parser": "^9.1.0",
124
- "vue-tsc": "^1.0.9"
125
+ "vue-markdown-wasm": "^0.0.1",
126
+ "vue-tsc": "^1.0.16"
125
127
  },
126
128
  "husky": {
127
129
  "hooks": {
@@ -131,8 +133,5 @@
131
133
  "lint-staged": {
132
134
  "*.{js,ts,json,htm,html}": "eslint --fix --cache --cache-location ./node_modules/.vite/vite-plugin-eslint",
133
135
  "*": "prettier -w -u"
134
- },
135
- "resolutions": {
136
- "prettier": "^2.7.1"
137
136
  }
138
137
  }
@@ -18,24 +18,33 @@ import {
18
18
 
19
19
  // Helpers
20
20
  import h, { slot } from '@/helpers/h-demi';
21
- import { compact, trim } from 'lodash';
21
+ import compact from 'lodash/compact';
22
+ import trim from 'lodash/trim';
22
23
 
23
24
  // CodeMirror
24
- import { EditorSelection, EditorState, StateEffect } from '@codemirror/state';
25
- import { EditorView, keymap, placeholder } from '@codemirror/view';
26
- import { linter, lintGutter, diagnosticCount } from '@codemirror/lint';
27
25
  import { basicSetup, minimalSetup } from 'codemirror';
28
- import { indentWithTab } from '@codemirror/commands';
29
-
30
- import type {
31
- Extension,
32
- SelectionRange,
33
- Text,
34
- Transaction,
26
+ import {
27
+ EditorSelection,
28
+ EditorState,
29
+ StateEffect,
30
+ type Extension,
31
+ type SelectionRange,
32
+ type Text,
35
33
  } from '@codemirror/state';
34
+ import {
35
+ EditorView,
36
+ keymap,
37
+ placeholder,
38
+ type ViewUpdate,
39
+ } from '@codemirror/view';
40
+ import { indentWithTab } from '@codemirror/commands';
41
+ import {
42
+ forceLinting,
43
+ linter,
44
+ lintGutter,
45
+ type LintSource,
46
+ } from '@codemirror/lint';
36
47
  import type { LanguageSupport } from '@codemirror/language';
37
- import type { ViewUpdate } from '@codemirror/view';
38
- import type { LintSource } from '@codemirror/lint';
39
48
  import type { StyleSpec } from 'style-mod';
40
49
 
41
50
  /** Emit Interface */
@@ -55,10 +64,10 @@ export interface CodeMirrorEmitsOptions extends ObjectEmitsOptions {
55
64
  container: HTMLElement;
56
65
  }
57
66
  ): void;
58
- /** onChange (same as update:modelValue) */
59
- (e: 'change', value: string | Text): void;
60
- /** CodeMirror linter active diagnostic aount */
61
- (e: 'diagnosticCount', value: number): void;
67
+ /** CodeMirror onDestroy */
68
+ (e: 'destroy'): void;
69
+ /** State Changed */
70
+ (e: 'change', value: EditorState): void;
62
71
  }
63
72
 
64
73
  /** CodeMirror Component */
@@ -80,13 +89,11 @@ export default defineComponent({
80
89
  /**
81
90
  * Theme
82
91
  *
83
- * @see {@link https://codemirror.net/6/examples/styling/ | Example: Styling}
92
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView^theme}
84
93
  */
85
94
  theme: {
86
95
  type: Object as PropType<{ [selector: string]: StyleSpec }>,
87
- default: () => {
88
- return {};
89
- },
96
+ default: () => {},
90
97
  },
91
98
  /** Dark Mode */
92
99
  dark: {
@@ -96,7 +103,7 @@ export default defineComponent({
96
103
  /**
97
104
  * Use Basic Setup
98
105
  *
99
- * @see {@link https://codemirror.net/docs/ref/#codemirror.basicSetup | basicSetup}
106
+ * @see {@link https://codemirror.net/docs/ref/#codemirror.basicSetup}
100
107
  */
101
108
  basic: {
102
109
  type: Boolean,
@@ -105,7 +112,7 @@ export default defineComponent({
105
112
  /**
106
113
  * Use Minimal Setup (The basic setting has priority.)
107
114
  *
108
- * @see {@link https://codemirror.net/docs/ref/#codemirror.minimalSetup | minimalSetup}
115
+ * @see {@link https://codemirror.net/docs/ref/#codemirror.minimalSetup}
109
116
  */
110
117
  minimal: {
111
118
  type: Boolean,
@@ -113,9 +120,11 @@ export default defineComponent({
113
120
  },
114
121
  /**
115
122
  * Placeholder
123
+ *
124
+ * @see {@link https://codemirror.net/docs/ref/#view.placeholder}
116
125
  */
117
126
  placeholder: {
118
- type: String,
127
+ type: String as PropType<string | HTMLElement>,
119
128
  default: undefined,
120
129
  },
121
130
  /**
@@ -123,7 +132,7 @@ export default defineComponent({
123
132
  *
124
133
  * An extension that enables line wrapping in the editor (by setting CSS white-space to pre-wrap in the content).
125
134
  *
126
- * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5ElineWrapping | LineWrapping}
135
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView%5ElineWrapping}
127
136
  */
128
137
  wrap: {
129
138
  type: Boolean,
@@ -132,16 +141,34 @@ export default defineComponent({
132
141
  /**
133
142
  * Allow tab key indent.
134
143
  *
135
- * @see {@link https://codemirror.net/6/examples/tab/ | Tab Handling}
144
+ * @see {@link https://codemirror.net/examples/tab/}
136
145
  */
137
146
  tab: {
138
147
  type: Boolean,
139
148
  default: false,
140
149
  },
150
+ /**
151
+ * Tab size
152
+ *
153
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState^tabSize}
154
+ */
155
+ tabSize: {
156
+ type: Number,
157
+ default: undefined,
158
+ },
159
+ /**
160
+ * Set line break (separetor) char.
161
+ *
162
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState^lineSeparator}
163
+ */
164
+ lineSeparator: {
165
+ type: String,
166
+ default: undefined,
167
+ },
141
168
  /**
142
169
  * Readonly
143
170
  *
144
- * @see {@link https://codemirror.net/6/docs/ref/#state.EditorState%5EreadOnly | readOnly}
171
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState^readOnly}
145
172
  */
146
173
  readonly: {
147
174
  type: Boolean,
@@ -153,7 +180,7 @@ export default defineComponent({
153
180
  * This is the reversed value of the CodeMirror editable.
154
181
  * Similar to `readonly`, but setting this value to true disables dragging.
155
182
  *
156
- * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5Eeditable | editable}
183
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView^editable}
157
184
  */
158
185
  disabled: {
159
186
  type: Boolean,
@@ -162,7 +189,7 @@ export default defineComponent({
162
189
  /**
163
190
  * Additional Extension
164
191
  *
165
- * @see {@link https://codemirror.net/6/docs/ref/#state.Extension | Extending Editor State}
192
+ * @see {@link https://codemirror.net/docs/ref/#state.Extension}
166
193
  */
167
194
  extensions: {
168
195
  type: Array as PropType<Extension[]>,
@@ -173,7 +200,7 @@ export default defineComponent({
173
200
  /**
174
201
  * Language Phreses
175
202
  *
176
- * @see {@link https://codemirror.net/6/examples/translate/ | Example: Internationalization}
203
+ * @see {@link https://codemirror.net/examples/translate/}
177
204
  */
178
205
  phrases: {
179
206
  type: Object as PropType<Record<string, string>>,
@@ -182,7 +209,7 @@ export default defineComponent({
182
209
  /**
183
210
  * CodeMirror Language
184
211
  *
185
- * @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
212
+ * @see {@link https://codemirror.net/docs/ref/#language}
186
213
  */
187
214
  lang: {
188
215
  type: Object as PropType<LanguageSupport>,
@@ -191,7 +218,7 @@ export default defineComponent({
191
218
  /**
192
219
  * CodeMirror Linter
193
220
  *
194
- * @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
221
+ * @see {@link https://codemirror.net/docs/ref/#lint.linter}
195
222
  */
196
223
  linter: {
197
224
  type: Function as PropType<LintSource>,
@@ -211,6 +238,8 @@ export default defineComponent({
211
238
  *
212
239
  * An area to 🔴 the lines with errors will be displayed.
213
240
  * This feature is not enabled if `linter` is not specified.
241
+ *
242
+ * @see {@link https://codemirror.net/docs/ref/#lint.lintGutter}
214
243
  */
215
244
  gutter: {
216
245
  type: Boolean,
@@ -234,14 +263,7 @@ export default defineComponent({
234
263
  },
235
264
  },
236
265
  /** Emits */
237
- emits: [
238
- 'update:modelValue',
239
- 'update',
240
- 'ready',
241
- 'focus',
242
- 'changed',
243
- 'diagnosticCount',
244
- ],
266
+ emits: ['update:modelValue', 'update', 'ready', 'focus', 'change', 'destroy'],
245
267
  /**
246
268
  * Setup
247
269
  *
@@ -255,28 +277,44 @@ export default defineComponent({
255
277
  /** Internal value */
256
278
  const doc: Ref<string | Text> = ref(props.modelValue);
257
279
 
258
- /** CodeMirror Editor View */
280
+ /**
281
+ * CodeMirror Editor View
282
+ *
283
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView}
284
+ */
259
285
  const view: ShallowRef<EditorView> = shallowRef(new EditorView());
260
286
 
261
- /** Selection */
287
+ /**
288
+ * Editor Selection
289
+ *
290
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorSelection}
291
+ */
262
292
  const selection: WritableComputedRef<EditorSelection> = computed({
263
293
  get: () => view.value.state.selection,
264
294
  set: s => view.value.dispatch({ selection: s }),
265
295
  });
266
296
 
267
- /** Cursor Position */
268
- const cursor: WritableComputedRef<number> = computed({
269
- get: () => selection.value.main.head || 0,
270
- set: a => view.value.dispatch({ selection: { anchor: a } }),
271
- });
272
-
273
- /** Editor State */
297
+ /**
298
+ * Editor State
299
+ *
300
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState}
301
+ */
274
302
  const state: WritableComputedRef<EditorState> = computed({
275
303
  get: () => view.value.state,
276
304
  set: s => view.value.setState(s),
277
305
  });
278
306
 
279
- /** Focus */
307
+ /** Cursor Position */
308
+ const cursor: WritableComputedRef<number> = computed({
309
+ get: () => view.value.state.selection.main.head || 0,
310
+ set: a => view.value.dispatch({ selection: { anchor: a } }),
311
+ });
312
+
313
+ /**
314
+ * Focus
315
+ *
316
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView.hasFocus}
317
+ */
280
318
  const focus: WritableComputedRef<boolean> = computed({
281
319
  get: () => view.value.hasFocus,
282
320
  set: f => {
@@ -286,6 +324,11 @@ export default defineComponent({
286
324
  },
287
325
  });
288
326
 
327
+ /** Text length */
328
+ const length: ComputedRef<number> = computed(
329
+ () => view.value.state.doc.length
330
+ );
331
+
289
332
  /** Get CodeMirror Extension */
290
333
  const extensions: ComputedRef<Extension[]> = computed(() =>
291
334
  // TODO: Ignore previous prop was not changed.
@@ -304,12 +347,18 @@ export default defineComponent({
304
347
  props.wrap ? EditorView.lineWrapping : undefined,
305
348
  // Indent with tab
306
349
  props.tab ? keymap.of([indentWithTab]) : undefined,
350
+ // Indent tab size
351
+ props.tabSize ? EditorState.tabSize.of(props.tabSize) : undefined,
307
352
  // locale settings
308
353
  props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
309
354
  // Readonly option
310
355
  EditorState.readOnly.of(props.readonly),
311
356
  // Editable option
312
357
  EditorView.editable.of(!props.disabled),
358
+ // Set Line break char
359
+ props.lineSeparator
360
+ ? EditorState.lineSeparator.of(props.lineSeparator)
361
+ : undefined,
313
362
  // Lang
314
363
  props.lang ? props.lang : undefined,
315
364
  // Append Linter settings
@@ -329,21 +378,15 @@ export default defineComponent({
329
378
  watch(
330
379
  () => props.modelValue,
331
380
  value => {
332
- if (!view.value || view.value.composing) {
381
+ if (view.value.composing) {
333
382
  // IME fix
334
383
  return;
335
384
  }
336
- /** Current Editor State */
337
- const current: EditorState = view.value.state;
338
385
 
339
- if (props.linter) {
340
- // Lint error count.
341
- context.emit('diagnosticCount', diagnosticCount(current));
342
- }
343
386
  // Update
344
387
  view.value.dispatch({
345
- changes: { from: 0, to: current.doc.length, insert: value },
346
- selection: current.selection,
388
+ changes: { from: 0, to: view.value.state.doc.length, insert: value },
389
+ selection: view.value.state.selection,
347
390
  scrollIntoView: true,
348
391
  });
349
392
  },
@@ -380,7 +423,7 @@ export default defineComponent({
380
423
  doc: value,
381
424
  extensions: extensions.value,
382
425
  parent: editor.value,
383
- dispatch: (tr: Transaction) => {
426
+ dispatch: tr => {
384
427
  view.value.update([tr]);
385
428
  // TODO: Emit lint error event
386
429
  // console.log(view.state.doc.toString(), tr);
@@ -388,23 +431,37 @@ export default defineComponent({
388
431
  // child-to-parent binding
389
432
  const v = view.value.state.doc.toString();
390
433
  context.emit('update:modelValue', v);
391
- context.emit('changed', v);
434
+ context.emit('change', view.value.state);
392
435
  },
393
436
  });
394
437
  await nextTick();
395
438
  context.emit('ready', {
396
439
  view: view,
397
- state: state.value,
440
+ state: view.value.state,
398
441
  container: editor.value,
399
442
  });
400
443
  });
401
444
 
402
445
  /** Destroy */
403
446
  onUnmounted(() => {
404
- view.value.destroy();
447
+ if (view.value) {
448
+ view.value.destroy();
449
+ context.emit('destroy');
450
+ }
405
451
  });
406
452
 
407
- // Bellow is experimental.
453
+ /**
454
+ * Forces any linters configured to run when the editor is idle to run right away.
455
+ *
456
+ * @see {@link https://codemirror.net/docs/ref/#lint.forceLinting}
457
+ */
458
+ const lint = () => {
459
+ if (props.linter) {
460
+ forceLinting(view.value);
461
+ }
462
+ };
463
+
464
+ /* ----- Bellow is experimental. ------ */
408
465
 
409
466
  /**
410
467
  * Get the text between the given points in the editor.
@@ -476,7 +533,7 @@ export default defineComponent({
476
533
  *
477
534
  * @param position - position.
478
535
  */
479
- const setCursor = (position: number) => (cursor.value = position);
536
+ const setCursor = (position: number): number => (cursor.value = position);
480
537
  /**
481
538
  * Set a single selection range.
482
539
  *
@@ -506,17 +563,19 @@ export default defineComponent({
506
563
  const extendSelectionsBy = (f: Function) =>
507
564
  view.value.dispatch({
508
565
  selection: EditorSelection.create(
509
- selection.value.ranges.map(r => r.extend(f(r)))
566
+ selection.value.ranges.map((r: SelectionRange) => r.extend(f(r)))
510
567
  ),
511
568
  });
512
569
 
513
- return {
514
- context,
570
+ /** Export properties and functions */
571
+ context.expose({
515
572
  editor,
516
573
  cursor,
517
574
  selection,
518
575
  state,
519
576
  focus,
577
+ length,
578
+ lint,
520
579
  // Bellow is CodeMirror5's function
521
580
  getRange,
522
581
  getLine,
@@ -532,6 +591,10 @@ export default defineComponent({
532
591
  setSelection,
533
592
  setSelections,
534
593
  extendSelectionsBy,
594
+ });
595
+
596
+ return {
597
+ editor,
535
598
  };
536
599
  },
537
600
  render() {
@@ -547,7 +610,8 @@ export default defineComponent({
547
610
  class: 'vue-codemirror',
548
611
  },
549
612
  this.$slots.default
550
- ? h(
613
+ ? // Hide original content
614
+ h(
551
615
  'aside',
552
616
  { style: 'display: none;', 'aria-hidden': 'true' },
553
617
  slot(this.$slots.default)