vue-codemirror6 0.3.7 → 0.5.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 +35 -23
- package/dist/components/{CodeMirror.vue.d.ts → CodeMirror.d.ts} +17 -6
- package/dist/components/CodeMirror.d.ts.map +1 -0
- package/dist/helpers/h-demi.d.ts +14 -0
- package/dist/helpers/h-demi.d.ts.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/{vue-codemirror6.es.js → index.es.js} +50 -47
- package/dist/index.umd.js +8 -0
- package/package.json +21 -16
- package/dist/components/CodeMirror.vue.d.ts.map +0 -1
- package/dist/vue-codemirror6.umd.js +0 -8
- package/src/components/CodeMirror.vue +0 -297
package/README.md
CHANGED
|
@@ -8,28 +8,36 @@
|
|
|
8
8
|
|
|
9
9
|
A component for using [CodeMirror6](https://codemirror.net/6/) with Vue. Unrelated to [surmon-china's vue-codemirror](https://github.com/surmon-china/vue-codemirror), it is for CodeMirror6.
|
|
10
10
|
|
|
11
|
-
If you are using Vue2, please use `0.1.x`. (The component itself supports both, but the script generated by vue-demi can only output one)
|
|
12
|
-
|
|
13
11
|
## Usage
|
|
14
12
|
|
|
13
|
+
```sh
|
|
14
|
+
yarn add vue-codemirror6
|
|
15
|
+
```
|
|
16
|
+
|
|
15
17
|
This component can handle bidirectional binding by `v-model` like a general Vue component.
|
|
18
|
+
|
|
16
19
|
When using with Vue2, [@vue/composition-api](https://www.npmjs.com/package/@vue/composition-api) is required separately.
|
|
17
20
|
|
|
21
|
+
```sh
|
|
22
|
+
yarn add vue-codemirror6 @vue/composition-api
|
|
23
|
+
```
|
|
24
|
+
|
|
18
25
|
### Props
|
|
19
26
|
|
|
20
|
-
| Props | Type | Information
|
|
21
|
-
| ---------- | --------------------------------- |
|
|
22
|
-
| basic | boolean | Use [basic-setup](https://codemirror.net/6/docs/ref/#basic-setup).
|
|
23
|
-
| dark | boolean | Toggle Darkmode. It can be changed in real time, but the input value will return to the initial value.
|
|
24
|
-
| wrap | boolean | Line text wrapping. see [lineWrapping](https://codemirror.net/6/docs/ref/#view.EditorView.lineWrapping).
|
|
25
|
-
| tab | boolean | Enables tab indentation.
|
|
26
|
-
| 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.
|
|
27
|
-
| readonly | boolean | Makes the cursor visible or you can drag the text but not edit the value.
|
|
28
|
-
| 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.
|
|
29
|
-
| lang | LanguageSupport | The language you want to have syntax highlighting. see <https://codemirror.net/6/#languages>
|
|
30
|
-
| phrases | Record<string, string> | Specify here if you want to make the displayed character string multilingual. see <https://codemirror.net/6/examples/translate/>
|
|
31
|
-
| extensions | Extension[] | Includes enhancements to extend CodeMirror.
|
|
32
|
-
| linter |
|
|
27
|
+
| Props | Type | Information |
|
|
28
|
+
| ---------- | --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
29
|
+
| basic | boolean | Use [basic-setup](https://codemirror.net/6/docs/ref/#basic-setup). |
|
|
30
|
+
| dark | boolean | Toggle Darkmode. It can be changed in real time, but the input value will return to the initial value. |
|
|
31
|
+
| wrap | boolean | Line text wrapping. see [lineWrapping](https://codemirror.net/6/docs/ref/#view.EditorView.lineWrapping). |
|
|
32
|
+
| tab | boolean | Enables tab indentation. |
|
|
33
|
+
| 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. |
|
|
34
|
+
| readonly | boolean | Makes the cursor visible or you can drag the text but not edit the value. |
|
|
35
|
+
| 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. |
|
|
36
|
+
| lang | LanguageSupport | The language you want to have syntax highlighting. see <https://codemirror.net/6/#languages> |
|
|
37
|
+
| phrases | Record<string, string> | Specify here if you want to make the displayed character string multilingual. see <https://codemirror.net/6/examples/translate/> |
|
|
38
|
+
| extensions | Extension[] | Includes enhancements to extend CodeMirror. |
|
|
39
|
+
| 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. |
|
|
40
|
+
| lintGutter | boolean | Display 🔴 on the line where there was an error when `linter` was specified. It will not work if `linter` is not specified. |
|
|
33
41
|
|
|
34
42
|
Notice: `lang` and `linter` can also be set together in `extensions`. This is defined for usability compatibility with past CodeMirrors.
|
|
35
43
|
|
|
@@ -88,7 +96,7 @@ Also, insert a `<pre>` tag to prevent the text in the slot from being automatica
|
|
|
88
96
|
|
|
89
97
|
```vue
|
|
90
98
|
<template>
|
|
91
|
-
<code-mirror :lang="lang" :
|
|
99
|
+
<code-mirror :lang="lang" :linter="linter">
|
|
92
100
|
<pre>
|
|
93
101
|
{
|
|
94
102
|
"key": "value"
|
|
@@ -100,7 +108,7 @@ Also, insert a `<pre>` tag to prevent the text in the slot from being automatica
|
|
|
100
108
|
<script>
|
|
101
109
|
import { ref, defineComponent } from 'vue';
|
|
102
110
|
|
|
103
|
-
import { json } from '@codemirror/lang-json';
|
|
111
|
+
import { json, jsonParseLinter } from '@codemirror/lang-json';
|
|
104
112
|
|
|
105
113
|
import CodeMirror from 'vue-codemirror6';
|
|
106
114
|
|
|
@@ -109,8 +117,9 @@ export default defineComponent({
|
|
|
109
117
|
CodeMirror,
|
|
110
118
|
},
|
|
111
119
|
setup() {
|
|
112
|
-
const lang =
|
|
113
|
-
|
|
120
|
+
const lang = json();
|
|
121
|
+
const linter = jsonParseLinter();
|
|
122
|
+
return { lang, linter };
|
|
114
123
|
},
|
|
115
124
|
});
|
|
116
125
|
</script>
|
|
@@ -118,7 +127,7 @@ export default defineComponent({
|
|
|
118
127
|
|
|
119
128
|
### Full Example
|
|
120
129
|
|
|
121
|
-
When using as a Markdown editor on [
|
|
130
|
+
When using as a Markdown editor on [vite-vue2-vuetify-ts-starter](https://github.com/logue/vite-vue2-vuetify-ts-starter).
|
|
122
131
|
|
|
123
132
|
```vue
|
|
124
133
|
<template>
|
|
@@ -162,7 +171,7 @@ export default defineComponent({
|
|
|
162
171
|
*
|
|
163
172
|
* @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
|
|
164
173
|
*/
|
|
165
|
-
const lang:
|
|
174
|
+
const lang: LanguageSupport = md();
|
|
166
175
|
|
|
167
176
|
/** Dark mode **/
|
|
168
177
|
const dark = ref(window.matchMedia('(prefers-color-scheme: dark)').matches);
|
|
@@ -173,6 +182,7 @@ export default defineComponent({
|
|
|
173
182
|
/**
|
|
174
183
|
* Internationalization Config.
|
|
175
184
|
* In this example, the display language to Japanese.
|
|
185
|
+
* Must be reactive when used in combination with vue-i18n.
|
|
176
186
|
*
|
|
177
187
|
* @see {@link https://codemirror.net/6/examples/translate/ | Example: Internationalization}
|
|
178
188
|
*/
|
|
@@ -232,10 +242,12 @@ const config: UserConfig = {
|
|
|
232
242
|
// ...
|
|
233
243
|
codemirror: [
|
|
234
244
|
// Split CodeMirror code.
|
|
235
|
-
'
|
|
245
|
+
'@codemirror/basic-setup',
|
|
246
|
+
'@codemirror/commands',
|
|
247
|
+
'@codemirror/language',
|
|
248
|
+
'@codemirror/lint',
|
|
236
249
|
'@codemirror/state',
|
|
237
250
|
'@codemirror/view',
|
|
238
|
-
'@codemirror/basic-setup',
|
|
239
251
|
// Add the following as needed.
|
|
240
252
|
'@codemirror/lang-html',
|
|
241
253
|
'@codemirror/lang-javascript',
|
|
@@ -2,7 +2,7 @@ import { type PropType, type Ref, type SetupContext } from 'vue-demi';
|
|
|
2
2
|
import { EditorSelection } from '@codemirror/state';
|
|
3
3
|
import type { Extension, Text } from '@codemirror/state';
|
|
4
4
|
import type { LanguageSupport } from '@codemirror/language';
|
|
5
|
-
import type {
|
|
5
|
+
import type { LintSource } from '@codemirror/lint';
|
|
6
6
|
import type { StyleSpec } from 'style-mod';
|
|
7
7
|
/** CodeMirror Component */
|
|
8
8
|
declare const _default: import("vue-demi").DefineComponent<{
|
|
@@ -117,9 +117,14 @@ declare const _default: import("vue-demi").DefineComponent<{
|
|
|
117
117
|
* @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
|
|
118
118
|
*/
|
|
119
119
|
linter: {
|
|
120
|
-
type: PropType<
|
|
120
|
+
type: PropType<LintSource>;
|
|
121
121
|
default: () => undefined;
|
|
122
122
|
};
|
|
123
|
+
/** Show Gutter */
|
|
124
|
+
lintGutter: {
|
|
125
|
+
type: BooleanConstructor;
|
|
126
|
+
defalt: boolean;
|
|
127
|
+
};
|
|
123
128
|
}, {
|
|
124
129
|
context: SetupContext<import("vue-demi").EmitsOptions>;
|
|
125
130
|
editor: Ref<Element | undefined>;
|
|
@@ -235,11 +240,15 @@ declare const _default: import("vue-demi").DefineComponent<{
|
|
|
235
240
|
* @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
|
|
236
241
|
*/
|
|
237
242
|
linter: {
|
|
238
|
-
type: PropType<
|
|
243
|
+
type: PropType<LintSource>;
|
|
239
244
|
default: () => undefined;
|
|
240
245
|
};
|
|
246
|
+
/** Show Gutter */
|
|
247
|
+
lintGutter: {
|
|
248
|
+
type: BooleanConstructor;
|
|
249
|
+
defalt: boolean;
|
|
250
|
+
};
|
|
241
251
|
}>>, {
|
|
242
|
-
readonly: boolean;
|
|
243
252
|
modelValue: string | Text;
|
|
244
253
|
selection: EditorSelection | {
|
|
245
254
|
anchor: number;
|
|
@@ -252,11 +261,13 @@ declare const _default: import("vue-demi").DefineComponent<{
|
|
|
252
261
|
basic: boolean;
|
|
253
262
|
wrap: boolean;
|
|
254
263
|
tab: boolean;
|
|
264
|
+
readonly: boolean;
|
|
255
265
|
editable: boolean;
|
|
256
266
|
extensions: Extension[];
|
|
257
267
|
phrases: Record<string, string>;
|
|
258
268
|
lang: LanguageSupport;
|
|
259
|
-
linter:
|
|
269
|
+
linter: LintSource;
|
|
270
|
+
lintGutter: boolean;
|
|
260
271
|
}>;
|
|
261
272
|
export default _default;
|
|
262
|
-
//# sourceMappingURL=CodeMirror.
|
|
273
|
+
//# sourceMappingURL=CodeMirror.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CodeMirror.d.ts","sourceRoot":"","sources":["../../src/components/CodeMirror.ts"],"names":[],"mappings":"AAAA,OAAO,EAWL,KAAK,QAAQ,EACb,KAAK,GAAG,EACR,KAAK,YAAY,EAClB,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,eAAe,EAAe,MAAM,mBAAmB,CAAC;AAWjE,OAAO,KAAK,EAAE,SAAS,EAAE,IAAI,EAAe,MAAM,mBAAmB,CAAC;AACtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,2BAA2B;;IAWvB,kBAAkB;;;;;IAKlB;;;;OAIG;;;oBAG6B,MAAM;;;;;IAItC;;;;OAIG;;;;;;;IAOH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAOH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH,kBAAkB;;;;;;;;;IAnHlB,kBAAkB;;;;;IAKlB;;;;OAIG;;;oBAG6B,MAAM;;;;;IAItC;;;;OAIG;;;;;;;IAOH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAOH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH,kBAAkB;;;;;;;;gBAvGc,MAAM;;;;;;;;;;;;;;;;;;AAtB1C,wBAmRG"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface Options {
|
|
2
|
+
class?: string;
|
|
3
|
+
domProps?: Record<any, any>;
|
|
4
|
+
on?: Record<any, any>;
|
|
5
|
+
props?: Record<any, any>;
|
|
6
|
+
ref?: string;
|
|
7
|
+
style?: string;
|
|
8
|
+
}
|
|
9
|
+
declare const h: (type: string | Record<any, any>, options?: Options, chidren?: any) => import("vue-demi").VNode<import("vue-demi").RendererNode, import("vue-demi").RendererElement, {
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}>;
|
|
12
|
+
declare const slot: (defaultSlots: any) => any;
|
|
13
|
+
export { slot, h as default };
|
|
14
|
+
//# sourceMappingURL=h-demi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"h-demi.d.ts","sourceRoot":"","sources":["../../src/helpers/h-demi.ts"],"names":[],"mappings":"AAEA,UAAU,OAAO;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC5B,EAAE,CAAC,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAWD,QAAA,MAAM,CAAC,SACC,MAAM,GAAG,OAAO,GAAG,EAAE,GAAG,CAAC,YACtB,OAAO,YACN,GAAG;;EAWd,CAAC;AAEF,QAAA,MAAM,IAAI,iBAAkB,GAAG,QACoC,CAAC;AAEpE,OAAO,EAAE,IAAI,EAAE,CAAC,IAAI,OAAO,EAAE,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @copyright 2022 Masashi Yoshikawa {@link https://logue.dev/} All rights reserved.
|
|
7
7
|
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
8
8
|
*/
|
|
9
|
-
import CodeMirror from './components/CodeMirror
|
|
10
|
-
declare const installCodeMirror:
|
|
9
|
+
import CodeMirror from './components/CodeMirror';
|
|
10
|
+
declare const installCodeMirror: (app: any) => any;
|
|
11
11
|
export { CodeMirror as default, installCodeMirror as install };
|
|
12
12
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,UAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,UAAU,MAAM,yBAAyB,CAAC;AAGjD,QAAA,MAAM,iBAAiB,mBAAiD,CAAC;AAEzE,OAAO,EAAE,UAAU,IAAI,OAAO,EAAE,iBAAiB,IAAI,OAAO,EAAE,CAAC"}
|
|
@@ -1,18 +1,28 @@
|
|
|
1
|
-
import { defineComponent, ref, toRefs, computed, toRaw, watch, onMounted, nextTick, onUnmounted
|
|
1
|
+
import { isVue2, h as h$1, defineComponent, ref, toRefs, computed, toRaw, watch, onMounted, nextTick, onUnmounted } from "vue-demi";
|
|
2
2
|
import { EditorState } from "@codemirror/state";
|
|
3
3
|
import { EditorView, keymap } from "@codemirror/view";
|
|
4
|
+
import { linter, lintGutter } from "@codemirror/lint";
|
|
4
5
|
import { basicSetup } from "@codemirror/basic-setup";
|
|
5
6
|
import { indentWithTab } from "@codemirror/commands";
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
import { compact, trim } from "lodash";
|
|
8
|
+
const adaptOnsV3 = (ons) => {
|
|
9
|
+
if (!ons)
|
|
10
|
+
return null;
|
|
11
|
+
return Object.entries(ons).reduce((ret, [key, handler]) => {
|
|
12
|
+
key = key.charAt(0).toUpperCase() + key.slice(1);
|
|
13
|
+
key = `on${key}`;
|
|
14
|
+
return { ...ret, [key]: handler };
|
|
15
|
+
}, {});
|
|
16
|
+
};
|
|
17
|
+
const h = (type, options = {}, chidren) => {
|
|
18
|
+
if (isVue2)
|
|
19
|
+
return h$1(type, options, chidren);
|
|
20
|
+
const { props, domProps, on, ...extraOptions } = options;
|
|
21
|
+
const ons = on ? adaptOnsV3(on) : {};
|
|
22
|
+
return h$1(type, { ...extraOptions, ...props, ...domProps, ...ons }, chidren);
|
|
14
23
|
};
|
|
15
|
-
const
|
|
24
|
+
const slot = (defaultSlots) => typeof defaultSlots == "function" ? defaultSlots() : defaultSlots;
|
|
25
|
+
var CodeMirror = defineComponent({
|
|
16
26
|
name: "CodeMirror",
|
|
17
27
|
model: {
|
|
18
28
|
prop: "modelValue",
|
|
@@ -72,8 +82,12 @@ const _sfc_main = defineComponent({
|
|
|
72
82
|
default: () => void 0
|
|
73
83
|
},
|
|
74
84
|
linter: {
|
|
75
|
-
type:
|
|
85
|
+
type: Function,
|
|
76
86
|
default: () => void 0
|
|
87
|
+
},
|
|
88
|
+
lintGutter: {
|
|
89
|
+
type: Boolean,
|
|
90
|
+
defalt: false
|
|
77
91
|
}
|
|
78
92
|
},
|
|
79
93
|
emits: ["update:modelValue", "update"],
|
|
@@ -82,26 +96,20 @@ const _sfc_main = defineComponent({
|
|
|
82
96
|
const doc = ref(props.modelValue);
|
|
83
97
|
const { dark } = toRefs(props);
|
|
84
98
|
const emit = context.emit;
|
|
85
|
-
const exts = computed(() =>
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
if (props.extensions) {
|
|
101
|
-
merge(ext, props.extensions);
|
|
102
|
-
}
|
|
103
|
-
return compact(ext);
|
|
104
|
-
});
|
|
99
|
+
const exts = computed(() => compact([
|
|
100
|
+
props.basic ? basicSetup : void 0,
|
|
101
|
+
EditorView.updateListener.of((update) => emit("update", update)),
|
|
102
|
+
EditorView.theme(props.theme, { dark: dark.value }),
|
|
103
|
+
props.wrap ? EditorView.lineWrapping : void 0,
|
|
104
|
+
props.tab ? keymap.of([indentWithTab]) : void 0,
|
|
105
|
+
props.phrases ? EditorState.phrases.of(props.phrases) : void 0,
|
|
106
|
+
props.readonly ? EditorState.readOnly.of(props.readonly) : void 0,
|
|
107
|
+
props.editable ? EditorView.editable.of(props.editable) : void 0,
|
|
108
|
+
props.lang ? toRaw(props.lang) : void 0,
|
|
109
|
+
props.linter ? linter(props.linter) : void 0,
|
|
110
|
+
props.linter && props.lintGutter ? lintGutter() : void 0,
|
|
111
|
+
...props.extensions
|
|
112
|
+
]));
|
|
105
113
|
let view;
|
|
106
114
|
watch(doc, (current) => {
|
|
107
115
|
if (view.composing) {
|
|
@@ -121,7 +129,10 @@ const _sfc_main = defineComponent({
|
|
|
121
129
|
}));
|
|
122
130
|
});
|
|
123
131
|
onMounted(async () => {
|
|
124
|
-
if (
|
|
132
|
+
if (editor.value && editor.value.childNodes[0]) {
|
|
133
|
+
if (doc.value !== "") {
|
|
134
|
+
console.warn("[CodeMirror.vue] The CodeMirror tag contains child elements that overwrite the model values.");
|
|
135
|
+
}
|
|
125
136
|
doc.value = trim(editor.value.childNodes[0].innerText);
|
|
126
137
|
}
|
|
127
138
|
view = new EditorView({
|
|
@@ -146,22 +157,14 @@ const _sfc_main = defineComponent({
|
|
|
146
157
|
context,
|
|
147
158
|
editor
|
|
148
159
|
};
|
|
160
|
+
},
|
|
161
|
+
render() {
|
|
162
|
+
return h("div", {
|
|
163
|
+
ref: "editor",
|
|
164
|
+
class: "vue-codemirror"
|
|
165
|
+
}, this.$slots.default ? h("aside", { style: "display: none;" }, slot(this.$slots.default)) : void 0);
|
|
149
166
|
}
|
|
150
167
|
});
|
|
151
|
-
const _hoisted_1 = {
|
|
152
|
-
ref: "editor",
|
|
153
|
-
class: "vue-codemirror"
|
|
154
|
-
};
|
|
155
|
-
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
|
|
156
|
-
return openBlock(), createElementBlock("div", _hoisted_1, [
|
|
157
|
-
withDirectives(createElementVNode("aside", null, [
|
|
158
|
-
renderSlot(_ctx.$slots, "default")
|
|
159
|
-
], 512), [
|
|
160
|
-
[vShow, !_ctx.context.slots.default]
|
|
161
|
-
])
|
|
162
|
-
], 512);
|
|
163
|
-
}
|
|
164
|
-
var CodeMirror = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
|
|
165
168
|
/**
|
|
166
169
|
* Vue CodeMirror6 Component
|
|
167
170
|
*
|
|
@@ -170,5 +173,5 @@ var CodeMirror = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]
|
|
|
170
173
|
* @copyright 2022 Masashi Yoshikawa {@link https://logue.dev/} All rights reserved.
|
|
171
174
|
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
172
175
|
*/
|
|
173
|
-
const installCodeMirror =
|
|
176
|
+
const installCodeMirror = (app) => app.component("CodeMirror", CodeMirror);
|
|
174
177
|
export { CodeMirror as default, installCodeMirror as install };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
(function(r,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("vue-demi"),require("@codemirror/state"),require("@codemirror/view"),require("@codemirror/lint"),require("@codemirror/basic-setup"),require("@codemirror/commands"),require("lodash")):typeof define=="function"&&define.amd?define(["exports","vue-demi","@codemirror/state","@codemirror/view","@codemirror/lint","@codemirror/basic-setup","@codemirror/commands","lodash"],t):(r=typeof globalThis<"u"?globalThis:r||self,t(r.CodeMirror={},r.VueDemi,r.state,r.view,r.lint,r.basicSetup,r.commands,r.lodash))})(this,function(r,t,s,n,m,y,b,h){"use strict";const V=e=>e?Object.entries(e).reduce((a,[o,i])=>(o=o.charAt(0).toUpperCase()+o.slice(1),o=`on${o}`,{...a,[o]:i}),{}):null,v=(e,a={},o)=>{if(t.isVue2)return t.h(e,a,o);const{props:i,domProps:f,on:u,...c}=a,d=u?V(u):{};return t.h(e,{...c,...i,...f,...d},o)},w=e=>typeof e=="function"?e():e;var p=t.defineComponent({name:"CodeMirror",model:{prop:"modelValue",event:"update:modelValue"},props:{modelValue:{type:String,default:""},selection:{type:Object,default:()=>{}},theme:{type:Object,default:()=>({})},dark:{type:Boolean,default:!1},basic:{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:()=>{}},lintGutter:{type:Boolean,defalt:!1}},emits:["update:modelValue","update"],setup(e,a){const o=t.ref(),i=t.ref(e.modelValue),{dark:f}=t.toRefs(e),u=a.emit,c=t.computed(()=>h.compact([e.basic?y.basicSetup:void 0,n.EditorView.updateListener.of(l=>u("update",l)),n.EditorView.theme(e.theme,{dark:f.value}),e.wrap?n.EditorView.lineWrapping:void 0,e.tab?n.keymap.of([b.indentWithTab]):void 0,e.phrases?s.EditorState.phrases.of(e.phrases):void 0,e.readonly?s.EditorState.readOnly.of(e.readonly):void 0,e.editable?n.EditorView.editable.of(e.editable):void 0,e.lang?t.toRaw(e.lang):void 0,e.linter?m.linter(e.linter):void 0,e.linter&&e.lintGutter?m.lintGutter():void 0,...e.extensions]));let d;return t.watch(i,l=>{if(d.composing)return;const E=d.state.selection;d.setState(s.EditorState.create({doc:l,extensions:c.value,selection:E}))}),t.watch(f,()=>{d.setState(s.EditorState.create({doc:i.value,extensions:c.value}))}),t.onMounted(async()=>{o.value&&o.value.childNodes[0]&&(i.value!==""&&console.warn("[CodeMirror.vue] The CodeMirror tag contains child elements that overwrite the model values."),i.value=h.trim(o.value.childNodes[0].innerText)),d=new n.EditorView({state:s.EditorState.create({doc:i.value,selection:e.selection,extensions:c.value}),parent:o.value,dispatch:l=>{d.update([l]),!l.changes.empty&&(i.value=d.state.doc.toString(),u("update:modelValue",i.value))}}),await t.nextTick()}),t.onUnmounted(()=>d.destroy()),{context:a,editor:o}},render(){return v("div",{ref:"editor",class:"vue-codemirror"},this.$slots.default?v("aside",{style:"display: none;"},w(this.$slots.default)):void 0)}});/**
|
|
2
|
+
* Vue CodeMirror6 Component
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @author Logue {@link logue@hotmail.co.jp}
|
|
6
|
+
* @copyright 2022 Masashi Yoshikawa {@link https://logue.dev/} All rights reserved.
|
|
7
|
+
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
8
|
+
*/const S=e=>e.component("CodeMirror",p);r.default=p,r.install=S,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "vue-codemirror6",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"description": "CodeMirror6 Component for vue3",
|
|
4
|
+
"version": "0.5.2",
|
|
5
|
+
"description": "CodeMirror6 Component for vue2 and vue3",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"vuejs",
|
|
8
8
|
"vue",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"vue-codemirror",
|
|
11
11
|
"code-editor",
|
|
12
12
|
"text-editor",
|
|
13
|
+
"vue2",
|
|
13
14
|
"vue3",
|
|
14
15
|
"web-editor",
|
|
15
16
|
"vue-plugin",
|
|
@@ -35,16 +36,17 @@
|
|
|
35
36
|
"node": ">=16.15.0",
|
|
36
37
|
"yarn": ">=1.22.4"
|
|
37
38
|
},
|
|
39
|
+
"packageManager": "yarn@3.2.1",
|
|
38
40
|
"license": "MIT",
|
|
39
|
-
"main": "./dist/
|
|
40
|
-
"module": "./dist/
|
|
41
|
+
"main": "./dist/index.umd.js",
|
|
42
|
+
"module": "./dist/index.es.js",
|
|
41
43
|
"browser": {
|
|
42
44
|
"./sfc": "./src/components/CodeMirror.vue"
|
|
43
45
|
},
|
|
44
46
|
"exports": {
|
|
45
47
|
".": {
|
|
46
|
-
"import": "./dist/
|
|
47
|
-
"require": "./dist/
|
|
48
|
+
"import": "./dist/index.es.js",
|
|
49
|
+
"require": "./dist/index.umd.js"
|
|
48
50
|
}
|
|
49
51
|
},
|
|
50
52
|
"types": "./dist/index.d.ts",
|
|
@@ -63,16 +65,18 @@
|
|
|
63
65
|
"lint": "eslint --fix --cache --cache-location ./node_modules/.vite/vite-plugin-eslint && prettier . -w -u",
|
|
64
66
|
"lint:markup": "vue-tsc --noEmit",
|
|
65
67
|
"preview": "vite preview --config vite.config.docs.ts",
|
|
66
|
-
"prepare": "husky install"
|
|
67
|
-
"postinstall": "npx vue-demi-fix"
|
|
68
|
+
"prepare": "husky install"
|
|
68
69
|
},
|
|
69
70
|
"dependencies": {
|
|
70
|
-
"
|
|
71
|
+
"@codemirror/basic-setup": "latest",
|
|
72
|
+
"@codemirror/commands": "latest",
|
|
73
|
+
"@codemirror/lint": "latest",
|
|
74
|
+
"@codemirror/state": "latest",
|
|
75
|
+
"@codemirror/view": "latest",
|
|
76
|
+
"vue-demi": "latest"
|
|
71
77
|
},
|
|
72
78
|
"peerDependencies": {
|
|
73
|
-
"@
|
|
74
|
-
"@codemirror/state": ">=0.20",
|
|
75
|
-
"@codemirror/view": ">=0.20",
|
|
79
|
+
"@vue/composition-api": "^1.6.2",
|
|
76
80
|
"vue": "^2.6.14 || ^3.2.36"
|
|
77
81
|
},
|
|
78
82
|
"peerDependenciesMeta": {
|
|
@@ -81,10 +85,10 @@
|
|
|
81
85
|
}
|
|
82
86
|
},
|
|
83
87
|
"devDependencies": {
|
|
84
|
-
"@codemirror/
|
|
88
|
+
"@codemirror/lang-javascript": "^0.20.0",
|
|
85
89
|
"@codemirror/lang-markdown": "^0.20.1",
|
|
86
90
|
"@types/lodash": "^4.14.182",
|
|
87
|
-
"@types/node": "^17.0.
|
|
91
|
+
"@types/node": "^17.0.36",
|
|
88
92
|
"@typescript-eslint/eslint-plugin": "^5.26.0",
|
|
89
93
|
"@typescript-eslint/parser": "^5.26.0",
|
|
90
94
|
"@vitejs/plugin-vue": "^2.3.3",
|
|
@@ -99,13 +103,14 @@
|
|
|
99
103
|
"eslint-plugin-tsdoc": "^0.2.16",
|
|
100
104
|
"eslint-plugin-vue": "^9.0.1",
|
|
101
105
|
"eslint-plugin-vuejs-accessibility": "^1.1.1",
|
|
106
|
+
"eslint4b-prebuilt": "^6.7.2",
|
|
102
107
|
"husky": "^8.0.1",
|
|
103
108
|
"lint-staged": "^12.4.2",
|
|
104
109
|
"lodash": "^4.17.21",
|
|
105
110
|
"prettier": "^2.6.2",
|
|
106
111
|
"prettier-plugin-jsdoc": "^0.3.38",
|
|
107
112
|
"prettier-plugin-md-nocjsp": "^1.2.0",
|
|
108
|
-
"prettier-plugin-sort-imports": "^1.
|
|
113
|
+
"prettier-plugin-sort-imports": "^1.7.0",
|
|
109
114
|
"rimraf": "^3.0.2",
|
|
110
115
|
"rollup-plugin-visualizer": "^5.6.0",
|
|
111
116
|
"typescript": "^4.7.2",
|
|
@@ -113,7 +118,7 @@
|
|
|
113
118
|
"vite-plugin-checker": "^0.4.6",
|
|
114
119
|
"vue": "^3.2.36",
|
|
115
120
|
"vue-eslint-parser": "^9.0.2",
|
|
116
|
-
"vue-tsc": "^0.
|
|
121
|
+
"vue-tsc": "^0.35.0"
|
|
117
122
|
},
|
|
118
123
|
"husky": {
|
|
119
124
|
"hooks": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CodeMirror.vue.d.ts","sourceRoot":"","sources":["../../src/components/CodeMirror.vue.ts"],"names":[],"mappings":"AACA,OAAO,EAWL,KAAK,QAAQ,EACb,KAAK,GAAG,EACR,KAAK,YAAY,EAClB,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,eAAe,EAAe,MAAM,mBAAmB,CAAC;AASjE,OAAO,KAAK,EAAE,SAAS,EAAE,IAAI,EAAe,MAAM,mBAAmB,CAAC;AACtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,2BAA2B;;IAWvB,kBAAkB;;;;;IAKlB;;;;OAIG;;;oBAG6B,MAAM;;;;;IAItC;;;;OAIG;;;;;;;IAOH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAOH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;;;;;IA9GH,kBAAkB;;;;;IAKlB;;;;OAIG;;;oBAG6B,MAAM;;;;;IAItC;;;;OAIG;;;;;;;IAOH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAOH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;;;;;gBAlG6B,MAAM;;;;;;;;;;;;;;;;AAtB1C,wBAgQG"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
(function(o,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("vue-demi"),require("@codemirror/state"),require("@codemirror/view"),require("@codemirror/basic-setup"),require("@codemirror/commands"),require("lodash"),require("vue")):typeof define=="function"&&define.amd?define(["exports","vue-demi","@codemirror/state","@codemirror/view","@codemirror/basic-setup","@codemirror/commands","lodash","vue"],t):(o=typeof globalThis<"u"?globalThis:o||self,t(o.CodeMirror={},o.VueDemi,o.state,o.view,o.basicSetup,o.commands,o.lodash,o.Vue))})(this,function(o,t,s,n,h,S,f,l){"use strict";var V=(e,c)=>{const a=e.__vccOpts||e;for(const[r,u]of c)a[r]=u;return a};const b=t.defineComponent({name:"CodeMirror",model:{prop:"modelValue",event:"update:modelValue"},props:{modelValue:{type:String,default:""},selection:{type:Object,default:()=>{}},theme:{type:Object,default:()=>({})},dark:{type:Boolean,default:!1},basic:{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:Array,default:()=>{}}},emits:["update:modelValue","update"],setup(e,c){const a=t.ref(),r=t.ref(e.modelValue),{dark:u}=t.toRefs(e),m=c.emit,y=t.computed(()=>{const i=[e.basic?h.basicSetup:void 0,n.EditorView.updateListener.of(p=>m("update",p)),n.EditorView.theme(e.theme,{dark:u.value}),e.wrap?n.EditorView.lineWrapping:void 0,e.tab?n.keymap.of([S.indentWithTab]):void 0,e.phrases?s.EditorState.phrases.of(e.phrases):void 0,e.readonly?s.EditorState.readOnly.of(e.readonly):void 0,e.editable?n.EditorView.editable.of(e.editable):void 0,e.lang?t.toRaw(e.lang):void 0];return e.linter&&f.merge(i,e.linter),e.extensions&&f.merge(i,e.extensions),f.compact(i)});let d;return t.watch(r,i=>{if(d.composing)return;const p=d.state.selection;d.setState(s.EditorState.create({doc:i,extensions:y.value,selection:p}))}),t.watch(u,()=>{d.setState(s.EditorState.create({doc:r.value,extensions:y.value}))}),t.onMounted(async()=>{r.value==""&&a.value&&(r.value=f.trim(a.value.childNodes[0].innerText)),d=new n.EditorView({state:s.EditorState.create({doc:r.value,selection:e.selection,extensions:y.value}),parent:a.value,dispatch:i=>{d.update([i]),!i.changes.empty&&(r.value=d.state.doc.toString(),m("update:modelValue",r.value))}}),await t.nextTick()}),t.onUnmounted(()=>d.destroy()),{context:c,editor:a}}}),w={ref:"editor",class:"vue-codemirror"};function E(e,c,a,r,u,m){return l.openBlock(),l.createElementBlock("div",w,[l.withDirectives(l.createElementVNode("aside",null,[l.renderSlot(e.$slots,"default")],512),[[l.vShow,!e.context.slots.default]])],512)}var v=V(b,[["render",E]]);/**
|
|
2
|
-
* Vue CodeMirror6 Component
|
|
3
|
-
*
|
|
4
|
-
* @license MIT
|
|
5
|
-
* @author Logue {@link logue@hotmail.co.jp}
|
|
6
|
-
* @copyright 2022 Masashi Yoshikawa {@link https://logue.dev/} All rights reserved.
|
|
7
|
-
* @see {@link https://github.com/logue/vue-codemirror6}
|
|
8
|
-
*/const _=t.isVue2?e=>e.component("CodeMirror",v):t.install();o.default=v,o.install=_,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
@@ -1,297 +0,0 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<div ref="editor" class="vue-codemirror">
|
|
3
|
-
<aside v-show="!context.slots.default"><slot /></aside>
|
|
4
|
-
</div>
|
|
5
|
-
</template>
|
|
6
|
-
|
|
7
|
-
<script lang="ts">
|
|
8
|
-
import {
|
|
9
|
-
computed,
|
|
10
|
-
defineComponent,
|
|
11
|
-
nextTick,
|
|
12
|
-
onMounted,
|
|
13
|
-
onUnmounted,
|
|
14
|
-
ref,
|
|
15
|
-
toRaw,
|
|
16
|
-
toRefs,
|
|
17
|
-
watch,
|
|
18
|
-
type ComputedRef,
|
|
19
|
-
type PropType,
|
|
20
|
-
type Ref,
|
|
21
|
-
type SetupContext,
|
|
22
|
-
} from 'vue-demi';
|
|
23
|
-
|
|
24
|
-
import { EditorSelection, EditorState } from '@codemirror/state';
|
|
25
|
-
import { EditorView, keymap } from '@codemirror/view';
|
|
26
|
-
import { basicSetup } from '@codemirror/basic-setup';
|
|
27
|
-
import { indentWithTab } from '@codemirror/commands';
|
|
28
|
-
|
|
29
|
-
import { compact, merge, trim } from 'lodash';
|
|
30
|
-
|
|
31
|
-
import type CodeMirrorEmitsInterface from '@/interfaces/CodeMirrorEmitsInterface';
|
|
32
|
-
|
|
33
|
-
import type { Extension, Text, Transaction } from '@codemirror/state';
|
|
34
|
-
import type { LanguageSupport } from '@codemirror/language';
|
|
35
|
-
import type { ViewUpdate } from '@codemirror/view';
|
|
36
|
-
import type { Diagnostic } from '@codemirror/lint';
|
|
37
|
-
import type { StyleSpec } from 'style-mod';
|
|
38
|
-
|
|
39
|
-
/** CodeMirror Component */
|
|
40
|
-
export default defineComponent({
|
|
41
|
-
/** Component Name */
|
|
42
|
-
name: 'CodeMirror',
|
|
43
|
-
/** Model Definition */
|
|
44
|
-
model: {
|
|
45
|
-
prop: 'modelValue',
|
|
46
|
-
event: 'update:modelValue',
|
|
47
|
-
},
|
|
48
|
-
/** Props Definition */
|
|
49
|
-
props: {
|
|
50
|
-
/** Model value */
|
|
51
|
-
modelValue: {
|
|
52
|
-
type: String as PropType<string | Text>,
|
|
53
|
-
default: '',
|
|
54
|
-
},
|
|
55
|
-
/**
|
|
56
|
-
* Selection
|
|
57
|
-
*
|
|
58
|
-
* @see {@link https://codemirror.net/6/docs/ref/#state.EditorSelection | EditorSelection}
|
|
59
|
-
*/
|
|
60
|
-
selection: {
|
|
61
|
-
type: Object as PropType<
|
|
62
|
-
EditorSelection | { anchor: number; head?: number }
|
|
63
|
-
>,
|
|
64
|
-
default: () => undefined,
|
|
65
|
-
},
|
|
66
|
-
/**
|
|
67
|
-
* Theme
|
|
68
|
-
*
|
|
69
|
-
* @see {@link https://codemirror.net/6/examples/styling/ | Example: Styling}
|
|
70
|
-
*/
|
|
71
|
-
theme: {
|
|
72
|
-
type: Object as PropType<{ [selector: string]: StyleSpec }>,
|
|
73
|
-
default: () => {
|
|
74
|
-
return {};
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
/** Dark Mode */
|
|
78
|
-
dark: {
|
|
79
|
-
type: Boolean,
|
|
80
|
-
default: false,
|
|
81
|
-
},
|
|
82
|
-
/**
|
|
83
|
-
* Use Basic Setup
|
|
84
|
-
*
|
|
85
|
-
* @see {@link https://codemirror.net/6/docs/ref/#basic-setup | basic-setup}
|
|
86
|
-
*/
|
|
87
|
-
basic: {
|
|
88
|
-
type: Boolean,
|
|
89
|
-
default: false,
|
|
90
|
-
},
|
|
91
|
-
/**
|
|
92
|
-
* Line wrapping
|
|
93
|
-
*
|
|
94
|
-
* @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5ElineWrapping | LineWrapping}
|
|
95
|
-
*/
|
|
96
|
-
wrap: {
|
|
97
|
-
type: Boolean,
|
|
98
|
-
default: false,
|
|
99
|
-
},
|
|
100
|
-
/**
|
|
101
|
-
* Tab handling
|
|
102
|
-
*
|
|
103
|
-
* @see {@link https://codemirror.net/6/examples/tab/ | Tab Handling}
|
|
104
|
-
*/
|
|
105
|
-
tab: {
|
|
106
|
-
type: Boolean,
|
|
107
|
-
default: false,
|
|
108
|
-
},
|
|
109
|
-
/**
|
|
110
|
-
* Readonly
|
|
111
|
-
*
|
|
112
|
-
* @see {@link https://codemirror.net/6/docs/ref/#state.EditorState%5EreadOnly | readOnly}
|
|
113
|
-
*/
|
|
114
|
-
readonly: {
|
|
115
|
-
type: Boolean,
|
|
116
|
-
default: false,
|
|
117
|
-
},
|
|
118
|
-
/**
|
|
119
|
-
* Editable
|
|
120
|
-
*
|
|
121
|
-
* @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5Eeditable | editable}
|
|
122
|
-
*/
|
|
123
|
-
editable: {
|
|
124
|
-
type: Boolean,
|
|
125
|
-
default: true,
|
|
126
|
-
},
|
|
127
|
-
/**
|
|
128
|
-
* Additional Extension
|
|
129
|
-
*
|
|
130
|
-
* @see {@link https://codemirror.net/6/docs/ref/#state.Extension | Extending Editor State}
|
|
131
|
-
*/
|
|
132
|
-
extensions: {
|
|
133
|
-
type: Array as PropType<Extension[]>,
|
|
134
|
-
default: () => {
|
|
135
|
-
return [];
|
|
136
|
-
},
|
|
137
|
-
},
|
|
138
|
-
/**
|
|
139
|
-
* Language Phreses
|
|
140
|
-
*
|
|
141
|
-
* @see {@link https://codemirror.net/6/examples/translate/ | Example: Internationalization}
|
|
142
|
-
*/
|
|
143
|
-
phrases: {
|
|
144
|
-
type: Object as PropType<Record<string, string>>,
|
|
145
|
-
default: () => undefined,
|
|
146
|
-
},
|
|
147
|
-
/**
|
|
148
|
-
* CodeMirror Language
|
|
149
|
-
*
|
|
150
|
-
* @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
|
|
151
|
-
*/
|
|
152
|
-
lang: {
|
|
153
|
-
type: Object as PropType<LanguageSupport>,
|
|
154
|
-
default: () => undefined,
|
|
155
|
-
},
|
|
156
|
-
/**
|
|
157
|
-
* CodeMirror Linter
|
|
158
|
-
*
|
|
159
|
-
* @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
|
|
160
|
-
*/
|
|
161
|
-
linter: {
|
|
162
|
-
type: Array as PropType<Diagnostic[]>,
|
|
163
|
-
default: () => undefined,
|
|
164
|
-
},
|
|
165
|
-
},
|
|
166
|
-
/** Emits */
|
|
167
|
-
emits: ['update:modelValue', 'update'],
|
|
168
|
-
/**
|
|
169
|
-
* Setup
|
|
170
|
-
*
|
|
171
|
-
* @param props - Props
|
|
172
|
-
* @param context - Context
|
|
173
|
-
*/
|
|
174
|
-
setup(props, context: SetupContext) {
|
|
175
|
-
/** Editor DOM */
|
|
176
|
-
const editor: Ref<Element | undefined> = ref();
|
|
177
|
-
|
|
178
|
-
/** Internal value */
|
|
179
|
-
const doc: Ref<string | Text | undefined> = ref(props.modelValue);
|
|
180
|
-
|
|
181
|
-
/** Dark mode */
|
|
182
|
-
const { dark } = toRefs(props);
|
|
183
|
-
|
|
184
|
-
/** Emits */
|
|
185
|
-
const emit = context.emit as CodeMirrorEmitsInterface;
|
|
186
|
-
|
|
187
|
-
/** CodeMirror Extension */
|
|
188
|
-
const exts: ComputedRef<Extension[]> = computed(() => {
|
|
189
|
-
/** Default extension */
|
|
190
|
-
const ext = [
|
|
191
|
-
// Toggle basic setup
|
|
192
|
-
props.basic ? basicSetup : undefined,
|
|
193
|
-
// ViewUpdate event listener
|
|
194
|
-
EditorView.updateListener.of((update: ViewUpdate) =>
|
|
195
|
-
emit('update', update)
|
|
196
|
-
),
|
|
197
|
-
// Toggle light/dark mode.
|
|
198
|
-
EditorView.theme(props.theme, { dark: dark.value }),
|
|
199
|
-
// Toggle line wrapping
|
|
200
|
-
props.wrap ? EditorView.lineWrapping : undefined,
|
|
201
|
-
// Indent with tab
|
|
202
|
-
props.tab ? keymap.of([indentWithTab]) : undefined,
|
|
203
|
-
// locale settings
|
|
204
|
-
props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
|
|
205
|
-
// Readonly option
|
|
206
|
-
props.readonly ? EditorState.readOnly.of(props.readonly) : undefined,
|
|
207
|
-
// Editable option
|
|
208
|
-
props.editable ? EditorView.editable.of(props.editable) : undefined,
|
|
209
|
-
// Lang
|
|
210
|
-
props.lang ? toRaw(props.lang) : undefined,
|
|
211
|
-
];
|
|
212
|
-
|
|
213
|
-
if (props.linter) {
|
|
214
|
-
// Append Linter settings
|
|
215
|
-
merge(ext, props.linter);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if (props.extensions) {
|
|
219
|
-
// Append Extensions (such as basic-setup)
|
|
220
|
-
merge(ext, props.extensions);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// console.debug('[CodeMirror.vue] Loaded extensions:', compact(ext));
|
|
224
|
-
return compact(ext);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
/** CodeMirror Editor View */
|
|
228
|
-
let view: EditorView;
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Input value changed
|
|
232
|
-
*
|
|
233
|
-
* @see {@link https://codemirror.net/6/docs/migration/#making-changes | Making Changes}
|
|
234
|
-
*/
|
|
235
|
-
watch(doc, current => {
|
|
236
|
-
if (view.composing) {
|
|
237
|
-
// IME fix
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/** Previous cursor location */
|
|
242
|
-
const previous = view.state.selection;
|
|
243
|
-
view.setState(
|
|
244
|
-
EditorState.create({
|
|
245
|
-
doc: current,
|
|
246
|
-
extensions: exts.value,
|
|
247
|
-
selection: previous,
|
|
248
|
-
})
|
|
249
|
-
);
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
watch(dark, () => {
|
|
253
|
-
view.setState(
|
|
254
|
-
EditorState.create({
|
|
255
|
-
doc: doc.value,
|
|
256
|
-
extensions: exts.value,
|
|
257
|
-
})
|
|
258
|
-
);
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
/** When loaded */
|
|
262
|
-
onMounted(async () => {
|
|
263
|
-
// overwrite initial value
|
|
264
|
-
if (doc.value == '' && editor.value) {
|
|
265
|
-
doc.value = trim((editor.value.childNodes[0] as HTMLElement).innerText);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// Register Codemirror
|
|
269
|
-
view = new EditorView({
|
|
270
|
-
state: EditorState.create({
|
|
271
|
-
doc: doc.value,
|
|
272
|
-
selection: props.selection,
|
|
273
|
-
extensions: exts.value,
|
|
274
|
-
}),
|
|
275
|
-
parent: editor.value,
|
|
276
|
-
dispatch: (tr: Transaction) => {
|
|
277
|
-
view.update([tr]);
|
|
278
|
-
|
|
279
|
-
if (tr.changes.empty) return;
|
|
280
|
-
// to parent binding
|
|
281
|
-
doc.value = view.state.doc.toString();
|
|
282
|
-
emit('update:modelValue', doc.value);
|
|
283
|
-
},
|
|
284
|
-
});
|
|
285
|
-
await nextTick();
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
/** Destroy */
|
|
289
|
-
onUnmounted(() => view.destroy());
|
|
290
|
-
|
|
291
|
-
return {
|
|
292
|
-
context,
|
|
293
|
-
editor,
|
|
294
|
-
};
|
|
295
|
-
},
|
|
296
|
-
});
|
|
297
|
-
</script>
|