vue-codemirror6 1.0.3 → 1.1.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.
@@ -6,7 +6,6 @@ import {
6
6
  onUnmounted,
7
7
  ref,
8
8
  shallowRef,
9
- toRaw,
10
9
  watch,
11
10
  type ComputedRef,
12
11
  type ObjectEmitsOptions,
@@ -14,6 +13,7 @@ import {
14
13
  type Ref,
15
14
  type SetupContext,
16
15
  type ShallowRef,
16
+ type WritableComputedRef,
17
17
  } from 'vue-demi';
18
18
 
19
19
  // Helpers
@@ -21,11 +21,11 @@ import h, { slot } from '@/helpers/h-demi';
21
21
  import { compact, trim } from 'lodash';
22
22
 
23
23
  // CodeMirror
24
- import { EditorSelection, EditorState, StateEffect } from '@codemirror/state';
25
- import { EditorView, keymap } from '@codemirror/view';
26
- import { linter, lintGutter } from '@codemirror/lint';
27
24
  import { basicSetup, minimalSetup } from 'codemirror';
25
+ import { EditorSelection, EditorState, StateEffect } from '@codemirror/state';
26
+ import { EditorView, keymap, placeholder } from '@codemirror/view';
28
27
  import { indentWithTab } from '@codemirror/commands';
28
+ import { forceLinting, linter, lintGutter } from '@codemirror/lint';
29
29
 
30
30
  import type {
31
31
  Extension,
@@ -34,9 +34,9 @@ import type {
34
34
  Transaction,
35
35
  } from '@codemirror/state';
36
36
  import type { LanguageSupport } from '@codemirror/language';
37
- import type { ViewUpdate } from '@codemirror/view';
38
37
  import type { LintSource } from '@codemirror/lint';
39
38
  import type { StyleSpec } from 'style-mod';
39
+ import type { ViewUpdate } from '@codemirror/view';
40
40
 
41
41
  /** Emit Interface */
42
42
  export interface CodeMirrorEmitsOptions extends ObjectEmitsOptions {
@@ -52,11 +52,11 @@ export interface CodeMirrorEmitsOptions extends ObjectEmitsOptions {
52
52
  value: {
53
53
  view: EditorView;
54
54
  state: EditorState;
55
- container: HTMLDivElement;
55
+ container: HTMLElement;
56
56
  }
57
57
  ): void;
58
- /** onChanged (same as update:modelValue) */
59
- (e: 'changed', value: string | Text): void;
58
+ /** onChange (same as update:modelValue) */
59
+ (e: 'change', value: string | Text): void;
60
60
  }
61
61
 
62
62
  /** CodeMirror Component */
@@ -78,13 +78,11 @@ export default defineComponent({
78
78
  /**
79
79
  * Theme
80
80
  *
81
- * @see {@link https://codemirror.net/6/examples/styling/ | Example: Styling}
81
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView^theme}
82
82
  */
83
83
  theme: {
84
84
  type: Object as PropType<{ [selector: string]: StyleSpec }>,
85
- default: () => {
86
- return {};
87
- },
85
+ default: () => {},
88
86
  },
89
87
  /** Dark Mode */
90
88
  dark: {
@@ -94,7 +92,7 @@ export default defineComponent({
94
92
  /**
95
93
  * Use Basic Setup
96
94
  *
97
- * @see {@link https://codemirror.net/docs/ref/#codemirror.basicSetup | basicSetup}
95
+ * @see {@link https://codemirror.net/docs/ref/#codemirror.basicSetup}
98
96
  */
99
97
  basic: {
100
98
  type: Boolean,
@@ -103,52 +101,84 @@ export default defineComponent({
103
101
  /**
104
102
  * Use Minimal Setup (The basic setting has priority.)
105
103
  *
106
- * @see {@link https://codemirror.net/docs/ref/#codemirror.minimalSetup | minimalSetup}
104
+ * @see {@link https://codemirror.net/docs/ref/#codemirror.minimalSetup}
107
105
  */
108
106
  minimal: {
109
107
  type: Boolean,
110
108
  default: false,
111
109
  },
110
+ /**
111
+ * Placeholder
112
+ *
113
+ * @see {@link https://codemirror.net/docs/ref/#view.placeholder}
114
+ */
115
+ placeholder: {
116
+ type: String as PropType<string | HTMLElement>,
117
+ default: undefined,
118
+ },
112
119
  /**
113
120
  * Line wrapping
114
121
  *
115
- * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5ElineWrapping | LineWrapping}
122
+ * An extension that enables line wrapping in the editor (by setting CSS white-space to pre-wrap in the content).
123
+ *
124
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView%5ElineWrapping}
116
125
  */
117
126
  wrap: {
118
127
  type: Boolean,
119
128
  default: false,
120
129
  },
121
130
  /**
122
- * Tab handling
131
+ * Allow tab key indent.
123
132
  *
124
- * @see {@link https://codemirror.net/6/examples/tab/ | Tab Handling}
133
+ * @see {@link https://codemirror.net/examples/tab/}
125
134
  */
126
135
  tab: {
127
136
  type: Boolean,
128
137
  default: false,
129
138
  },
139
+ /**
140
+ * Tab size
141
+ *
142
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState^tabSize}
143
+ */
144
+ tabSize: {
145
+ type: Number,
146
+ default: undefined,
147
+ },
148
+ /**
149
+ * Set line break (separetor) char.
150
+ *
151
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState^lineSeparator}
152
+ */
153
+ lineSeparator: {
154
+ type: String,
155
+ default: undefined,
156
+ },
130
157
  /**
131
158
  * Readonly
132
159
  *
133
- * @see {@link https://codemirror.net/6/docs/ref/#state.EditorState%5EreadOnly | readOnly}
160
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState^readOnly}
134
161
  */
135
162
  readonly: {
136
163
  type: Boolean,
137
164
  default: false,
138
165
  },
139
166
  /**
140
- * Editable
167
+ * Disable input.
141
168
  *
142
- * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5Eeditable | editable}
169
+ * This is the reversed value of the CodeMirror editable.
170
+ * Similar to `readonly`, but setting this value to true disables dragging.
171
+ *
172
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView^editable}
143
173
  */
144
- editable: {
174
+ disabled: {
145
175
  type: Boolean,
146
- default: true,
176
+ default: false,
147
177
  },
148
178
  /**
149
179
  * Additional Extension
150
180
  *
151
- * @see {@link https://codemirror.net/6/docs/ref/#state.Extension | Extending Editor State}
181
+ * @see {@link https://codemirror.net/docs/ref/#state.Extension}
152
182
  */
153
183
  extensions: {
154
184
  type: Array as PropType<Extension[]>,
@@ -159,7 +189,7 @@ export default defineComponent({
159
189
  /**
160
190
  * Language Phreses
161
191
  *
162
- * @see {@link https://codemirror.net/6/examples/translate/ | Example: Internationalization}
192
+ * @see {@link https://codemirror.net/examples/translate/}
163
193
  */
164
194
  phrases: {
165
195
  type: Object as PropType<Record<string, string>>,
@@ -168,7 +198,7 @@ export default defineComponent({
168
198
  /**
169
199
  * CodeMirror Language
170
200
  *
171
- * @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
201
+ * @see {@link https://codemirror.net/docs/ref/#language}
172
202
  */
173
203
  lang: {
174
204
  type: Object as PropType<LanguageSupport>,
@@ -177,25 +207,52 @@ export default defineComponent({
177
207
  /**
178
208
  * CodeMirror Linter
179
209
  *
180
- * @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
210
+ * @see {@link https://codemirror.net/docs/ref/#lint.linter}
181
211
  */
182
212
  linter: {
183
213
  type: Function as PropType<LintSource>,
184
214
  default: undefined,
185
215
  },
186
- /** Show Gutter */
187
- lintGutter: {
216
+ /**
217
+ * Linter Config
218
+ *
219
+ * @see {@link https://codemirror.net/docs/ref/#lint.linter^config}
220
+ */
221
+ linterConfig: {
222
+ type: Object,
223
+ default: () => {},
224
+ },
225
+ /**
226
+ * Show Linter Gutter
227
+ *
228
+ * An area to 🔴 the lines with errors will be displayed.
229
+ * This feature is not enabled if `linter` is not specified.
230
+ *
231
+ * @see {@link https://codemirror.net/docs/ref/#lint.lintGutter}
232
+ */
233
+ gutter: {
188
234
  type: Boolean,
189
235
  defalt: false,
190
236
  },
191
- /** Using tag */
237
+ /**
238
+ * Gutter Config
239
+ *
240
+ * @see {@link https://codemirror.net/docs/ref/#lint.lintGutter^config}
241
+ */
242
+ gutterConfig: {
243
+ type: Object,
244
+ default: () => {},
245
+ },
246
+ /**
247
+ * Using tag
248
+ */
192
249
  tag: {
193
250
  type: String,
194
251
  default: 'div',
195
252
  },
196
253
  },
197
254
  /** Emits */
198
- emits: ['update:modelValue', 'update', 'ready', 'focus', 'changed'],
255
+ emits: ['update:modelValue', 'update', 'ready', 'focus', 'change'],
199
256
  /**
200
257
  * Setup
201
258
  *
@@ -209,29 +266,45 @@ export default defineComponent({
209
266
  /** Internal value */
210
267
  const doc: Ref<string | Text> = ref(props.modelValue);
211
268
 
212
- /** CodeMirror Editor View */
269
+ /**
270
+ * CodeMirror Editor View
271
+ *
272
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView}
273
+ */
213
274
  const view: ShallowRef<EditorView> = shallowRef(new EditorView());
214
275
 
215
- /** Selection */
216
- const selection: Ref<EditorSelection> = computed({
276
+ /**
277
+ * Editor Selection
278
+ *
279
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorSelection}
280
+ */
281
+ const selection: WritableComputedRef<EditorSelection> = computed({
217
282
  get: () => view.value.state.selection,
218
283
  set: s => view.value.dispatch({ selection: s }),
219
284
  });
220
285
 
221
- /** Cursor Position */
222
- const cursor: Ref<number> = computed({
223
- get: () => selection.value.main.head || 0,
224
- set: a => view.value.dispatch({ selection: { anchor: a } }),
225
- });
226
-
227
- /** Editor State */
228
- const state: Ref<EditorState> = computed({
286
+ /**
287
+ * Editor State
288
+ *
289
+ * @see {@link https://codemirror.net/docs/ref/#state.EditorState}
290
+ */
291
+ const state: WritableComputedRef<EditorState> = computed({
229
292
  get: () => view.value.state,
230
293
  set: s => view.value.setState(s),
231
294
  });
232
295
 
233
- /** Focus */
234
- const focus: Ref<boolean> = computed({
296
+ /** Cursor Position */
297
+ const cursor: WritableComputedRef<number> = computed({
298
+ get: () => view.value.state.selection.main.head || 0,
299
+ set: a => view.value.dispatch({ selection: { anchor: a } }),
300
+ });
301
+
302
+ /**
303
+ * Focus
304
+ *
305
+ * @see {@link https://codemirror.net/docs/ref/#view.EditorView.hasFocus}
306
+ */
307
+ const focus: WritableComputedRef<boolean> = computed({
235
308
  get: () => view.value.hasFocus,
236
309
  set: f => {
237
310
  if (f) {
@@ -240,6 +313,11 @@ export default defineComponent({
240
313
  },
241
314
  });
242
315
 
316
+ /** Text length */
317
+ const length: ComputedRef<number> = computed(
318
+ () => view.value.state.doc.length
319
+ );
320
+
243
321
  /** Get CodeMirror Extension */
244
322
  const extensions: ComputedRef<Extension[]> = computed(() =>
245
323
  // TODO: Ignore previous prop was not changed.
@@ -258,18 +336,28 @@ export default defineComponent({
258
336
  props.wrap ? EditorView.lineWrapping : undefined,
259
337
  // Indent with tab
260
338
  props.tab ? keymap.of([indentWithTab]) : undefined,
339
+ // Indent tab size
340
+ props.tabSize ? EditorState.tabSize.of(props.tabSize) : undefined,
261
341
  // locale settings
262
342
  props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
263
343
  // Readonly option
264
344
  EditorState.readOnly.of(props.readonly),
265
345
  // Editable option
266
- EditorView.editable.of(props.editable),
346
+ EditorView.editable.of(!props.disabled),
347
+ // Set Line break char
348
+ props.lineSeparator
349
+ ? EditorState.lineSeparator.of(props.lineSeparator)
350
+ : undefined,
267
351
  // Lang
268
- props.lang ? toRaw(props.lang) : undefined,
352
+ props.lang ? props.lang : undefined,
269
353
  // Append Linter settings
270
- props.linter ? linter(props.linter) : undefined,
354
+ props.linter ? linter(props.linter, props.linterConfig) : undefined,
271
355
  // Show 🔴 to error line when linter enabled.
272
- props.linter && props.lintGutter ? lintGutter() : undefined,
356
+ props.linter && props.gutter
357
+ ? lintGutter(props.gutterConfig)
358
+ : undefined,
359
+ // Placeholder
360
+ props.placeholder ? placeholder(props.placeholder) : undefined,
273
361
  // Append Extensions (such as basic-setup)
274
362
  ...props.extensions,
275
363
  ])
@@ -279,34 +367,31 @@ export default defineComponent({
279
367
  watch(
280
368
  () => props.modelValue,
281
369
  value => {
282
- if (!view.value || view.value.composing) {
370
+ if (view.value.composing) {
283
371
  // IME fix
284
372
  return;
285
373
  }
286
- /** Previous cursor location */
287
- const previous = view.value.state.selection;
288
374
 
289
375
  // Update
290
376
  view.value.dispatch({
291
377
  changes: { from: 0, to: view.value.state.doc.length, insert: value },
292
- selection: previous,
378
+ selection: view.value.state.selection,
379
+ scrollIntoView: true,
293
380
  });
294
381
  },
295
382
  { immediate: true }
296
383
  );
297
384
 
298
385
  // Extension (mostly props) Changed
299
- watch(extensions, e => {
386
+ watch(extensions, exts =>
300
387
  // TODO: Reduce unchanched value
301
388
  view.value.dispatch({
302
- effects: StateEffect.reconfigure.of(e),
303
- });
304
- });
389
+ effects: StateEffect.reconfigure.of(exts),
390
+ })
391
+ );
305
392
 
306
393
  // focus changed
307
- watch(focus, isFocus => {
308
- context.emit('focus', isFocus);
309
- });
394
+ watch(focus, isFocus => context.emit('focus', isFocus));
310
395
 
311
396
  /** When loaded */
312
397
  onMounted(async () => {
@@ -335,7 +420,7 @@ export default defineComponent({
335
420
  // child-to-parent binding
336
421
  const v = view.value.state.doc.toString();
337
422
  context.emit('update:modelValue', v);
338
- context.emit('changed', v);
423
+ context.emit('change', v);
339
424
  },
340
425
  });
341
426
  await nextTick();
@@ -347,7 +432,16 @@ export default defineComponent({
347
432
  });
348
433
 
349
434
  /** Destroy */
350
- onUnmounted(() => view.value.destroy());
435
+ onUnmounted(() => {
436
+ view.value.destroy();
437
+ });
438
+
439
+ /**
440
+ * Forces any linters configured to run when the editor is idle to run right away.
441
+ *
442
+ * @see {@link https://codemirror.net/docs/ref/#lint.forceLinting}
443
+ */
444
+ const lint = () => forceLinting(view.value);
351
445
 
352
446
  // Bellow is experimental.
353
447
 
@@ -462,6 +556,8 @@ export default defineComponent({
462
556
  selection,
463
557
  state,
464
558
  focus,
559
+ length,
560
+ lint,
465
561
  // Bellow is CodeMirror5's function
466
562
  getRange,
467
563
  getLine,