vue-codemirror6 1.0.1 → 1.0.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/dist/index.es.js CHANGED
@@ -5,27 +5,42 @@
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.3
9
9
  * @see {@link https://github.com/logue/vue-codemirror6}
10
10
  */
11
11
 
12
- import { isVue2 as C, Vue2 as V, h as p, defineComponent as B, ref as g, shallowRef as x, computed as u, toRaw as O, watch as m, onMounted as M, nextTick as R, onUnmounted as j } from "vue-demi";
13
- import { compact as E, trim as T } from "lodash";
14
- import { EditorState as y, StateEffect as $, EditorSelection as S } from "@codemirror/state";
15
- import { EditorView as c, keymap as D } from "@codemirror/view";
16
- import { linter as G, lintGutter as L } from "@codemirror/lint";
17
- import { basicSetup as A, minimalSetup as F } from "codemirror";
18
- import { indentWithTab as N } from "@codemirror/commands";
19
- const U = (a) => a ? Object.entries(a).reduce((l, [o, s]) => (o = o.charAt(0).toUpperCase() + o.slice(1), o = `on${o}`, { ...l, [o]: s }), {}) : {}, w = (a, l = {}, o) => {
20
- if (C && parseInt(V.version) < 2.7)
21
- return p(a, l, o);
22
- const { props: s, domProps: t, on: i, ...r } = l, d = i ? U(i) : {};
23
- return p(
24
- a,
25
- { ...r, ...s, ...t, ...d },
26
- o
12
+ import { isVue2, Vue2, h as h$1, defineComponent, ref, shallowRef, computed, toRaw, watch, onMounted, nextTick, onUnmounted } from 'vue-demi';
13
+ import { compact, trim } from 'lodash';
14
+ import { EditorState, StateEffect, EditorSelection } from '@codemirror/state';
15
+ import { EditorView, keymap } from '@codemirror/view';
16
+ import { linter, lintGutter } from '@codemirror/lint';
17
+ import { basicSetup, minimalSetup } from 'codemirror';
18
+ import { indentWithTab } from '@codemirror/commands';
19
+
20
+ const adaptOnsV3 = (ons) => {
21
+ if (!ons)
22
+ return {};
23
+ return Object.entries(ons).reduce((ret, [key, handler]) => {
24
+ key = key.charAt(0).toUpperCase() + key.slice(1);
25
+ key = `on${key}`;
26
+ return { ...ret, [key]: handler };
27
+ }, {});
28
+ };
29
+ const h = (type, options = {}, chidren) => {
30
+ if (isVue2 && parseInt(Vue2.version) < 2.7) {
31
+ return h$1(type, options, chidren);
32
+ }
33
+ const { props, domProps, on, ...extraOptions } = options;
34
+ const ons = on ? adaptOnsV3(on) : {};
35
+ return h$1(
36
+ type,
37
+ { ...extraOptions, ...props, ...domProps, ...ons },
38
+ chidren
27
39
  );
28
- }, W = (a) => typeof a == "function" ? a() : a, b = B({
40
+ };
41
+ const slot = (defaultSlots) => typeof defaultSlots == "function" ? defaultSlots() : defaultSlots;
42
+
43
+ const CodeMirror = defineComponent({
29
44
  name: "CodeMirror",
30
45
  model: {
31
46
  prop: "modelValue",
@@ -38,49 +53,51 @@ const U = (a) => a ? Object.entries(a).reduce((l, [o, s]) => (o = o.charAt(0).to
38
53
  },
39
54
  theme: {
40
55
  type: Object,
41
- default: () => ({})
56
+ default: () => {
57
+ return {};
58
+ }
42
59
  },
43
60
  dark: {
44
61
  type: Boolean,
45
- default: !1
62
+ default: false
46
63
  },
47
64
  basic: {
48
65
  type: Boolean,
49
- default: !1
66
+ default: false
50
67
  },
51
68
  minimal: {
52
69
  type: Boolean,
53
- default: !1
70
+ default: false
54
71
  },
55
72
  wrap: {
56
73
  type: Boolean,
57
- default: !1
74
+ default: false
58
75
  },
59
76
  tab: {
60
77
  type: Boolean,
61
- default: !1
78
+ default: false
62
79
  },
63
80
  readonly: {
64
81
  type: Boolean,
65
- default: !1
82
+ default: false
66
83
  },
67
84
  editable: {
68
85
  type: Boolean,
69
- default: !0
86
+ default: true
70
87
  },
71
88
  extensions: {
72
89
  type: Array,
73
- default: () => []
90
+ default: () => {
91
+ return [];
92
+ }
74
93
  },
75
94
  phrases: {
76
95
  type: Object,
77
- default: () => {
78
- }
96
+ default: () => void 0
79
97
  },
80
98
  lang: {
81
99
  type: Object,
82
- default: () => {
83
- }
100
+ default: () => void 0
84
101
  },
85
102
  linter: {
86
103
  type: Function,
@@ -88,133 +105,182 @@ const U = (a) => a ? Object.entries(a).reduce((l, [o, s]) => (o = o.charAt(0).to
88
105
  },
89
106
  lintGutter: {
90
107
  type: Boolean,
91
- defalt: !1
108
+ defalt: false
109
+ },
110
+ tag: {
111
+ type: String,
112
+ default: "div"
92
113
  }
93
114
  },
94
115
  emits: ["update:modelValue", "update", "ready", "focus", "changed"],
95
- setup(a, l) {
96
- const o = g(), s = g(a.modelValue), t = x(new c()), i = u({
97
- get: () => t.value.state.selection,
98
- set: (e) => t.value.dispatch({ selection: e })
99
- }), r = u({
100
- get: () => i.value.main.head || 0,
101
- set: (e) => t.value.dispatch({ selection: { anchor: e } })
102
- }), d = u({
103
- get: () => t.value.state,
104
- set: (e) => t.value.setState(e)
105
- }), f = u({
106
- get: () => t.value.hasFocus,
107
- set: (e) => {
108
- e && t.value.focus();
116
+ setup(props, context) {
117
+ const editor = ref();
118
+ const doc = ref(props.modelValue);
119
+ const view = shallowRef(new EditorView());
120
+ const selection = computed({
121
+ get: () => view.value.state.selection,
122
+ set: (s) => view.value.dispatch({ selection: s })
123
+ });
124
+ const cursor = computed({
125
+ get: () => selection.value.main.head || 0,
126
+ set: (a) => view.value.dispatch({ selection: { anchor: a } })
127
+ });
128
+ const state = computed({
129
+ get: () => view.value.state,
130
+ set: (s) => view.value.setState(s)
131
+ });
132
+ const focus = computed({
133
+ get: () => view.value.hasFocus,
134
+ set: (f) => {
135
+ if (f) {
136
+ view.value.focus();
137
+ }
109
138
  }
110
- }), h = u(
111
- () => E([
112
- a.basic ? A : void 0,
113
- a.minimal && !a.basic ? F : void 0,
114
- c.updateListener.of(
115
- (e) => l.emit("update", e)
139
+ });
140
+ const extensions = computed(
141
+ () => compact([
142
+ props.basic ? basicSetup : void 0,
143
+ props.minimal && !props.basic ? minimalSetup : void 0,
144
+ EditorView.updateListener.of(
145
+ (update) => context.emit("update", update)
116
146
  ),
117
- c.theme(a.theme, { dark: a.dark }),
118
- a.wrap ? c.lineWrapping : void 0,
119
- a.tab ? D.of([N]) : void 0,
120
- a.phrases ? y.phrases.of(a.phrases) : void 0,
121
- y.readOnly.of(a.readonly),
122
- c.editable.of(a.editable),
123
- a.lang ? O(a.lang) : void 0,
124
- a.linter ? G(a.linter) : void 0,
125
- a.linter && a.lintGutter ? L() : void 0,
126
- ...a.extensions
147
+ EditorView.theme(props.theme, { dark: props.dark }),
148
+ props.wrap ? EditorView.lineWrapping : void 0,
149
+ props.tab ? keymap.of([indentWithTab]) : void 0,
150
+ props.phrases ? EditorState.phrases.of(props.phrases) : void 0,
151
+ EditorState.readOnly.of(props.readonly),
152
+ EditorView.editable.of(props.editable),
153
+ props.lang ? toRaw(props.lang) : void 0,
154
+ props.linter ? linter(props.linter) : void 0,
155
+ props.linter && props.lintGutter ? lintGutter() : void 0,
156
+ ...props.extensions
127
157
  ])
128
158
  );
129
- return m(
130
- () => a.modelValue,
131
- (e) => {
132
- !t.value || t.value.composing || t.value.dispatch({
133
- changes: { from: 0, to: t.value.state.doc.length, insert: e },
134
- selection: i.value
159
+ watch(
160
+ () => props.modelValue,
161
+ (value) => {
162
+ if (!view.value || view.value.composing) {
163
+ return;
164
+ }
165
+ const previous = view.value.state.selection;
166
+ view.value.dispatch({
167
+ changes: { from: 0, to: view.value.state.doc.length, insert: value },
168
+ selection: previous
135
169
  });
136
170
  },
137
- { immediate: !0 }
138
- ), m(h, (e) => {
139
- t.value.dispatch({
140
- effects: $.reconfigure.of(e)
171
+ { immediate: true }
172
+ );
173
+ watch(extensions, (e) => {
174
+ view.value.dispatch({
175
+ effects: StateEffect.reconfigure.of(e)
141
176
  });
142
- }), m(f, (e) => {
143
- l.emit("focus", e);
144
- }), M(async () => {
145
- let e = s.value;
146
- o.value && o.value.childNodes[0] && (s.value !== "" && console.warn(
147
- "[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."
148
- ), e = T(o.value.childNodes[0].innerText)), t.value = new c({
149
- doc: e,
150
- extensions: h.value,
151
- parent: o.value,
152
- dispatch: (n) => {
153
- if (t.value.update([n]), n.changes.empty)
177
+ });
178
+ watch(focus, (isFocus) => {
179
+ context.emit("focus", isFocus);
180
+ });
181
+ onMounted(async () => {
182
+ let value = doc.value;
183
+ if (editor.value && editor.value.childNodes[0]) {
184
+ if (doc.value !== "") ;
185
+ value = trim(editor.value.childNodes[0].innerText);
186
+ }
187
+ view.value = new EditorView({
188
+ doc: value,
189
+ extensions: extensions.value,
190
+ parent: editor.value,
191
+ dispatch: (tr) => {
192
+ view.value.update([tr]);
193
+ if (tr.changes.empty)
154
194
  return;
155
- const v = t.value.state.doc.toString();
156
- l.emit("update:modelValue", v), l.emit("changed", v);
195
+ const v = view.value.state.doc.toString();
196
+ context.emit("update:modelValue", v);
197
+ context.emit("changed", v);
157
198
  }
158
- }), await R(), l.emit("ready", {
159
- view: t,
160
- state: d.value,
161
- container: o.value
162
199
  });
163
- }), j(() => t.value.destroy()), {
164
- context: l,
165
- editor: o,
166
- cursor: r,
167
- selection: i,
168
- state: d,
169
- focus: f,
170
- getRange: (e, n) => t.value.state.sliceDoc(e, n),
171
- getLine: (e) => t.value.state.doc.line(e + 1).text,
172
- lineCount: () => t.value.state.doc.lines,
173
- getCursor: () => r.value,
174
- listSelections: () => t.value.state.selection.ranges,
175
- getSelection: () => t.value.state.sliceDoc(
176
- t.value.state.selection.main.from,
177
- t.value.state.selection.main.to
178
- ),
179
- getSelections: () => t.value.state.selection.ranges.map(
180
- (e) => t.value.state.sliceDoc(e.from, e.to)
181
- ),
182
- somethingSelected: () => t.value.state.selection.ranges.some(
183
- (e) => !e.empty
184
- ),
185
- replaceRange: (e, n, v) => t.value.dispatch({
186
- changes: { from: n, to: v, insert: e }
187
- }),
188
- replaceSelection: (e) => t.value.dispatch(t.value.state.replaceSelection(e)),
189
- setCursor: (e) => r.value = e,
190
- setSelection: (e, n) => t.value.dispatch({ selection: { anchor: e, head: n } }),
191
- setSelections: (e, n) => t.value.dispatch({
192
- selection: S.create(e, n)
193
- }),
194
- extendSelectionsBy: (e) => t.value.dispatch({
195
- selection: S.create(
196
- i.value.ranges.map((n) => n.extend(e(n)))
197
- )
198
- })
200
+ await nextTick();
201
+ context.emit("ready", {
202
+ view,
203
+ state: state.value,
204
+ container: editor.value
205
+ });
206
+ });
207
+ onUnmounted(() => view.value.destroy());
208
+ const getRange = (from, to) => view.value.state.sliceDoc(from, to);
209
+ const getLine = (number) => view.value.state.doc.line(number + 1).text;
210
+ const lineCount = () => view.value.state.doc.lines;
211
+ const getCursor = () => cursor.value;
212
+ const listSelections = () => view.value.state.selection.ranges;
213
+ const getSelection = () => view.value.state.sliceDoc(
214
+ view.value.state.selection.main.from,
215
+ view.value.state.selection.main.to
216
+ );
217
+ const getSelections = () => view.value.state.selection.ranges.map(
218
+ (r) => view.value.state.sliceDoc(r.from, r.to)
219
+ );
220
+ const somethingSelected = () => view.value.state.selection.ranges.some(
221
+ (r) => !r.empty
222
+ );
223
+ const replaceRange = (replacement, from, to) => view.value.dispatch({
224
+ changes: { from, to, insert: replacement }
225
+ });
226
+ const replaceSelection = (replacement) => view.value.dispatch(view.value.state.replaceSelection(replacement));
227
+ const setCursor = (position) => cursor.value = position;
228
+ const setSelection = (anchor, head) => view.value.dispatch({ selection: { anchor, head } });
229
+ const setSelections = (ranges, primary) => view.value.dispatch({
230
+ selection: EditorSelection.create(ranges, primary)
231
+ });
232
+ const extendSelectionsBy = (f) => view.value.dispatch({
233
+ selection: EditorSelection.create(
234
+ selection.value.ranges.map((r) => r.extend(f(r)))
235
+ )
236
+ });
237
+ return {
238
+ context,
239
+ editor,
240
+ cursor,
241
+ selection,
242
+ state,
243
+ focus,
244
+ getRange,
245
+ getLine,
246
+ lineCount,
247
+ getCursor,
248
+ listSelections,
249
+ getSelection,
250
+ getSelections,
251
+ somethingSelected,
252
+ replaceRange,
253
+ replaceSelection,
254
+ setCursor,
255
+ setSelection,
256
+ setSelections,
257
+ extendSelectionsBy
199
258
  };
200
259
  },
201
260
  render() {
202
- return w(
203
- "div",
261
+ return h(
262
+ this.$props.tag,
204
263
  {
205
264
  ref: "editor",
206
265
  class: "vue-codemirror"
207
266
  },
208
- this.$slots.default ? w(
267
+ this.$slots.default ? h(
209
268
  "aside",
210
269
  { style: "display: none;", "aria-hidden": "true" },
211
- W(this.$slots.default)
270
+ slot(this.$slots.default)
212
271
  ) : void 0
213
272
  );
214
273
  }
215
- }), ce = (a) => a.component("CodeMirror", b);
216
- typeof window < "u" && window.Vue && window.Vue.use(b);
217
- export {
218
- b as default,
219
- ce as install
274
+ });
275
+
276
+ const meta = {
277
+ version: "1.0.3",
278
+ date: "2022-10-11T23:57:24.765Z"
220
279
  };
280
+
281
+ const installCodeMirror = (app) => app.component("CodeMirror", CodeMirror);
282
+ if (typeof window !== "undefined" && window.Vue) {
283
+ window.Vue.use(CodeMirror);
284
+ }
285
+
286
+ export { meta as Meta, CodeMirror as default, installCodeMirror as install };