vue-codemirror6 1.1.16 → 1.1.18

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 CHANGED
@@ -4,8 +4,16 @@ All notable changes to this project will be documented in this file. Dates are d
4
4
 
5
5
  Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
6
6
 
7
+ #### [1.1.18](https://github.com/logue/vue-codemirror6/compare/1.1.16...1.1.18)
8
+
9
+ - Fix for #17. [`5a45c9d`](https://github.com/logue/vue-codemirror6/commit/5a45c9d05d7c49ec5f738bc0af5317a6b6405e70)
10
+ - Changed diagnostic counting method. Implemented to hook to ViewUpdate. [`cba57dd`](https://github.com/logue/vue-codemirror6/commit/cba57dd00333cbec27849776abd5f4f98cd4720d)
11
+ - Fixed a bug that was updating the state twice in the binding process to the parent direction. [`2c303cd`](https://github.com/logue/vue-codemirror6/commit/2c303cd9616ada6d3ac07c038baa30b9de65a9ef)
12
+
7
13
  #### [1.1.16](https://github.com/logue/vue-codemirror6/compare/1.1.13...1.1.16)
8
14
 
15
+ > 18 March 2023
16
+
9
17
  - Expose `view`. #16 [`daf3e6b`](https://github.com/logue/vue-codemirror6/commit/daf3e6b17a161a5c0c5a35ea2442d85421f05d82)
10
18
  - Fixed Maximum call stack size exceeded error. [`f10eaae`](https://github.com/logue/vue-codemirror6/commit/f10eaaea9145bc1d7a6605ab3cb8a82c0ca12df6)
11
19
  - Partially fixed the problem that the values defined in props disappeared under certain conditions. [`1cc4468`](https://github.com/logue/vue-codemirror6/commit/1cc4468e35e39ee8eaae1b2f007e3425a757217f)
package/README.md CHANGED
@@ -321,20 +321,40 @@ export default defineComponent({
321
321
  | update | When CodeMirror state changed. Returns [ViewUpdate](https://codemirror.net/docs/ref/#view.ViewUpdate) object. |
322
322
  | change | Value changed. Returns [EditorState](https://codemirror.net/docs/ref/#state.EditorState) |
323
323
 
324
- ## Methods
324
+ ## Parameter / Function
325
325
 
326
- | Method | Description |
327
- | ---------------- | --------------------------------------------------------------------------------------------------- |
328
- | selection | Get and set the [EditorSelection](https://codemirror.net/docs/ref/#state.EditorSelection) instance. |
329
- | cursor | Get and set the [cursor](https://codemirror.net/docs/ref/#state.EditorSelection^cursor) location. |
330
- | state | Get and set [EditorState](https://codemirror.net/docs/ref/#state.EditorState). |
331
- | focus | Get and set [focus](https://codemirror.net/docs/ref/#view.EditorView.focus). |
332
- | lint | Force run linter (Only if `linter` prop is specified) |
333
- | forceReconfigure | Re register all extensions. |
326
+ ```vue
327
+ <script setup lang="ts">
328
+ import { ref, onMounted, type Ref, type PropType } from 'vue';
329
+ import CodeMirror from 'vue-codemirror6';
330
+
331
+ const cm: Ref<InstanceType<typeof CodeMirror> | undefined> = ref();
332
+
333
+ onMounted(() => {
334
+ console.log(cm.value?.json);
335
+ });
336
+ </script>
337
+ <template>
338
+ <code-mirror ref="cm" />
339
+ </template>
340
+ ```
341
+
342
+ | Function / Parameter | Description |
343
+ | -------------------- | --------------------------------------------------------------------------------------------------- |
344
+ | view | Get and set [EditorView](https://codemirror.net/docs/ref/#view.EditorView). |
345
+ | selection | Get and set the [EditorSelection](https://codemirror.net/docs/ref/#state.EditorSelection) instance. |
346
+ | cursor | Get and set the [cursor](https://codemirror.net/docs/ref/#state.EditorSelection^cursor) location. |
347
+ | json | Get and set state to a JSON-serializable object. |
348
+ | focus | Get and set [focus](https://codemirror.net/docs/ref/#view.EditorView.focus). |
349
+ | lint() | Force run linter (Only if `linter` prop is specified) |
350
+ | forceReconfigure() | Re register all extensions. |
351
+
352
+ ### CodeMirror5 backward compatible functions
334
353
 
335
- The instructions below are compatible methods for those familiar with [codemirror5](https://codemirror.net/5/). Since the above method is usually sufficient, its active use is not recommended.
354
+ The instructions below are compatible methods for those familiar with [codemirror5](https://codemirror.net/5/).
355
+ Since the above method is usually sufficient, its **active use is not recommended**.
336
356
 
337
- | Method | Description |
357
+ | Function | Description |
338
358
  | ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
339
359
  | getRange(from?: number, to?: number) | Get the text between the given points in the editor. |
340
360
  | getLine(number: number) | Get the content of line. |
package/dist/index.es.js CHANGED
@@ -5,17 +5,16 @@
5
5
  * @author Logue <logue@hotmail.co.jp>
6
6
  * @copyright 2022-2023 By Masashi Yoshikawa All rights reserved.
7
7
  * @license MIT
8
- * @version 1.1.16
8
+ * @version 1.1.18
9
9
  * @see {@link https://github.com/logue/vue-codemirror6}
10
10
  */
11
11
 
12
12
  import { isVue2, h as h$1, defineComponent, ref, shallowRef, computed, watch, onMounted, nextTick, onUnmounted } from 'vue-demi';
13
- import { compact, trim } from 'lodash';
14
13
  import { basicSetup, minimalSetup } from 'codemirror';
15
- import { Compartment, EditorState, StateEffect, EditorSelection } from '@codemirror/state';
16
14
  import { EditorView, keymap, placeholder } from '@codemirror/view';
15
+ import { EditorState, Compartment, StateEffect, EditorSelection } from '@codemirror/state';
16
+ import { forceLinting, linter, lintGutter, diagnosticCount } from '@codemirror/lint';
17
17
  import { indentWithTab } from '@codemirror/commands';
18
- import { linter, lintGutter, forceLinting } from '@codemirror/lint';
19
18
 
20
19
  const adaptOnsV3 = (ons) => {
21
20
  if (!ons)
@@ -210,7 +209,9 @@ var CodeMirror = defineComponent({
210
209
  */
211
210
  linterConfig: {
212
211
  type: Object,
213
- default: () => void 0
212
+ default: () => {
213
+ return { needsRefresh: true };
214
+ }
214
215
  },
215
216
  /**
216
217
  * Forces any linters configured to run when the editor is idle to run right away.
@@ -251,55 +252,77 @@ var CodeMirror = defineComponent({
251
252
  }
252
253
  },
253
254
  /** Emits */
254
- emits: ["update:modelValue", "update", "ready", "focus", "change", "destroy"],
255
+ emits: {
256
+ /** Model Update */
257
+ "update:modelValue": (value) => true,
258
+ /** CodeMirror ViewUpdate */
259
+ update: (value) => true,
260
+ /** CodeMirror onReady */
261
+ ready: (value) => true,
262
+ /** CodeMirror onFocus */
263
+ focus: (value) => true,
264
+ /** State Changed */
265
+ change: (value) => true,
266
+ /** CodeMirror onDestroy */
267
+ destroy: () => true
268
+ },
255
269
  /**
256
270
  * Setup
257
271
  *
258
272
  * @param props - Props
259
273
  * @param context - Context
260
274
  */
261
- // @ts-expect-error
262
275
  setup(props, context) {
263
276
  const editor = ref();
264
277
  const doc = ref(props.modelValue);
265
278
  const view = shallowRef(new EditorView());
279
+ const focus = computed({
280
+ get: () => view.value.hasFocus,
281
+ set: (f) => {
282
+ if (f) {
283
+ view.value.focus();
284
+ }
285
+ }
286
+ });
266
287
  const selection = computed({
267
288
  get: () => view.value.state.selection,
268
289
  set: (s) => view.value.dispatch({ selection: s })
269
290
  });
270
- const state = computed({
271
- get: () => view.value.state,
272
- set: (s) => view.value.setState(s)
273
- });
274
291
  const cursor = computed({
275
292
  get: () => view.value.state.selection.main.head,
276
293
  set: (a) => view.value.dispatch({ selection: { anchor: a } })
277
294
  });
278
- const focus = computed({
279
- get: () => view.value.hasFocus,
280
- set: (f) => {
281
- if (f) {
282
- view.value.focus();
283
- }
295
+ const json = computed(
296
+ {
297
+ get: () => view.value.state.toJSON(),
298
+ set: (j) => view.value.setState(EditorState.fromJSON(j))
284
299
  }
285
- });
286
- const length = computed(
287
- () => view.value.state.doc.length
288
300
  );
301
+ const length = ref(0);
302
+ const diagnosticCount$1 = ref(0);
289
303
  const language = new Compartment();
290
304
  const tabSize = new Compartment();
291
305
  const extensions = computed(
292
306
  () => (
293
307
  // TODO: Ignore previous prop was not changed.
294
- compact([
308
+ [
295
309
  // Toggle basic setup
296
310
  props.basic ? basicSetup : void 0,
297
311
  // Toggle minimal setup
298
312
  props.minimal && !props.basic ? minimalSetup : void 0,
299
313
  // ViewUpdate event listener
300
- EditorView.updateListener.of(
301
- (update) => context.emit("update", update)
302
- ),
314
+ EditorView.updateListener.of((update) => {
315
+ if (update.changes.empty || !update.docChanged) {
316
+ return;
317
+ }
318
+ if (props.linter) {
319
+ if (props.forceLinting) {
320
+ forceLinting(view.value);
321
+ }
322
+ diagnosticCount$1.value = props.linter(view.value).length;
323
+ }
324
+ context.emit("update", update);
325
+ }),
303
326
  // Toggle light/dark mode.
304
327
  EditorView.theme(props.theme, { dark: props.dark }),
305
328
  // Toggle line wrapping
@@ -328,61 +351,62 @@ var CodeMirror = defineComponent({
328
351
  props.placeholder ? placeholder(props.placeholder) : void 0,
329
352
  // Append Extensions
330
353
  ...props.extensions
331
- ])
354
+ ].filter((extension) => !!extension)
332
355
  )
333
356
  );
357
+ watch(
358
+ extensions,
359
+ (exts) => {
360
+ view.value?.dispatch({
361
+ effects: StateEffect.reconfigure.of(exts)
362
+ });
363
+ },
364
+ { immediate: true }
365
+ );
334
366
  watch(
335
367
  () => props.modelValue,
336
- (value) => {
368
+ async (value) => {
337
369
  if (view.value.composing) {
338
370
  return;
339
371
  }
340
- if (props.linter && props.forceLinting) {
341
- forceLinting(view.value);
342
- }
343
372
  view.value.dispatch({
344
373
  changes: { from: 0, to: view.value.state.doc.length, insert: value },
345
374
  selection: view.value.state.selection,
346
375
  scrollIntoView: true
347
376
  });
377
+ length.value = view.value.state.doc.length;
348
378
  },
349
379
  { immediate: true }
350
380
  );
351
- watch(
352
- () => extensions.value,
353
- (exts) => {
354
- view.value.dispatch({
355
- effects: StateEffect.reconfigure.of(exts)
356
- });
357
- }
358
- );
359
381
  watch(focus, (isFocus) => context.emit("focus", isFocus));
360
382
  onMounted(async () => {
361
383
  let value = doc.value;
362
- if (editor.value?.childNodes[0]) {
384
+ if (!editor.value) {
385
+ return;
386
+ }
387
+ if (editor.value.childNodes[0]) {
363
388
  if (doc.value !== "") {
364
389
  console.warn(
365
390
  "[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."
366
391
  );
367
392
  }
368
- value = trim(editor.value.childNodes[0].innerText);
393
+ value = editor.value.childNodes[0].innerText.trim();
369
394
  }
370
395
  view.value = new EditorView({
371
- doc: value,
372
- extensions: extensions.value,
373
396
  parent: editor.value,
397
+ state: EditorState.create({ doc: value, extensions: extensions.value }),
374
398
  dispatch: (tr) => {
375
399
  view.value.update([tr]);
376
- if (tr.changes.empty)
400
+ if (tr.changes.empty || !tr.docChanged) {
377
401
  return;
378
- const v = view.value.state.doc.toString();
379
- context.emit("update:modelValue", v);
380
- context.emit("change", view.value.state);
402
+ }
403
+ context.emit("update:modelValue", tr.state.doc.toString());
404
+ context.emit("change", tr.state);
381
405
  }
382
406
  });
383
407
  await nextTick();
384
408
  context.emit("ready", {
385
- view,
409
+ view: view.value,
386
410
  state: view.value.state,
387
411
  container: editor.value
388
412
  });
@@ -392,30 +416,40 @@ var CodeMirror = defineComponent({
392
416
  context.emit("destroy");
393
417
  });
394
418
  const lint = () => {
395
- if (props.linter) {
419
+ if (!props.linter || !view.value) {
420
+ return;
421
+ }
422
+ if (props.forceLinting) {
396
423
  forceLinting(view.value);
397
424
  }
425
+ diagnosticCount$1.value = diagnosticCount(view.value.state);
398
426
  };
399
427
  const forceReconfigure = () => {
400
- view.value.dispatch({
428
+ view.value?.dispatch({
401
429
  effects: StateEffect.reconfigure.of([])
402
430
  });
403
- view.value.dispatch({
431
+ view.value?.dispatch({
404
432
  effects: StateEffect.appendConfig.of(extensions.value)
405
433
  });
406
434
  };
407
435
  const getRange = (from, to) => view.value.state.sliceDoc(from, to);
408
436
  const getLine = (number) => view.value.state.doc.line(number + 1).text;
409
437
  const lineCount = () => view.value.state.doc.lines;
410
- const getCursor = () => cursor.value;
411
- const listSelections = () => view.value.state.selection.ranges;
438
+ const getCursor = () => view.value.state.selection.main.head;
439
+ const listSelections = () => view.value.state.selection.ranges ?? [];
412
440
  const getSelection = () => view.value.state.sliceDoc(
413
441
  view.value.state.selection.main.from,
414
442
  view.value.state.selection.main.to
415
- );
416
- const getSelections = () => view.value.state.selection.ranges.map(
417
- (r) => view.value.state.sliceDoc(r.from, r.to)
418
- );
443
+ ) ?? "";
444
+ const getSelections = () => {
445
+ const s = view.value.state;
446
+ if (!s) {
447
+ return [];
448
+ }
449
+ return s.selection.ranges.map(
450
+ (r) => s.sliceDoc(r.from, r.to)
451
+ );
452
+ };
419
453
  const somethingSelected = () => view.value.state.selection.ranges.some(
420
454
  (r) => !r.empty
421
455
  );
@@ -423,7 +457,7 @@ var CodeMirror = defineComponent({
423
457
  changes: { from, to, insert: replacement }
424
458
  });
425
459
  const replaceSelection = (replacement) => view.value.dispatch(view.value.state.replaceSelection(replacement));
426
- const setCursor = (position) => cursor.value = position;
460
+ const setCursor = (position) => view.value.dispatch({ selection: { anchor: position } });
427
461
  const setSelection = (anchor, head) => view.value.dispatch({ selection: { anchor, head } });
428
462
  const setSelections = (ranges, primary) => view.value.dispatch({
429
463
  selection: EditorSelection.create(ranges, primary)
@@ -438,9 +472,10 @@ var CodeMirror = defineComponent({
438
472
  view,
439
473
  cursor,
440
474
  selection,
441
- state,
442
475
  focus,
443
476
  length,
477
+ json,
478
+ diagnosticCount: diagnosticCount$1,
444
479
  lint,
445
480
  forceReconfigure,
446
481
  // Bellow is CodeMirror5's function
@@ -465,19 +500,16 @@ var CodeMirror = defineComponent({
465
500
  },
466
501
  render() {
467
502
  return h(
468
- // @ts-expect-error
469
503
  this.$props.tag,
470
504
  {
471
505
  ref: "editor",
472
506
  class: "vue-codemirror"
473
507
  },
474
- // @ts-expect-error
475
508
  this.$slots.default ? (
476
509
  // Hide original content
477
510
  h(
478
511
  "aside",
479
512
  { style: "display: none;", "aria-hidden": "true" },
480
- // @ts-expect-error
481
513
  slot(this.$slots.default)
482
514
  )
483
515
  ) : void 0
@@ -486,8 +518,8 @@ var CodeMirror = defineComponent({
486
518
  });
487
519
 
488
520
  const meta = {
489
- version: "1.1.16",
490
- date: "2023-03-18T00:54:23.388Z"
521
+ version: "1.1.18",
522
+ date: "2023-04-04T00:08:06.631Z"
491
523
  };
492
524
 
493
525
  const installCodeMirror = (app) => app.component("CodeMirror", CodeMirror);