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/dist/index.umd.js CHANGED
@@ -5,15 +5,15 @@
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
  (function (global, factory) {
13
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vue-demi'), require('lodash'), require('codemirror'), require('@codemirror/state'), require('@codemirror/view'), require('@codemirror/commands'), require('@codemirror/lint')) :
14
- typeof define === 'function' && define.amd ? define(['exports', 'vue-demi', 'lodash', 'codemirror', '@codemirror/state', '@codemirror/view', '@codemirror/commands', '@codemirror/lint'], factory) :
15
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.CodeMirror = {}, global.VueDemi, global.lodash, global.codemirror, global.state, global.view, global.commands, global.lint));
16
- })(this, (function (exports, vueDemi, lodash, codemirror, state, view, commands, lint) { 'use strict';
13
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vue-demi'), require('codemirror'), require('@codemirror/view'), require('@codemirror/state'), require('@codemirror/lint'), require('@codemirror/commands')) :
14
+ typeof define === 'function' && define.amd ? define(['exports', 'vue-demi', 'codemirror', '@codemirror/view', '@codemirror/state', '@codemirror/lint', '@codemirror/commands'], factory) :
15
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.CodeMirror = {}, global.VueDemi, global.codemirror, global.view, global.state, global.lint, global.commands));
16
+ })(this, (function (exports, vueDemi, codemirror, view, state, lint, commands) { 'use strict';
17
17
 
18
18
  const adaptOnsV3 = (ons) => {
19
19
  if (!ons)
@@ -36,7 +36,7 @@
36
36
  chidren
37
37
  );
38
38
  }
39
- const slot = (defaultSlots) => typeof defaultSlots == "function" ? defaultSlots() : defaultSlots;
39
+ const slot = (defaultSlots) => typeof defaultSlots === "function" ? defaultSlots() : defaultSlots;
40
40
 
41
41
  var CodeMirror = vueDemi.defineComponent({
42
42
  /** Component Name */
@@ -208,7 +208,9 @@
208
208
  */
209
209
  linterConfig: {
210
210
  type: Object,
211
- default: () => void 0
211
+ default: () => {
212
+ return { needsRefresh: true };
213
+ }
212
214
  },
213
215
  /**
214
216
  * Forces any linters configured to run when the editor is idle to run right away.
@@ -249,30 +251,30 @@
249
251
  }
250
252
  },
251
253
  /** Emits */
252
- emits: ["update:modelValue", "update", "ready", "focus", "change", "destroy"],
254
+ emits: {
255
+ /** Model Update */
256
+ "update:modelValue": (value) => true,
257
+ /** CodeMirror ViewUpdate */
258
+ update: (value) => true,
259
+ /** CodeMirror onReady */
260
+ ready: (value) => true,
261
+ /** CodeMirror onFocus */
262
+ focus: (value) => true,
263
+ /** State Changed */
264
+ change: (value) => true,
265
+ /** CodeMirror onDestroy */
266
+ destroy: () => true
267
+ },
253
268
  /**
254
269
  * Setup
255
270
  *
256
271
  * @param props - Props
257
272
  * @param context - Context
258
273
  */
259
- // @ts-ignore
260
274
  setup(props, context) {
261
275
  const editor = vueDemi.ref();
262
276
  const doc = vueDemi.ref(props.modelValue);
263
277
  const view$1 = vueDemi.shallowRef(new view.EditorView());
264
- const selection = vueDemi.computed({
265
- get: () => view$1.value.state.selection,
266
- set: (s) => view$1.value.dispatch({ selection: s })
267
- });
268
- const state$1 = vueDemi.computed({
269
- get: () => view$1.value.state,
270
- set: (s) => view$1.value.setState(s)
271
- });
272
- const cursor = vueDemi.computed({
273
- get: () => view$1.value.state.selection.main.head || 0,
274
- set: (a) => view$1.value.dispatch({ selection: { anchor: a } })
275
- });
276
278
  const focus = vueDemi.computed({
277
279
  get: () => view$1.value.hasFocus,
278
280
  set: (f) => {
@@ -281,21 +283,45 @@
281
283
  }
282
284
  }
283
285
  });
284
- const length = vueDemi.computed(
285
- () => view$1.value.state.doc.length
286
+ const selection = vueDemi.computed({
287
+ get: () => view$1.value.state.selection,
288
+ set: (s) => view$1.value.dispatch({ selection: s })
289
+ });
290
+ const cursor = vueDemi.computed({
291
+ get: () => view$1.value.state.selection.main.head,
292
+ set: (a) => view$1.value.dispatch({ selection: { anchor: a } })
293
+ });
294
+ const json = vueDemi.computed(
295
+ {
296
+ get: () => view$1.value.state.toJSON(),
297
+ set: (j) => view$1.value.setState(state.EditorState.fromJSON(j))
298
+ }
286
299
  );
300
+ const length = vueDemi.ref(0);
301
+ const diagnosticCount = vueDemi.ref(0);
302
+ const language = new state.Compartment();
303
+ const tabSize = new state.Compartment();
287
304
  const extensions = vueDemi.computed(
288
305
  () => (
289
306
  // TODO: Ignore previous prop was not changed.
290
- lodash.compact([
307
+ [
291
308
  // Toggle basic setup
292
309
  props.basic ? codemirror.basicSetup : void 0,
293
310
  // Toggle minimal setup
294
311
  props.minimal && !props.basic ? codemirror.minimalSetup : void 0,
295
312
  // ViewUpdate event listener
296
- view.EditorView.updateListener.of(
297
- (update) => context.emit("update", update)
298
- ),
313
+ view.EditorView.updateListener.of((update) => {
314
+ if (update.changes.empty || !update.docChanged) {
315
+ return;
316
+ }
317
+ if (props.linter) {
318
+ if (props.forceLinting) {
319
+ lint.forceLinting(view$1.value);
320
+ }
321
+ diagnosticCount.value = props.linter(view$1.value).length;
322
+ }
323
+ context.emit("update", update);
324
+ }),
299
325
  // Toggle light/dark mode.
300
326
  view.EditorView.theme(props.theme, { dark: props.dark }),
301
327
  // Toggle line wrapping
@@ -305,7 +331,7 @@
305
331
  // Allow Multiple Selections
306
332
  state.EditorState.allowMultipleSelections.of(props.allowMultipleSelections),
307
333
  // Indent tab size
308
- props.tabSize ? state.EditorState.tabSize.of(props.tabSize) : void 0,
334
+ props.tabSize ? tabSize.of(state.EditorState.tabSize.of(props.tabSize)) : void 0,
309
335
  // locale settings
310
336
  props.phrases ? state.EditorState.phrases.of(props.phrases) : void 0,
311
337
  // Readonly option
@@ -315,7 +341,7 @@
315
341
  // Set Line break char
316
342
  props.lineSeparator ? state.EditorState.lineSeparator.of(props.lineSeparator) : void 0,
317
343
  // Lang
318
- props.lang ? props.lang.extension : void 0,
344
+ props.lang ? language.of(props.lang) : void 0,
319
345
  // Append Linter settings
320
346
  props.linter ? lint.linter(props.linter, props.linterConfig) : void 0,
321
347
  // Show 🔴 to error line when linter enabled.
@@ -324,96 +350,105 @@
324
350
  props.placeholder ? view.placeholder(props.placeholder) : void 0,
325
351
  // Append Extensions
326
352
  ...props.extensions
327
- ])
353
+ ].filter((extension) => !!extension)
328
354
  )
329
355
  );
356
+ vueDemi.watch(
357
+ extensions,
358
+ (exts) => {
359
+ view$1.value?.dispatch({
360
+ effects: state.StateEffect.reconfigure.of(exts)
361
+ });
362
+ },
363
+ { immediate: true }
364
+ );
330
365
  vueDemi.watch(
331
366
  () => props.modelValue,
332
- (value) => {
367
+ async (value) => {
333
368
  if (view$1.value.composing) {
334
369
  return;
335
370
  }
336
- if (props.linter && props.forceLinting) {
337
- lint.forceLinting(view$1.value);
338
- }
339
371
  view$1.value.dispatch({
340
372
  changes: { from: 0, to: view$1.value.state.doc.length, insert: value },
341
373
  selection: view$1.value.state.selection,
342
374
  scrollIntoView: true
343
375
  });
376
+ length.value = view$1.value.state.doc.length;
344
377
  },
345
378
  { immediate: true }
346
379
  );
347
- vueDemi.watch(
348
- () => extensions.value,
349
- (exts) => {
350
- view$1.value.dispatch({
351
- effects: state.StateEffect.reconfigure.of(exts)
352
- });
353
- }
354
- );
355
380
  vueDemi.watch(focus, (isFocus) => context.emit("focus", isFocus));
356
381
  vueDemi.onMounted(async () => {
357
382
  let value = doc.value;
358
- if (editor.value && editor.value.childNodes[0]) {
383
+ if (!editor.value) {
384
+ return;
385
+ }
386
+ if (editor.value.childNodes[0]) {
359
387
  if (doc.value !== "") {
360
388
  console.warn(
361
389
  "[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."
362
390
  );
363
391
  }
364
- value = lodash.trim(editor.value.childNodes[0].innerText);
392
+ value = editor.value.childNodes[0].innerText.trim();
365
393
  }
366
394
  view$1.value = new view.EditorView({
367
- doc: value,
368
- extensions: extensions.value,
369
395
  parent: editor.value,
396
+ state: state.EditorState.create({ doc: value, extensions: extensions.value }),
370
397
  dispatch: (tr) => {
371
398
  view$1.value.update([tr]);
372
- if (tr.changes.empty)
399
+ if (tr.changes.empty || !tr.docChanged) {
373
400
  return;
374
- const v = view$1.value.state.doc.toString();
375
- context.emit("update:modelValue", v);
376
- context.emit("change", view$1.value.state);
401
+ }
402
+ context.emit("update:modelValue", tr.state.doc.toString());
403
+ context.emit("change", tr.state);
377
404
  }
378
405
  });
379
406
  await vueDemi.nextTick();
380
407
  context.emit("ready", {
381
- view: view$1,
408
+ view: view$1.value,
382
409
  state: view$1.value.state,
383
410
  container: editor.value
384
411
  });
385
412
  });
386
413
  vueDemi.onUnmounted(() => {
387
- if (view$1.value) {
388
- view$1.value.destroy();
389
- context.emit("destroy");
390
- }
414
+ view$1.value.destroy();
415
+ context.emit("destroy");
391
416
  });
392
417
  const lint$1 = () => {
393
- if (props.linter) {
418
+ if (!props.linter || !view$1.value) {
419
+ return;
420
+ }
421
+ if (props.forceLinting) {
394
422
  lint.forceLinting(view$1.value);
395
423
  }
424
+ diagnosticCount.value = lint.diagnosticCount(view$1.value.state);
396
425
  };
397
426
  const forceReconfigure = () => {
398
- view$1.value.dispatch({
427
+ view$1.value?.dispatch({
399
428
  effects: state.StateEffect.reconfigure.of([])
400
429
  });
401
- view$1.value.dispatch({
430
+ view$1.value?.dispatch({
402
431
  effects: state.StateEffect.appendConfig.of(extensions.value)
403
432
  });
404
433
  };
405
434
  const getRange = (from, to) => view$1.value.state.sliceDoc(from, to);
406
435
  const getLine = (number) => view$1.value.state.doc.line(number + 1).text;
407
436
  const lineCount = () => view$1.value.state.doc.lines;
408
- const getCursor = () => cursor.value;
409
- const listSelections = () => view$1.value.state.selection.ranges;
437
+ const getCursor = () => view$1.value.state.selection.main.head;
438
+ const listSelections = () => view$1.value.state.selection.ranges ?? [];
410
439
  const getSelection = () => view$1.value.state.sliceDoc(
411
440
  view$1.value.state.selection.main.from,
412
441
  view$1.value.state.selection.main.to
413
- );
414
- const getSelections = () => view$1.value.state.selection.ranges.map(
415
- (r) => view$1.value.state.sliceDoc(r.from, r.to)
416
- );
442
+ ) ?? "";
443
+ const getSelections = () => {
444
+ const s = view$1.value.state;
445
+ if (!s) {
446
+ return [];
447
+ }
448
+ return s.selection.ranges.map(
449
+ (r) => s.sliceDoc(r.from, r.to)
450
+ );
451
+ };
417
452
  const somethingSelected = () => view$1.value.state.selection.ranges.some(
418
453
  (r) => !r.empty
419
454
  );
@@ -421,7 +456,7 @@
421
456
  changes: { from, to, insert: replacement }
422
457
  });
423
458
  const replaceSelection = (replacement) => view$1.value.dispatch(view$1.value.state.replaceSelection(replacement));
424
- const setCursor = (position) => cursor.value = position;
459
+ const setCursor = (position) => view$1.value.dispatch({ selection: { anchor: position } });
425
460
  const setSelection = (anchor, head) => view$1.value.dispatch({ selection: { anchor, head } });
426
461
  const setSelections = (ranges, primary) => view$1.value.dispatch({
427
462
  selection: state.EditorSelection.create(ranges, primary)
@@ -433,11 +468,13 @@
433
468
  });
434
469
  context.expose({
435
470
  editor,
471
+ view: view$1,
436
472
  cursor,
437
473
  selection,
438
- state: state$1,
439
474
  focus,
440
475
  length,
476
+ json,
477
+ diagnosticCount,
441
478
  lint: lint$1,
442
479
  forceReconfigure,
443
480
  // Bellow is CodeMirror5's function
@@ -462,19 +499,16 @@
462
499
  },
463
500
  render() {
464
501
  return h(
465
- // @ts-ignore
466
502
  this.$props.tag,
467
503
  {
468
504
  ref: "editor",
469
505
  class: "vue-codemirror"
470
506
  },
471
- // @ts-ignore
472
507
  this.$slots.default ? (
473
508
  // Hide original content
474
509
  h(
475
510
  "aside",
476
511
  { style: "display: none;", "aria-hidden": "true" },
477
- // @ts-ignore
478
512
  slot(this.$slots.default)
479
513
  )
480
514
  ) : void 0
@@ -483,8 +517,8 @@
483
517
  });
484
518
 
485
519
  const meta = {
486
- version: "1.1.15",
487
- date: "2023-03-14T09:58:31.925Z"
520
+ version: "1.1.18",
521
+ date: "2023-04-04T00:08:06.631Z"
488
522
  };
489
523
 
490
524
  const installCodeMirror = (app) => app.component("CodeMirror", CodeMirror);