vue-codemirror6 0.5.0 → 0.5.3

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/README.md CHANGED
@@ -24,19 +24,20 @@ yarn add vue-codemirror6 @vue/composition-api
24
24
 
25
25
  ### Props
26
26
 
27
- | Props | Type | Information |
28
- | ---------- | --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
29
- | basic | boolean | Use [basic-setup](https://codemirror.net/6/docs/ref/#basic-setup). |
30
- | dark | boolean | Toggle Darkmode. It can be changed in real time, but the input value will return to the initial value. |
31
- | wrap | boolean | Line text wrapping. see [lineWrapping](https://codemirror.net/6/docs/ref/#view.EditorView.lineWrapping). |
32
- | tab | boolean | Enables tab indentation. |
33
- | theme | { [selector: string]: StyleSpec } | Specify the theme. For example, if you use [@codemirror/theme-one-dark](https://github.com/codemirror/theme-one-dark), import `oneDark` and put it in this prop. |
34
- | readonly | boolean | Makes the cursor visible or you can drag the text but not edit the value. |
35
- | editable | boolean | When this is set to false, it is similar to `readonly`, except that the cursor is not displayed like the normal pre tag. |
36
- | lang | LanguageSupport | The language you want to have syntax highlighting. see <https://codemirror.net/6/#languages> |
37
- | phrases | Record&lt;string, string&gt; | Specify here if you want to make the displayed character string multilingual. see <https://codemirror.net/6/examples/translate/> |
38
- | extensions | Extension[] | Includes enhancements to extend CodeMirror. |
39
- | linter | Diagnostic[] | Set Linter. see example <https://codesandbox.io/s/f6nb0?file=/src/index.js> |
27
+ | Props | Type | Information |
28
+ | ---------- | --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
29
+ | basic | boolean | Use [basic-setup](https://codemirror.net/6/docs/ref/#basic-setup). |
30
+ | dark | boolean | Toggle Darkmode. It can be changed in real time, but the input value will return to the initial value. |
31
+ | wrap | boolean | Line text wrapping. see [lineWrapping](https://codemirror.net/6/docs/ref/#view.EditorView.lineWrapping). |
32
+ | tab | boolean | Enables tab indentation. |
33
+ | theme | { [selector: string]: StyleSpec } | Specify the theme. For example, if you use [@codemirror/theme-one-dark](https://github.com/codemirror/theme-one-dark), import `oneDark` and put it in this prop. |
34
+ | readonly | boolean | Makes the cursor visible or you can drag the text but not edit the value. |
35
+ | editable | boolean | When this is set to false, it is similar to `readonly`, except that the cursor is not displayed like the normal pre tag. |
36
+ | lang | LanguageSupport | The language you want to have syntax highlighting. see <https://codemirror.net/6/#languages> |
37
+ | phrases | Record&lt;string, string&gt; | Specify here if you want to make the displayed character string multilingual. see <https://codemirror.net/6/examples/translate/> |
38
+ | extensions | Extension[] | Includes enhancements to extend CodeMirror. |
39
+ | linter | LintSource | Set Linter. Enter a linter (eg `esLint([arbitrary rule])` function for `@codemirror / lang-javascript`, `jsonParseLinter()`function for`@codemirror/json`). See the sources for various language libraries for more information. |
40
+ | lintGutter | boolean | Display 🔴 on the line where there was an error when `linter` was specified. It will not work if `linter` is not specified. |
40
41
 
41
42
  Notice: `lang` and `linter` can also be set together in `extensions`. This is defined for usability compatibility with past CodeMirrors.
42
43
 
@@ -95,7 +96,7 @@ Also, insert a `<pre>` tag to prevent the text in the slot from being automatica
95
96
 
96
97
  ```vue
97
98
  <template>
98
- <code-mirror :lang="lang" :editable="false">
99
+ <code-mirror :lang="lang" :linter="linter">
99
100
  <pre>
100
101
  {
101
102
  "key": "value"
@@ -107,7 +108,7 @@ Also, insert a `<pre>` tag to prevent the text in the slot from being automatica
107
108
  <script>
108
109
  import { ref, defineComponent } from 'vue';
109
110
 
110
- import { json } from '@codemirror/lang-json';
111
+ import { json, jsonParseLinter } from '@codemirror/lang-json';
111
112
 
112
113
  import CodeMirror from 'vue-codemirror6';
113
114
 
@@ -116,8 +117,9 @@ export default defineComponent({
116
117
  CodeMirror,
117
118
  },
118
119
  setup() {
119
- const lang = ref(json());
120
- return { lang };
120
+ const lang = json();
121
+ const linter = jsonParseLinter();
122
+ return { lang, linter };
121
123
  },
122
124
  });
123
125
  </script>
@@ -125,7 +127,7 @@ export default defineComponent({
125
127
 
126
128
  ### Full Example
127
129
 
128
- When using as a Markdown editor on [Vuetify](https://vuetifyjs.com/).
130
+ When using as a Markdown editor on [vite-vue2-vuetify-ts-starter](https://github.com/logue/vite-vue2-vuetify-ts-starter).
129
131
 
130
132
  ```vue
131
133
  <template>
@@ -169,7 +171,7 @@ export default defineComponent({
169
171
  *
170
172
  * @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
171
173
  */
172
- const lang: Ref<LanguageSupport> = ref(md());
174
+ const lang: LanguageSupport = md();
173
175
 
174
176
  /** Dark mode **/
175
177
  const dark = ref(window.matchMedia('(prefers-color-scheme: dark)').matches);
@@ -180,6 +182,7 @@ export default defineComponent({
180
182
  /**
181
183
  * Internationalization Config.
182
184
  * In this example, the display language to Japanese.
185
+ * Must be reactive when used in combination with vue-i18n.
183
186
  *
184
187
  * @see {@link https://codemirror.net/6/examples/translate/ | Example: Internationalization}
185
188
  */
@@ -239,10 +242,12 @@ const config: UserConfig = {
239
242
  // ...
240
243
  codemirror: [
241
244
  // Split CodeMirror code.
242
- 'vue-codemirror6',
245
+ '@codemirror/basic-setup',
246
+ '@codemirror/commands',
247
+ '@codemirror/language',
248
+ '@codemirror/lint',
243
249
  '@codemirror/state',
244
250
  '@codemirror/view',
245
- '@codemirror/basic-setup',
246
251
  // Add the following as needed.
247
252
  '@codemirror/lang-html',
248
253
  '@codemirror/lang-javascript',
@@ -1,9 +1,17 @@
1
1
  import { type PropType, type Ref, type SetupContext } from 'vue-demi';
2
2
  import { EditorSelection } from '@codemirror/state';
3
- import type { Extension, Text } from '@codemirror/state';
3
+ import type { Extension, SelectionRange, Text } from '@codemirror/state';
4
4
  import type { LanguageSupport } from '@codemirror/language';
5
- import type { Diagnostic } from '@codemirror/lint';
5
+ import type { ViewUpdate } from '@codemirror/view';
6
+ import type { LintSource } from '@codemirror/lint';
6
7
  import type { StyleSpec } from 'style-mod';
8
+ /** Emit Interface */
9
+ export interface CodeMirrorEmitsInterface {
10
+ /** Model Update */
11
+ (e: 'update:modelValue', value: string | Text): void;
12
+ /** CodeMirror ViewUpdate */
13
+ (e: 'update', value: ViewUpdate): void;
14
+ }
7
15
  /** CodeMirror Component */
8
16
  declare const _default: import("vue-demi").DefineComponent<{
9
17
  /** Model value */
@@ -11,18 +19,6 @@ declare const _default: import("vue-demi").DefineComponent<{
11
19
  type: PropType<string | Text>;
12
20
  default: string;
13
21
  };
14
- /**
15
- * Selection
16
- *
17
- * @see {@link https://codemirror.net/6/docs/ref/#state.EditorSelection | EditorSelection}
18
- */
19
- selection: {
20
- type: PropType<EditorSelection | {
21
- anchor: number;
22
- head?: number | undefined;
23
- }>;
24
- default: () => undefined;
25
- };
26
22
  /**
27
23
  * Theme
28
24
  *
@@ -117,30 +113,40 @@ declare const _default: import("vue-demi").DefineComponent<{
117
113
  * @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
118
114
  */
119
115
  linter: {
120
- type: PropType<Diagnostic[]>;
121
- default: () => undefined;
116
+ type: PropType<LintSource>;
117
+ default: undefined;
118
+ };
119
+ /** Show Gutter */
120
+ lintGutter: {
121
+ type: BooleanConstructor;
122
+ defalt: boolean;
122
123
  };
123
124
  }, {
124
125
  context: SetupContext<import("vue-demi").EmitsOptions>;
125
126
  editor: Ref<Element | undefined>;
127
+ selection: () => EditorSelection;
128
+ getRange: (a?: number, b?: number) => string;
129
+ getLine: (n: number) => string;
130
+ lineCount: () => number;
131
+ getCursor: () => number;
132
+ listSelections: () => readonly SelectionRange[];
133
+ getSelection: () => string;
134
+ getSelections: () => string[];
135
+ somethingSelected: () => boolean;
136
+ replaceRange: (text: string | Text, from: number, to: number) => void;
137
+ replaceSelection: (text: string | Text) => void;
138
+ setCursor: (pos: number) => void;
139
+ setSelection: (anchor: number, head: number) => void;
140
+ setSelections: (ranges: readonly SelectionRange[], mainIndex?: number | undefined) => void;
141
+ extendSelectionsBy: (f: any) => void;
142
+ focus: () => void;
143
+ hasFocus: () => boolean;
126
144
  }, unknown, {}, {}, import("vue-demi").ComponentOptionsMixin, import("vue-demi").ComponentOptionsMixin, import("vue-demi").EmitsOptions, "update:modelValue" | "update", import("vue-demi").VNodeProps & import("vue-demi").AllowedComponentProps & import("vue-demi").ComponentCustomProps, Readonly<import("vue-demi").ExtractPropTypes<{
127
145
  /** Model value */
128
146
  modelValue: {
129
147
  type: PropType<string | Text>;
130
148
  default: string;
131
149
  };
132
- /**
133
- * Selection
134
- *
135
- * @see {@link https://codemirror.net/6/docs/ref/#state.EditorSelection | EditorSelection}
136
- */
137
- selection: {
138
- type: PropType<EditorSelection | {
139
- anchor: number;
140
- head?: number | undefined;
141
- }>;
142
- default: () => undefined;
143
- };
144
150
  /**
145
151
  * Theme
146
152
  *
@@ -235,15 +241,16 @@ declare const _default: import("vue-demi").DefineComponent<{
235
241
  * @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
236
242
  */
237
243
  linter: {
238
- type: PropType<Diagnostic[]>;
239
- default: () => undefined;
244
+ type: PropType<LintSource>;
245
+ default: undefined;
246
+ };
247
+ /** Show Gutter */
248
+ lintGutter: {
249
+ type: BooleanConstructor;
250
+ defalt: boolean;
240
251
  };
241
252
  }>>, {
242
253
  modelValue: string | Text;
243
- selection: EditorSelection | {
244
- anchor: number;
245
- head?: number | undefined;
246
- };
247
254
  theme: {
248
255
  [selector: string]: StyleSpec;
249
256
  };
@@ -256,7 +263,8 @@ declare const _default: import("vue-demi").DefineComponent<{
256
263
  extensions: Extension[];
257
264
  phrases: Record<string, string>;
258
265
  lang: LanguageSupport;
259
- linter: Diagnostic[];
266
+ linter: LintSource;
267
+ lintGutter: boolean;
260
268
  }>;
261
269
  export default _default;
262
270
  //# sourceMappingURL=CodeMirror.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"CodeMirror.d.ts","sourceRoot":"","sources":["../../src/components/CodeMirror.ts"],"names":[],"mappings":"AAAA,OAAO,EAWL,KAAK,QAAQ,EACb,KAAK,GAAG,EACR,KAAK,YAA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;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAOH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH,kBAAkB;;;;;;;;;;;;;;;;;;;;;;AAlHtB,wBA2VG"}
@@ -1,9 +1,9 @@
1
1
  interface Options {
2
+ class?: string;
2
3
  domProps?: Record<any, any>;
3
4
  on?: Record<any, any>;
4
5
  props?: Record<any, any>;
5
6
  ref?: string;
6
- class?: string;
7
7
  style?: string;
8
8
  }
9
9
  declare const h: (type: string | Record<any, any>, options?: Options, chidren?: any) => import("vue-demi").VNode<import("vue-demi").RendererNode, import("vue-demi").RendererElement, {
@@ -1 +1 @@
1
- {"version":3,"file":"h-demi.d.ts","sourceRoot":"","sources":["../../src/helpers/h-demi.ts"],"names":[],"mappings":"AAEA,UAAU,OAAO;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC5B,EAAE,CAAC,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAWD,QAAA,MAAM,CAAC,SACC,MAAM,GAAG,OAAO,GAAG,EAAE,GAAG,CAAC,YACtB,OAAO,YACN,GAAG;;EASd,CAAC;AAEF,QAAA,MAAM,IAAI,iBAAkB,GAAG,QAG9B,CAAC;AACF,OAAO,EAAE,IAAI,EAAE,CAAC,IAAI,OAAO,EAAE,CAAC"}
1
+ {"version":3,"file":"h-demi.d.ts","sourceRoot":"","sources":["../../src/helpers/h-demi.ts"],"names":[],"mappings":"AAEA,UAAU,OAAO;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC5B,EAAE,CAAC,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAWD,QAAA,MAAM,CAAC,SACC,MAAM,GAAG,OAAO,GAAG,EAAE,GAAG,CAAC,YACtB,OAAO,YACN,GAAG;;EAWd,CAAC;AAEF,QAAA,MAAM,IAAI,iBAAkB,GAAG,QACoC,CAAC;AAEpE,OAAO,EAAE,IAAI,EAAE,CAAC,IAAI,OAAO,EAAE,CAAC"}
package/dist/index.es.js CHANGED
@@ -1,9 +1,10 @@
1
- import { isVue2, h as h$1, defineComponent, ref, toRefs, computed, toRaw, watch, onMounted, nextTick, onUnmounted } from "vue-demi";
2
- import { EditorState } from "@codemirror/state";
1
+ import { isVue2, h as h$1, defineComponent, ref, toRefs, watch, onMounted, nextTick, onUnmounted, toRaw } from "vue-demi";
2
+ import { EditorState, EditorSelection } from "@codemirror/state";
3
3
  import { EditorView, keymap } from "@codemirror/view";
4
+ import { linter, lintGutter } from "@codemirror/lint";
4
5
  import { basicSetup } from "@codemirror/basic-setup";
5
6
  import { indentWithTab } from "@codemirror/commands";
6
- import { merge, compact, trim } from "lodash";
7
+ import { trim, compact } from "lodash";
7
8
  const adaptOnsV3 = (ons) => {
8
9
  if (!ons)
9
10
  return null;
@@ -18,14 +19,9 @@ const h = (type, options = {}, chidren) => {
18
19
  return h$1(type, options, chidren);
19
20
  const { props, domProps, on, ...extraOptions } = options;
20
21
  const ons = on ? adaptOnsV3(on) : {};
21
- const params = { ...extraOptions, ...props, ...domProps, ...ons };
22
- return h$1(type, params, chidren);
23
- };
24
- const slot = (defaultSlots) => {
25
- if (typeof defaultSlots == "function")
26
- return defaultSlots();
27
- return defaultSlots;
22
+ return h$1(type, { ...extraOptions, ...props, ...domProps, ...ons }, chidren);
28
23
  };
24
+ const slot = (defaultSlots) => typeof defaultSlots == "function" ? defaultSlots() : defaultSlots;
29
25
  var CodeMirror = defineComponent({
30
26
  name: "CodeMirror",
31
27
  model: {
@@ -37,10 +33,6 @@ var CodeMirror = defineComponent({
37
33
  type: String,
38
34
  default: ""
39
35
  },
40
- selection: {
41
- type: Object,
42
- default: () => void 0
43
- },
44
36
  theme: {
45
37
  type: Object,
46
38
  default: () => {
@@ -86,63 +78,50 @@ var CodeMirror = defineComponent({
86
78
  default: () => void 0
87
79
  },
88
80
  linter: {
89
- type: Array,
90
- default: () => void 0
81
+ type: Function,
82
+ default: void 0
83
+ },
84
+ lintGutter: {
85
+ type: Boolean,
86
+ defalt: false
91
87
  }
92
88
  },
93
89
  emits: ["update:modelValue", "update"],
94
90
  setup(props, context) {
95
91
  const editor = ref();
96
- const doc = ref(props.modelValue);
97
92
  const { dark } = toRefs(props);
93
+ const doc = ref(props.modelValue);
98
94
  const emit = context.emit;
99
- const exts = computed(() => {
100
- const ext = [
101
- props.basic ? basicSetup : void 0,
102
- EditorView.updateListener.of((update) => emit("update", update)),
103
- EditorView.theme(props.theme, { dark: dark.value }),
104
- props.wrap ? EditorView.lineWrapping : void 0,
105
- props.tab ? keymap.of([indentWithTab]) : void 0,
106
- props.phrases ? EditorState.phrases.of(props.phrases) : void 0,
107
- props.readonly ? EditorState.readOnly.of(props.readonly) : void 0,
108
- props.editable ? EditorView.editable.of(props.editable) : void 0,
109
- props.lang ? toRaw(props.lang) : void 0
110
- ];
111
- if (props.linter) {
112
- merge(ext, props.linter);
113
- }
114
- if (props.extensions) {
115
- merge(ext, props.extensions);
116
- }
117
- return compact(ext);
118
- });
119
95
  let view;
120
- watch(doc, (current) => {
96
+ watch(doc, (value) => {
121
97
  if (view.composing) {
122
98
  return;
123
99
  }
124
100
  const previous = view.state.selection;
125
101
  view.setState(EditorState.create({
126
- doc: current,
127
- extensions: exts.value,
102
+ doc: value,
103
+ extensions: getExtensions(),
128
104
  selection: previous
129
105
  }));
130
106
  });
107
+ watch(() => props.modelValue, (v) => view.setState(EditorState.create({ doc: v, extensions: getExtensions() })));
131
108
  watch(dark, () => {
132
109
  view.setState(EditorState.create({
133
110
  doc: doc.value,
134
- extensions: exts.value
111
+ extensions: getExtensions()
135
112
  }));
136
113
  });
137
114
  onMounted(async () => {
138
- if (doc.value == "" && editor.value) {
115
+ if (editor.value && editor.value.childNodes[0]) {
116
+ if (doc.value !== "") {
117
+ console.warn("[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values.");
118
+ }
139
119
  doc.value = trim(editor.value.childNodes[0].innerText);
140
120
  }
141
121
  view = new EditorView({
142
122
  state: EditorState.create({
143
123
  doc: doc.value,
144
- selection: props.selection,
145
- extensions: exts.value
124
+ extensions: getExtensions()
146
125
  }),
147
126
  parent: editor.value,
148
127
  dispatch: (tr) => {
@@ -156,9 +135,67 @@ var CodeMirror = defineComponent({
156
135
  await nextTick();
157
136
  });
158
137
  onUnmounted(() => view.destroy());
138
+ const getExtensions = () => {
139
+ const extensions = compact([
140
+ props.basic ? basicSetup : void 0,
141
+ EditorView.updateListener.of((update) => emit("update", update)),
142
+ EditorView.theme(props.theme, { dark: dark.value }),
143
+ props.wrap ? EditorView.lineWrapping : void 0,
144
+ props.tab ? keymap.of([indentWithTab]) : void 0,
145
+ props.phrases ? EditorState.phrases.of(props.phrases) : void 0,
146
+ props.readonly ? EditorState.readOnly.of(props.readonly) : void 0,
147
+ props.editable ? EditorView.editable.of(props.editable) : void 0,
148
+ props.lang ? toRaw(props.lang) : void 0,
149
+ props.linter ? linter(props.linter) : void 0,
150
+ props.linter && props.lintGutter ? lintGutter() : void 0,
151
+ ...props.extensions
152
+ ]);
153
+ console.debug("[CodeMirror.vue] Loaded extensions: ", extensions);
154
+ return extensions;
155
+ };
156
+ const selection = () => view.state.selection;
157
+ const getRange = (a, b) => view.state.sliceDoc(a, b);
158
+ const getLine = (n) => view.state.doc.line(n + 1).text;
159
+ const lineCount = () => view.state.doc.lines;
160
+ const getCursor = () => selection().main.head;
161
+ const listSelections = () => selection().ranges;
162
+ const getSelection = () => view.state.sliceDoc(selection().main.from, selection().main.to);
163
+ const getSelections = () => selection().ranges.map((r) => view.state.sliceDoc(r.from, r.to));
164
+ const somethingSelected = () => selection().ranges.some((r) => !r.empty);
165
+ const replaceRange = (text, from, to) => view.dispatch({
166
+ changes: { from, to, insert: text }
167
+ });
168
+ const replaceSelection = (text) => view.dispatch(view.state.replaceSelection(text));
169
+ const setCursor = (pos) => view.dispatch({ selection: { anchor: pos } });
170
+ const setSelection = (anchor, head) => view.dispatch({ selection: { anchor, head } });
171
+ const setSelections = (ranges, mainIndex) => view.dispatch({
172
+ selection: EditorSelection.create(ranges, mainIndex)
173
+ });
174
+ const extendSelectionsBy = (f) => view.dispatch({
175
+ selection: EditorSelection.create(selection().ranges.map((r) => r.extend(f(r))))
176
+ });
177
+ const focus = () => view.focus();
178
+ const hasFocus = () => view.hasFocus;
159
179
  return {
160
180
  context,
161
- editor
181
+ editor,
182
+ selection,
183
+ getRange,
184
+ getLine,
185
+ lineCount,
186
+ getCursor,
187
+ listSelections,
188
+ getSelection,
189
+ getSelections,
190
+ somethingSelected,
191
+ replaceRange,
192
+ replaceSelection,
193
+ setCursor,
194
+ setSelection,
195
+ setSelections,
196
+ extendSelectionsBy,
197
+ focus,
198
+ hasFocus
162
199
  };
163
200
  },
164
201
  render() {
package/dist/index.umd.js CHANGED
@@ -1,8 +1,8 @@
1
- (function(r,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("vue-demi"),require("@codemirror/state"),require("@codemirror/view"),require("@codemirror/basic-setup"),require("@codemirror/commands"),require("lodash")):typeof define=="function"&&define.amd?define(["exports","vue-demi","@codemirror/state","@codemirror/view","@codemirror/basic-setup","@codemirror/commands","lodash"],t):(r=typeof globalThis<"u"?globalThis:r||self,t(r.CodeMirror={},r.VueDemi,r.state,r.view,r.basicSetup,r.commands,r.lodash))})(this,function(r,t,l,s,v,b,f){"use strict";const V=e=>e?Object.entries(e).reduce((i,[o,a])=>(o=o.charAt(0).toUpperCase()+o.slice(1),o=`on${o}`,{...i,[o]:a}),{}):null,y=(e,i={},o)=>{if(t.isVue2)return t.h(e,i,o);const{props:a,domProps:m,on:u,...c}=i,n=u?V(u):{},d={...c,...a,...m,...n};return t.h(e,d,o)},w=e=>typeof e=="function"?e():e;var h=t.defineComponent({name:"CodeMirror",model:{prop:"modelValue",event:"update:modelValue"},props:{modelValue:{type:String,default:""},selection:{type:Object,default:()=>{}},theme:{type:Object,default:()=>({})},dark:{type:Boolean,default:!1},basic:{type:Boolean,default:!1},wrap:{type:Boolean,default:!1},tab:{type:Boolean,default:!1},readonly:{type:Boolean,default:!1},editable:{type:Boolean,default:!0},extensions:{type:Array,default:()=>[]},phrases:{type:Object,default:()=>{}},lang:{type:Object,default:()=>{}},linter:{type:Array,default:()=>{}}},emits:["update:modelValue","update"],setup(e,i){const o=t.ref(),a=t.ref(e.modelValue),{dark:m}=t.toRefs(e),u=i.emit,c=t.computed(()=>{const d=[e.basic?v.basicSetup:void 0,s.EditorView.updateListener.of(p=>u("update",p)),s.EditorView.theme(e.theme,{dark:m.value}),e.wrap?s.EditorView.lineWrapping:void 0,e.tab?s.keymap.of([b.indentWithTab]):void 0,e.phrases?l.EditorState.phrases.of(e.phrases):void 0,e.readonly?l.EditorState.readOnly.of(e.readonly):void 0,e.editable?s.EditorView.editable.of(e.editable):void 0,e.lang?t.toRaw(e.lang):void 0];return e.linter&&f.merge(d,e.linter),e.extensions&&f.merge(d,e.extensions),f.compact(d)});let n;return t.watch(a,d=>{if(n.composing)return;const p=n.state.selection;n.setState(l.EditorState.create({doc:d,extensions:c.value,selection:p}))}),t.watch(m,()=>{n.setState(l.EditorState.create({doc:a.value,extensions:c.value}))}),t.onMounted(async()=>{a.value==""&&o.value&&(a.value=f.trim(o.value.childNodes[0].innerText)),n=new s.EditorView({state:l.EditorState.create({doc:a.value,selection:e.selection,extensions:c.value}),parent:o.value,dispatch:d=>{n.update([d]),!d.changes.empty&&(a.value=n.state.doc.toString(),u("update:modelValue",a.value))}}),await t.nextTick()}),t.onUnmounted(()=>n.destroy()),{context:i,editor:o}},render(){return y("div",{ref:"editor",class:"vue-codemirror"},this.$slots.default?y("aside",{style:"display: none;"},w(this.$slots.default)):void 0)}});/**
1
+ (function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("vue-demi"),require("@codemirror/state"),require("@codemirror/view"),require("@codemirror/lint"),require("@codemirror/basic-setup"),require("@codemirror/commands"),require("lodash")):typeof define=="function"&&define.amd?define(["exports","vue-demi","@codemirror/state","@codemirror/view","@codemirror/lint","@codemirror/basic-setup","@codemirror/commands","lodash"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r.CodeMirror={},r.VueDemi,r.state,r.view,r.lint,r.basicSetup,r.commands,r.lodash))})(this,function(r,n,d,u,p,v,x,S){"use strict";const C=e=>e?Object.entries(e).reduce((l,[i,f])=>(i=i.charAt(0).toUpperCase()+i.slice(1),i=`on${i}`,{...l,[i]:f}),{}):null,g=(e,l={},i)=>{if(n.isVue2)return n.h(e,l,i);const{props:f,domProps:a,on:h,...o}=l,m=h?C(h):{};return n.h(e,{...o,...f,...a,...m},i)},V=e=>typeof e=="function"?e():e;var y=n.defineComponent({name:"CodeMirror",model:{prop:"modelValue",event:"update:modelValue"},props:{modelValue:{type:String,default:""},theme:{type:Object,default:()=>({})},dark:{type:Boolean,default:!1},basic:{type:Boolean,default:!1},wrap:{type:Boolean,default:!1},tab:{type:Boolean,default:!1},readonly:{type:Boolean,default:!1},editable:{type:Boolean,default:!0},extensions:{type:Array,default:()=>[]},phrases:{type:Object,default:()=>{}},lang:{type:Object,default:()=>{}},linter:{type:Function,default:void 0},lintGutter:{type:Boolean,defalt:!1}},emits:["update:modelValue","update"],setup(e,l){const i=n.ref(),{dark:f}=n.toRefs(e),a=n.ref(e.modelValue),h=l.emit;let o;n.watch(a,t=>{if(o.composing)return;const s=o.state.selection;o.setState(d.EditorState.create({doc:t,extensions:m(),selection:s}))}),n.watch(()=>e.modelValue,t=>o.setState(d.EditorState.create({doc:t,extensions:m()}))),n.watch(f,()=>{o.setState(d.EditorState.create({doc:a.value,extensions:m()}))}),n.onMounted(async()=>{i.value&&i.value.childNodes[0]&&(a.value!==""&&console.warn("[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."),a.value=S.trim(i.value.childNodes[0].innerText)),o=new u.EditorView({state:d.EditorState.create({doc:a.value,extensions:m()}),parent:i.value,dispatch:t=>{o.update([t]),!t.changes.empty&&(a.value=o.state.doc.toString(),h("update:modelValue",a.value))}}),await n.nextTick()}),n.onUnmounted(()=>o.destroy());const m=()=>{const t=S.compact([e.basic?v.basicSetup:void 0,u.EditorView.updateListener.of(s=>h("update",s)),u.EditorView.theme(e.theme,{dark:f.value}),e.wrap?u.EditorView.lineWrapping:void 0,e.tab?u.keymap.of([x.indentWithTab]):void 0,e.phrases?d.EditorState.phrases.of(e.phrases):void 0,e.readonly?d.EditorState.readOnly.of(e.readonly):void 0,e.editable?u.EditorView.editable.of(e.editable):void 0,e.lang?n.toRaw(e.lang):void 0,e.linter?p.linter(e.linter):void 0,e.linter&&e.lintGutter?p.lintGutter():void 0,...e.extensions]);return console.debug("[CodeMirror.vue] Loaded extensions: ",t),t},c=()=>o.state.selection;return{context:l,editor:i,selection:c,getRange:(t,s)=>o.state.sliceDoc(t,s),getLine:t=>o.state.doc.line(t+1).text,lineCount:()=>o.state.doc.lines,getCursor:()=>c().main.head,listSelections:()=>c().ranges,getSelection:()=>o.state.sliceDoc(c().main.from,c().main.to),getSelections:()=>c().ranges.map(t=>o.state.sliceDoc(t.from,t.to)),somethingSelected:()=>c().ranges.some(t=>!t.empty),replaceRange:(t,s,w)=>o.dispatch({changes:{from:s,to:w,insert:t}}),replaceSelection:t=>o.dispatch(o.state.replaceSelection(t)),setCursor:t=>o.dispatch({selection:{anchor:t}}),setSelection:(t,s)=>o.dispatch({selection:{anchor:t,head:s}}),setSelections:(t,s)=>o.dispatch({selection:d.EditorSelection.create(t,s)}),extendSelectionsBy:t=>o.dispatch({selection:d.EditorSelection.create(c().ranges.map(s=>s.extend(t(s))))}),focus:()=>o.focus(),hasFocus:()=>o.hasFocus}},render(){return g("div",{ref:"editor",class:"vue-codemirror"},this.$slots.default?g("aside",{style:"display: none;"},V(this.$slots.default)):void 0)}});/**
2
2
  * Vue CodeMirror6 Component
3
3
  *
4
4
  * @license MIT
5
5
  * @author Logue {@link logue@hotmail.co.jp}
6
6
  * @copyright 2022 Masashi Yoshikawa {@link https://logue.dev/} All rights reserved.
7
7
  * @see {@link https://github.com/logue/vue-codemirror6}
8
- */const x=e=>e.component("CodeMirror",h);r.default=h,r.install=x,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
8
+ */const b=e=>e.component("CodeMirror",y);r.default=y,r.install=b,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "vue-codemirror6",
4
- "version": "0.5.0",
4
+ "version": "0.5.3",
5
5
  "description": "CodeMirror6 Component for vue2 and vue3",
6
6
  "keywords": [
7
7
  "vuejs",
@@ -40,9 +40,6 @@
40
40
  "license": "MIT",
41
41
  "main": "./dist/index.umd.js",
42
42
  "module": "./dist/index.es.js",
43
- "browser": {
44
- "./sfc": "./src/components/CodeMirror.vue"
45
- },
46
43
  "exports": {
47
44
  ".": {
48
45
  "import": "./dist/index.es.js",
@@ -51,8 +48,7 @@
51
48
  },
52
49
  "types": "./dist/index.d.ts",
53
50
  "files": [
54
- "/dist",
55
- "/src/components/CodeMirror.vue"
51
+ "/dist"
56
52
  ],
57
53
  "sideEffects": false,
58
54
  "scripts": {
@@ -65,14 +61,15 @@
65
61
  "lint": "eslint --fix --cache --cache-location ./node_modules/.vite/vite-plugin-eslint && prettier . -w -u",
66
62
  "lint:markup": "vue-tsc --noEmit",
67
63
  "preview": "vite preview --config vite.config.docs.ts",
68
- "prepare": "husky install",
69
- "postinstall": "npx vue-demi-fix"
64
+ "prepare": "husky install"
70
65
  },
71
66
  "dependencies": {
72
- "@codemirror/basic-setup": "^0.20.0",
73
- "@codemirror/state": "^0.20.0",
74
- "@codemirror/view": "^0.20.6",
75
- "vue-demi": "^0.12.5"
67
+ "@codemirror/basic-setup": "latest",
68
+ "@codemirror/commands": "latest",
69
+ "@codemirror/lint": "latest",
70
+ "@codemirror/state": "latest",
71
+ "@codemirror/view": "latest",
72
+ "vue-demi": "latest"
76
73
  },
77
74
  "peerDependencies": {
78
75
  "@vue/composition-api": "^1.6.2",
@@ -84,11 +81,12 @@
84
81
  }
85
82
  },
86
83
  "devDependencies": {
84
+ "@codemirror/lang-javascript": "^0.20.0",
87
85
  "@codemirror/lang-markdown": "^0.20.1",
88
86
  "@types/lodash": "^4.14.182",
89
- "@types/node": "^17.0.35",
90
- "@typescript-eslint/eslint-plugin": "^5.26.0",
91
- "@typescript-eslint/parser": "^5.26.0",
87
+ "@types/node": "^17.0.36",
88
+ "@typescript-eslint/eslint-plugin": "^5.27.0",
89
+ "@typescript-eslint/parser": "^5.27.0",
92
90
  "@vitejs/plugin-vue": "^2.3.3",
93
91
  "@vue/eslint-config-prettier": "^7.0.0",
94
92
  "eslint": "^8.16.0",
@@ -96,18 +94,18 @@
96
94
  "eslint-config-prettier": "^8.5.0",
97
95
  "eslint-import-resolver-alias": "^1.1.2",
98
96
  "eslint-import-resolver-typescript": "^2.7.1",
97
+ "eslint-plugin-html": "^6.2.0",
99
98
  "eslint-plugin-import": "^2.26.0",
100
99
  "eslint-plugin-jsdoc": "^39.3.2",
101
100
  "eslint-plugin-tsdoc": "^0.2.16",
102
101
  "eslint-plugin-vue": "^9.0.1",
103
102
  "eslint-plugin-vuejs-accessibility": "^1.1.1",
103
+ "eslint-plugin-yaml": "^0.5.0",
104
+ "eslint4b-prebuilt": "^6.7.2",
104
105
  "husky": "^8.0.1",
105
- "lint-staged": "^12.4.2",
106
+ "lint-staged": "^12.4.3",
106
107
  "lodash": "^4.17.21",
107
108
  "prettier": "^2.6.2",
108
- "prettier-plugin-jsdoc": "^0.3.38",
109
- "prettier-plugin-md-nocjsp": "^1.2.0",
110
- "prettier-plugin-sort-imports": "^1.6.1",
111
109
  "rimraf": "^3.0.2",
112
110
  "rollup-plugin-visualizer": "^5.6.0",
113
111
  "typescript": "^4.7.2",
@@ -115,7 +113,7 @@
115
113
  "vite-plugin-checker": "^0.4.6",
116
114
  "vue": "^3.2.36",
117
115
  "vue-eslint-parser": "^9.0.2",
118
- "vue-tsc": "^0.34.16"
116
+ "vue-tsc": "^0.35.2"
119
117
  },
120
118
  "husky": {
121
119
  "hooks": {
@@ -1,9 +0,0 @@
1
- import type { ViewUpdate } from '@codemirror/view';
2
- /** Emit Interface */
3
- export default interface CodeMirrorEmitsInterface {
4
- /** Model Update */
5
- (e: 'update:modelValue', value: string | Text): void;
6
- /** CodeMirror ViewUpdate */
7
- (e: 'update', value: ViewUpdate): void;
8
- }
9
- //# sourceMappingURL=CodeMirrorEmitsInterface.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"CodeMirrorEmitsInterface.d.ts","sourceRoot":"","sources":["../../src/interfaces/CodeMirrorEmitsInterface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,qBAAqB;AACrB,MAAM,CAAC,OAAO,WAAW,wBAAwB;IAC/C,mBAAmB;IACnB,CAAC,CAAC,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;IACrD,4BAA4B;IAC5B,CAAC,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;CACxC"}