vue-codemirror6 1.1.15 → 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,11 +4,19 @@ 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.15](https://github.com/logue/vue-codemirror6/compare/1.1.13...1.1.15)
7
+ #### [1.1.18](https://github.com/logue/vue-codemirror6/compare/1.1.16...1.1.18)
8
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
+
13
+ #### [1.1.16](https://github.com/logue/vue-codemirror6/compare/1.1.13...1.1.16)
14
+
15
+ > 18 March 2023
16
+
17
+ - Expose `view`. #16 [`daf3e6b`](https://github.com/logue/vue-codemirror6/commit/daf3e6b17a161a5c0c5a35ea2442d85421f05d82)
9
18
  - Fixed Maximum call stack size exceeded error. [`f10eaae`](https://github.com/logue/vue-codemirror6/commit/f10eaaea9145bc1d7a6605ab3cb8a82c0ca12df6)
10
19
  - Partially fixed the problem that the values defined in props disappeared under certain conditions. [`1cc4468`](https://github.com/logue/vue-codemirror6/commit/1cc4468e35e39ee8eaae1b2f007e3425a757217f)
11
- - Update CHANGELOG.md. [`f313ddc`](https://github.com/logue/vue-codemirror6/commit/f313ddc4d5e622b8ae4b52842178bb88149ec99c)
12
20
 
13
21
  #### [1.1.13](https://github.com/logue/vue-codemirror6/compare/1.1.12...1.1.13)
14
22
 
@@ -24,7 +32,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
24
32
 
25
33
  - chore(CODEOWNERS): CODEOWNERS.com Bot, at your service! [`#14`](https://github.com/logue/vue-codemirror6/pull/14)
26
34
  - Update demo code. (Replaced @codemirror/html to @codemirror/vue.) [`8da1a8a`](https://github.com/logue/vue-codemirror6/commit/8da1a8a4e37d713cc78281ce9c304c42a06940dc)
27
- - Changed implementation to generate \*.d.ts with vite-plugin-dts. [`1caaa20`](https://github.com/logue/vue-codemirror6/commit/1caaa20b1423a3d252a6c770a630fb60e2e3440d)
35
+ - Changed implementation to generate *.d.ts with vite-plugin-dts. [`1caaa20`](https://github.com/logue/vue-codemirror6/commit/1caaa20b1423a3d252a6c770a630fb60e2e3440d)
28
36
  - Update README.md. [`f8dd493`](https://github.com/logue/vue-codemirror6/commit/f8dd4934a7b9cc089cd830473db5250d28b3389b)
29
37
 
30
38
  #### [1.1.11](https://github.com/logue/vue-codemirror6/compare/1.1.3...1.1.11)
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.15
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 { 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)
@@ -38,7 +37,7 @@ function h(type, options = {}, chidren) {
38
37
  chidren
39
38
  );
40
39
  }
41
- const slot = (defaultSlots) => typeof defaultSlots == "function" ? defaultSlots() : defaultSlots;
40
+ const slot = (defaultSlots) => typeof defaultSlots === "function" ? defaultSlots() : defaultSlots;
42
41
 
43
42
  var CodeMirror = defineComponent({
44
43
  /** Component Name */
@@ -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,30 +252,30 @@ 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-ignore
262
275
  setup(props, context) {
263
276
  const editor = ref();
264
277
  const doc = ref(props.modelValue);
265
278
  const view = shallowRef(new EditorView());
266
- const selection = computed({
267
- get: () => view.value.state.selection,
268
- set: (s) => view.value.dispatch({ selection: s })
269
- });
270
- const state = computed({
271
- get: () => view.value.state,
272
- set: (s) => view.value.setState(s)
273
- });
274
- const cursor = computed({
275
- get: () => view.value.state.selection.main.head || 0,
276
- set: (a) => view.value.dispatch({ selection: { anchor: a } })
277
- });
278
279
  const focus = computed({
279
280
  get: () => view.value.hasFocus,
280
281
  set: (f) => {
@@ -283,21 +284,45 @@ var CodeMirror = defineComponent({
283
284
  }
284
285
  }
285
286
  });
286
- const length = computed(
287
- () => view.value.state.doc.length
287
+ const selection = computed({
288
+ get: () => view.value.state.selection,
289
+ set: (s) => view.value.dispatch({ selection: s })
290
+ });
291
+ const cursor = computed({
292
+ get: () => view.value.state.selection.main.head,
293
+ set: (a) => view.value.dispatch({ selection: { anchor: a } })
294
+ });
295
+ const json = computed(
296
+ {
297
+ get: () => view.value.state.toJSON(),
298
+ set: (j) => view.value.setState(EditorState.fromJSON(j))
299
+ }
288
300
  );
301
+ const length = ref(0);
302
+ const diagnosticCount$1 = ref(0);
303
+ const language = new Compartment();
304
+ const tabSize = new Compartment();
289
305
  const extensions = computed(
290
306
  () => (
291
307
  // TODO: Ignore previous prop was not changed.
292
- compact([
308
+ [
293
309
  // Toggle basic setup
294
310
  props.basic ? basicSetup : void 0,
295
311
  // Toggle minimal setup
296
312
  props.minimal && !props.basic ? minimalSetup : void 0,
297
313
  // ViewUpdate event listener
298
- EditorView.updateListener.of(
299
- (update) => context.emit("update", update)
300
- ),
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
+ }),
301
326
  // Toggle light/dark mode.
302
327
  EditorView.theme(props.theme, { dark: props.dark }),
303
328
  // Toggle line wrapping
@@ -307,7 +332,7 @@ var CodeMirror = defineComponent({
307
332
  // Allow Multiple Selections
308
333
  EditorState.allowMultipleSelections.of(props.allowMultipleSelections),
309
334
  // Indent tab size
310
- props.tabSize ? EditorState.tabSize.of(props.tabSize) : void 0,
335
+ props.tabSize ? tabSize.of(EditorState.tabSize.of(props.tabSize)) : void 0,
311
336
  // locale settings
312
337
  props.phrases ? EditorState.phrases.of(props.phrases) : void 0,
313
338
  // Readonly option
@@ -317,7 +342,7 @@ var CodeMirror = defineComponent({
317
342
  // Set Line break char
318
343
  props.lineSeparator ? EditorState.lineSeparator.of(props.lineSeparator) : void 0,
319
344
  // Lang
320
- props.lang ? props.lang.extension : void 0,
345
+ props.lang ? language.of(props.lang) : void 0,
321
346
  // Append Linter settings
322
347
  props.linter ? linter(props.linter, props.linterConfig) : void 0,
323
348
  // Show 🔴 to error line when linter enabled.
@@ -326,96 +351,105 @@ var CodeMirror = defineComponent({
326
351
  props.placeholder ? placeholder(props.placeholder) : void 0,
327
352
  // Append Extensions
328
353
  ...props.extensions
329
- ])
354
+ ].filter((extension) => !!extension)
330
355
  )
331
356
  );
357
+ watch(
358
+ extensions,
359
+ (exts) => {
360
+ view.value?.dispatch({
361
+ effects: StateEffect.reconfigure.of(exts)
362
+ });
363
+ },
364
+ { immediate: true }
365
+ );
332
366
  watch(
333
367
  () => props.modelValue,
334
- (value) => {
368
+ async (value) => {
335
369
  if (view.value.composing) {
336
370
  return;
337
371
  }
338
- if (props.linter && props.forceLinting) {
339
- forceLinting(view.value);
340
- }
341
372
  view.value.dispatch({
342
373
  changes: { from: 0, to: view.value.state.doc.length, insert: value },
343
374
  selection: view.value.state.selection,
344
375
  scrollIntoView: true
345
376
  });
377
+ length.value = view.value.state.doc.length;
346
378
  },
347
379
  { immediate: true }
348
380
  );
349
- watch(
350
- () => extensions.value,
351
- (exts) => {
352
- view.value.dispatch({
353
- effects: StateEffect.reconfigure.of(exts)
354
- });
355
- }
356
- );
357
381
  watch(focus, (isFocus) => context.emit("focus", isFocus));
358
382
  onMounted(async () => {
359
383
  let value = doc.value;
360
- if (editor.value && editor.value.childNodes[0]) {
384
+ if (!editor.value) {
385
+ return;
386
+ }
387
+ if (editor.value.childNodes[0]) {
361
388
  if (doc.value !== "") {
362
389
  console.warn(
363
390
  "[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."
364
391
  );
365
392
  }
366
- value = trim(editor.value.childNodes[0].innerText);
393
+ value = editor.value.childNodes[0].innerText.trim();
367
394
  }
368
395
  view.value = new EditorView({
369
- doc: value,
370
- extensions: extensions.value,
371
396
  parent: editor.value,
397
+ state: EditorState.create({ doc: value, extensions: extensions.value }),
372
398
  dispatch: (tr) => {
373
399
  view.value.update([tr]);
374
- if (tr.changes.empty)
400
+ if (tr.changes.empty || !tr.docChanged) {
375
401
  return;
376
- const v = view.value.state.doc.toString();
377
- context.emit("update:modelValue", v);
378
- context.emit("change", view.value.state);
402
+ }
403
+ context.emit("update:modelValue", tr.state.doc.toString());
404
+ context.emit("change", tr.state);
379
405
  }
380
406
  });
381
407
  await nextTick();
382
408
  context.emit("ready", {
383
- view,
409
+ view: view.value,
384
410
  state: view.value.state,
385
411
  container: editor.value
386
412
  });
387
413
  });
388
414
  onUnmounted(() => {
389
- if (view.value) {
390
- view.value.destroy();
391
- context.emit("destroy");
392
- }
415
+ view.value.destroy();
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)
@@ -435,11 +469,13 @@ var CodeMirror = defineComponent({
435
469
  });
436
470
  context.expose({
437
471
  editor,
472
+ view,
438
473
  cursor,
439
474
  selection,
440
- state,
441
475
  focus,
442
476
  length,
477
+ json,
478
+ diagnosticCount: diagnosticCount$1,
443
479
  lint,
444
480
  forceReconfigure,
445
481
  // Bellow is CodeMirror5's function
@@ -464,19 +500,16 @@ var CodeMirror = defineComponent({
464
500
  },
465
501
  render() {
466
502
  return h(
467
- // @ts-ignore
468
503
  this.$props.tag,
469
504
  {
470
505
  ref: "editor",
471
506
  class: "vue-codemirror"
472
507
  },
473
- // @ts-ignore
474
508
  this.$slots.default ? (
475
509
  // Hide original content
476
510
  h(
477
511
  "aside",
478
512
  { style: "display: none;", "aria-hidden": "true" },
479
- // @ts-ignore
480
513
  slot(this.$slots.default)
481
514
  )
482
515
  ) : void 0
@@ -485,8 +518,8 @@ var CodeMirror = defineComponent({
485
518
  });
486
519
 
487
520
  const meta = {
488
- version: "1.1.15",
489
- date: "2023-03-14T09:58:31.925Z"
521
+ version: "1.1.18",
522
+ date: "2023-04-04T00:08:06.631Z"
490
523
  };
491
524
 
492
525
  const installCodeMirror = (app) => app.component("CodeMirror", CodeMirror);