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/README.md +68 -55
- package/dist/Meta.d.ts +4 -0
- package/dist/Meta.d.ts.map +1 -0
- package/dist/components/CodeMirror.d.ts +12 -1
- package/dist/components/CodeMirror.d.ts.map +1 -1
- package/dist/helpers/h-demi.d.ts +5 -0
- package/dist/helpers/h-demi.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +200 -134
- package/dist/index.iife.js +279 -2
- package/dist/index.umd.js +280 -2
- package/dist/interfaces/MetaInterface.d.ts +8 -0
- package/dist/interfaces/MetaInterface.d.ts.map +1 -0
- package/dist/stats.html +4034 -0
- package/package.json +21 -21
- package/src/components/CodeMirror.ts +12 -5
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.
|
|
8
|
+
* @version 1.0.3
|
|
9
9
|
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { isVue2
|
|
13
|
-
import { compact
|
|
14
|
-
import { EditorState
|
|
15
|
-
import { EditorView
|
|
16
|
-
import { linter
|
|
17
|
-
import { basicSetup
|
|
18
|
-
import { indentWithTab
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
}
|
|
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:
|
|
62
|
+
default: false
|
|
46
63
|
},
|
|
47
64
|
basic: {
|
|
48
65
|
type: Boolean,
|
|
49
|
-
default:
|
|
66
|
+
default: false
|
|
50
67
|
},
|
|
51
68
|
minimal: {
|
|
52
69
|
type: Boolean,
|
|
53
|
-
default:
|
|
70
|
+
default: false
|
|
54
71
|
},
|
|
55
72
|
wrap: {
|
|
56
73
|
type: Boolean,
|
|
57
|
-
default:
|
|
74
|
+
default: false
|
|
58
75
|
},
|
|
59
76
|
tab: {
|
|
60
77
|
type: Boolean,
|
|
61
|
-
default:
|
|
78
|
+
default: false
|
|
62
79
|
},
|
|
63
80
|
readonly: {
|
|
64
81
|
type: Boolean,
|
|
65
|
-
default:
|
|
82
|
+
default: false
|
|
66
83
|
},
|
|
67
84
|
editable: {
|
|
68
85
|
type: Boolean,
|
|
69
|
-
default:
|
|
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:
|
|
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(
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
get: () =>
|
|
101
|
-
set: (
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
})
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
...
|
|
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
|
-
|
|
130
|
-
() =>
|
|
131
|
-
(
|
|
132
|
-
!
|
|
133
|
-
|
|
134
|
-
|
|
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:
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
|
|
171
|
+
{ immediate: true }
|
|
172
|
+
);
|
|
173
|
+
watch(extensions, (e) => {
|
|
174
|
+
view.value.dispatch({
|
|
175
|
+
effects: StateEffect.reconfigure.of(e)
|
|
141
176
|
});
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
doc
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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 =
|
|
156
|
-
|
|
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
|
-
|
|
164
|
-
context
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
),
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
)
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
|
203
|
-
|
|
261
|
+
return h(
|
|
262
|
+
this.$props.tag,
|
|
204
263
|
{
|
|
205
264
|
ref: "editor",
|
|
206
265
|
class: "vue-codemirror"
|
|
207
266
|
},
|
|
208
|
-
this.$slots.default ?
|
|
267
|
+
this.$slots.default ? h(
|
|
209
268
|
"aside",
|
|
210
269
|
{ style: "display: none;", "aria-hidden": "true" },
|
|
211
|
-
|
|
270
|
+
slot(this.$slots.default)
|
|
212
271
|
) : void 0
|
|
213
272
|
);
|
|
214
273
|
}
|
|
215
|
-
})
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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 };
|