vue-codemirror6 1.0.1 → 1.0.2

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.
@@ -5,8 +5,283 @@
5
5
  * @author Logue <logue@hotmail.co.jp>
6
6
  * @copyright 2022 By Masashi Yoshikawa All rights reserved.
7
7
  * @license MIT
8
- * @version 1.0.1
8
+ * @version 1.0.2
9
9
  * @see {@link https://github.com/logue/vue-codemirror6}
10
10
  */
11
11
 
12
- var CodeMirror=function(f,n,m,r,i,p,S,b){"use strict";const C=a=>a?Object.entries(a).reduce((s,[o,d])=>(o=o.charAt(0).toUpperCase()+o.slice(1),o=`on${o}`,{...s,[o]:d}),{}):{},y=(a,s={},o)=>{if(n.isVue2&&parseInt(n.Vue2.version)<2.7)return n.h(a,s,o);const{props:d,domProps:t,on:c,...u}=s,v=c?C(c):{};return n.h(a,{...u,...d,...t,...v},o)},E=a=>typeof a=="function"?a():a,g=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},minimal:{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","ready","focus","changed"],setup(a,s){const o=n.ref(),d=n.ref(a.modelValue),t=n.shallowRef(new i.EditorView),c=n.computed({get:()=>t.value.state.selection,set:e=>t.value.dispatch({selection:e})}),u=n.computed({get:()=>c.value.main.head||0,set:e=>t.value.dispatch({selection:{anchor:e}})}),v=n.computed({get:()=>t.value.state,set:e=>t.value.setState(e)}),w=n.computed({get:()=>t.value.hasFocus,set:e=>{e&&t.value.focus()}}),V=n.computed(()=>m.compact([a.basic?S.basicSetup:void 0,a.minimal&&!a.basic?S.minimalSetup:void 0,i.EditorView.updateListener.of(e=>s.emit("update",e)),i.EditorView.theme(a.theme,{dark:a.dark}),a.wrap?i.EditorView.lineWrapping:void 0,a.tab?i.keymap.of([b.indentWithTab]):void 0,a.phrases?r.EditorState.phrases.of(a.phrases):void 0,r.EditorState.readOnly.of(a.readonly),i.EditorView.editable.of(a.editable),a.lang?n.toRaw(a.lang):void 0,a.linter?p.linter(a.linter):void 0,a.linter&&a.lintGutter?p.lintGutter():void 0,...a.extensions]));return n.watch(()=>a.modelValue,e=>{!t.value||t.value.composing||t.value.dispatch({changes:{from:0,to:t.value.state.doc.length,insert:e},selection:c.value})},{immediate:!0}),n.watch(V,e=>{t.value.dispatch({effects:r.StateEffect.reconfigure.of(e)})}),n.watch(w,e=>{s.emit("focus",e)}),n.onMounted(async()=>{let e=d.value;o.value&&o.value.childNodes[0]&&(d.value!==""&&console.warn("[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."),e=m.trim(o.value.childNodes[0].innerText)),t.value=new i.EditorView({doc:e,extensions:V.value,parent:o.value,dispatch:l=>{if(t.value.update([l]),l.changes.empty)return;const h=t.value.state.doc.toString();s.emit("update:modelValue",h),s.emit("changed",h)}}),await n.nextTick(),s.emit("ready",{view:t,state:v.value,container:o.value})}),n.onUnmounted(()=>t.value.destroy()),{context:s,editor:o,cursor:u,selection:c,state:v,focus:w,getRange:(e,l)=>t.value.state.sliceDoc(e,l),getLine:e=>t.value.state.doc.line(e+1).text,lineCount:()=>t.value.state.doc.lines,getCursor:()=>u.value,listSelections:()=>t.value.state.selection.ranges,getSelection:()=>t.value.state.sliceDoc(t.value.state.selection.main.from,t.value.state.selection.main.to),getSelections:()=>t.value.state.selection.ranges.map(e=>t.value.state.sliceDoc(e.from,e.to)),somethingSelected:()=>t.value.state.selection.ranges.some(e=>!e.empty),replaceRange:(e,l,h)=>t.value.dispatch({changes:{from:l,to:h,insert:e}}),replaceSelection:e=>t.value.dispatch(t.value.state.replaceSelection(e)),setCursor:e=>u.value=e,setSelection:(e,l)=>t.value.dispatch({selection:{anchor:e,head:l}}),setSelections:(e,l)=>t.value.dispatch({selection:r.EditorSelection.create(e,l)}),extendSelectionsBy:e=>t.value.dispatch({selection:r.EditorSelection.create(c.value.ranges.map(l=>l.extend(e(l))))})}},render(){return y("div",{ref:"editor",class:"vue-codemirror"},this.$slots.default?y("aside",{style:"display: none;","aria-hidden":"true"},E(this.$slots.default)):void 0)}}),B=a=>a.component("CodeMirror",g);return typeof window<"u"&&window.Vue&&window.Vue.use(g),f.default=g,f.install=B,Object.defineProperties(f,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),f}({},VueDemi,lodash,state,view,lint,codemirror,commands);
12
+ var CodeMirror = (function (exports, vueDemi, lodash, state, view, lint, codemirror, commands) {
13
+ 'use strict';
14
+
15
+ const adaptOnsV3 = (ons) => {
16
+ if (!ons)
17
+ return {};
18
+ return Object.entries(ons).reduce((ret, [key, handler]) => {
19
+ key = key.charAt(0).toUpperCase() + key.slice(1);
20
+ key = `on${key}`;
21
+ return { ...ret, [key]: handler };
22
+ }, {});
23
+ };
24
+ const h = (type, options = {}, chidren) => {
25
+ if (vueDemi.isVue2 && parseInt(vueDemi.Vue2.version) < 2.7) {
26
+ return vueDemi.h(type, options, chidren);
27
+ }
28
+ const { props, domProps, on, ...extraOptions } = options;
29
+ const ons = on ? adaptOnsV3(on) : {};
30
+ return vueDemi.h(
31
+ type,
32
+ { ...extraOptions, ...props, ...domProps, ...ons },
33
+ chidren
34
+ );
35
+ };
36
+ const slot = (defaultSlots) => typeof defaultSlots == "function" ? defaultSlots() : defaultSlots;
37
+
38
+ const CodeMirror = vueDemi.defineComponent({
39
+ name: "CodeMirror",
40
+ model: {
41
+ prop: "modelValue",
42
+ event: "update:modelValue"
43
+ },
44
+ props: {
45
+ modelValue: {
46
+ type: String,
47
+ default: ""
48
+ },
49
+ theme: {
50
+ type: Object,
51
+ default: () => {
52
+ return {};
53
+ }
54
+ },
55
+ dark: {
56
+ type: Boolean,
57
+ default: false
58
+ },
59
+ basic: {
60
+ type: Boolean,
61
+ default: false
62
+ },
63
+ minimal: {
64
+ type: Boolean,
65
+ default: false
66
+ },
67
+ wrap: {
68
+ type: Boolean,
69
+ default: false
70
+ },
71
+ tab: {
72
+ type: Boolean,
73
+ default: false
74
+ },
75
+ readonly: {
76
+ type: Boolean,
77
+ default: false
78
+ },
79
+ editable: {
80
+ type: Boolean,
81
+ default: true
82
+ },
83
+ extensions: {
84
+ type: Array,
85
+ default: () => {
86
+ return [];
87
+ }
88
+ },
89
+ phrases: {
90
+ type: Object,
91
+ default: () => void 0
92
+ },
93
+ lang: {
94
+ type: Object,
95
+ default: () => void 0
96
+ },
97
+ linter: {
98
+ type: Function,
99
+ default: void 0
100
+ },
101
+ lintGutter: {
102
+ type: Boolean,
103
+ defalt: false
104
+ },
105
+ tag: {
106
+ type: String,
107
+ default: "div"
108
+ }
109
+ },
110
+ emits: ["update:modelValue", "update", "ready", "focus", "changed"],
111
+ setup(props, context) {
112
+ const editor = vueDemi.ref();
113
+ const doc = vueDemi.ref(props.modelValue);
114
+ const view$1 = vueDemi.shallowRef(new view.EditorView());
115
+ const selection = vueDemi.computed({
116
+ get: () => view$1.value.state.selection,
117
+ set: (s) => view$1.value.dispatch({ selection: s })
118
+ });
119
+ const cursor = vueDemi.computed({
120
+ get: () => selection.value.main.head || 0,
121
+ set: (a) => view$1.value.dispatch({ selection: { anchor: a } })
122
+ });
123
+ const state$1 = vueDemi.computed({
124
+ get: () => view$1.value.state,
125
+ set: (s) => view$1.value.setState(s)
126
+ });
127
+ const focus = vueDemi.computed({
128
+ get: () => view$1.value.hasFocus,
129
+ set: (f) => {
130
+ if (f) {
131
+ view$1.value.focus();
132
+ }
133
+ }
134
+ });
135
+ const extensions = vueDemi.computed(
136
+ () => lodash.compact([
137
+ props.basic ? codemirror.basicSetup : void 0,
138
+ props.minimal && !props.basic ? codemirror.minimalSetup : void 0,
139
+ view.EditorView.updateListener.of(
140
+ (update) => context.emit("update", update)
141
+ ),
142
+ view.EditorView.theme(props.theme, { dark: props.dark }),
143
+ props.wrap ? view.EditorView.lineWrapping : void 0,
144
+ props.tab ? view.keymap.of([commands.indentWithTab]) : void 0,
145
+ props.phrases ? state.EditorState.phrases.of(props.phrases) : void 0,
146
+ state.EditorState.readOnly.of(props.readonly),
147
+ view.EditorView.editable.of(props.editable),
148
+ props.lang ? vueDemi.toRaw(props.lang) : void 0,
149
+ props.linter ? lint.linter(props.linter) : void 0,
150
+ props.linter && props.lintGutter ? lint.lintGutter() : void 0,
151
+ ...props.extensions
152
+ ])
153
+ );
154
+ vueDemi.watch(
155
+ () => props.modelValue,
156
+ (value) => {
157
+ if (!view$1.value || view$1.value.composing) {
158
+ return;
159
+ }
160
+ const previous = view$1.value.state.selection;
161
+ view$1.value.dispatch({
162
+ changes: { from: 0, to: view$1.value.state.doc.length, insert: value },
163
+ selection: previous
164
+ });
165
+ },
166
+ { immediate: true }
167
+ );
168
+ vueDemi.watch(extensions, (e) => {
169
+ view$1.value.dispatch({
170
+ effects: state.StateEffect.reconfigure.of(e)
171
+ });
172
+ });
173
+ vueDemi.watch(focus, (isFocus) => {
174
+ context.emit("focus", isFocus);
175
+ });
176
+ vueDemi.onMounted(async () => {
177
+ let value = doc.value;
178
+ if (editor.value && editor.value.childNodes[0]) {
179
+ if (doc.value !== "") {
180
+ console.warn(
181
+ "[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."
182
+ );
183
+ }
184
+ value = lodash.trim(editor.value.childNodes[0].innerText);
185
+ }
186
+ view$1.value = new view.EditorView({
187
+ doc: value,
188
+ extensions: extensions.value,
189
+ parent: editor.value,
190
+ dispatch: (tr) => {
191
+ view$1.value.update([tr]);
192
+ if (tr.changes.empty)
193
+ return;
194
+ const v = view$1.value.state.doc.toString();
195
+ context.emit("update:modelValue", v);
196
+ context.emit("changed", v);
197
+ }
198
+ });
199
+ await vueDemi.nextTick();
200
+ context.emit("ready", {
201
+ view: view$1,
202
+ state: state$1.value,
203
+ container: editor.value
204
+ });
205
+ });
206
+ vueDemi.onUnmounted(() => view$1.value.destroy());
207
+ const getRange = (from, to) => view$1.value.state.sliceDoc(from, to);
208
+ const getLine = (number) => view$1.value.state.doc.line(number + 1).text;
209
+ const lineCount = () => view$1.value.state.doc.lines;
210
+ const getCursor = () => cursor.value;
211
+ const listSelections = () => view$1.value.state.selection.ranges;
212
+ const getSelection = () => view$1.value.state.sliceDoc(
213
+ view$1.value.state.selection.main.from,
214
+ view$1.value.state.selection.main.to
215
+ );
216
+ const getSelections = () => view$1.value.state.selection.ranges.map(
217
+ (r) => view$1.value.state.sliceDoc(r.from, r.to)
218
+ );
219
+ const somethingSelected = () => view$1.value.state.selection.ranges.some(
220
+ (r) => !r.empty
221
+ );
222
+ const replaceRange = (replacement, from, to) => view$1.value.dispatch({
223
+ changes: { from, to, insert: replacement }
224
+ });
225
+ const replaceSelection = (replacement) => view$1.value.dispatch(view$1.value.state.replaceSelection(replacement));
226
+ const setCursor = (position) => cursor.value = position;
227
+ const setSelection = (anchor, head) => view$1.value.dispatch({ selection: { anchor, head } });
228
+ const setSelections = (ranges, primary) => view$1.value.dispatch({
229
+ selection: state.EditorSelection.create(ranges, primary)
230
+ });
231
+ const extendSelectionsBy = (f) => view$1.value.dispatch({
232
+ selection: state.EditorSelection.create(
233
+ selection.value.ranges.map((r) => r.extend(f(r)))
234
+ )
235
+ });
236
+ return {
237
+ context,
238
+ editor,
239
+ cursor,
240
+ selection,
241
+ state: state$1,
242
+ focus,
243
+ getRange,
244
+ getLine,
245
+ lineCount,
246
+ getCursor,
247
+ listSelections,
248
+ getSelection,
249
+ getSelections,
250
+ somethingSelected,
251
+ replaceRange,
252
+ replaceSelection,
253
+ setCursor,
254
+ setSelection,
255
+ setSelections,
256
+ extendSelectionsBy
257
+ };
258
+ },
259
+ render() {
260
+ return h(
261
+ this.$props.tag,
262
+ {
263
+ ref: "editor",
264
+ class: "vue-codemirror"
265
+ },
266
+ this.$slots.default ? h(
267
+ "aside",
268
+ { style: "display: none;", "aria-hidden": "true" },
269
+ slot(this.$slots.default)
270
+ ) : void 0
271
+ );
272
+ }
273
+ });
274
+
275
+ const installCodeMirror = (app) => app.component("CodeMirror", CodeMirror);
276
+ if (typeof window !== "undefined" && window.Vue) {
277
+ window.Vue.use(CodeMirror);
278
+ }
279
+
280
+ exports.default = CodeMirror;
281
+ exports.install = installCodeMirror;
282
+
283
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
284
+
285
+ return exports;
286
+
287
+ })({}, VueDemi, lodash, state, view, lint, codemirror, commands);
package/dist/index.umd.js CHANGED
@@ -5,8 +5,284 @@
5
5
  * @author Logue <logue@hotmail.co.jp>
6
6
  * @copyright 2022 By Masashi Yoshikawa All rights reserved.
7
7
  * @license MIT
8
- * @version 1.0.1
8
+ * @version 1.0.2
9
9
  * @see {@link https://github.com/logue/vue-codemirror6}
10
10
  */
11
11
 
12
- (function(i,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("vue-demi"),require("lodash"),require("@codemirror/state"),require("@codemirror/view"),require("@codemirror/lint"),require("codemirror"),require("@codemirror/commands")):typeof define=="function"&&define.amd?define(["exports","vue-demi","lodash","@codemirror/state","@codemirror/view","@codemirror/lint","codemirror","@codemirror/commands"],a):(i=typeof globalThis<"u"?globalThis:i||self,a(i.CodeMirror={},i.VueDemi,i.lodash,i.state,i.view,i.lint,i.codemirror,i.commands))})(this,function(i,a,p,u,r,g,y,C){"use strict";const E=o=>o?Object.entries(o).reduce((l,[n,c])=>(n=n.charAt(0).toUpperCase()+n.slice(1),n=`on${n}`,{...l,[n]:c}),{}):{},S=(o,l={},n)=>{if(a.isVue2&&parseInt(a.Vue2.version)<2.7)return a.h(o,l,n);const{props:c,domProps:t,on:d,...f}=l,m=d?E(d):{};return a.h(o,{...f,...c,...t,...m},n)},b=o=>typeof o=="function"?o():o,h=a.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},minimal:{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","ready","focus","changed"],setup(o,l){const n=a.ref(),c=a.ref(o.modelValue),t=a.shallowRef(new r.EditorView),d=a.computed({get:()=>t.value.state.selection,set:e=>t.value.dispatch({selection:e})}),f=a.computed({get:()=>d.value.main.head||0,set:e=>t.value.dispatch({selection:{anchor:e}})}),m=a.computed({get:()=>t.value.state,set:e=>t.value.setState(e)}),w=a.computed({get:()=>t.value.hasFocus,set:e=>{e&&t.value.focus()}}),V=a.computed(()=>p.compact([o.basic?y.basicSetup:void 0,o.minimal&&!o.basic?y.minimalSetup:void 0,r.EditorView.updateListener.of(e=>l.emit("update",e)),r.EditorView.theme(o.theme,{dark:o.dark}),o.wrap?r.EditorView.lineWrapping:void 0,o.tab?r.keymap.of([C.indentWithTab]):void 0,o.phrases?u.EditorState.phrases.of(o.phrases):void 0,u.EditorState.readOnly.of(o.readonly),r.EditorView.editable.of(o.editable),o.lang?a.toRaw(o.lang):void 0,o.linter?g.linter(o.linter):void 0,o.linter&&o.lintGutter?g.lintGutter():void 0,...o.extensions]));return a.watch(()=>o.modelValue,e=>{!t.value||t.value.composing||t.value.dispatch({changes:{from:0,to:t.value.state.doc.length,insert:e},selection:d.value})},{immediate:!0}),a.watch(V,e=>{t.value.dispatch({effects:u.StateEffect.reconfigure.of(e)})}),a.watch(w,e=>{l.emit("focus",e)}),a.onMounted(async()=>{let e=c.value;n.value&&n.value.childNodes[0]&&(c.value!==""&&console.warn("[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."),e=p.trim(n.value.childNodes[0].innerText)),t.value=new r.EditorView({doc:e,extensions:V.value,parent:n.value,dispatch:s=>{if(t.value.update([s]),s.changes.empty)return;const v=t.value.state.doc.toString();l.emit("update:modelValue",v),l.emit("changed",v)}}),await a.nextTick(),l.emit("ready",{view:t,state:m.value,container:n.value})}),a.onUnmounted(()=>t.value.destroy()),{context:l,editor:n,cursor:f,selection:d,state:m,focus:w,getRange:(e,s)=>t.value.state.sliceDoc(e,s),getLine:e=>t.value.state.doc.line(e+1).text,lineCount:()=>t.value.state.doc.lines,getCursor:()=>f.value,listSelections:()=>t.value.state.selection.ranges,getSelection:()=>t.value.state.sliceDoc(t.value.state.selection.main.from,t.value.state.selection.main.to),getSelections:()=>t.value.state.selection.ranges.map(e=>t.value.state.sliceDoc(e.from,e.to)),somethingSelected:()=>t.value.state.selection.ranges.some(e=>!e.empty),replaceRange:(e,s,v)=>t.value.dispatch({changes:{from:s,to:v,insert:e}}),replaceSelection:e=>t.value.dispatch(t.value.state.replaceSelection(e)),setCursor:e=>f.value=e,setSelection:(e,s)=>t.value.dispatch({selection:{anchor:e,head:s}}),setSelections:(e,s)=>t.value.dispatch({selection:u.EditorSelection.create(e,s)}),extendSelectionsBy:e=>t.value.dispatch({selection:u.EditorSelection.create(d.value.ranges.map(s=>s.extend(e(s))))})}},render(){return S("div",{ref:"editor",class:"vue-codemirror"},this.$slots.default?S("aside",{style:"display: none;","aria-hidden":"true"},b(this.$slots.default)):void 0)}}),B=o=>o.component("CodeMirror",h);typeof window<"u"&&window.Vue&&window.Vue.use(h),i.default=h,i.install=B,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
12
+ (function (global, factory) {
13
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vue-demi'), require('lodash'), require('@codemirror/state'), require('@codemirror/view'), require('@codemirror/lint'), require('codemirror'), require('@codemirror/commands')) :
14
+ typeof define === 'function' && define.amd ? define(['exports', 'vue-demi', 'lodash', '@codemirror/state', '@codemirror/view', '@codemirror/lint', 'codemirror', '@codemirror/commands'], factory) :
15
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.CodeMirror = {}, global.VueDemi, global.lodash, global.state, global.view, global.lint, global.codemirror, global.commands));
16
+ })(this, (function (exports, vueDemi, lodash, state, view, lint, codemirror, commands) { 'use strict';
17
+
18
+ const adaptOnsV3 = (ons) => {
19
+ if (!ons)
20
+ return {};
21
+ return Object.entries(ons).reduce((ret, [key, handler]) => {
22
+ key = key.charAt(0).toUpperCase() + key.slice(1);
23
+ key = `on${key}`;
24
+ return { ...ret, [key]: handler };
25
+ }, {});
26
+ };
27
+ const h = (type, options = {}, chidren) => {
28
+ if (vueDemi.isVue2 && parseInt(vueDemi.Vue2.version) < 2.7) {
29
+ return vueDemi.h(type, options, chidren);
30
+ }
31
+ const { props, domProps, on, ...extraOptions } = options;
32
+ const ons = on ? adaptOnsV3(on) : {};
33
+ return vueDemi.h(
34
+ type,
35
+ { ...extraOptions, ...props, ...domProps, ...ons },
36
+ chidren
37
+ );
38
+ };
39
+ const slot = (defaultSlots) => typeof defaultSlots == "function" ? defaultSlots() : defaultSlots;
40
+
41
+ const CodeMirror = vueDemi.defineComponent({
42
+ name: "CodeMirror",
43
+ model: {
44
+ prop: "modelValue",
45
+ event: "update:modelValue"
46
+ },
47
+ props: {
48
+ modelValue: {
49
+ type: String,
50
+ default: ""
51
+ },
52
+ theme: {
53
+ type: Object,
54
+ default: () => {
55
+ return {};
56
+ }
57
+ },
58
+ dark: {
59
+ type: Boolean,
60
+ default: false
61
+ },
62
+ basic: {
63
+ type: Boolean,
64
+ default: false
65
+ },
66
+ minimal: {
67
+ type: Boolean,
68
+ default: false
69
+ },
70
+ wrap: {
71
+ type: Boolean,
72
+ default: false
73
+ },
74
+ tab: {
75
+ type: Boolean,
76
+ default: false
77
+ },
78
+ readonly: {
79
+ type: Boolean,
80
+ default: false
81
+ },
82
+ editable: {
83
+ type: Boolean,
84
+ default: true
85
+ },
86
+ extensions: {
87
+ type: Array,
88
+ default: () => {
89
+ return [];
90
+ }
91
+ },
92
+ phrases: {
93
+ type: Object,
94
+ default: () => void 0
95
+ },
96
+ lang: {
97
+ type: Object,
98
+ default: () => void 0
99
+ },
100
+ linter: {
101
+ type: Function,
102
+ default: void 0
103
+ },
104
+ lintGutter: {
105
+ type: Boolean,
106
+ defalt: false
107
+ },
108
+ tag: {
109
+ type: String,
110
+ default: "div"
111
+ }
112
+ },
113
+ emits: ["update:modelValue", "update", "ready", "focus", "changed"],
114
+ setup(props, context) {
115
+ const editor = vueDemi.ref();
116
+ const doc = vueDemi.ref(props.modelValue);
117
+ const view$1 = vueDemi.shallowRef(new view.EditorView());
118
+ const selection = vueDemi.computed({
119
+ get: () => view$1.value.state.selection,
120
+ set: (s) => view$1.value.dispatch({ selection: s })
121
+ });
122
+ const cursor = vueDemi.computed({
123
+ get: () => selection.value.main.head || 0,
124
+ set: (a) => view$1.value.dispatch({ selection: { anchor: a } })
125
+ });
126
+ const state$1 = vueDemi.computed({
127
+ get: () => view$1.value.state,
128
+ set: (s) => view$1.value.setState(s)
129
+ });
130
+ const focus = vueDemi.computed({
131
+ get: () => view$1.value.hasFocus,
132
+ set: (f) => {
133
+ if (f) {
134
+ view$1.value.focus();
135
+ }
136
+ }
137
+ });
138
+ const extensions = vueDemi.computed(
139
+ () => lodash.compact([
140
+ props.basic ? codemirror.basicSetup : void 0,
141
+ props.minimal && !props.basic ? codemirror.minimalSetup : void 0,
142
+ view.EditorView.updateListener.of(
143
+ (update) => context.emit("update", update)
144
+ ),
145
+ view.EditorView.theme(props.theme, { dark: props.dark }),
146
+ props.wrap ? view.EditorView.lineWrapping : void 0,
147
+ props.tab ? view.keymap.of([commands.indentWithTab]) : void 0,
148
+ props.phrases ? state.EditorState.phrases.of(props.phrases) : void 0,
149
+ state.EditorState.readOnly.of(props.readonly),
150
+ view.EditorView.editable.of(props.editable),
151
+ props.lang ? vueDemi.toRaw(props.lang) : void 0,
152
+ props.linter ? lint.linter(props.linter) : void 0,
153
+ props.linter && props.lintGutter ? lint.lintGutter() : void 0,
154
+ ...props.extensions
155
+ ])
156
+ );
157
+ vueDemi.watch(
158
+ () => props.modelValue,
159
+ (value) => {
160
+ if (!view$1.value || view$1.value.composing) {
161
+ return;
162
+ }
163
+ const previous = view$1.value.state.selection;
164
+ view$1.value.dispatch({
165
+ changes: { from: 0, to: view$1.value.state.doc.length, insert: value },
166
+ selection: previous
167
+ });
168
+ },
169
+ { immediate: true }
170
+ );
171
+ vueDemi.watch(extensions, (e) => {
172
+ view$1.value.dispatch({
173
+ effects: state.StateEffect.reconfigure.of(e)
174
+ });
175
+ });
176
+ vueDemi.watch(focus, (isFocus) => {
177
+ context.emit("focus", isFocus);
178
+ });
179
+ vueDemi.onMounted(async () => {
180
+ let value = doc.value;
181
+ if (editor.value && editor.value.childNodes[0]) {
182
+ if (doc.value !== "") {
183
+ console.warn(
184
+ "[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."
185
+ );
186
+ }
187
+ value = lodash.trim(editor.value.childNodes[0].innerText);
188
+ }
189
+ view$1.value = new view.EditorView({
190
+ doc: value,
191
+ extensions: extensions.value,
192
+ parent: editor.value,
193
+ dispatch: (tr) => {
194
+ view$1.value.update([tr]);
195
+ if (tr.changes.empty)
196
+ return;
197
+ const v = view$1.value.state.doc.toString();
198
+ context.emit("update:modelValue", v);
199
+ context.emit("changed", v);
200
+ }
201
+ });
202
+ await vueDemi.nextTick();
203
+ context.emit("ready", {
204
+ view: view$1,
205
+ state: state$1.value,
206
+ container: editor.value
207
+ });
208
+ });
209
+ vueDemi.onUnmounted(() => view$1.value.destroy());
210
+ const getRange = (from, to) => view$1.value.state.sliceDoc(from, to);
211
+ const getLine = (number) => view$1.value.state.doc.line(number + 1).text;
212
+ const lineCount = () => view$1.value.state.doc.lines;
213
+ const getCursor = () => cursor.value;
214
+ const listSelections = () => view$1.value.state.selection.ranges;
215
+ const getSelection = () => view$1.value.state.sliceDoc(
216
+ view$1.value.state.selection.main.from,
217
+ view$1.value.state.selection.main.to
218
+ );
219
+ const getSelections = () => view$1.value.state.selection.ranges.map(
220
+ (r) => view$1.value.state.sliceDoc(r.from, r.to)
221
+ );
222
+ const somethingSelected = () => view$1.value.state.selection.ranges.some(
223
+ (r) => !r.empty
224
+ );
225
+ const replaceRange = (replacement, from, to) => view$1.value.dispatch({
226
+ changes: { from, to, insert: replacement }
227
+ });
228
+ const replaceSelection = (replacement) => view$1.value.dispatch(view$1.value.state.replaceSelection(replacement));
229
+ const setCursor = (position) => cursor.value = position;
230
+ const setSelection = (anchor, head) => view$1.value.dispatch({ selection: { anchor, head } });
231
+ const setSelections = (ranges, primary) => view$1.value.dispatch({
232
+ selection: state.EditorSelection.create(ranges, primary)
233
+ });
234
+ const extendSelectionsBy = (f) => view$1.value.dispatch({
235
+ selection: state.EditorSelection.create(
236
+ selection.value.ranges.map((r) => r.extend(f(r)))
237
+ )
238
+ });
239
+ return {
240
+ context,
241
+ editor,
242
+ cursor,
243
+ selection,
244
+ state: state$1,
245
+ focus,
246
+ getRange,
247
+ getLine,
248
+ lineCount,
249
+ getCursor,
250
+ listSelections,
251
+ getSelection,
252
+ getSelections,
253
+ somethingSelected,
254
+ replaceRange,
255
+ replaceSelection,
256
+ setCursor,
257
+ setSelection,
258
+ setSelections,
259
+ extendSelectionsBy
260
+ };
261
+ },
262
+ render() {
263
+ return h(
264
+ this.$props.tag,
265
+ {
266
+ ref: "editor",
267
+ class: "vue-codemirror"
268
+ },
269
+ this.$slots.default ? h(
270
+ "aside",
271
+ { style: "display: none;", "aria-hidden": "true" },
272
+ slot(this.$slots.default)
273
+ ) : void 0
274
+ );
275
+ }
276
+ });
277
+
278
+ const installCodeMirror = (app) => app.component("CodeMirror", CodeMirror);
279
+ if (typeof window !== "undefined" && window.Vue) {
280
+ window.Vue.use(CodeMirror);
281
+ }
282
+
283
+ exports.default = CodeMirror;
284
+ exports.install = installCodeMirror;
285
+
286
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
287
+
288
+ }));
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": "1.0.1",
4
+ "version": "1.0.2",
5
5
  "license": "MIT",
6
6
  "description": "CodeMirror6 Component for vue2 and vue3.",
7
7
  "keywords": [
@@ -84,15 +84,15 @@
84
84
  "devDependencies": {
85
85
  "@codemirror/lang-javascript": "^6.0.2",
86
86
  "@codemirror/lang-markdown": "^6.0.1",
87
- "@types/lodash": "^4.14.184",
88
- "@types/node": "^18.7.16",
89
- "@typescript-eslint/eslint-plugin": "^5.36.2",
90
- "@typescript-eslint/parser": "^5.36.2",
87
+ "@types/lodash": "^4.14.185",
88
+ "@types/node": "^18.7.18",
89
+ "@typescript-eslint/eslint-plugin": "^5.37.0",
90
+ "@typescript-eslint/parser": "^5.37.0",
91
91
  "@vitejs/plugin-vue": "^3.1.0",
92
92
  "@vue/eslint-config-prettier": "^7.0.0",
93
93
  "@vue/tsconfig": "^0.1.3",
94
94
  "codemirror": "^6.0.1",
95
- "eslint": "^8.23.0",
95
+ "eslint": "^8.23.1",
96
96
  "eslint-config-google": "^0.14.0",
97
97
  "eslint-config-prettier": "^8.5.0",
98
98
  "eslint-import-resolver-alias": "^1.1.2",
@@ -100,7 +100,7 @@
100
100
  "eslint-plugin-html": "^7.1.0",
101
101
  "eslint-plugin-import": "^2.26.0",
102
102
  "eslint-plugin-jsdoc": "^39.3.6",
103
- "eslint-plugin-tsdoc": "^0.2.16",
103
+ "eslint-plugin-tsdoc": "^0.2.17",
104
104
  "eslint-plugin-vue": "^9.4.0",
105
105
  "eslint-plugin-vuejs-accessibility": "^1.2.0",
106
106
  "eslint-plugin-yaml": "^0.5.0",
@@ -111,12 +111,12 @@
111
111
  "prettier": "^2.7.1",
112
112
  "rimraf": "^3.0.2",
113
113
  "rollup-plugin-visualizer": "^5.8.1",
114
- "typescript": "^4.8.2",
115
- "vite": "^3.1.0",
114
+ "typescript": "^4.8.3",
115
+ "vite": "^3.1.1",
116
116
  "vite-plugin-banner": "^0.5.0",
117
117
  "vite-plugin-checker": "^0.5.1",
118
118
  "vue": "^3.2.39",
119
- "vue-eslint-parser": "^9.0.3",
119
+ "vue-eslint-parser": "^9.1.0",
120
120
  "vue-tsc": "^0.40.13"
121
121
  },
122
122
  "husky": {
@@ -188,6 +188,11 @@ export default defineComponent({
188
188
  type: Boolean,
189
189
  defalt: false,
190
190
  },
191
+ /** Using tag */
192
+ tag: {
193
+ type: String,
194
+ default: 'div',
195
+ },
191
196
  },
192
197
  /** Emits */
193
198
  emits: ['update:modelValue', 'update', 'ready', 'focus', 'changed'],
@@ -199,7 +204,7 @@ export default defineComponent({
199
204
  */
200
205
  setup(props, context: SetupContext<CodeMirrorEmitsOptions>) {
201
206
  /** Editor DOM */
202
- const editor: Ref<Element | undefined> = ref();
207
+ const editor: Ref<HTMLElement | undefined> = ref();
203
208
 
204
209
  /** Internal value */
205
210
  const doc: Ref<string | Text> = ref(props.modelValue);
@@ -273,16 +278,18 @@ export default defineComponent({
273
278
  // for parent-to-child binding.
274
279
  watch(
275
280
  () => props.modelValue,
276
- text => {
281
+ value => {
277
282
  if (!view.value || view.value.composing) {
278
283
  // IME fix
279
284
  return;
280
285
  }
286
+ /** Previous cursor location */
287
+ const previous = view.value.state.selection;
281
288
 
282
289
  // Update
283
290
  view.value.dispatch({
284
- changes: { from: 0, to: view.value.state.doc.length, insert: text },
285
- selection: selection.value,
291
+ changes: { from: 0, to: view.value.state.doc.length, insert: value },
292
+ selection: previous,
286
293
  });
287
294
  },
288
295
  { immediate: true }
@@ -479,7 +486,7 @@ export default defineComponent({
479
486
  // </div>
480
487
  // </template>
481
488
  return h(
482
- 'div',
489
+ this.$props.tag,
483
490
  {
484
491
  ref: 'editor',
485
492
  class: 'vue-codemirror',