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.
- package/README.md +59 -46
- package/dist/components/CodeMirror.d.ts +12 -1
- package/dist/components/CodeMirror.d.ts.map +1 -1
- package/dist/index.es.js +200 -135
- package/dist/index.iife.js +277 -2
- package/dist/index.umd.js +278 -2
- package/package.json +10 -10
- package/src/components/CodeMirror.ts +12 -5
package/dist/index.iife.js
CHANGED
|
@@ -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.
|
|
8
|
+
* @version 1.0.2
|
|
9
9
|
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
var CodeMirror
|
|
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.
|
|
8
|
+
* @version 1.0.2
|
|
9
9
|
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
(function
|
|
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.
|
|
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.
|
|
88
|
-
"@types/node": "^18.7.
|
|
89
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
90
|
-
"@typescript-eslint/parser": "^5.
|
|
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.
|
|
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.
|
|
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.
|
|
115
|
-
"vite": "^3.1.
|
|
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
|
|
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<
|
|
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
|
-
|
|
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:
|
|
285
|
-
selection:
|
|
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
|
-
|
|
489
|
+
this.$props.tag,
|
|
483
490
|
{
|
|
484
491
|
ref: 'editor',
|
|
485
492
|
class: 'vue-codemirror',
|