vue-codemirror6 1.0.0 → 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 +13 -2
- package/dist/components/CodeMirror.d.ts.map +1 -1
- package/dist/index.es.js +215 -119
- package/dist/index.iife.js +287 -0
- package/dist/index.umd.js +279 -3
- package/package.json +21 -21
- package/src/components/CodeMirror.ts +12 -5
package/README.md
CHANGED
|
@@ -11,12 +11,12 @@ A component for using [CodeMirror6](https://codemirror.net/6/) with Vue. Unrelat
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
13
|
```sh
|
|
14
|
-
yarn add vue-codemirror6
|
|
14
|
+
yarn add vue-codemirror6 codemirror
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
This component can handle bidirectional binding by `v-model` like a general Vue component.
|
|
18
18
|
|
|
19
|
-
When using with Vue 2.6, [@vue/composition-api](https://www.npmjs.com/package/@vue/composition-api) is required separately.
|
|
19
|
+
When using with Vue <2.6, [@vue/composition-api](https://www.npmjs.com/package/@vue/composition-api) is required separately.
|
|
20
20
|
|
|
21
21
|
```sh
|
|
22
22
|
yarn add vue-codemirror6 @vue/composition-api
|
|
@@ -26,21 +26,22 @@ For Vue 2.7 or later, import vue directly like Vue3.
|
|
|
26
26
|
|
|
27
27
|
### Props
|
|
28
28
|
|
|
29
|
-
| Props | Type | Information
|
|
30
|
-
| ---------- | --------------------------------- |
|
|
31
|
-
| basic | boolean | Use [basicSetup](https://codemirror.net/docs/ref/#codemirror.basicSetup).
|
|
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.
|
|
34
|
-
| wrap | boolean | Line text wrapping. see [lineWrapping](https://codemirror.net/6/docs/ref/#view.EditorView.lineWrapping).
|
|
35
|
-
| tab | boolean | Enables tab indentation.
|
|
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.
|
|
37
|
-
| readonly | boolean | Makes the cursor visible or you can drag the text but not edit the value.
|
|
38
|
-
| editable | boolean | When this is set to false, it is similar to `readonly`, except that the cursor is not displayed like the normal pre tag.
|
|
39
|
-
| lang | LanguageSupport | The language you want to have syntax highlighting. see <https://codemirror.net/6/#languages>
|
|
40
|
-
| phrases | Record<string, string> | Specify here if you want to make the displayed character string multilingual. see <https://codemirror.net/6/examples/translate/>
|
|
41
|
-
| extensions | Extension[] | Includes enhancements to extend CodeMirror.
|
|
42
|
-
| linter | LintSource | Set Linter. Enter a linter (eg `esLint([arbitrary rule])` function for `@codemirror
|
|
43
|
-
| lintGutter | boolean | Display 🔴 on the line where there was an error when `linter` was specified. It will not work if `linter` is not specified.
|
|
29
|
+
| Props | Type | Information |
|
|
30
|
+
| ---------- | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
31
|
+
| basic | boolean | Use [basicSetup](https://codemirror.net/docs/ref/#codemirror.basicSetup). |
|
|
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. |
|
|
34
|
+
| wrap | boolean | Line text wrapping. see [lineWrapping](https://codemirror.net/6/docs/ref/#view.EditorView.lineWrapping). |
|
|
35
|
+
| tab | boolean | Enables tab indentation. |
|
|
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. |
|
|
37
|
+
| readonly | boolean | Makes the cursor visible or you can drag the text but not edit the value. |
|
|
38
|
+
| editable | boolean | When this is set to false, it is similar to `readonly`, except that the cursor is not displayed like the normal pre tag. |
|
|
39
|
+
| lang | LanguageSupport | The language you want to have syntax highlighting. see <https://codemirror.net/6/#languages> |
|
|
40
|
+
| phrases | Record<string, string> | Specify here if you want to make the displayed character string multilingual. see <https://codemirror.net/6/examples/translate/> |
|
|
41
|
+
| extensions | Extension[] | Includes enhancements to extend CodeMirror. |
|
|
42
|
+
| linter | LintSource | Set Linter. Enter a linter (eg `esLint([arbitrary rule])` function for `@codemirror/lang-javascript`, `jsonParseLinter()`function for`@codemirror/json`). See the sources for various language libraries for more information. |
|
|
43
|
+
| lintGutter | boolean | Display 🔴 on the line where there was an error when `linter` was specified. It will not work if `linter` is not specified. |
|
|
44
|
+
| tag | string | HTML tags used in the component. (Default is `div` tag.) |
|
|
44
45
|
|
|
45
46
|
Notice: `lang` and `linter` can also be set together in `extensions`. This is defined for usability compatibility with past CodeMirrors.
|
|
46
47
|
|
|
@@ -192,12 +193,14 @@ export default defineComponent({
|
|
|
192
193
|
const phrases: Ref<Record<string, string>> = ref({
|
|
193
194
|
// @codemirror/view
|
|
194
195
|
'Control character': '制御文字',
|
|
195
|
-
// @codemirror/
|
|
196
|
+
// @codemirror/commands
|
|
197
|
+
'Selection deleted': '選択を削除',
|
|
198
|
+
// @codemirror/language
|
|
196
199
|
'Folded lines': '折り畳まれた行',
|
|
197
200
|
'Unfolded lines': '折り畳める行',
|
|
198
201
|
to: '行き先',
|
|
199
202
|
'folded code': '折り畳まれたコード',
|
|
200
|
-
unfold: '
|
|
203
|
+
unfold: '折り畳みを解除',
|
|
201
204
|
'Fold line': '行を折り畳む',
|
|
202
205
|
'Unfold line': '行の折り畳む解除',
|
|
203
206
|
// @codemirror/search
|
|
@@ -209,12 +212,17 @@ export default defineComponent({
|
|
|
209
212
|
previous: '▲',
|
|
210
213
|
all: 'すべて',
|
|
211
214
|
'match case': '一致条件',
|
|
215
|
+
'by word': '全文検索',
|
|
212
216
|
regexp: '正規表現',
|
|
213
217
|
replace: '置き換え',
|
|
214
218
|
'replace all': 'すべてを置き換え',
|
|
215
219
|
close: '閉じる',
|
|
216
220
|
'current match': '現在の一致',
|
|
221
|
+
'replaced $ matches': '$ 件の一致を置き換え',
|
|
222
|
+
'replaced match on line $': '$ 行の一致を置き換え',
|
|
217
223
|
'on line': 'した行',
|
|
224
|
+
// @codemirror/autocomplete
|
|
225
|
+
Completions: '自動補完',
|
|
218
226
|
// @codemirror/lint
|
|
219
227
|
Diagnostics: 'エラー',
|
|
220
228
|
'No diagnostics': 'エラーなし',
|
|
@@ -236,39 +244,44 @@ export default defineComponent({
|
|
|
236
244
|
|
|
237
245
|
## Events
|
|
238
246
|
|
|
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.)
|
|
247
|
+
| Event | Description |
|
|
248
|
+
| ------- | ------------------------------------------------------------------------------------------------------------- |
|
|
249
|
+
| ready | When CodeMirror loaded. |
|
|
250
|
+
| focus | When focus changed. |
|
|
251
|
+
| update | When CodeMirror state changed. Returns [ViewUpdate](https://codemirror.net/docs/ref/#view.ViewUpdate) object. |
|
|
252
|
+
| changed | Value changed. (Please implement with `v-model` unless there is a special reason.) |
|
|
245
253
|
|
|
246
254
|
## Methods
|
|
247
255
|
|
|
248
|
-
| Method
|
|
249
|
-
|
|
|
250
|
-
| selection
|
|
251
|
-
| cursor
|
|
252
|
-
| state
|
|
253
|
-
| focus
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
|
258
|
-
|
|
|
259
|
-
|
|
|
260
|
-
|
|
|
261
|
-
|
|
|
262
|
-
|
|
|
263
|
-
|
|
|
264
|
-
|
|
|
265
|
-
|
|
|
266
|
-
|
|
|
267
|
-
|
|
|
256
|
+
| Method | Description |
|
|
257
|
+
| --------- | --------------------------------------------------------------------------------------------------- |
|
|
258
|
+
| selection | Get and set the [EditorSelection](https://codemirror.net/docs/ref/#state.EditorSelection) instance. |
|
|
259
|
+
| cursor | Get and set the [cursor](https://codemirror.net/docs/ref/#state.EditorSelection^cursor) location. |
|
|
260
|
+
| state | Get and set [EditorState](https://codemirror.net/docs/ref/#state.EditorState). |
|
|
261
|
+
| focus | Get and set [focus](https://codemirror.net/docs/ref/#view.EditorView.focus). |
|
|
262
|
+
|
|
263
|
+
The instructions below are compatible methods for those familiar with [codemirror5](https://codemirror.net/5/). Since the above method is usually sufficient, its active use is not recommended.
|
|
264
|
+
|
|
265
|
+
| Method | Description |
|
|
266
|
+
| ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
|
|
267
|
+
| getRange(from?: number, to?: number) | Get the text between the given points in the editor. |
|
|
268
|
+
| getLine(number: number) | Get the content of line. |
|
|
269
|
+
| lineCount() | Get the number of lines in the editor. |
|
|
270
|
+
| getCursor() | Retrieve one end of the primary selection. |
|
|
271
|
+
| listSelections() | Retrieves a list of all current selections. |
|
|
272
|
+
| getSelection() | Get the currently selected code. |
|
|
273
|
+
| getSelections() | The length of the given array should be the same as the number of active selections. |
|
|
274
|
+
| somethingSelected() | Return true if any text is selected. |
|
|
275
|
+
| replaceRange(replacement: string \| Text, from: number, to: number) | Replace the part of the document between from and to with the given string. |
|
|
276
|
+
| replaceSelection(replacement: string \| Text) | Replace the selection(s) with the given string. |
|
|
277
|
+
| setCursor(position: number) | Set the cursor position. |
|
|
278
|
+
| setSelection(anchor: number, head?: number) | Set a single selection range. |
|
|
279
|
+
| setSelections(ranges: readonly SelectionRange[], primary?: number) | Sets a new set of selections. |
|
|
280
|
+
| extendSelectionsBy(f: Function) | Applies the given function to all existing selections, and calls extendSelections on the result. |
|
|
268
281
|
|
|
269
282
|
## Recommendations
|
|
270
283
|
|
|
271
|
-
Since CodeMirror has a relatively large capacity, when using vite, it is recommended to set it to output as a separate file using the [`manualChunks`](https://vitejs.dev/guide/build.html#chunking-strategy) option at build time as shown below.
|
|
284
|
+
Since CodeMirror has a relatively large capacity, when using [vite](https://vitejs.dev), it is recommended to set it to output as a separate file using the [`manualChunks`](https://vitejs.dev/guide/build.html#chunking-strategy) option at build time as shown below.
|
|
272
285
|
|
|
273
286
|
```ts
|
|
274
287
|
const config: UserConfig = {
|
|
@@ -141,9 +141,14 @@ declare const _default: import("vue-demi").DefineComponent<{
|
|
|
141
141
|
type: BooleanConstructor;
|
|
142
142
|
defalt: boolean;
|
|
143
143
|
};
|
|
144
|
+
/** Using tag */
|
|
145
|
+
tag: {
|
|
146
|
+
type: StringConstructor;
|
|
147
|
+
default: string;
|
|
148
|
+
};
|
|
144
149
|
}, {
|
|
145
150
|
context: SetupContext<CodeMirrorEmitsOptions>;
|
|
146
|
-
editor: Ref<
|
|
151
|
+
editor: Ref<HTMLElement | undefined>;
|
|
147
152
|
cursor: Ref<number>;
|
|
148
153
|
selection: Ref<EditorSelection>;
|
|
149
154
|
state: Ref<EditorState>;
|
|
@@ -279,8 +284,13 @@ declare const _default: import("vue-demi").DefineComponent<{
|
|
|
279
284
|
type: BooleanConstructor;
|
|
280
285
|
defalt: boolean;
|
|
281
286
|
};
|
|
287
|
+
/** Using tag */
|
|
288
|
+
tag: {
|
|
289
|
+
type: StringConstructor;
|
|
290
|
+
default: string;
|
|
291
|
+
};
|
|
282
292
|
}>> & {
|
|
283
|
-
[x: `on${string}`]: ((...args: never) => any) | undefined;
|
|
293
|
+
[x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
|
|
284
294
|
}, {
|
|
285
295
|
modelValue: string | Text;
|
|
286
296
|
theme: {
|
|
@@ -298,6 +308,7 @@ declare const _default: import("vue-demi").DefineComponent<{
|
|
|
298
308
|
lang: LanguageSupport;
|
|
299
309
|
linter: LintSource;
|
|
300
310
|
lintGutter: boolean;
|
|
311
|
+
tag: string;
|
|
301
312
|
}>;
|
|
302
313
|
export default _default;
|
|
303
314
|
//# sourceMappingURL=CodeMirror.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Code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;;;IAKlB,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;AAhIpB,wBAwbG"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,27 +1,46 @@
|
|
|
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 1.0.
|
|
8
|
+
* @version 1.0.2
|
|
9
9
|
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { isVue2
|
|
13
|
-
import { compact
|
|
14
|
-
import { EditorState
|
|
15
|
-
import { EditorView
|
|
16
|
-
import { linter
|
|
17
|
-
import { basicSetup
|
|
18
|
-
import { indentWithTab
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return
|
|
24
|
-
|
|
12
|
+
import { isVue2, Vue2, h as h$1, defineComponent, ref, shallowRef, computed, toRaw, watch, onMounted, nextTick, onUnmounted } from 'vue-demi';
|
|
13
|
+
import { compact, trim } from 'lodash';
|
|
14
|
+
import { EditorState, StateEffect, EditorSelection } from '@codemirror/state';
|
|
15
|
+
import { EditorView, keymap } from '@codemirror/view';
|
|
16
|
+
import { linter, lintGutter } from '@codemirror/lint';
|
|
17
|
+
import { basicSetup, minimalSetup } from 'codemirror';
|
|
18
|
+
import { indentWithTab } from '@codemirror/commands';
|
|
19
|
+
|
|
20
|
+
const adaptOnsV3 = (ons) => {
|
|
21
|
+
if (!ons)
|
|
22
|
+
return {};
|
|
23
|
+
return Object.entries(ons).reduce((ret, [key, handler]) => {
|
|
24
|
+
key = key.charAt(0).toUpperCase() + key.slice(1);
|
|
25
|
+
key = `on${key}`;
|
|
26
|
+
return { ...ret, [key]: handler };
|
|
27
|
+
}, {});
|
|
28
|
+
};
|
|
29
|
+
const h = (type, options = {}, chidren) => {
|
|
30
|
+
if (isVue2 && parseInt(Vue2.version) < 2.7) {
|
|
31
|
+
return h$1(type, options, chidren);
|
|
32
|
+
}
|
|
33
|
+
const { props, domProps, on, ...extraOptions } = options;
|
|
34
|
+
const ons = on ? adaptOnsV3(on) : {};
|
|
35
|
+
return h$1(
|
|
36
|
+
type,
|
|
37
|
+
{ ...extraOptions, ...props, ...domProps, ...ons },
|
|
38
|
+
chidren
|
|
39
|
+
);
|
|
40
|
+
};
|
|
41
|
+
const slot = (defaultSlots) => typeof defaultSlots == "function" ? defaultSlots() : defaultSlots;
|
|
42
|
+
|
|
43
|
+
const CodeMirror = defineComponent({
|
|
25
44
|
name: "CodeMirror",
|
|
26
45
|
model: {
|
|
27
46
|
prop: "modelValue",
|
|
@@ -34,49 +53,51 @@ const U = (a) => a ? Object.entries(a).reduce((l, [o, s]) => (o = o.charAt(0).to
|
|
|
34
53
|
},
|
|
35
54
|
theme: {
|
|
36
55
|
type: Object,
|
|
37
|
-
default: () =>
|
|
56
|
+
default: () => {
|
|
57
|
+
return {};
|
|
58
|
+
}
|
|
38
59
|
},
|
|
39
60
|
dark: {
|
|
40
61
|
type: Boolean,
|
|
41
|
-
default:
|
|
62
|
+
default: false
|
|
42
63
|
},
|
|
43
64
|
basic: {
|
|
44
65
|
type: Boolean,
|
|
45
|
-
default:
|
|
66
|
+
default: false
|
|
46
67
|
},
|
|
47
68
|
minimal: {
|
|
48
69
|
type: Boolean,
|
|
49
|
-
default:
|
|
70
|
+
default: false
|
|
50
71
|
},
|
|
51
72
|
wrap: {
|
|
52
73
|
type: Boolean,
|
|
53
|
-
default:
|
|
74
|
+
default: false
|
|
54
75
|
},
|
|
55
76
|
tab: {
|
|
56
77
|
type: Boolean,
|
|
57
|
-
default:
|
|
78
|
+
default: false
|
|
58
79
|
},
|
|
59
80
|
readonly: {
|
|
60
81
|
type: Boolean,
|
|
61
|
-
default:
|
|
82
|
+
default: false
|
|
62
83
|
},
|
|
63
84
|
editable: {
|
|
64
85
|
type: Boolean,
|
|
65
|
-
default:
|
|
86
|
+
default: true
|
|
66
87
|
},
|
|
67
88
|
extensions: {
|
|
68
89
|
type: Array,
|
|
69
|
-
default: () =>
|
|
90
|
+
default: () => {
|
|
91
|
+
return [];
|
|
92
|
+
}
|
|
70
93
|
},
|
|
71
94
|
phrases: {
|
|
72
95
|
type: Object,
|
|
73
|
-
default: () =>
|
|
74
|
-
}
|
|
96
|
+
default: () => void 0
|
|
75
97
|
},
|
|
76
98
|
lang: {
|
|
77
99
|
type: Object,
|
|
78
|
-
default: () =>
|
|
79
|
-
}
|
|
100
|
+
default: () => void 0
|
|
80
101
|
},
|
|
81
102
|
linter: {
|
|
82
103
|
type: Function,
|
|
@@ -84,106 +105,181 @@ const U = (a) => a ? Object.entries(a).reduce((l, [o, s]) => (o = o.charAt(0).to
|
|
|
84
105
|
},
|
|
85
106
|
lintGutter: {
|
|
86
107
|
type: Boolean,
|
|
87
|
-
defalt:
|
|
108
|
+
defalt: false
|
|
109
|
+
},
|
|
110
|
+
tag: {
|
|
111
|
+
type: String,
|
|
112
|
+
default: "div"
|
|
88
113
|
}
|
|
89
114
|
},
|
|
90
115
|
emits: ["update:modelValue", "update", "ready", "focus", "changed"],
|
|
91
|
-
setup(
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
get: () =>
|
|
97
|
-
set: (
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
116
|
+
setup(props, context) {
|
|
117
|
+
const editor = ref();
|
|
118
|
+
const doc = ref(props.modelValue);
|
|
119
|
+
const view = shallowRef(new EditorView());
|
|
120
|
+
const selection = computed({
|
|
121
|
+
get: () => view.value.state.selection,
|
|
122
|
+
set: (s) => view.value.dispatch({ selection: s })
|
|
123
|
+
});
|
|
124
|
+
const cursor = computed({
|
|
125
|
+
get: () => selection.value.main.head || 0,
|
|
126
|
+
set: (a) => view.value.dispatch({ selection: { anchor: a } })
|
|
127
|
+
});
|
|
128
|
+
const state = computed({
|
|
129
|
+
get: () => view.value.state,
|
|
130
|
+
set: (s) => view.value.setState(s)
|
|
131
|
+
});
|
|
132
|
+
const focus = computed({
|
|
133
|
+
get: () => view.value.hasFocus,
|
|
134
|
+
set: (f) => {
|
|
135
|
+
if (f) {
|
|
136
|
+
view.value.focus();
|
|
137
|
+
}
|
|
105
138
|
}
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
139
|
+
});
|
|
140
|
+
const extensions = computed(
|
|
141
|
+
() => compact([
|
|
142
|
+
props.basic ? basicSetup : void 0,
|
|
143
|
+
props.minimal && !props.basic ? minimalSetup : void 0,
|
|
144
|
+
EditorView.updateListener.of(
|
|
145
|
+
(update) => context.emit("update", update)
|
|
146
|
+
),
|
|
147
|
+
EditorView.theme(props.theme, { dark: props.dark }),
|
|
148
|
+
props.wrap ? EditorView.lineWrapping : void 0,
|
|
149
|
+
props.tab ? keymap.of([indentWithTab]) : void 0,
|
|
150
|
+
props.phrases ? EditorState.phrases.of(props.phrases) : void 0,
|
|
151
|
+
EditorState.readOnly.of(props.readonly),
|
|
152
|
+
EditorView.editable.of(props.editable),
|
|
153
|
+
props.lang ? toRaw(props.lang) : void 0,
|
|
154
|
+
props.linter ? linter(props.linter) : void 0,
|
|
155
|
+
props.linter && props.lintGutter ? lintGutter() : void 0,
|
|
156
|
+
...props.extensions
|
|
157
|
+
])
|
|
158
|
+
);
|
|
159
|
+
watch(
|
|
160
|
+
() => props.modelValue,
|
|
161
|
+
(value) => {
|
|
162
|
+
if (!view.value || view.value.composing) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const previous = view.value.state.selection;
|
|
166
|
+
view.value.dispatch({
|
|
167
|
+
changes: { from: 0, to: view.value.state.doc.length, insert: value },
|
|
168
|
+
selection: previous
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
{ immediate: true }
|
|
172
|
+
);
|
|
173
|
+
watch(extensions, (e) => {
|
|
174
|
+
view.value.dispatch({
|
|
175
|
+
effects: StateEffect.reconfigure.of(e)
|
|
129
176
|
});
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
177
|
+
});
|
|
178
|
+
watch(focus, (isFocus) => {
|
|
179
|
+
context.emit("focus", isFocus);
|
|
180
|
+
});
|
|
181
|
+
onMounted(async () => {
|
|
182
|
+
let value = doc.value;
|
|
183
|
+
if (editor.value && editor.value.childNodes[0]) {
|
|
184
|
+
if (doc.value !== "") {
|
|
185
|
+
console.warn(
|
|
186
|
+
"[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values."
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
value = trim(editor.value.childNodes[0].innerText);
|
|
190
|
+
}
|
|
191
|
+
view.value = new EditorView({
|
|
192
|
+
doc: value,
|
|
193
|
+
extensions: extensions.value,
|
|
194
|
+
parent: editor.value,
|
|
195
|
+
dispatch: (tr) => {
|
|
196
|
+
view.value.update([tr]);
|
|
197
|
+
if (tr.changes.empty)
|
|
140
198
|
return;
|
|
141
|
-
const v =
|
|
142
|
-
|
|
199
|
+
const v = view.value.state.doc.toString();
|
|
200
|
+
context.emit("update:modelValue", v);
|
|
201
|
+
context.emit("changed", v);
|
|
143
202
|
}
|
|
144
|
-
}), await R(), l.emit("ready", {
|
|
145
|
-
view: t,
|
|
146
|
-
state: d.value,
|
|
147
|
-
container: o.value
|
|
148
203
|
});
|
|
149
|
-
|
|
150
|
-
context
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
204
|
+
await nextTick();
|
|
205
|
+
context.emit("ready", {
|
|
206
|
+
view,
|
|
207
|
+
state: state.value,
|
|
208
|
+
container: editor.value
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
onUnmounted(() => view.value.destroy());
|
|
212
|
+
const getRange = (from, to) => view.value.state.sliceDoc(from, to);
|
|
213
|
+
const getLine = (number) => view.value.state.doc.line(number + 1).text;
|
|
214
|
+
const lineCount = () => view.value.state.doc.lines;
|
|
215
|
+
const getCursor = () => cursor.value;
|
|
216
|
+
const listSelections = () => view.value.state.selection.ranges;
|
|
217
|
+
const getSelection = () => view.value.state.sliceDoc(
|
|
218
|
+
view.value.state.selection.main.from,
|
|
219
|
+
view.value.state.selection.main.to
|
|
220
|
+
);
|
|
221
|
+
const getSelections = () => view.value.state.selection.ranges.map(
|
|
222
|
+
(r) => view.value.state.sliceDoc(r.from, r.to)
|
|
223
|
+
);
|
|
224
|
+
const somethingSelected = () => view.value.state.selection.ranges.some(
|
|
225
|
+
(r) => !r.empty
|
|
226
|
+
);
|
|
227
|
+
const replaceRange = (replacement, from, to) => view.value.dispatch({
|
|
228
|
+
changes: { from, to, insert: replacement }
|
|
229
|
+
});
|
|
230
|
+
const replaceSelection = (replacement) => view.value.dispatch(view.value.state.replaceSelection(replacement));
|
|
231
|
+
const setCursor = (position) => cursor.value = position;
|
|
232
|
+
const setSelection = (anchor, head) => view.value.dispatch({ selection: { anchor, head } });
|
|
233
|
+
const setSelections = (ranges, primary) => view.value.dispatch({
|
|
234
|
+
selection: EditorSelection.create(ranges, primary)
|
|
235
|
+
});
|
|
236
|
+
const extendSelectionsBy = (f) => view.value.dispatch({
|
|
237
|
+
selection: EditorSelection.create(
|
|
238
|
+
selection.value.ranges.map((r) => r.extend(f(r)))
|
|
239
|
+
)
|
|
240
|
+
});
|
|
241
|
+
return {
|
|
242
|
+
context,
|
|
243
|
+
editor,
|
|
244
|
+
cursor,
|
|
245
|
+
selection,
|
|
246
|
+
state,
|
|
247
|
+
focus,
|
|
248
|
+
getRange,
|
|
249
|
+
getLine,
|
|
250
|
+
lineCount,
|
|
251
|
+
getCursor,
|
|
252
|
+
listSelections,
|
|
253
|
+
getSelection,
|
|
254
|
+
getSelections,
|
|
255
|
+
somethingSelected,
|
|
256
|
+
replaceRange,
|
|
257
|
+
replaceSelection,
|
|
258
|
+
setCursor,
|
|
259
|
+
setSelection,
|
|
260
|
+
setSelections,
|
|
261
|
+
extendSelectionsBy
|
|
176
262
|
};
|
|
177
263
|
},
|
|
178
264
|
render() {
|
|
179
|
-
return
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
265
|
+
return h(
|
|
266
|
+
this.$props.tag,
|
|
267
|
+
{
|
|
268
|
+
ref: "editor",
|
|
269
|
+
class: "vue-codemirror"
|
|
270
|
+
},
|
|
271
|
+
this.$slots.default ? h(
|
|
272
|
+
"aside",
|
|
273
|
+
{ style: "display: none;", "aria-hidden": "true" },
|
|
274
|
+
slot(this.$slots.default)
|
|
275
|
+
) : void 0
|
|
276
|
+
);
|
|
183
277
|
}
|
|
184
|
-
})
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const installCodeMirror = (app) => app.component("CodeMirror", CodeMirror);
|
|
281
|
+
if (typeof window !== "undefined" && window.Vue) {
|
|
282
|
+
window.Vue.use(CodeMirror);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export { CodeMirror as default, installCodeMirror as install };
|