vue-codemirror6 0.1.4 → 0.1.6

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
@@ -8,7 +8,7 @@
8
8
 
9
9
  A component for using [CodeMirror6](https://codemirror.net/6/) with Vue. Unrelated to [surmon-china's vue-codemirror](https://github.com/surmon-china/vue-codemirror), it is for CodeMirror6.
10
10
 
11
- If you are using Vue2, please use `0.1.x`. (The component itself supports both, but the script generated by vue-demi can only output one)
11
+ If you are using Vue3, please use `0.3.x`. (The component itself supports both, but the script generated by vue-demi can only output one)
12
12
 
13
13
  ## Usage
14
14
 
@@ -214,6 +214,38 @@ export default defineComponent({
214
214
  </script>
215
215
  ```
216
216
 
217
+ ## Recommendations
218
+
219
+ Since CodeMirror has a relatively large capacity, when using vite, it is recommended to set it to output as a separate file using the [`manualChunks`](https://vitejs.dev/guide/build.html#chunking-strategy) option at build time as shown below.
220
+
221
+ ```ts
222
+ const config: UserConfig = {
223
+ // ...
224
+ build: {
225
+ rollupOptions: {
226
+ output: {
227
+ manualChunks: {
228
+ // ...
229
+ codemirror: [
230
+ // Split CodeMirror code.
231
+ 'vue-codemirror6',
232
+ '@codemirror/state',
233
+ '@codemirror/view',
234
+ // Add the following as needed.
235
+ '@codemirror/basic-setup',
236
+ '@codemirror/lang-html',
237
+ '@codemirror/lang-javascript',
238
+ '@codemirror/lang-markdown',
239
+ ],
240
+ // ...
241
+ },
242
+ },
243
+ },
244
+ },
245
+ // ...
246
+ };
247
+ ```
248
+
217
249
  ## LICENSE
218
250
 
219
251
  [MIT](LICENSE)
@@ -1,5 +1,6 @@
1
- import type { Ref, PropType, SetupContext } from 'vue-demi';
2
- import type { Extension } from '@codemirror/state';
1
+ import { type PropType, type Ref, type SetupContext } from 'vue-demi';
2
+ import { EditorSelection } from '@codemirror/state';
3
+ import type { Extension, Text } from '@codemirror/state';
3
4
  import type { LanguageSupport } from '@codemirror/language';
4
5
  import type { Diagnostic } from '@codemirror/lint';
5
6
  import type { StyleSpec } from 'style-mod';
@@ -9,9 +10,21 @@ declare const _default: import("vue").ComponentOptions<import("vue").default, im
9
10
  }> & import("@vue/composition-api").Data, {}, {}, {
10
11
  /** Model value */
11
12
  modelValue: {
12
- type: StringConstructor;
13
+ type: PropType<string | Text>;
13
14
  default: string;
14
15
  };
16
+ /**
17
+ * Selection
18
+ *
19
+ * @see {@link https://codemirror.net/6/docs/ref/#state.EditorSelection | EditorSelection}
20
+ */
21
+ selection: {
22
+ type: PropType<EditorSelection | {
23
+ anchor: number;
24
+ head?: number | undefined;
25
+ }>;
26
+ default: undefined;
27
+ };
15
28
  /**
16
29
  * Theme
17
30
  *
@@ -21,13 +34,40 @@ declare const _default: import("vue").ComponentOptions<import("vue").default, im
21
34
  type: PropType<{
22
35
  [selector: string]: StyleSpec;
23
36
  }>;
24
- default: undefined;
37
+ default: () => {};
25
38
  };
26
39
  /** Dark Mode */
27
40
  dark: {
28
41
  type: BooleanConstructor;
29
42
  default: boolean;
30
43
  };
44
+ /**
45
+ * Use Basic Setup
46
+ *
47
+ * @see {@link https://codemirror.net/6/docs/ref/#basic-setup | basic-setup}
48
+ */
49
+ basic: {
50
+ type: BooleanConstructor;
51
+ default: boolean;
52
+ };
53
+ /**
54
+ * Line wrapping
55
+ *
56
+ * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5ElineWrapping | LineWrapping}
57
+ */
58
+ wrap: {
59
+ type: BooleanConstructor;
60
+ default: boolean;
61
+ };
62
+ /**
63
+ * Tab handling
64
+ *
65
+ * @see {@link https://codemirror.net/6/examples/tab/ | Tab Handling}
66
+ */
67
+ tab: {
68
+ type: BooleanConstructor;
69
+ default: boolean;
70
+ };
31
71
  /**
32
72
  * Readonly
33
73
  *
@@ -83,11 +123,18 @@ declare const _default: import("vue").ComponentOptions<import("vue").default, im
83
123
  default: undefined;
84
124
  };
85
125
  }, {
86
- modelValue: string;
126
+ modelValue: string | Text;
127
+ selection: EditorSelection | {
128
+ anchor: number;
129
+ head?: number | undefined;
130
+ };
87
131
  theme: {
88
132
  [selector: string]: StyleSpec;
89
133
  };
90
134
  dark: boolean;
135
+ basic: boolean;
136
+ wrap: boolean;
137
+ tab: boolean;
91
138
  readonly: boolean;
92
139
  editable: boolean;
93
140
  extensions: Extension[];
@@ -95,11 +142,18 @@ declare const _default: import("vue").ComponentOptions<import("vue").default, im
95
142
  lang: LanguageSupport;
96
143
  linter: Diagnostic[];
97
144
  } & {}> & Omit<import("vue").VueConstructor<import("vue").default>, never> & (new (...args: any[]) => import("@vue/composition-api").ComponentRenderProxy<{
98
- modelValue: string;
145
+ modelValue: string | Text;
146
+ selection: EditorSelection | {
147
+ anchor: number;
148
+ head?: number | undefined;
149
+ };
99
150
  theme: {
100
151
  [selector: string]: StyleSpec;
101
152
  };
102
153
  dark: boolean;
154
+ basic: boolean;
155
+ wrap: boolean;
156
+ tab: boolean;
103
157
  readonly: boolean;
104
158
  editable: boolean;
105
159
  extensions: Extension[];
@@ -110,11 +164,18 @@ declare const _default: import("vue").ComponentOptions<import("vue").default, im
110
164
  context: SetupContext<{}>;
111
165
  editor: Ref<Element | undefined>;
112
166
  }>, import("@vue/composition-api").Data, {}, {}, {}, {}, {}, {
113
- modelValue: string;
167
+ modelValue: string | Text;
168
+ selection: EditorSelection | {
169
+ anchor: number;
170
+ head?: number | undefined;
171
+ };
114
172
  theme: {
115
173
  [selector: string]: StyleSpec;
116
174
  };
117
175
  dark: boolean;
176
+ basic: boolean;
177
+ wrap: boolean;
178
+ tab: boolean;
118
179
  readonly: boolean;
119
180
  editable: boolean;
120
181
  extensions: Extension[];
@@ -122,11 +183,18 @@ declare const _default: import("vue").ComponentOptions<import("vue").default, im
122
183
  lang: LanguageSupport;
123
184
  linter: Diagnostic[];
124
185
  } & {}, {
125
- modelValue: string;
186
+ modelValue: string | Text;
187
+ selection: EditorSelection | {
188
+ anchor: number;
189
+ head?: number | undefined;
190
+ };
126
191
  theme: {
127
192
  [selector: string]: StyleSpec;
128
193
  };
129
194
  dark: boolean;
195
+ basic: boolean;
196
+ wrap: boolean;
197
+ tab: boolean;
130
198
  readonly: boolean;
131
199
  editable: boolean;
132
200
  extensions: Extension[];
@@ -1 +1 @@
1
- {"version":3,"file":"CodeMirror.vue.d.ts","sourceRoot":"","sources":["../../src/components/CodeMirror.vue.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAe,GAAG,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAIzE,OAAO,KAAK,EAAE,SAAS,EAAe,MAAM,mBAAmB,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;;;;;IAavC,kBAAkB;;;;;IAKlB;;;;OAIG;;;;;;;IAKH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA/EP,2BAA2B;AAC3B,wBA+MG"}
1
+ {"version":3,"file":"CodeMirror.vue.d.ts","sourceRoot":"","sources":["../../src/components/CodeMirror.vue.ts"],"names":[],"mappings":"AACA,OAAO,EAWL,KAAK,QAAQ,EACb,KAAK,GAAG,EACR,KAAK,YAAY,EAClB,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,eAAe,EAAe,MAAM,mBAAmB,CAAC;AASjE,OAAO,KAAK,EAAE,SAAS,EAAE,IAAI,EAAe,MAAM,mBAAmB,CAAC;AACtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;;;;;IAavC,kBAAkB;;;;;IAKlB;;;;OAIG;;;oBAG6B,MAAM;;;;;IAItC;;;;OAIG;;;;;;;IAOH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;;;;gBAhG6B,MAAM;;;;;;;;;;;;;;;;;;;gBAAN,MAAM;;;;;;;;;;;;;;;;;;;;;;gBAAN,MAAM;;;;;;;;;;;;;;;;;;;gBAAN,MAAM;;;;;;;;;;;;;;;;;AAvB1C,2BAA2B;AAC3B,wBA6PG"}
package/dist/index.d.ts CHANGED
@@ -8,7 +8,6 @@
8
8
  */
9
9
  import CodeMirror from './components/CodeMirror.vue';
10
10
  import { App } from 'vue-demi';
11
- declare const installCodeMirror: (app: App) => void;
12
- export { CodeMirror, installCodeMirror as install };
13
- export default CodeMirror;
11
+ declare const installCodeMirror: void | ((app: App) => void);
12
+ export { CodeMirror as default, installCodeMirror as install };
14
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,UAAU,MAAM,6BAA6B,CAAC;AACrD,OAAO,EAAW,GAAG,EAAE,MAAM,UAAU,CAAC;AAIxC,QAAA,MAAM,iBAAiB,QAAS,GAAG,SAElC,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,iBAAiB,IAAI,OAAO,EAAE,CAAC;AACpD,eAAe,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,UAAU,MAAM,6BAA6B,CAAC;AACrD,OAAO,EAAmB,GAAG,EAAE,MAAM,UAAU,CAAC;AAEhD,QAAA,MAAM,iBAAiB,gBACb,GAAG,UAGA,CAAC;AAEd,OAAO,EAAE,UAAU,IAAI,OAAO,EAAE,iBAAiB,IAAI,OAAO,EAAE,CAAC"}
@@ -1,7 +1,9 @@
1
- import { defineComponent, ref, computed, watch, onMounted, onUnmounted, install } from "vue-demi";
1
+ import { defineComponent, ref, toRefs, computed, toRaw, watch, onMounted, nextTick, onUnmounted, isVue2, install } from "vue-demi";
2
2
  import { EditorState } from "@codemirror/state";
3
- import { EditorView } from "@codemirror/view";
4
- import { merge, compact } from "lodash";
3
+ import { EditorView, keymap } from "@codemirror/view";
4
+ import { basicSetup } from "@codemirror/basic-setup";
5
+ import { indentWithTab } from "@codemirror/commands";
6
+ import { merge, compact, trim } from "lodash";
5
7
  var __vue2_script = defineComponent({
6
8
  name: "CodeMirror",
7
9
  model: {
@@ -13,14 +15,32 @@ var __vue2_script = defineComponent({
13
15
  type: String,
14
16
  default: ""
15
17
  },
16
- theme: {
18
+ selection: {
17
19
  type: Object,
18
20
  default: void 0
19
21
  },
22
+ theme: {
23
+ type: Object,
24
+ default: () => {
25
+ return {};
26
+ }
27
+ },
20
28
  dark: {
21
29
  type: Boolean,
22
30
  default: false
23
31
  },
32
+ basic: {
33
+ type: Boolean,
34
+ default: false
35
+ },
36
+ wrap: {
37
+ type: Boolean,
38
+ default: false
39
+ },
40
+ tab: {
41
+ type: Boolean,
42
+ default: false
43
+ },
24
44
  readonly: {
25
45
  type: Boolean,
26
46
  default: false
@@ -49,19 +69,20 @@ var __vue2_script = defineComponent({
49
69
  emits: ["update:modelValue", "update"],
50
70
  setup(props, context) {
51
71
  const editor = ref();
52
- const modelValue = ref(props.modelValue);
53
- const dark = ref(props.dark);
72
+ const doc = ref(props.modelValue);
73
+ const { dark } = toRefs(props);
54
74
  const emit = context.emit;
55
- const extensions = computed(() => {
75
+ const exts = computed(() => {
56
76
  const ext = [
57
- EditorView.updateListener.of((update) => {
58
- emit("update", update);
59
- }),
60
- EditorView.theme(props.theme || {}, { dark: dark.value }),
77
+ props.basic ? basicSetup : void 0,
78
+ EditorView.updateListener.of((update) => emit("update", update)),
79
+ EditorView.theme(props.theme, { dark: dark.value }),
80
+ props.wrap ? EditorView.lineWrapping : void 0,
81
+ props.tab ? keymap.of([indentWithTab]) : void 0,
61
82
  props.phrases ? EditorState.phrases.of(props.phrases) : void 0,
62
- props.lang,
63
83
  props.readonly ? EditorState.readOnly.of(props.readonly) : void 0,
64
- props.editable ? EditorView.editable.of(props.editable) : void 0
84
+ props.editable ? EditorView.editable.of(props.editable) : void 0,
85
+ props.lang ? toRaw(props.lang) : void 0
65
86
  ];
66
87
  if (props.linter) {
67
88
  merge(ext, props.linter);
@@ -72,39 +93,43 @@ var __vue2_script = defineComponent({
72
93
  return compact(ext);
73
94
  });
74
95
  let view;
75
- watch(modelValue, (current) => {
96
+ watch(doc, (current) => {
76
97
  if (view.composing) {
77
98
  return;
78
99
  }
79
100
  const previous = view.state.selection;
80
101
  view.setState(EditorState.create({
81
102
  doc: current,
82
- extensions: extensions.value,
103
+ extensions: exts.value,
83
104
  selection: previous
84
105
  }));
85
106
  });
86
107
  watch(dark, () => {
87
108
  view.setState(EditorState.create({
88
- doc: modelValue.value,
89
- extensions: extensions.value
109
+ doc: doc.value,
110
+ extensions: exts.value
90
111
  }));
91
112
  });
92
- onMounted(() => {
93
- const value = !modelValue.value && editor.value ? editor.value.childNodes[0].innerText : modelValue.value;
113
+ onMounted(async () => {
114
+ if (doc.value == "" && editor.value) {
115
+ doc.value = trim(editor.value.childNodes[0].innerText);
116
+ }
94
117
  view = new EditorView({
95
118
  state: EditorState.create({
96
- doc: value,
97
- extensions: extensions.value
119
+ doc: doc.value,
120
+ selection: props.selection,
121
+ extensions: exts.value
98
122
  }),
99
123
  parent: editor.value,
100
124
  dispatch: (tr) => {
101
125
  view.update([tr]);
102
126
  if (tr.changes.empty)
103
127
  return;
104
- modelValue.value = view.state.doc.toString();
105
- emit("update:modelValue", modelValue.value);
128
+ doc.value = view.state.doc.toString();
129
+ emit("update:modelValue", doc.value);
106
130
  }
107
131
  });
132
+ await nextTick();
108
133
  });
109
134
  onUnmounted(() => view.destroy());
110
135
  return {
@@ -199,8 +224,7 @@ var CodeMirror = /* @__PURE__ */ function() {
199
224
  * @copyright 2022 Masashi Yoshikawa {@link https://logue.dev/} All rights reserved.
200
225
  * @see {@link https://github.com/logue/vue-codemirror6}
201
226
  */
202
- install();
203
- const installCodeMirror = (app) => {
227
+ const installCodeMirror = isVue2 ? (app) => {
204
228
  app.component("CodeMirror", CodeMirror);
205
- };
206
- export { CodeMirror, CodeMirror as default, installCodeMirror as install };
229
+ } : install();
230
+ export { CodeMirror as default, installCodeMirror as install };
@@ -1,8 +1,8 @@
1
- (function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("vue-demi"),require("@codemirror/state"),require("@codemirror/view"),require("lodash")):typeof define=="function"&&define.amd?define(["exports","vue-demi","@codemirror/state","@codemirror/view","lodash"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r.CodeMirror={},r.VueDemi,r.state,r.view,r.lodash))})(this,function(r,n,c,_,m){"use strict";var S=n.defineComponent({name:"CodeMirror",model:{prop:"modelValue",event:"update:modelValue"},props:{modelValue:{type:String,default:""},theme:{type:Object,default:void 0},dark:{type:Boolean,default:!1},readonly:{type:Boolean,default:!1},editable:{type:Boolean,default:!0},extensions:{type:Array,default:void 0},phrases:{type:Object,default:void 0},lang:{type:Object,default:void 0},linter:{type:Array,default:void 0}},emits:["update:modelValue","update"],setup(e,i){const d=n.ref(),s=n.ref(e.modelValue),u=n.ref(e.dark),v=i.emit,f=n.computed(()=>{const t=[_.EditorView.updateListener.of(o=>{v("update",o)}),_.EditorView.theme(e.theme||{},{dark:u.value}),e.phrases?c.EditorState.phrases.of(e.phrases):void 0,e.lang,e.readonly?c.EditorState.readOnly.of(e.readonly):void 0,e.editable?_.EditorView.editable.of(e.editable):void 0];return e.linter&&m.merge(t,e.linter),e.extensions&&m.merge(t,e.extensions),m.compact(t)});let a;return n.watch(s,t=>{if(a.composing)return;const o=a.state.selection;a.setState(c.EditorState.create({doc:t,extensions:f.value,selection:o}))}),n.watch(u,()=>{a.setState(c.EditorState.create({doc:s.value,extensions:f.value}))}),n.onMounted(()=>{const t=!s.value&&d.value?d.value.childNodes[0].innerText:s.value;a=new _.EditorView({state:c.EditorState.create({doc:t,extensions:f.value}),parent:d.value,dispatch:o=>{a.update([o]),!o.changes.empty&&(s.value=a.state.doc.toString(),v("update:modelValue",s.value))}})}),n.onUnmounted(()=>a.destroy()),{context:i,editor:d}}}),V=function(){var e=this,i=e.$createElement,d=e._self._c||i;return d("div",{ref:"editor",staticClass:"vue-codemirror"},[d("aside",{directives:[{name:"show",rawName:"v-show",value:!e.context.slots.default,expression:"!context.slots.default"}]},[e._t("default")],2)])},w=[];function E(e,i,d,s,u,v,f,a){var t=typeof e=="function"?e.options:e;i&&(t.render=i,t.staticRenderFns=d,t._compiled=!0),s&&(t.functional=!0),v&&(t._scopeId="data-v-"+v);var o;if(f?(o=function(l){l=l||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,!l&&typeof __VUE_SSR_CONTEXT__<"u"&&(l=__VUE_SSR_CONTEXT__),u&&u.call(this,l),l&&l._registeredComponents&&l._registeredComponents.add(f)},t._ssrRegister=o):u&&(o=a?function(){u.call(this,(t.functional?this.parent:this).$root.$options.shadowRoot)}:u),o)if(t.functional){t._injectStyles=o;var T=t.render;t.render=function($,C){return o.call(C),T($,C)}}else{var y=t.beforeCreate;t.beforeCreate=y?[].concat(y,o):[o]}return{exports:e,options:t}}const p={};var M=E(S,V,w,!1,b,null,null,null);function b(e){for(let i in p)this[i]=p[i]}var h=function(){return M.exports}();/**
1
+ (function(n,o){typeof exports=="object"&&typeof module<"u"?o(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"],o):(n=typeof globalThis<"u"?globalThis:n||self,o(n.CodeMirror={},n.VueDemi,n.state,n.view,n.basicSetup,n.commands,n.lodash))})(this,function(n,o,m,c,C,S,_){"use strict";var w=o.defineComponent({name:"CodeMirror",model:{prop:"modelValue",event:"update:modelValue"},props:{modelValue:{type:String,default:""},selection:{type:Object,default:void 0},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:void 0},phrases:{type:Object,default:void 0},lang:{type:Object,default:void 0},linter:{type:Array,default:void 0}},emits:["update:modelValue","update"],setup(e,r){const d=o.ref(),s=o.ref(e.modelValue),{dark:u}=o.toRefs(e),v=r.emit,f=o.computed(()=>{const t=[e.basic?C.basicSetup:void 0,c.EditorView.updateListener.of(a=>v("update",a)),c.EditorView.theme(e.theme,{dark:u.value}),e.wrap?c.EditorView.lineWrapping:void 0,e.tab?c.keymap.of([S.indentWithTab]):void 0,e.phrases?m.EditorState.phrases.of(e.phrases):void 0,e.readonly?m.EditorState.readOnly.of(e.readonly):void 0,e.editable?c.EditorView.editable.of(e.editable):void 0,e.lang?o.toRaw(e.lang):void 0];return e.linter&&_.merge(t,e.linter),e.extensions&&_.merge(t,e.extensions),_.compact(t)});let i;return o.watch(s,t=>{if(i.composing)return;const a=i.state.selection;i.setState(m.EditorState.create({doc:t,extensions:f.value,selection:a}))}),o.watch(u,()=>{i.setState(m.EditorState.create({doc:s.value,extensions:f.value}))}),o.onMounted(async()=>{s.value==""&&d.value&&(s.value=_.trim(d.value.childNodes[0].innerText)),i=new c.EditorView({state:m.EditorState.create({doc:s.value,selection:e.selection,extensions:f.value}),parent:d.value,dispatch:t=>{i.update([t]),!t.changes.empty&&(s.value=i.state.doc.toString(),v("update:modelValue",s.value))}}),await o.nextTick()}),o.onUnmounted(()=>i.destroy()),{context:r,editor:d}}}),V=function(){var e=this,r=e.$createElement,d=e._self._c||r;return d("div",{ref:"editor",staticClass:"vue-codemirror"},[d("aside",{directives:[{name:"show",rawName:"v-show",value:!e.context.slots.default,expression:"!context.slots.default"}]},[e._t("default")],2)])},E=[];function T(e,r,d,s,u,v,f,i){var t=typeof e=="function"?e.options:e;r&&(t.render=r,t.staticRenderFns=d,t._compiled=!0),s&&(t.functional=!0),v&&(t._scopeId="data-v-"+v);var a;if(f?(a=function(l){l=l||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,!l&&typeof __VUE_SSR_CONTEXT__<"u"&&(l=__VUE_SSR_CONTEXT__),u&&u.call(this,l),l&&l._registeredComponents&&l._registeredComponents.add(f)},t._ssrRegister=a):u&&(a=i?function(){u.call(this,(t.functional?this.parent:this).$root.$options.shadowRoot)}:u),a)if(t.functional){t._injectStyles=a;var R=t.render;t.render=function($,b){return a.call(b),R($,b)}}else{var y=t.beforeCreate;t.beforeCreate=y?[].concat(y,a):[a]}return{exports:e,options:t}}const h={};var M=T(w,V,E,!1,g,null,null,null);function g(e){for(let r in h)this[r]=h[r]}var p=function(){return M.exports}();/**
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
- */n.install();const g=e=>{e.component("CodeMirror",h)};r.CodeMirror=h,r.default=h,r.install=g,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
8
+ */const O=o.isVue2?e=>{e.component("CodeMirror",p)}:o.install();n.default=p,n.install=O,Object.defineProperties(n,{__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.1.4",
4
+ "version": "0.1.6",
5
5
  "description": "CodeMirror6 Component for vue2",
6
6
  "keywords": [
7
7
  "vuejs",
@@ -32,7 +32,8 @@
32
32
  "url": "https://github.com/logue/vue-codemirror6/issues"
33
33
  },
34
34
  "engines": {
35
- "node": ">=16"
35
+ "node": ">=16.15.0",
36
+ "yarn": ">=1.22.4"
36
37
  },
37
38
  "license": "MIT",
38
39
  "main": "./dist/vue-codemirror6.umd.js",
@@ -57,7 +58,7 @@
57
58
  "clean": "rimraf node_modules/.vite",
58
59
  "build": "vite build & vue-tsc --declaration --emitDeclarationOnly",
59
60
  "build:clean": "rimraf dist",
60
- "lint": "eslint . --fix --cache --cache-location ./node_modules/.vite/vite-plugin-eslint && prettier . --write",
61
+ "lint": "eslint . --fix --cache --cache-location ./node_modules/.vite/vite-plugin-eslint && prettier . -w -u",
61
62
  "lint:markup": "vue-tsc --noEmit",
62
63
  "switch:2": "vue-demi-switch 2 & npx vue-demi-fix",
63
64
  "switch:3": "vue-demi-switch 3 vue3 & npx vue-demi-fix",
@@ -68,6 +69,7 @@
68
69
  "vue-demi": "latest"
69
70
  },
70
71
  "peerDependencies": {
72
+ "@codemirror/basic-setup": "latest",
71
73
  "@codemirror/state": "latest",
72
74
  "@codemirror/view": "latest",
73
75
  "@vue/composition-api": "latest",
@@ -81,16 +83,16 @@
81
83
  "devDependencies": {
82
84
  "@codemirror/basic-setup": "^0.20.0",
83
85
  "@codemirror/lang-markdown": "^0.20.0",
84
- "@codemirror/language": "^0.20.0",
85
- "@codemirror/lint": "^0.20.1",
86
- "@modyqyw/vite-plugin-eslint": "^1.3.0",
86
+ "@codemirror/language": "^0.20.1",
87
+ "@codemirror/lint": "^0.20.2",
88
+ "@modyqyw/vite-plugin-eslint": "^1.3.2",
87
89
  "@types/lodash": "^4.14.182",
88
- "@types/node": "^17.0.27",
89
- "@typescript-eslint/eslint-plugin": "^5.21.0",
90
- "@typescript-eslint/parser": "^5.21.0",
91
- "@vue/composition-api": "^1.5.0",
90
+ "@types/node": "^17.0.34",
91
+ "@typescript-eslint/eslint-plugin": "^5.25.0",
92
+ "@typescript-eslint/parser": "^5.25.0",
93
+ "@vue/composition-api": "^1.6.1",
92
94
  "@vue/eslint-config-typescript": "^10.0.0",
93
- "eslint": "^8.14.0",
95
+ "eslint": "^8.15.0",
94
96
  "eslint-config-google": "^0.14.0",
95
97
  "eslint-config-prettier": "^8.5.0",
96
98
  "eslint-import-resolver-alias": "^1.1.2",
@@ -99,23 +101,23 @@
99
101
  "eslint-plugin-prettier": "^4.0.0",
100
102
  "eslint-plugin-prettier-vue": "^3.1.0",
101
103
  "eslint-plugin-tsdoc": "^0.2.16",
102
- "eslint-plugin-vue": "^8.7.1",
104
+ "eslint-plugin-vue": "^9.0.1",
103
105
  "eslint-plugin-vuejs-accessibility": "^1.1.1",
104
- "husky": "^7.0.4",
105
- "lint-staged": "^12.4.0",
106
+ "husky": "^8.0.1",
107
+ "lint-staged": "^12.4.1",
106
108
  "lodash": "^4.17.21",
107
109
  "prettier": "^2.6.2",
108
110
  "prettier-plugin-jsdoc": "^0.3.38",
109
111
  "prettier-plugin-md-nocjsp": "^1.2.0",
110
112
  "prettier-plugin-sort-imports": "^1.6.0",
111
113
  "rimraf": "^3.0.2",
112
- "typescript": "^4.6.3",
113
- "vite": "^2.9.5",
114
- "vite-plugin-vue2": "^2.0.0",
114
+ "typescript": "^4.6.4",
115
+ "vite": "^2.9.9",
116
+ "vite-plugin-vue2": "^2.0.1",
115
117
  "vue": "^2.6.14",
116
- "vue-eslint-parser": "^8.3.0",
118
+ "vue-eslint-parser": "^9.0.2",
117
119
  "vue-template-compiler": "^2.6.14",
118
- "vue-tsc": "^0.34.10"
120
+ "vue-tsc": "^0.34.15"
119
121
  },
120
122
  "husky": {
121
123
  "hooks": {
@@ -7,21 +7,30 @@
7
7
  <script lang="ts">
8
8
  import {
9
9
  computed,
10
- ref,
11
- watch,
10
+ defineComponent,
11
+ nextTick,
12
12
  onMounted,
13
13
  onUnmounted,
14
- defineComponent,
14
+ ref,
15
+ toRaw,
16
+ toRefs,
17
+ watch,
18
+ type ComputedRef,
19
+ type PropType,
20
+ type Ref,
21
+ type SetupContext,
15
22
  } from 'vue-demi';
16
- import { EditorState } from '@codemirror/state';
17
- import { EditorView } from '@codemirror/view';
18
- import { compact, merge } from 'lodash';
19
23
 
20
- import type { ComputedRef, Ref, PropType, SetupContext } from 'vue-demi';
24
+ import { EditorSelection, EditorState } from '@codemirror/state';
25
+ import { EditorView, keymap } from '@codemirror/view';
26
+ import { basicSetup } from '@codemirror/basic-setup';
27
+ import { indentWithTab } from '@codemirror/commands';
28
+
29
+ import { compact, merge, trim } from 'lodash';
21
30
 
22
31
  import type CodeMirrorEmitsInterface from '@/interfaces/CodeMirrorEmitsInterface';
23
32
 
24
- import type { Extension, Transaction } from '@codemirror/state';
33
+ import type { Extension, Text, Transaction } from '@codemirror/state';
25
34
  import type { LanguageSupport } from '@codemirror/language';
26
35
  import type { ViewUpdate } from '@codemirror/view';
27
36
  import type { Diagnostic } from '@codemirror/lint';
@@ -40,9 +49,20 @@ export default defineComponent({
40
49
  props: {
41
50
  /** Model value */
42
51
  modelValue: {
43
- type: String,
52
+ type: String as PropType<string | Text>,
44
53
  default: '',
45
54
  },
55
+ /**
56
+ * Selection
57
+ *
58
+ * @see {@link https://codemirror.net/6/docs/ref/#state.EditorSelection | EditorSelection}
59
+ */
60
+ selection: {
61
+ type: Object as PropType<
62
+ EditorSelection | { anchor: number; head?: number }
63
+ >,
64
+ default: undefined,
65
+ },
46
66
  /**
47
67
  * Theme
48
68
  *
@@ -50,13 +70,42 @@ export default defineComponent({
50
70
  */
51
71
  theme: {
52
72
  type: Object as PropType<{ [selector: string]: StyleSpec }>,
53
- default: undefined,
73
+ default: () => {
74
+ return {};
75
+ },
54
76
  },
55
77
  /** Dark Mode */
56
78
  dark: {
57
79
  type: Boolean,
58
80
  default: false,
59
81
  },
82
+ /**
83
+ * Use Basic Setup
84
+ *
85
+ * @see {@link https://codemirror.net/6/docs/ref/#basic-setup | basic-setup}
86
+ */
87
+ basic: {
88
+ type: Boolean,
89
+ default: false,
90
+ },
91
+ /**
92
+ * Line wrapping
93
+ *
94
+ * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5ElineWrapping | LineWrapping}
95
+ */
96
+ wrap: {
97
+ type: Boolean,
98
+ default: false,
99
+ },
100
+ /**
101
+ * Tab handling
102
+ *
103
+ * @see {@link https://codemirror.net/6/examples/tab/ | Tab Handling}
104
+ */
105
+ tab: {
106
+ type: Boolean,
107
+ default: false,
108
+ },
60
109
  /**
61
110
  * Readonly
62
111
  *
@@ -121,35 +170,41 @@ export default defineComponent({
121
170
  */
122
171
  setup(props, context: SetupContext) {
123
172
  /** Editor DOM */
124
- const editor: Ref<Element | undefined> = ref<Element>();
173
+ const editor: Ref<Element | undefined> = ref();
125
174
 
126
- /** Model */
127
- const modelValue: Ref<string> = ref(props.modelValue);
175
+ /** Internal value */
176
+ const doc: Ref<string | Text | undefined> = ref(props.modelValue);
128
177
 
129
178
  /** Dark mode */
130
- const dark: Ref<boolean | undefined> = ref(props.dark);
179
+ const { dark } = toRefs(props);
131
180
 
132
181
  /** Emits */
133
182
  const emit = context.emit as CodeMirrorEmitsInterface;
134
183
 
135
184
  /** CodeMirror Extension */
136
- const extensions: ComputedRef<Extension[]> = computed(() => {
185
+ const exts: ComputedRef<Extension[]> = computed(() => {
137
186
  /** Default extension */
138
187
  const ext = [
188
+ // Toggle basic setup
189
+ props.basic ? basicSetup : undefined,
139
190
  // ViewUpdate event listener
140
- EditorView.updateListener.of((update: ViewUpdate) => {
141
- emit('update', update);
142
- }),
191
+ EditorView.updateListener.of((update: ViewUpdate) =>
192
+ emit('update', update)
193
+ ),
143
194
  // Toggle light/dark mode.
144
- EditorView.theme(props.theme || {}, { dark: dark.value }),
195
+ EditorView.theme(props.theme, { dark: dark.value }),
196
+ // Toggle line wrapping
197
+ props.wrap ? EditorView.lineWrapping : undefined,
198
+ // Indent with tab
199
+ props.tab ? keymap.of([indentWithTab]) : undefined,
145
200
  // locale settings
146
201
  props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
147
- // Parser language setting
148
- props.lang,
149
202
  // Readonly option
150
203
  props.readonly ? EditorState.readOnly.of(props.readonly) : undefined,
151
204
  // Editable option
152
205
  props.editable ? EditorView.editable.of(props.editable) : undefined,
206
+ // Lang
207
+ props.lang ? toRaw(props.lang) : undefined,
153
208
  ];
154
209
 
155
210
  if (props.linter) {
@@ -162,19 +217,19 @@ export default defineComponent({
162
217
  merge(ext, props.extensions);
163
218
  }
164
219
 
165
- // console.debug('[CodeMirror.vue] Loaded extensions:', ext, compact(ext));
220
+ // console.debug('[CodeMirror.vue] Loaded extensions:', compact(ext));
166
221
  return compact(ext);
167
222
  });
168
223
 
169
224
  /** CodeMirror Editor View */
170
- let view!: EditorView;
225
+ let view: EditorView;
171
226
 
172
227
  /**
173
228
  * Input value changed
174
229
  *
175
230
  * @see {@link https://codemirror.net/6/docs/migration/#making-changes | Making Changes}
176
231
  */
177
- watch(modelValue, current => {
232
+ watch(doc, current => {
178
233
  if (view.composing) {
179
234
  // IME fix
180
235
  return;
@@ -185,35 +240,34 @@ export default defineComponent({
185
240
  view.setState(
186
241
  EditorState.create({
187
242
  doc: current,
188
- extensions: extensions.value,
243
+ extensions: exts.value,
189
244
  selection: previous,
190
245
  })
191
246
  );
192
247
  });
193
248
 
194
- /** Toggle Dark mode */
195
249
  watch(dark, () => {
196
250
  view.setState(
197
251
  EditorState.create({
198
- doc: modelValue.value,
199
- extensions: extensions.value,
252
+ doc: doc.value,
253
+ extensions: exts.value,
200
254
  })
201
255
  );
202
256
  });
203
257
 
204
258
  /** When loaded */
205
- onMounted(() => {
259
+ onMounted(async () => {
206
260
  /** Initial Value */
207
- const value =
208
- !modelValue.value && editor.value
209
- ? (editor.value.childNodes[0] as HTMLDivElement).innerText
210
- : modelValue.value;
261
+ if (doc.value == '' && editor.value) {
262
+ doc.value = trim((editor.value.childNodes[0] as HTMLElement).innerText);
263
+ }
211
264
 
212
265
  // Register Codemirror
213
266
  view = new EditorView({
214
267
  state: EditorState.create({
215
- doc: value,
216
- extensions: extensions.value,
268
+ doc: doc.value,
269
+ selection: props.selection,
270
+ extensions: exts.value,
217
271
  }),
218
272
  parent: editor.value,
219
273
  dispatch: (tr: Transaction) => {
@@ -221,10 +275,11 @@ export default defineComponent({
221
275
 
222
276
  if (tr.changes.empty) return;
223
277
  // to parent binding
224
- modelValue.value = view.state.doc.toString();
225
- emit('update:modelValue', modelValue.value);
278
+ doc.value = view.state.doc.toString();
279
+ emit('update:modelValue', doc.value);
226
280
  },
227
281
  });
282
+ await nextTick();
228
283
  });
229
284
 
230
285
  /** Destroy */