vue-codemirror6 0.6.7 → 1.0.1
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 +21 -8
- package/dist/components/CodeMirror.d.ts +19 -6
- package/dist/components/CodeMirror.d.ts.map +1 -1
- package/dist/index.es.js +126 -94
- package/dist/index.iife.js +12 -0
- package/dist/index.umd.js +3 -3
- package/package.json +33 -28
- package/src/components/CodeMirror.ts +496 -0
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ For Vue 2.7 or later, import vue directly like Vue3.
|
|
|
30
30
|
| ---------- | --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
31
31
|
| basic | boolean | Use [basicSetup](https://codemirror.net/docs/ref/#codemirror.basicSetup). |
|
|
32
32
|
| minimal | boolean | Use [miniSetup](https://codemirror.net/docs/ref/#codemirror.minimalSetup). If a `basic` prop is also specified, that setting will take precedence. |
|
|
33
|
-
| dark | boolean | Toggle Darkmode.
|
|
33
|
+
| dark | boolean | Toggle Darkmode. |
|
|
34
34
|
| wrap | boolean | Line text wrapping. see [lineWrapping](https://codemirror.net/6/docs/ref/#view.EditorView.lineWrapping). |
|
|
35
35
|
| tab | boolean | Enables tab indentation. |
|
|
36
36
|
| theme | { [selector: string]: StyleSpec } | Specify the theme. For example, if you use [@codemirror/theme-one-dark](https://github.com/codemirror/theme-one-dark), import `oneDark` and put it in this prop. |
|
|
@@ -170,6 +170,11 @@ export default defineComponent({
|
|
|
170
170
|
/** text */
|
|
171
171
|
const value: Ref<string> = ref('');
|
|
172
172
|
|
|
173
|
+
/** Dark mode **/
|
|
174
|
+
const dark: Ref<boolean> = ref(
|
|
175
|
+
window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
176
|
+
);
|
|
177
|
+
|
|
173
178
|
/**
|
|
174
179
|
* CodeMirror Language
|
|
175
180
|
*
|
|
@@ -177,12 +182,6 @@ export default defineComponent({
|
|
|
177
182
|
*/
|
|
178
183
|
const lang: LanguageSupport = md();
|
|
179
184
|
|
|
180
|
-
/** Dark mode **/
|
|
181
|
-
const dark = ref(window.matchMedia('(prefers-color-scheme: dark)').matches);
|
|
182
|
-
|
|
183
|
-
/** When dark value changed, sync vuetify's dark mode */
|
|
184
|
-
watch(dark, v => (vuetify.theme.dark = v));
|
|
185
|
-
|
|
186
185
|
/**
|
|
187
186
|
* Internationalization Config.
|
|
188
187
|
* In this example, the display language to Japanese.
|
|
@@ -221,6 +220,9 @@ export default defineComponent({
|
|
|
221
220
|
'No diagnostics': 'エラーなし',
|
|
222
221
|
});
|
|
223
222
|
|
|
223
|
+
/** When dark value changed, sync vuetify's dark mode */
|
|
224
|
+
watch(dark, v => (vuetify.theme.dark = v));
|
|
225
|
+
|
|
224
226
|
return {
|
|
225
227
|
dark,
|
|
226
228
|
value,
|
|
@@ -232,9 +234,18 @@ export default defineComponent({
|
|
|
232
234
|
</script>
|
|
233
235
|
```
|
|
234
236
|
|
|
237
|
+
## Events
|
|
238
|
+
|
|
239
|
+
| Event | Description |
|
|
240
|
+
| ------- | ---------------------------------------------------------------------------------- |
|
|
241
|
+
| ready | When CodeMirror loaded. |
|
|
242
|
+
| focus | When focus changed. |
|
|
243
|
+
| update | When CodeMirror state changed |
|
|
244
|
+
| changed | Value changed. (Please implement with `v-model` unless there is a special reason.) |
|
|
245
|
+
|
|
235
246
|
## Methods
|
|
236
247
|
|
|
237
|
-
|
|
|
248
|
+
| Method | Description |
|
|
238
249
|
| ------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
|
|
239
250
|
| selection | Get and set the [EditorSelection](https://codemirror.net/docs/ref/#state.EditorSelection) instance. |
|
|
240
251
|
| cursor | Get and set the cursor location. |
|
|
@@ -295,3 +306,5 @@ const config: UserConfig = {
|
|
|
295
306
|
## LICENSE
|
|
296
307
|
|
|
297
308
|
[MIT](LICENSE)
|
|
309
|
+
|
|
310
|
+
© 2022 by Logue.
|
|
@@ -1,16 +1,27 @@
|
|
|
1
|
-
import { type PropType, type Ref, type SetupContext } from 'vue-demi';
|
|
1
|
+
import { type ObjectEmitsOptions, type PropType, type Ref, type SetupContext } from 'vue-demi';
|
|
2
2
|
import { EditorSelection, EditorState } from '@codemirror/state';
|
|
3
|
+
import { EditorView } from '@codemirror/view';
|
|
3
4
|
import type { Extension, SelectionRange, Text } from '@codemirror/state';
|
|
4
5
|
import type { LanguageSupport } from '@codemirror/language';
|
|
5
6
|
import type { ViewUpdate } from '@codemirror/view';
|
|
6
7
|
import type { LintSource } from '@codemirror/lint';
|
|
7
8
|
import type { StyleSpec } from 'style-mod';
|
|
8
9
|
/** Emit Interface */
|
|
9
|
-
export interface
|
|
10
|
+
export interface CodeMirrorEmitsOptions extends ObjectEmitsOptions {
|
|
10
11
|
/** Model Update */
|
|
11
12
|
(e: 'update:modelValue', value: string | Text): void;
|
|
12
13
|
/** CodeMirror ViewUpdate */
|
|
13
|
-
(e: 'update
|
|
14
|
+
(e: 'update', value: ViewUpdate): void;
|
|
15
|
+
/** CodeMirror onFocus */
|
|
16
|
+
(e: 'focus', value: boolean): void;
|
|
17
|
+
/** CodeMirror onReady */
|
|
18
|
+
(e: 'ready', value: {
|
|
19
|
+
view: EditorView;
|
|
20
|
+
state: EditorState;
|
|
21
|
+
container: HTMLDivElement;
|
|
22
|
+
}): void;
|
|
23
|
+
/** onChanged (same as update:modelValue) */
|
|
24
|
+
(e: 'changed', value: string | Text): void;
|
|
14
25
|
}
|
|
15
26
|
/** CodeMirror Component */
|
|
16
27
|
declare const _default: import("vue-demi").DefineComponent<{
|
|
@@ -131,7 +142,7 @@ declare const _default: import("vue-demi").DefineComponent<{
|
|
|
131
142
|
defalt: boolean;
|
|
132
143
|
};
|
|
133
144
|
}, {
|
|
134
|
-
context: SetupContext<
|
|
145
|
+
context: SetupContext<CodeMirrorEmitsOptions>;
|
|
135
146
|
editor: Ref<Element | undefined>;
|
|
136
147
|
cursor: Ref<number>;
|
|
137
148
|
selection: Ref<EditorSelection>;
|
|
@@ -151,7 +162,7 @@ declare const _default: import("vue-demi").DefineComponent<{
|
|
|
151
162
|
setSelection: (anchor: number, head?: number) => void;
|
|
152
163
|
setSelections: (ranges: readonly SelectionRange[], primary?: number) => void;
|
|
153
164
|
extendSelectionsBy: (f: Function) => void;
|
|
154
|
-
}, unknown, {}, {}, import("vue-demi").ComponentOptionsMixin, import("vue-demi").ComponentOptionsMixin,
|
|
165
|
+
}, unknown, {}, {}, import("vue-demi").ComponentOptionsMixin, import("vue-demi").ComponentOptionsMixin, CodeMirrorEmitsOptions, "update:modelValue" | "update" | "focus" | "ready" | "changed", import("vue-demi").VNodeProps & import("vue-demi").AllowedComponentProps & import("vue-demi").ComponentCustomProps, Readonly<import("vue-demi").ExtractPropTypes<{
|
|
155
166
|
/** Model value */
|
|
156
167
|
modelValue: {
|
|
157
168
|
type: PropType<string | Text>;
|
|
@@ -268,7 +279,9 @@ declare const _default: import("vue-demi").DefineComponent<{
|
|
|
268
279
|
type: BooleanConstructor;
|
|
269
280
|
defalt: boolean;
|
|
270
281
|
};
|
|
271
|
-
}
|
|
282
|
+
}>> & {
|
|
283
|
+
[x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
|
|
284
|
+
}, {
|
|
272
285
|
modelValue: string | Text;
|
|
273
286
|
theme: {
|
|
274
287
|
[selector: string]: StyleSpec;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodeMirror.d.ts","sourceRoot":"","sources":["../../src/components/CodeMirror.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"CodeMirror.d.ts","sourceRoot":"","sources":["../../src/components/CodeMirror.ts"],"names":[],"mappings":"AAAA,OAAO,EAWL,KAAK,kBAAkB,EACvB,KAAK,QAAQ,EACb,KAAK,GAAG,EACR,KAAK,YAAY,EAElB,MAAM,UAAU,CAAC;AAOlB,OAAO,EAAE,eAAe,EAAE,WAAW,EAAe,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,UAAU,EAAU,MAAM,kBAAkB,CAAC;AAKtD,OAAO,KAAK,EACV,SAAS,EACT,cAAc,EACd,IAAI,EAEL,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,qBAAqB;AACrB,MAAM,WAAW,sBAAuB,SAAQ,kBAAkB;IAChE,mBAAmB;IACnB,CAAC,CAAC,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;IACrD,4BAA4B;IAC5B,CAAC,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;IACvC,yBAAyB;IACzB,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IACnC,yBAAyB;IACzB,CACE,CAAC,EAAE,OAAO,EACV,KAAK,EAAE;QACL,IAAI,EAAE,UAAU,CAAC;QACjB,KAAK,EAAE,WAAW,CAAC;QACnB,SAAS,EAAE,cAAc,CAAC;KAC3B,GACA,IAAI,CAAC;IACR,4CAA4C;IAC5C,CAAC,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;CAC5C;AAED,2BAA2B;;IAWvB,kBAAkB;;;;;IAKlB;;;;OAIG;;;;;;;IAOH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAOH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH,kBAAkB;;;;;;;;;;;;sBAuKO,MAAM,OAAO,MAAM,KAAG,MAAM;sBAO5B,MAAM,KAAG,MAAM;qBAGlB,MAAM;qBAEN,MAAM;0BAED,SAAS,cAAc,EAAE;wBAG3B,MAAM;yBASL,MAAM,EAAE;6BAKJ,OAAO;gCAatB,MAAM,GAAG,IAAI,QACpB,MAAM,MACR,MAAM;oCAW2B,MAAM,GAAG,IAAI;0BAOvB,MAAM;2BAOL,MAAM,SAAS,MAAM;4BASzC,SAAS,cAAc,EAAE,YACvB,MAAM;4BAUa,QAAQ;;IAnXvC,kBAAkB;;;;;IAKlB;;;;OAIG;;;;;;;IAOH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAOH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;AA3HtB,wBAibG"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* vue-codemirror6
|
|
3
3
|
*
|
|
4
|
-
* @description CodeMirror6 Component for vue2 and vue3
|
|
4
|
+
* @description CodeMirror6 Component for vue2 and vue3.
|
|
5
5
|
* @author Logue <logue@hotmail.co.jp>
|
|
6
6
|
* @copyright 2022 By Masashi Yoshikawa All rights reserved.
|
|
7
7
|
* @license MIT
|
|
8
|
-
* @version 0.
|
|
8
|
+
* @version 1.0.1
|
|
9
9
|
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { isVue2 as
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import { EditorView as
|
|
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
16
|
import { linter as G, lintGutter as L } from "@codemirror/lint";
|
|
17
17
|
import { basicSetup as A, minimalSetup as F } from "codemirror";
|
|
18
18
|
import { indentWithTab as N } from "@codemirror/commands";
|
|
19
|
-
const U = (
|
|
20
|
-
if (
|
|
21
|
-
return
|
|
22
|
-
const { props: s, domProps:
|
|
23
|
-
return
|
|
24
|
-
|
|
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
|
|
27
|
+
);
|
|
28
|
+
}, W = (a) => typeof a == "function" ? a() : a, b = B({
|
|
25
29
|
name: "CodeMirror",
|
|
26
30
|
model: {
|
|
27
31
|
prop: "modelValue",
|
|
@@ -87,102 +91,130 @@ const U = (t) => t ? Object.entries(t).reduce((i, [n, s]) => (n = n.charAt(0).to
|
|
|
87
91
|
defalt: !1
|
|
88
92
|
}
|
|
89
93
|
},
|
|
90
|
-
emits: ["update:modelValue", "update
|
|
91
|
-
setup(
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}), w = d({
|
|
104
|
-
get: () => o.hasFocus,
|
|
94
|
+
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,
|
|
105
107
|
set: (e) => {
|
|
106
|
-
e &&
|
|
108
|
+
e && t.value.focus();
|
|
107
109
|
}
|
|
108
|
-
}),
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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)
|
|
116
|
+
),
|
|
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
|
|
127
|
+
])
|
|
128
|
+
);
|
|
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
|
|
117
135
|
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
136
|
+
},
|
|
137
|
+
{ immediate: !0 }
|
|
138
|
+
), m(h, (e) => {
|
|
139
|
+
t.value.dispatch({
|
|
140
|
+
effects: $.reconfigure.of(e)
|
|
121
141
|
});
|
|
122
|
-
}),
|
|
142
|
+
}), m(f, (e) => {
|
|
143
|
+
l.emit("focus", e);
|
|
144
|
+
}), M(async () => {
|
|
123
145
|
let e = s.value;
|
|
124
|
-
|
|
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({
|
|
125
149
|
doc: e,
|
|
126
|
-
extensions:
|
|
127
|
-
parent:
|
|
128
|
-
dispatch: (
|
|
129
|
-
|
|
150
|
+
extensions: h.value,
|
|
151
|
+
parent: o.value,
|
|
152
|
+
dispatch: (n) => {
|
|
153
|
+
if (t.value.update([n]), n.changes.empty)
|
|
154
|
+
return;
|
|
155
|
+
const v = t.value.state.doc.toString();
|
|
156
|
+
l.emit("update:modelValue", v), l.emit("changed", v);
|
|
130
157
|
}
|
|
131
|
-
}), await
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
getSelection: () => o.state.sliceDoc(o.state.selection.main.from, o.state.selection.main.to),
|
|
161
|
-
getSelections: () => o.state.selection.ranges.map((e) => o.state.sliceDoc(e.from, e.to)),
|
|
162
|
-
somethingSelected: () => o.state.selection.ranges.some((e) => !e.empty),
|
|
163
|
-
replaceRange: (e, a, b) => o.dispatch({
|
|
164
|
-
changes: { from: a, to: b, insert: e }
|
|
158
|
+
}), await R(), l.emit("ready", {
|
|
159
|
+
view: t,
|
|
160
|
+
state: d.value,
|
|
161
|
+
container: o.value
|
|
162
|
+
});
|
|
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 }
|
|
165
187
|
}),
|
|
166
|
-
replaceSelection: (e) =>
|
|
167
|
-
setCursor: (e) =>
|
|
168
|
-
setSelection: (e,
|
|
169
|
-
setSelections: (e,
|
|
170
|
-
selection:
|
|
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)
|
|
171
193
|
}),
|
|
172
|
-
extendSelectionsBy: (e) =>
|
|
173
|
-
selection:
|
|
194
|
+
extendSelectionsBy: (e) => t.value.dispatch({
|
|
195
|
+
selection: S.create(
|
|
196
|
+
i.value.ranges.map((n) => n.extend(e(n)))
|
|
197
|
+
)
|
|
174
198
|
})
|
|
175
199
|
};
|
|
176
200
|
},
|
|
177
201
|
render() {
|
|
178
|
-
return
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
202
|
+
return w(
|
|
203
|
+
"div",
|
|
204
|
+
{
|
|
205
|
+
ref: "editor",
|
|
206
|
+
class: "vue-codemirror"
|
|
207
|
+
},
|
|
208
|
+
this.$slots.default ? w(
|
|
209
|
+
"aside",
|
|
210
|
+
{ style: "display: none;", "aria-hidden": "true" },
|
|
211
|
+
W(this.$slots.default)
|
|
212
|
+
) : void 0
|
|
213
|
+
);
|
|
182
214
|
}
|
|
183
|
-
}), ce = (
|
|
184
|
-
typeof window < "u" && window.Vue && window.Vue.use(
|
|
215
|
+
}), ce = (a) => a.component("CodeMirror", b);
|
|
216
|
+
typeof window < "u" && window.Vue && window.Vue.use(b);
|
|
185
217
|
export {
|
|
186
|
-
|
|
218
|
+
b as default,
|
|
187
219
|
ce as install
|
|
188
220
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vue-codemirror6
|
|
3
|
+
*
|
|
4
|
+
* @description CodeMirror6 Component for vue2 and vue3.
|
|
5
|
+
* @author Logue <logue@hotmail.co.jp>
|
|
6
|
+
* @copyright 2022 By Masashi Yoshikawa All rights reserved.
|
|
7
|
+
* @license MIT
|
|
8
|
+
* @version 1.0.1
|
|
9
|
+
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
10
|
+
*/
|
|
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);
|
package/dist/index.umd.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* vue-codemirror6
|
|
3
3
|
*
|
|
4
|
-
* @description CodeMirror6 Component for vue2 and vue3
|
|
4
|
+
* @description CodeMirror6 Component for vue2 and vue3.
|
|
5
5
|
* @author Logue <logue@hotmail.co.jp>
|
|
6
6
|
* @copyright 2022 By Masashi Yoshikawa All rights reserved.
|
|
7
7
|
* @license MIT
|
|
8
|
-
* @version 0.
|
|
8
|
+
* @version 1.0.1
|
|
9
9
|
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
(function(a
|
|
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"}})});
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "vue-codemirror6",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"description": "CodeMirror6 Component for vue2 and vue3",
|
|
6
|
+
"description": "CodeMirror6 Component for vue2 and vue3.",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"vuejs",
|
|
9
9
|
"vue",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"vue-resource",
|
|
21
21
|
"codemirror6"
|
|
22
22
|
],
|
|
23
|
+
"type": "module",
|
|
23
24
|
"author": {
|
|
24
25
|
"name": "Logue",
|
|
25
26
|
"email": "logue@hotmail.co.jp",
|
|
@@ -33,9 +34,9 @@
|
|
|
33
34
|
"bugs": {
|
|
34
35
|
"url": "https://github.com/logue/vue-codemirror6/issues"
|
|
35
36
|
},
|
|
36
|
-
"main": "
|
|
37
|
-
"module": "
|
|
38
|
-
"types": "
|
|
37
|
+
"main": "dist/index.umd.js",
|
|
38
|
+
"module": "dist/index.es.js",
|
|
39
|
+
"types": "dist/index.d.ts",
|
|
39
40
|
"exports": {
|
|
40
41
|
".": {
|
|
41
42
|
"import": "./dist/index.es.js",
|
|
@@ -43,13 +44,17 @@
|
|
|
43
44
|
}
|
|
44
45
|
},
|
|
45
46
|
"files": [
|
|
46
|
-
"/dist"
|
|
47
|
+
"/dist",
|
|
48
|
+
"./src/components/CodeMirror.ts"
|
|
47
49
|
],
|
|
50
|
+
"browser": {
|
|
51
|
+
"./sfc": "./src/components/CodeMirror.ts"
|
|
52
|
+
},
|
|
48
53
|
"engines": {
|
|
49
|
-
"node": ">=16.
|
|
50
|
-
"yarn": ">=1.22.
|
|
54
|
+
"node": ">=16.17.0",
|
|
55
|
+
"yarn": ">=1.22.10"
|
|
51
56
|
},
|
|
52
|
-
"packageManager": "yarn@3.2.
|
|
57
|
+
"packageManager": "yarn@3.2.3",
|
|
53
58
|
"sideEffects": false,
|
|
54
59
|
"scripts": {
|
|
55
60
|
"dev": "vite",
|
|
@@ -69,7 +74,7 @@
|
|
|
69
74
|
"peerDependencies": {
|
|
70
75
|
"@vue/composition-api": "^1.7.0",
|
|
71
76
|
"codemirror": "^6.0.1",
|
|
72
|
-
"vue": "^2.6.14 || ^3.2.
|
|
77
|
+
"vue": "^2.6.14 || ^3.2.39"
|
|
73
78
|
},
|
|
74
79
|
"peerDependenciesMeta": {
|
|
75
80
|
"@vue/composition-api": {
|
|
@@ -78,25 +83,25 @@
|
|
|
78
83
|
},
|
|
79
84
|
"devDependencies": {
|
|
80
85
|
"@codemirror/lang-javascript": "^6.0.2",
|
|
81
|
-
"@codemirror/lang-markdown": "^6.0.
|
|
82
|
-
"@types/lodash": "^4.14.
|
|
83
|
-
"@types/node": "^18.
|
|
84
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
85
|
-
"@typescript-eslint/parser": "^5.
|
|
86
|
-
"@vitejs/plugin-vue": "^3.0
|
|
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",
|
|
91
|
+
"@vitejs/plugin-vue": "^3.1.0",
|
|
87
92
|
"@vue/eslint-config-prettier": "^7.0.0",
|
|
88
93
|
"@vue/tsconfig": "^0.1.3",
|
|
89
94
|
"codemirror": "^6.0.1",
|
|
90
|
-
"eslint": "^8.
|
|
95
|
+
"eslint": "^8.23.0",
|
|
91
96
|
"eslint-config-google": "^0.14.0",
|
|
92
97
|
"eslint-config-prettier": "^8.5.0",
|
|
93
98
|
"eslint-import-resolver-alias": "^1.1.2",
|
|
94
|
-
"eslint-import-resolver-typescript": "^3.
|
|
95
|
-
"eslint-plugin-html": "^7.
|
|
99
|
+
"eslint-import-resolver-typescript": "^3.5.1",
|
|
100
|
+
"eslint-plugin-html": "^7.1.0",
|
|
96
101
|
"eslint-plugin-import": "^2.26.0",
|
|
97
|
-
"eslint-plugin-jsdoc": "^39.3.
|
|
102
|
+
"eslint-plugin-jsdoc": "^39.3.6",
|
|
98
103
|
"eslint-plugin-tsdoc": "^0.2.16",
|
|
99
|
-
"eslint-plugin-vue": "^9.
|
|
104
|
+
"eslint-plugin-vue": "^9.4.0",
|
|
100
105
|
"eslint-plugin-vuejs-accessibility": "^1.2.0",
|
|
101
106
|
"eslint-plugin-yaml": "^0.5.0",
|
|
102
107
|
"eslint4b-prebuilt": "^6.7.2",
|
|
@@ -105,14 +110,14 @@
|
|
|
105
110
|
"lodash": "^4.17.21",
|
|
106
111
|
"prettier": "^2.7.1",
|
|
107
112
|
"rimraf": "^3.0.2",
|
|
108
|
-
"rollup-plugin-visualizer": "^5.
|
|
109
|
-
"typescript": "^4.
|
|
110
|
-
"vite": "^3.0
|
|
111
|
-
"vite-plugin-banner": "^0.
|
|
112
|
-
"vite-plugin-checker": "^0.
|
|
113
|
-
"vue": "^3.2.
|
|
113
|
+
"rollup-plugin-visualizer": "^5.8.1",
|
|
114
|
+
"typescript": "^4.8.2",
|
|
115
|
+
"vite": "^3.1.0",
|
|
116
|
+
"vite-plugin-banner": "^0.5.0",
|
|
117
|
+
"vite-plugin-checker": "^0.5.1",
|
|
118
|
+
"vue": "^3.2.39",
|
|
114
119
|
"vue-eslint-parser": "^9.0.3",
|
|
115
|
-
"vue-tsc": "^0.
|
|
120
|
+
"vue-tsc": "^0.40.13"
|
|
116
121
|
},
|
|
117
122
|
"husky": {
|
|
118
123
|
"hooks": {
|
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
import {
|
|
2
|
+
computed,
|
|
3
|
+
defineComponent,
|
|
4
|
+
nextTick,
|
|
5
|
+
onMounted,
|
|
6
|
+
onUnmounted,
|
|
7
|
+
ref,
|
|
8
|
+
shallowRef,
|
|
9
|
+
toRaw,
|
|
10
|
+
watch,
|
|
11
|
+
type ComputedRef,
|
|
12
|
+
type ObjectEmitsOptions,
|
|
13
|
+
type PropType,
|
|
14
|
+
type Ref,
|
|
15
|
+
type SetupContext,
|
|
16
|
+
type ShallowRef,
|
|
17
|
+
} from 'vue-demi';
|
|
18
|
+
|
|
19
|
+
// Helpers
|
|
20
|
+
import h, { slot } from '@/helpers/h-demi';
|
|
21
|
+
import { compact, trim } from 'lodash';
|
|
22
|
+
|
|
23
|
+
// CodeMirror
|
|
24
|
+
import { EditorSelection, EditorState, StateEffect } from '@codemirror/state';
|
|
25
|
+
import { EditorView, keymap } from '@codemirror/view';
|
|
26
|
+
import { linter, lintGutter } from '@codemirror/lint';
|
|
27
|
+
import { basicSetup, minimalSetup } from 'codemirror';
|
|
28
|
+
import { indentWithTab } from '@codemirror/commands';
|
|
29
|
+
|
|
30
|
+
import type {
|
|
31
|
+
Extension,
|
|
32
|
+
SelectionRange,
|
|
33
|
+
Text,
|
|
34
|
+
Transaction,
|
|
35
|
+
} from '@codemirror/state';
|
|
36
|
+
import type { LanguageSupport } from '@codemirror/language';
|
|
37
|
+
import type { ViewUpdate } from '@codemirror/view';
|
|
38
|
+
import type { LintSource } from '@codemirror/lint';
|
|
39
|
+
import type { StyleSpec } from 'style-mod';
|
|
40
|
+
|
|
41
|
+
/** Emit Interface */
|
|
42
|
+
export interface CodeMirrorEmitsOptions extends ObjectEmitsOptions {
|
|
43
|
+
/** Model Update */
|
|
44
|
+
(e: 'update:modelValue', value: string | Text): void;
|
|
45
|
+
/** CodeMirror ViewUpdate */
|
|
46
|
+
(e: 'update', value: ViewUpdate): void;
|
|
47
|
+
/** CodeMirror onFocus */
|
|
48
|
+
(e: 'focus', value: boolean): void;
|
|
49
|
+
/** CodeMirror onReady */
|
|
50
|
+
(
|
|
51
|
+
e: 'ready',
|
|
52
|
+
value: {
|
|
53
|
+
view: EditorView;
|
|
54
|
+
state: EditorState;
|
|
55
|
+
container: HTMLDivElement;
|
|
56
|
+
}
|
|
57
|
+
): void;
|
|
58
|
+
/** onChanged (same as update:modelValue) */
|
|
59
|
+
(e: 'changed', value: string | Text): void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** CodeMirror Component */
|
|
63
|
+
export default defineComponent({
|
|
64
|
+
/** Component Name */
|
|
65
|
+
name: 'CodeMirror',
|
|
66
|
+
/** Model Definition */
|
|
67
|
+
model: {
|
|
68
|
+
prop: 'modelValue',
|
|
69
|
+
event: 'update:modelValue',
|
|
70
|
+
},
|
|
71
|
+
/** Props Definition */
|
|
72
|
+
props: {
|
|
73
|
+
/** Model value */
|
|
74
|
+
modelValue: {
|
|
75
|
+
type: String as PropType<string | Text>,
|
|
76
|
+
default: '',
|
|
77
|
+
},
|
|
78
|
+
/**
|
|
79
|
+
* Theme
|
|
80
|
+
*
|
|
81
|
+
* @see {@link https://codemirror.net/6/examples/styling/ | Example: Styling}
|
|
82
|
+
*/
|
|
83
|
+
theme: {
|
|
84
|
+
type: Object as PropType<{ [selector: string]: StyleSpec }>,
|
|
85
|
+
default: () => {
|
|
86
|
+
return {};
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
/** Dark Mode */
|
|
90
|
+
dark: {
|
|
91
|
+
type: Boolean,
|
|
92
|
+
default: false,
|
|
93
|
+
},
|
|
94
|
+
/**
|
|
95
|
+
* Use Basic Setup
|
|
96
|
+
*
|
|
97
|
+
* @see {@link https://codemirror.net/docs/ref/#codemirror.basicSetup | basicSetup}
|
|
98
|
+
*/
|
|
99
|
+
basic: {
|
|
100
|
+
type: Boolean,
|
|
101
|
+
default: false,
|
|
102
|
+
},
|
|
103
|
+
/**
|
|
104
|
+
* Use Minimal Setup (The basic setting has priority.)
|
|
105
|
+
*
|
|
106
|
+
* @see {@link https://codemirror.net/docs/ref/#codemirror.minimalSetup | minimalSetup}
|
|
107
|
+
*/
|
|
108
|
+
minimal: {
|
|
109
|
+
type: Boolean,
|
|
110
|
+
default: false,
|
|
111
|
+
},
|
|
112
|
+
/**
|
|
113
|
+
* Line wrapping
|
|
114
|
+
*
|
|
115
|
+
* @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5ElineWrapping | LineWrapping}
|
|
116
|
+
*/
|
|
117
|
+
wrap: {
|
|
118
|
+
type: Boolean,
|
|
119
|
+
default: false,
|
|
120
|
+
},
|
|
121
|
+
/**
|
|
122
|
+
* Tab handling
|
|
123
|
+
*
|
|
124
|
+
* @see {@link https://codemirror.net/6/examples/tab/ | Tab Handling}
|
|
125
|
+
*/
|
|
126
|
+
tab: {
|
|
127
|
+
type: Boolean,
|
|
128
|
+
default: false,
|
|
129
|
+
},
|
|
130
|
+
/**
|
|
131
|
+
* Readonly
|
|
132
|
+
*
|
|
133
|
+
* @see {@link https://codemirror.net/6/docs/ref/#state.EditorState%5EreadOnly | readOnly}
|
|
134
|
+
*/
|
|
135
|
+
readonly: {
|
|
136
|
+
type: Boolean,
|
|
137
|
+
default: false,
|
|
138
|
+
},
|
|
139
|
+
/**
|
|
140
|
+
* Editable
|
|
141
|
+
*
|
|
142
|
+
* @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5Eeditable | editable}
|
|
143
|
+
*/
|
|
144
|
+
editable: {
|
|
145
|
+
type: Boolean,
|
|
146
|
+
default: true,
|
|
147
|
+
},
|
|
148
|
+
/**
|
|
149
|
+
* Additional Extension
|
|
150
|
+
*
|
|
151
|
+
* @see {@link https://codemirror.net/6/docs/ref/#state.Extension | Extending Editor State}
|
|
152
|
+
*/
|
|
153
|
+
extensions: {
|
|
154
|
+
type: Array as PropType<Extension[]>,
|
|
155
|
+
default: () => {
|
|
156
|
+
return [];
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
/**
|
|
160
|
+
* Language Phreses
|
|
161
|
+
*
|
|
162
|
+
* @see {@link https://codemirror.net/6/examples/translate/ | Example: Internationalization}
|
|
163
|
+
*/
|
|
164
|
+
phrases: {
|
|
165
|
+
type: Object as PropType<Record<string, string>>,
|
|
166
|
+
default: () => undefined,
|
|
167
|
+
},
|
|
168
|
+
/**
|
|
169
|
+
* CodeMirror Language
|
|
170
|
+
*
|
|
171
|
+
* @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
|
|
172
|
+
*/
|
|
173
|
+
lang: {
|
|
174
|
+
type: Object as PropType<LanguageSupport>,
|
|
175
|
+
default: () => undefined,
|
|
176
|
+
},
|
|
177
|
+
/**
|
|
178
|
+
* CodeMirror Linter
|
|
179
|
+
*
|
|
180
|
+
* @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
|
|
181
|
+
*/
|
|
182
|
+
linter: {
|
|
183
|
+
type: Function as PropType<LintSource>,
|
|
184
|
+
default: undefined,
|
|
185
|
+
},
|
|
186
|
+
/** Show Gutter */
|
|
187
|
+
lintGutter: {
|
|
188
|
+
type: Boolean,
|
|
189
|
+
defalt: false,
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
/** Emits */
|
|
193
|
+
emits: ['update:modelValue', 'update', 'ready', 'focus', 'changed'],
|
|
194
|
+
/**
|
|
195
|
+
* Setup
|
|
196
|
+
*
|
|
197
|
+
* @param props - Props
|
|
198
|
+
* @param context - Context
|
|
199
|
+
*/
|
|
200
|
+
setup(props, context: SetupContext<CodeMirrorEmitsOptions>) {
|
|
201
|
+
/** Editor DOM */
|
|
202
|
+
const editor: Ref<Element | undefined> = ref();
|
|
203
|
+
|
|
204
|
+
/** Internal value */
|
|
205
|
+
const doc: Ref<string | Text> = ref(props.modelValue);
|
|
206
|
+
|
|
207
|
+
/** CodeMirror Editor View */
|
|
208
|
+
const view: ShallowRef<EditorView> = shallowRef(new EditorView());
|
|
209
|
+
|
|
210
|
+
/** Selection */
|
|
211
|
+
const selection: Ref<EditorSelection> = computed({
|
|
212
|
+
get: () => view.value.state.selection,
|
|
213
|
+
set: s => view.value.dispatch({ selection: s }),
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
/** Cursor Position */
|
|
217
|
+
const cursor: Ref<number> = computed({
|
|
218
|
+
get: () => selection.value.main.head || 0,
|
|
219
|
+
set: a => view.value.dispatch({ selection: { anchor: a } }),
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
/** Editor State */
|
|
223
|
+
const state: Ref<EditorState> = computed({
|
|
224
|
+
get: () => view.value.state,
|
|
225
|
+
set: s => view.value.setState(s),
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
/** Focus */
|
|
229
|
+
const focus: Ref<boolean> = computed({
|
|
230
|
+
get: () => view.value.hasFocus,
|
|
231
|
+
set: f => {
|
|
232
|
+
if (f) {
|
|
233
|
+
view.value.focus();
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
/** Get CodeMirror Extension */
|
|
239
|
+
const extensions: ComputedRef<Extension[]> = computed(() =>
|
|
240
|
+
// TODO: Ignore previous prop was not changed.
|
|
241
|
+
compact([
|
|
242
|
+
// Toggle basic setup
|
|
243
|
+
props.basic ? basicSetup : undefined,
|
|
244
|
+
// Toggle minimal setup
|
|
245
|
+
props.minimal && !props.basic ? minimalSetup : undefined,
|
|
246
|
+
// ViewUpdate event listener
|
|
247
|
+
EditorView.updateListener.of((update: ViewUpdate) =>
|
|
248
|
+
context.emit('update', update)
|
|
249
|
+
),
|
|
250
|
+
// Toggle light/dark mode.
|
|
251
|
+
EditorView.theme(props.theme, { dark: props.dark }),
|
|
252
|
+
// Toggle line wrapping
|
|
253
|
+
props.wrap ? EditorView.lineWrapping : undefined,
|
|
254
|
+
// Indent with tab
|
|
255
|
+
props.tab ? keymap.of([indentWithTab]) : undefined,
|
|
256
|
+
// locale settings
|
|
257
|
+
props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
|
|
258
|
+
// Readonly option
|
|
259
|
+
EditorState.readOnly.of(props.readonly),
|
|
260
|
+
// Editable option
|
|
261
|
+
EditorView.editable.of(props.editable),
|
|
262
|
+
// Lang
|
|
263
|
+
props.lang ? toRaw(props.lang) : undefined,
|
|
264
|
+
// Append Linter settings
|
|
265
|
+
props.linter ? linter(props.linter) : undefined,
|
|
266
|
+
// Show 🔴 to error line when linter enabled.
|
|
267
|
+
props.linter && props.lintGutter ? lintGutter() : undefined,
|
|
268
|
+
// Append Extensions (such as basic-setup)
|
|
269
|
+
...props.extensions,
|
|
270
|
+
])
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
// for parent-to-child binding.
|
|
274
|
+
watch(
|
|
275
|
+
() => props.modelValue,
|
|
276
|
+
text => {
|
|
277
|
+
if (!view.value || view.value.composing) {
|
|
278
|
+
// IME fix
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Update
|
|
283
|
+
view.value.dispatch({
|
|
284
|
+
changes: { from: 0, to: view.value.state.doc.length, insert: text },
|
|
285
|
+
selection: selection.value,
|
|
286
|
+
});
|
|
287
|
+
},
|
|
288
|
+
{ immediate: true }
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
// Extension (mostly props) Changed
|
|
292
|
+
watch(extensions, e => {
|
|
293
|
+
// TODO: Reduce unchanched value
|
|
294
|
+
view.value.dispatch({
|
|
295
|
+
effects: StateEffect.reconfigure.of(e),
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// focus changed
|
|
300
|
+
watch(focus, isFocus => {
|
|
301
|
+
context.emit('focus', isFocus);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
/** When loaded */
|
|
305
|
+
onMounted(async () => {
|
|
306
|
+
/** Initial value */
|
|
307
|
+
let value: string | Text = doc.value;
|
|
308
|
+
if (editor.value && editor.value.childNodes[0]) {
|
|
309
|
+
// when slot mode, overwrite initial value
|
|
310
|
+
if (doc.value !== '') {
|
|
311
|
+
console.warn(
|
|
312
|
+
'[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values.'
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
value = trim((editor.value.childNodes[0] as HTMLElement).innerText);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Register Codemirror
|
|
319
|
+
view.value = new EditorView({
|
|
320
|
+
doc: value,
|
|
321
|
+
extensions: extensions.value,
|
|
322
|
+
parent: editor.value,
|
|
323
|
+
dispatch: (tr: Transaction) => {
|
|
324
|
+
view.value.update([tr]);
|
|
325
|
+
// TODO: Emit lint error event
|
|
326
|
+
// console.log(view.state.doc.toString(), tr);
|
|
327
|
+
if (tr.changes.empty) return;
|
|
328
|
+
// child-to-parent binding
|
|
329
|
+
const v = view.value.state.doc.toString();
|
|
330
|
+
context.emit('update:modelValue', v);
|
|
331
|
+
context.emit('changed', v);
|
|
332
|
+
},
|
|
333
|
+
});
|
|
334
|
+
await nextTick();
|
|
335
|
+
context.emit('ready', {
|
|
336
|
+
view: view,
|
|
337
|
+
state: state.value,
|
|
338
|
+
container: editor.value,
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
/** Destroy */
|
|
343
|
+
onUnmounted(() => view.value.destroy());
|
|
344
|
+
|
|
345
|
+
// Bellow is experimental.
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Get the text between the given points in the editor.
|
|
349
|
+
*
|
|
350
|
+
* @param from - start line number
|
|
351
|
+
* @param to - end line number
|
|
352
|
+
*/
|
|
353
|
+
const getRange = (from?: number, to?: number): string =>
|
|
354
|
+
view.value.state.sliceDoc(from, to);
|
|
355
|
+
/**
|
|
356
|
+
* Get the content of line.
|
|
357
|
+
*
|
|
358
|
+
* @param number - line number
|
|
359
|
+
*/
|
|
360
|
+
const getLine = (number: number): string =>
|
|
361
|
+
view.value.state.doc.line(number + 1).text;
|
|
362
|
+
/** Get the number of lines in the editor. */
|
|
363
|
+
const lineCount = (): number => view.value.state.doc.lines;
|
|
364
|
+
/** Retrieve one end of the primary selection. */
|
|
365
|
+
const getCursor = (): number => cursor.value;
|
|
366
|
+
/** Retrieves a list of all current selections. */
|
|
367
|
+
const listSelections = (): readonly SelectionRange[] =>
|
|
368
|
+
view.value.state.selection.ranges;
|
|
369
|
+
/** Get the currently selected code. */
|
|
370
|
+
const getSelection = (): string =>
|
|
371
|
+
view.value.state.sliceDoc(
|
|
372
|
+
view.value.state.selection.main.from,
|
|
373
|
+
view.value.state.selection.main.to
|
|
374
|
+
);
|
|
375
|
+
/**
|
|
376
|
+
* The length of the given array should be the same as the number of active selections.
|
|
377
|
+
* Replaces the content of the selections with the strings in the array.
|
|
378
|
+
*/
|
|
379
|
+
const getSelections = (): string[] =>
|
|
380
|
+
view.value.state.selection.ranges.map((r: { from: number; to: number }) =>
|
|
381
|
+
view.value.state.sliceDoc(r.from, r.to)
|
|
382
|
+
);
|
|
383
|
+
/** Return true if any text is selected. */
|
|
384
|
+
const somethingSelected = (): boolean =>
|
|
385
|
+
view.value.state.selection.ranges.some(
|
|
386
|
+
(r: { empty: boolean }) => !r.empty
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Replace the part of the document between from and to with the given string.
|
|
391
|
+
*
|
|
392
|
+
* @param replacement - replacement text
|
|
393
|
+
* @param from - start string at position
|
|
394
|
+
* @param to - insert the string at position
|
|
395
|
+
*/
|
|
396
|
+
const replaceRange = (
|
|
397
|
+
replacement: string | Text,
|
|
398
|
+
from: number,
|
|
399
|
+
to: number
|
|
400
|
+
) =>
|
|
401
|
+
view.value.dispatch({
|
|
402
|
+
changes: { from, to, insert: replacement },
|
|
403
|
+
});
|
|
404
|
+
/**
|
|
405
|
+
* Replace the selection(s) with the given string.
|
|
406
|
+
* By default, the new selection ends up after the inserted text.
|
|
407
|
+
*
|
|
408
|
+
* @param replacement - replacement text
|
|
409
|
+
*/
|
|
410
|
+
const replaceSelection = (replacement: string | Text) =>
|
|
411
|
+
view.value.dispatch(view.value.state.replaceSelection(replacement));
|
|
412
|
+
/**
|
|
413
|
+
* Set the cursor position.
|
|
414
|
+
*
|
|
415
|
+
* @param position - position.
|
|
416
|
+
*/
|
|
417
|
+
const setCursor = (position: number) => (cursor.value = position);
|
|
418
|
+
/**
|
|
419
|
+
* Set a single selection range.
|
|
420
|
+
*
|
|
421
|
+
* @param anchor - anchor position
|
|
422
|
+
* @param head -
|
|
423
|
+
*/
|
|
424
|
+
const setSelection = (anchor: number, head?: number) =>
|
|
425
|
+
view.value.dispatch({ selection: { anchor, head } });
|
|
426
|
+
/**
|
|
427
|
+
* Sets a new set of selections. There must be at least one selection in the given array.
|
|
428
|
+
*
|
|
429
|
+
* @param ranges - Selection range
|
|
430
|
+
* @param primary -
|
|
431
|
+
*/
|
|
432
|
+
const setSelections = (
|
|
433
|
+
ranges: readonly SelectionRange[],
|
|
434
|
+
primary?: number
|
|
435
|
+
) =>
|
|
436
|
+
view.value.dispatch({
|
|
437
|
+
selection: EditorSelection.create(ranges, primary),
|
|
438
|
+
});
|
|
439
|
+
/**
|
|
440
|
+
* Applies the given function to all existing selections, and calls extendSelections on the result.
|
|
441
|
+
*
|
|
442
|
+
* @param f - function
|
|
443
|
+
*/
|
|
444
|
+
const extendSelectionsBy = (f: Function) =>
|
|
445
|
+
view.value.dispatch({
|
|
446
|
+
selection: EditorSelection.create(
|
|
447
|
+
selection.value.ranges.map(r => r.extend(f(r)))
|
|
448
|
+
),
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
return {
|
|
452
|
+
context,
|
|
453
|
+
editor,
|
|
454
|
+
cursor,
|
|
455
|
+
selection,
|
|
456
|
+
state,
|
|
457
|
+
focus,
|
|
458
|
+
// Bellow is CodeMirror5's function
|
|
459
|
+
getRange,
|
|
460
|
+
getLine,
|
|
461
|
+
lineCount,
|
|
462
|
+
getCursor,
|
|
463
|
+
listSelections,
|
|
464
|
+
getSelection,
|
|
465
|
+
getSelections,
|
|
466
|
+
somethingSelected,
|
|
467
|
+
replaceRange,
|
|
468
|
+
replaceSelection,
|
|
469
|
+
setCursor,
|
|
470
|
+
setSelection,
|
|
471
|
+
setSelections,
|
|
472
|
+
extendSelectionsBy,
|
|
473
|
+
};
|
|
474
|
+
},
|
|
475
|
+
render() {
|
|
476
|
+
// <template>
|
|
477
|
+
// <div ref="editor" class="vue-codemirror">
|
|
478
|
+
// <aside v-show="!context.slots.default" aria-hidden><slot /></aside>
|
|
479
|
+
// </div>
|
|
480
|
+
// </template>
|
|
481
|
+
return h(
|
|
482
|
+
'div',
|
|
483
|
+
{
|
|
484
|
+
ref: 'editor',
|
|
485
|
+
class: 'vue-codemirror',
|
|
486
|
+
},
|
|
487
|
+
this.$slots.default
|
|
488
|
+
? h(
|
|
489
|
+
'aside',
|
|
490
|
+
{ style: 'display: none;', 'aria-hidden': 'true' },
|
|
491
|
+
slot(this.$slots.default)
|
|
492
|
+
)
|
|
493
|
+
: undefined
|
|
494
|
+
);
|
|
495
|
+
},
|
|
496
|
+
});
|