vue-codemirror6 1.0.3 → 1.1.0

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
@@ -22,8 +22,8 @@ import { compact, trim } from 'lodash';
22
22
 
23
23
  // CodeMirror
24
24
  import { EditorSelection, EditorState, StateEffect } from '@codemirror/state';
25
- import { EditorView, keymap } from '@codemirror/view';
26
- import { linter, lintGutter } from '@codemirror/lint';
25
+ import { EditorView, keymap, placeholder } from '@codemirror/view';
26
+ import { linter, lintGutter, diagnosticCount } from '@codemirror/lint';
27
27
  import { basicSetup, minimalSetup } from 'codemirror';
28
28
  import { indentWithTab } from '@codemirror/commands';
29
29
 
@@ -52,11 +52,13 @@ 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
+ /** CodeMirror linter active diagnostic aount */
61
+ (e: 'diagnosticCount', value: number): void;
60
62
  }
61
63
 
62
64
  /** CodeMirror Component */
@@ -109,9 +111,18 @@ export default defineComponent({
109
111
  type: Boolean,
110
112
  default: false,
111
113
  },
114
+ /**
115
+ * Placeholder
116
+ */
117
+ placeholder: {
118
+ type: String,
119
+ default: undefined,
120
+ },
112
121
  /**
113
122
  * Line wrapping
114
123
  *
124
+ * An extension that enables line wrapping in the editor (by setting CSS white-space to pre-wrap in the content).
125
+ *
115
126
  * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5ElineWrapping | LineWrapping}
116
127
  */
117
128
  wrap: {
@@ -119,7 +130,7 @@ export default defineComponent({
119
130
  default: false,
120
131
  },
121
132
  /**
122
- * Tab handling
133
+ * Allow tab key indent.
123
134
  *
124
135
  * @see {@link https://codemirror.net/6/examples/tab/ | Tab Handling}
125
136
  */
@@ -137,13 +148,16 @@ export default defineComponent({
137
148
  default: false,
138
149
  },
139
150
  /**
140
- * Editable
151
+ * Disable input.
152
+ *
153
+ * This is the reversed value of the CodeMirror editable.
154
+ * Similar to `readonly`, but setting this value to true disables dragging.
141
155
  *
142
156
  * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5Eeditable | editable}
143
157
  */
144
- editable: {
158
+ disabled: {
145
159
  type: Boolean,
146
- default: true,
160
+ default: false,
147
161
  },
148
162
  /**
149
163
  * Additional Extension
@@ -183,19 +197,51 @@ export default defineComponent({
183
197
  type: Function as PropType<LintSource>,
184
198
  default: undefined,
185
199
  },
186
- /** Show Gutter */
187
- lintGutter: {
200
+ /**
201
+ * Linter Config
202
+ *
203
+ * @see {@link https://codemirror.net/docs/ref/#lint.linter^config}
204
+ */
205
+ linterConfig: {
206
+ type: Object,
207
+ default: () => {},
208
+ },
209
+ /**
210
+ * Show Linter Gutter
211
+ *
212
+ * An area to 🔴 the lines with errors will be displayed.
213
+ * This feature is not enabled if `linter` is not specified.
214
+ */
215
+ gutter: {
188
216
  type: Boolean,
189
217
  defalt: false,
190
218
  },
191
- /** Using tag */
219
+ /**
220
+ * Gutter Config
221
+ *
222
+ * @see {@link https://codemirror.net/docs/ref/#lint.lintGutter^config}
223
+ */
224
+ gutterConfig: {
225
+ type: Object,
226
+ default: () => {},
227
+ },
228
+ /**
229
+ * Using tag
230
+ */
192
231
  tag: {
193
232
  type: String,
194
233
  default: 'div',
195
234
  },
196
235
  },
197
236
  /** Emits */
198
- emits: ['update:modelValue', 'update', 'ready', 'focus', 'changed'],
237
+ emits: [
238
+ 'update:modelValue',
239
+ 'update',
240
+ 'ready',
241
+ 'focus',
242
+ 'changed',
243
+ 'diagnosticCount',
244
+ ],
199
245
  /**
200
246
  * Setup
201
247
  *
@@ -213,25 +259,25 @@ export default defineComponent({
213
259
  const view: ShallowRef<EditorView> = shallowRef(new EditorView());
214
260
 
215
261
  /** Selection */
216
- const selection: Ref<EditorSelection> = computed({
262
+ const selection: WritableComputedRef<EditorSelection> = computed({
217
263
  get: () => view.value.state.selection,
218
264
  set: s => view.value.dispatch({ selection: s }),
219
265
  });
220
266
 
221
267
  /** Cursor Position */
222
- const cursor: Ref<number> = computed({
268
+ const cursor: WritableComputedRef<number> = computed({
223
269
  get: () => selection.value.main.head || 0,
224
270
  set: a => view.value.dispatch({ selection: { anchor: a } }),
225
271
  });
226
272
 
227
273
  /** Editor State */
228
- const state: Ref<EditorState> = computed({
274
+ const state: WritableComputedRef<EditorState> = computed({
229
275
  get: () => view.value.state,
230
276
  set: s => view.value.setState(s),
231
277
  });
232
278
 
233
279
  /** Focus */
234
- const focus: Ref<boolean> = computed({
280
+ const focus: WritableComputedRef<boolean> = computed({
235
281
  get: () => view.value.hasFocus,
236
282
  set: f => {
237
283
  if (f) {
@@ -263,13 +309,17 @@ export default defineComponent({
263
309
  // Readonly option
264
310
  EditorState.readOnly.of(props.readonly),
265
311
  // Editable option
266
- EditorView.editable.of(props.editable),
312
+ EditorView.editable.of(!props.disabled),
267
313
  // Lang
268
- props.lang ? toRaw(props.lang) : undefined,
314
+ props.lang ? props.lang : undefined,
269
315
  // Append Linter settings
270
- props.linter ? linter(props.linter) : undefined,
316
+ props.linter ? linter(props.linter, props.linterConfig) : undefined,
271
317
  // Show 🔴 to error line when linter enabled.
272
- props.linter && props.lintGutter ? lintGutter() : undefined,
318
+ props.linter && props.gutter
319
+ ? lintGutter(props.gutterConfig)
320
+ : undefined,
321
+ // Placeholder
322
+ props.placeholder ? placeholder(props.placeholder) : undefined,
273
323
  // Append Extensions (such as basic-setup)
274
324
  ...props.extensions,
275
325
  ])
@@ -283,30 +333,33 @@ export default defineComponent({
283
333
  // IME fix
284
334
  return;
285
335
  }
286
- /** Previous cursor location */
287
- const previous = view.value.state.selection;
336
+ /** Current Editor State */
337
+ const current: EditorState = view.value.state;
288
338
 
339
+ if (props.linter) {
340
+ // Lint error count.
341
+ context.emit('diagnosticCount', diagnosticCount(current));
342
+ }
289
343
  // Update
290
344
  view.value.dispatch({
291
- changes: { from: 0, to: view.value.state.doc.length, insert: value },
292
- selection: previous,
345
+ changes: { from: 0, to: current.doc.length, insert: value },
346
+ selection: current.selection,
347
+ scrollIntoView: true,
293
348
  });
294
349
  },
295
350
  { immediate: true }
296
351
  );
297
352
 
298
353
  // Extension (mostly props) Changed
299
- watch(extensions, e => {
354
+ watch(extensions, exts =>
300
355
  // TODO: Reduce unchanched value
301
356
  view.value.dispatch({
302
- effects: StateEffect.reconfigure.of(e),
303
- });
304
- });
357
+ effects: StateEffect.reconfigure.of(exts),
358
+ })
359
+ );
305
360
 
306
361
  // focus changed
307
- watch(focus, isFocus => {
308
- context.emit('focus', isFocus);
309
- });
362
+ watch(focus, isFocus => context.emit('focus', isFocus));
310
363
 
311
364
  /** When loaded */
312
365
  onMounted(async () => {
@@ -347,7 +400,9 @@ export default defineComponent({
347
400
  });
348
401
 
349
402
  /** Destroy */
350
- onUnmounted(() => view.value.destroy());
403
+ onUnmounted(() => {
404
+ view.value.destroy();
405
+ });
351
406
 
352
407
  // Bellow is experimental.
353
408