vue-codemirror6 0.6.8 → 1.0.0
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 +10 -1
- package/dist/components/CodeMirror.d.ts +19 -6
- package/dist/components/CodeMirror.d.ts.map +1 -1
- package/dist/index.es.js +89 -85
- package/dist/index.umd.js +2 -2
- package/package.json +23 -18
- package/src/components/CodeMirror.ts +496 -0
package/README.md
CHANGED
|
@@ -234,9 +234,18 @@ export default defineComponent({
|
|
|
234
234
|
</script>
|
|
235
235
|
```
|
|
236
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
|
+
|
|
237
246
|
## Methods
|
|
238
247
|
|
|
239
|
-
|
|
|
248
|
+
| Method | Description |
|
|
240
249
|
| ------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
|
|
241
250
|
| selection | Get and set the [EditorSelection](https://codemirror.net/docs/ref/#state.EditorSelection) instance. |
|
|
242
251
|
| cursor | Get and set the cursor location. |
|
|
@@ -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${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;;;;;;;;;;;;sBAsKO,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;;IAlXvC,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,wBAgbG"}
|
package/dist/index.es.js
CHANGED
|
@@ -5,23 +5,23 @@
|
|
|
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.0
|
|
9
9
|
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { isVue2 as
|
|
12
|
+
import { isVue2 as C, Vue2 as V, h as g, defineComponent as B, ref as p, 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
13
|
import { compact as E, trim as T } from "lodash";
|
|
14
|
-
import { EditorState as
|
|
15
|
-
import { EditorView as
|
|
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
|
-
}, W = (
|
|
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 g(a, l, o);
|
|
22
|
+
const { props: s, domProps: t, on: i, ...r } = l, d = i ? U(i) : {};
|
|
23
|
+
return g(a, { ...r, ...s, ...t, ...d }, o);
|
|
24
|
+
}, W = (a) => typeof a == "function" ? a() : a, b = B({
|
|
25
25
|
name: "CodeMirror",
|
|
26
26
|
model: {
|
|
27
27
|
prop: "modelValue",
|
|
@@ -87,99 +87,103 @@ const U = (t) => t ? Object.entries(t).reduce((i, [n, s]) => (n = n.charAt(0).to
|
|
|
87
87
|
defalt: !1
|
|
88
88
|
}
|
|
89
89
|
},
|
|
90
|
-
emits: ["update:modelValue", "update
|
|
91
|
-
setup(
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}), b = r({
|
|
104
|
-
get: () => o.hasFocus,
|
|
90
|
+
emits: ["update:modelValue", "update", "ready", "focus", "changed"],
|
|
91
|
+
setup(a, l) {
|
|
92
|
+
const o = p(), s = p(a.modelValue), t = x(new c()), i = u({
|
|
93
|
+
get: () => t.value.state.selection,
|
|
94
|
+
set: (e) => t.value.dispatch({ selection: e })
|
|
95
|
+
}), r = u({
|
|
96
|
+
get: () => i.value.main.head || 0,
|
|
97
|
+
set: (e) => t.value.dispatch({ selection: { anchor: e } })
|
|
98
|
+
}), d = u({
|
|
99
|
+
get: () => t.value.state,
|
|
100
|
+
set: (e) => t.value.setState(e)
|
|
101
|
+
}), f = u({
|
|
102
|
+
get: () => t.value.hasFocus,
|
|
105
103
|
set: (e) => {
|
|
106
|
-
e &&
|
|
104
|
+
e && t.value.focus();
|
|
107
105
|
}
|
|
108
|
-
}),
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
...
|
|
122
|
-
]))
|
|
123
|
-
return
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
o.dispatch({
|
|
128
|
-
changes: { from: 0, to: o.state.doc.length, insert: e },
|
|
129
|
-
selection: a
|
|
106
|
+
}), h = u(() => E([
|
|
107
|
+
a.basic ? A : void 0,
|
|
108
|
+
a.minimal && !a.basic ? F : void 0,
|
|
109
|
+
c.updateListener.of((e) => l.emit("update", e)),
|
|
110
|
+
c.theme(a.theme, { dark: a.dark }),
|
|
111
|
+
a.wrap ? c.lineWrapping : void 0,
|
|
112
|
+
a.tab ? D.of([N]) : void 0,
|
|
113
|
+
a.phrases ? y.phrases.of(a.phrases) : void 0,
|
|
114
|
+
a.readonly ? y.readOnly.of(a.readonly) : void 0,
|
|
115
|
+
a.editable ? c.editable.of(a.editable) : void 0,
|
|
116
|
+
a.lang ? O(a.lang) : void 0,
|
|
117
|
+
a.linter ? G(a.linter) : void 0,
|
|
118
|
+
a.linter && a.lintGutter ? L() : void 0,
|
|
119
|
+
...a.extensions
|
|
120
|
+
]));
|
|
121
|
+
return m(() => a.modelValue, (e) => {
|
|
122
|
+
!t.value || t.value.composing || t.value.dispatch({
|
|
123
|
+
changes: { from: 0, to: t.value.state.doc.length, insert: e },
|
|
124
|
+
selection: i.value
|
|
130
125
|
});
|
|
131
|
-
}),
|
|
132
|
-
|
|
126
|
+
}, { immediate: !0 }), m(h, (e) => {
|
|
127
|
+
t.value.dispatch({
|
|
133
128
|
effects: $.reconfigure.of(e)
|
|
134
129
|
});
|
|
130
|
+
}), m(f, (e) => {
|
|
131
|
+
l.emit("focus", e);
|
|
135
132
|
}), M(async () => {
|
|
136
133
|
let e = s.value;
|
|
137
|
-
|
|
134
|
+
o.value && o.value.childNodes[0] && (s.value !== "" && console.warn("[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."), e = T(o.value.childNodes[0].innerText)), t.value = new c({
|
|
138
135
|
doc: e,
|
|
139
|
-
extensions:
|
|
140
|
-
parent:
|
|
141
|
-
dispatch: (
|
|
142
|
-
|
|
136
|
+
extensions: h.value,
|
|
137
|
+
parent: o.value,
|
|
138
|
+
dispatch: (n) => {
|
|
139
|
+
if (t.value.update([n]), n.changes.empty)
|
|
140
|
+
return;
|
|
141
|
+
const v = t.value.state.doc.toString();
|
|
142
|
+
l.emit("update:modelValue", v), l.emit("changed", v);
|
|
143
143
|
}
|
|
144
|
-
}), await R()
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
144
|
+
}), await R(), l.emit("ready", {
|
|
145
|
+
view: t,
|
|
146
|
+
state: d.value,
|
|
147
|
+
container: o.value
|
|
148
|
+
});
|
|
149
|
+
}), j(() => t.value.destroy()), {
|
|
150
|
+
context: l,
|
|
151
|
+
editor: o,
|
|
152
|
+
cursor: r,
|
|
153
|
+
selection: i,
|
|
154
|
+
state: d,
|
|
155
|
+
focus: f,
|
|
156
|
+
getRange: (e, n) => t.value.state.sliceDoc(e, n),
|
|
157
|
+
getLine: (e) => t.value.state.doc.line(e + 1).text,
|
|
158
|
+
lineCount: () => t.value.state.doc.lines,
|
|
159
|
+
getCursor: () => r.value,
|
|
160
|
+
listSelections: () => t.value.state.selection.ranges,
|
|
161
|
+
getSelection: () => t.value.state.sliceDoc(t.value.state.selection.main.from, t.value.state.selection.main.to),
|
|
162
|
+
getSelections: () => t.value.state.selection.ranges.map((e) => t.value.state.sliceDoc(e.from, e.to)),
|
|
163
|
+
somethingSelected: () => t.value.state.selection.ranges.some((e) => !e.empty),
|
|
164
|
+
replaceRange: (e, n, v) => t.value.dispatch({
|
|
165
|
+
changes: { from: n, to: v, insert: e }
|
|
162
166
|
}),
|
|
163
|
-
replaceSelection: (e) =>
|
|
164
|
-
setCursor: (e) =>
|
|
165
|
-
setSelection: (e,
|
|
166
|
-
setSelections: (e,
|
|
167
|
-
selection:
|
|
167
|
+
replaceSelection: (e) => t.value.dispatch(t.value.state.replaceSelection(e)),
|
|
168
|
+
setCursor: (e) => r.value = e,
|
|
169
|
+
setSelection: (e, n) => t.value.dispatch({ selection: { anchor: e, head: n } }),
|
|
170
|
+
setSelections: (e, n) => t.value.dispatch({
|
|
171
|
+
selection: S.create(e, n)
|
|
168
172
|
}),
|
|
169
|
-
extendSelectionsBy: (e) =>
|
|
170
|
-
selection:
|
|
173
|
+
extendSelectionsBy: (e) => t.value.dispatch({
|
|
174
|
+
selection: S.create(i.value.ranges.map((n) => n.extend(e(n))))
|
|
171
175
|
})
|
|
172
176
|
};
|
|
173
177
|
},
|
|
174
178
|
render() {
|
|
175
|
-
return
|
|
179
|
+
return w("div", {
|
|
176
180
|
ref: "editor",
|
|
177
181
|
class: "vue-codemirror"
|
|
178
|
-
}, this.$slots.default ?
|
|
182
|
+
}, this.$slots.default ? w("aside", { style: "display: none;", "aria-hidden": "true" }, W(this.$slots.default)) : void 0);
|
|
179
183
|
}
|
|
180
|
-
}), ce = (
|
|
181
|
-
typeof window < "u" && window.Vue && window.Vue.use(
|
|
184
|
+
}), ce = (a) => a.component("CodeMirror", b);
|
|
185
|
+
typeof window < "u" && window.Vue && window.Vue.use(b);
|
|
182
186
|
export {
|
|
183
|
-
|
|
187
|
+
b as default,
|
|
184
188
|
ce as install
|
|
185
189
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
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.0
|
|
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 b=o=>o?Object.entries(o).reduce((s,[n,c])=>(n=n.charAt(0).toUpperCase()+n.slice(1),n=`on${n}`,{...s,[n]:c}),{}):{},S=(o,s={},n)=>{if(a.isVue2&&parseInt(a.Vue2.version)<2.7)return a.h(o,s,n);const{props:c,domProps:t,on:d,...f}=s,m=d?b(d):{};return a.h(o,{...f,...c,...t,...m},n)},E=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,s){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=>s.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,o.readonly?u.EditorState.readOnly.of(o.readonly):void 0,o.editable?r.EditorView.editable.of(o.editable):void 0,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=>{s.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:l=>{if(t.value.update([l]),l.changes.empty)return;const v=t.value.state.doc.toString();s.emit("update:modelValue",v),s.emit("changed",v)}}),await a.nextTick(),s.emit("ready",{view:t,state:m.value,container:n.value})}),a.onUnmounted(()=>t.value.destroy()),{context:s,editor:n,cursor:f,selection:d,state:m,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:()=>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,l,v)=>t.value.dispatch({changes:{from:l,to:v,insert:e}}),replaceSelection:e=>t.value.dispatch(t.value.state.replaceSelection(e)),setCursor:e=>f.value=e,setSelection:(e,l)=>t.value.dispatch({selection:{anchor:e,head:l}}),setSelections:(e,l)=>t.value.dispatch({selection:u.EditorSelection.create(e,l)}),extendSelectionsBy:e=>t.value.dispatch({selection:u.EditorSelection.create(d.value.ranges.map(l=>l.extend(e(l))))})}},render(){return S("div",{ref:"editor",class:"vue-codemirror"},this.$slots.default?S("aside",{style:"display: none;","aria-hidden":"true"},E(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,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "vue-codemirror6",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "CodeMirror6 Component for vue2 and vue3",
|
|
7
7
|
"keywords": [
|
|
@@ -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.2",
|
|
53
58
|
"sideEffects": false,
|
|
54
59
|
"scripts": {
|
|
55
60
|
"dev": "vite",
|
|
@@ -79,22 +84,22 @@
|
|
|
79
84
|
"devDependencies": {
|
|
80
85
|
"@codemirror/lang-javascript": "^6.0.2",
|
|
81
86
|
"@codemirror/lang-markdown": "^6.0.1",
|
|
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.
|
|
87
|
+
"@types/lodash": "^4.14.184",
|
|
88
|
+
"@types/node": "^18.7.8",
|
|
89
|
+
"@typescript-eslint/eslint-plugin": "^5.33.1",
|
|
90
|
+
"@typescript-eslint/parser": "^5.33.1",
|
|
91
|
+
"@vitejs/plugin-vue": "^3.0.3",
|
|
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.22.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.
|
|
99
|
+
"eslint-import-resolver-typescript": "^3.4.2",
|
|
95
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
104
|
"eslint-plugin-vue": "^9.3.0",
|
|
100
105
|
"eslint-plugin-vuejs-accessibility": "^1.2.0",
|
|
@@ -107,12 +112,12 @@
|
|
|
107
112
|
"rimraf": "^3.0.2",
|
|
108
113
|
"rollup-plugin-visualizer": "^5.7.1",
|
|
109
114
|
"typescript": "^4.7.4",
|
|
110
|
-
"vite": "^3.0.
|
|
115
|
+
"vite": "^3.0.9",
|
|
111
116
|
"vite-plugin-banner": "^0.3.0",
|
|
112
117
|
"vite-plugin-checker": "^0.4.9",
|
|
113
118
|
"vue": "^3.2.37",
|
|
114
119
|
"vue-eslint-parser": "^9.0.3",
|
|
115
|
-
"vue-tsc": "^0.
|
|
120
|
+
"vue-tsc": "^0.40.1"
|
|
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
|
+
});
|