vue-codemirror6 0.3.5 → 0.5.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 CHANGED
@@ -8,24 +8,34 @@
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
27
  | Props | Type | Information |
21
28
  | ---------- | --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
22
- | dark | boolean | Toggle Darkmode. If you use Vuetify, I recommend that you enter `$vuetify.theme.dark`. |
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. |
23
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. |
24
34
  | readonly | boolean | Makes the cursor visible or you can drag the text but not edit the value. |
25
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. |
26
36
  | lang | LanguageSupport | The language you want to have syntax highlighting. see <https://codemirror.net/6/#languages> |
27
37
  | phrases | Record&lt;string, string&gt; | Specify here if you want to make the displayed character string multilingual. see <https://codemirror.net/6/examples/translate/> |
28
- | extensions | Extension[] | Includes enhancements to extend CodeMirror. Such as [@codemirror/basic-setup](https://github.com/codemirror/basic-setup). |
38
+ | extensions | Extension[] | Includes enhancements to extend CodeMirror. |
29
39
  | linter | Diagnostic[] | Set Linter. see example <https://codesandbox.io/s/f6nb0?file=/src/index.js> |
30
40
 
31
41
  Notice: `lang` and `linter` can also be set together in `extensions`. This is defined for usability compatibility with past CodeMirrors.
@@ -121,23 +131,22 @@ When using as a Markdown editor on [Vuetify](https://vuetifyjs.com/).
121
131
  <template>
122
132
  <code-mirror
123
133
  v-model="value"
134
+ basic
135
+ :dark="dark"
124
136
  :lang="lang"
125
137
  :phrases="phreses"
126
- :extensions="extensions"
127
- :dark="$vuetify.theme.dark"
128
138
  />
129
139
  </template>
130
140
 
131
141
  <script lang="ts">
132
- import { ref, type Ref, defineComponent } from 'vue';
142
+ import { ref, defineComponent, type Ref } from 'vue';
133
143
 
134
144
  // Load component
135
145
  import CodeMirror from 'vue-codemirror6';
136
146
 
137
147
  // CodeMirror extensions
138
148
  import type { LanguageSupport } from '@codemirror/language';
139
- import { markdown } from '@codemirror/lang-markdown';
140
- import { basicSetup } from '@codemirror/basic-setup';
149
+ import { markdown as md } from '@codemirror/lang-markdown';
141
150
  import type { Extension } from '@codemirror/state';
142
151
  import type { ViewUpdate } from '@codemirror/view';
143
152
 
@@ -146,6 +155,12 @@ export default defineComponent({
146
155
  CodeMirror,
147
156
  },
148
157
  setup() {
158
+ /**
159
+ * Get Vuetify instance
160
+ * @see {@link https://github.com/logue/vite-vue2-vuetify-ts-starter | vite-vue2-vuetify-ts-starter}
161
+ */
162
+ const vuetify = useVuetify();
163
+
149
164
  /** text */
150
165
  const value: Ref<string> = ref('');
151
166
 
@@ -154,7 +169,13 @@ export default defineComponent({
154
169
  *
155
170
  * @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
156
171
  */
157
- const lang: Ref<LanguageSupport> = ref(markdown());
172
+ const lang: Ref<LanguageSupport> = ref(md());
173
+
174
+ /** Dark mode **/
175
+ const dark = ref(window.matchMedia('(prefers-color-scheme: dark)').matches);
176
+
177
+ /** When dark value changed, sync vuetify's dark mode */
178
+ watch(dark, v => (vuetify.theme.dark = v));
158
179
 
159
180
  /**
160
181
  * Internationalization Config.
@@ -193,21 +214,11 @@ export default defineComponent({
193
214
  'No diagnostics': 'エラーなし',
194
215
  });
195
216
 
196
- /**
197
- * CodeMirror Extensions
198
- *
199
- * @see {@link:https://codemirror.net/6/docs/ref/#state.Extension | Extending Editor State}
200
- */
201
- const extensions: Ref<Extension[]> = ref([
202
- /** @see {@link:https://codemirror.net/6/docs/ref/#basic-setup | basic-setup} */
203
- basicSetup,
204
- ]);
205
-
206
217
  return {
218
+ dark,
207
219
  value,
208
220
  lang,
209
221
  phrases,
210
- extensions,
211
222
  };
212
223
  },
213
224
  });
@@ -231,8 +242,8 @@ const config: UserConfig = {
231
242
  'vue-codemirror6',
232
243
  '@codemirror/state',
233
244
  '@codemirror/view',
234
- // Add the following as needed.
235
245
  '@codemirror/basic-setup',
246
+ // Add the following as needed.
236
247
  '@codemirror/lang-html',
237
248
  '@codemirror/lang-javascript',
238
249
  '@codemirror/lang-markdown',
@@ -1,14 +1,28 @@
1
- import { type Ref, type PropType, type SetupContext } from 'vue-demi';
2
- import type { Extension } from '@codemirror/state';
1
+ import { type PropType, type Ref, type SetupContext } from 'vue-demi';
2
+ import { EditorSelection } from '@codemirror/state';
3
+ import type { Extension, Text } from '@codemirror/state';
3
4
  import type { LanguageSupport } from '@codemirror/language';
4
5
  import type { Diagnostic } from '@codemirror/lint';
5
6
  import type { StyleSpec } from 'style-mod';
7
+ /** CodeMirror Component */
6
8
  declare const _default: import("vue-demi").DefineComponent<{
7
9
  /** Model value */
8
10
  modelValue: {
9
- type: StringConstructor;
11
+ type: PropType<string | Text>;
10
12
  default: string;
11
13
  };
14
+ /**
15
+ * Selection
16
+ *
17
+ * @see {@link https://codemirror.net/6/docs/ref/#state.EditorSelection | EditorSelection}
18
+ */
19
+ selection: {
20
+ type: PropType<EditorSelection | {
21
+ anchor: number;
22
+ head?: number | undefined;
23
+ }>;
24
+ default: () => undefined;
25
+ };
12
26
  /**
13
27
  * Theme
14
28
  *
@@ -18,13 +32,40 @@ declare const _default: import("vue-demi").DefineComponent<{
18
32
  type: PropType<{
19
33
  [selector: string]: StyleSpec;
20
34
  }>;
21
- default: () => void;
35
+ default: () => {};
22
36
  };
23
37
  /** Dark Mode */
24
38
  dark: {
25
39
  type: BooleanConstructor;
26
40
  default: boolean;
27
41
  };
42
+ /**
43
+ * Use Basic Setup
44
+ *
45
+ * @see {@link https://codemirror.net/6/docs/ref/#basic-setup | basic-setup}
46
+ */
47
+ basic: {
48
+ type: BooleanConstructor;
49
+ default: boolean;
50
+ };
51
+ /**
52
+ * Line wrapping
53
+ *
54
+ * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5ElineWrapping | LineWrapping}
55
+ */
56
+ wrap: {
57
+ type: BooleanConstructor;
58
+ default: boolean;
59
+ };
60
+ /**
61
+ * Tab handling
62
+ *
63
+ * @see {@link https://codemirror.net/6/examples/tab/ | Tab Handling}
64
+ */
65
+ tab: {
66
+ type: BooleanConstructor;
67
+ default: boolean;
68
+ };
28
69
  /**
29
70
  * Readonly
30
71
  *
@@ -50,7 +91,7 @@ declare const _default: import("vue-demi").DefineComponent<{
50
91
  */
51
92
  extensions: {
52
93
  type: PropType<Extension[]>;
53
- default: undefined;
94
+ default: () => never[];
54
95
  };
55
96
  /**
56
97
  * Language Phreses
@@ -59,7 +100,7 @@ declare const _default: import("vue-demi").DefineComponent<{
59
100
  */
60
101
  phrases: {
61
102
  type: PropType<Record<string, string>>;
62
- default: undefined;
103
+ default: () => undefined;
63
104
  };
64
105
  /**
65
106
  * CodeMirror Language
@@ -68,7 +109,7 @@ declare const _default: import("vue-demi").DefineComponent<{
68
109
  */
69
110
  lang: {
70
111
  type: PropType<LanguageSupport>;
71
- default: undefined;
112
+ default: () => undefined;
72
113
  };
73
114
  /**
74
115
  * CodeMirror Linter
@@ -77,7 +118,7 @@ declare const _default: import("vue-demi").DefineComponent<{
77
118
  */
78
119
  linter: {
79
120
  type: PropType<Diagnostic[]>;
80
- default: undefined;
121
+ default: () => undefined;
81
122
  };
82
123
  }, {
83
124
  context: SetupContext<import("vue-demi").EmitsOptions>;
@@ -85,9 +126,21 @@ declare const _default: import("vue-demi").DefineComponent<{
85
126
  }, unknown, {}, {}, import("vue-demi").ComponentOptionsMixin, import("vue-demi").ComponentOptionsMixin, import("vue-demi").EmitsOptions, "update:modelValue" | "update", import("vue-demi").VNodeProps & import("vue-demi").AllowedComponentProps & import("vue-demi").ComponentCustomProps, Readonly<import("vue-demi").ExtractPropTypes<{
86
127
  /** Model value */
87
128
  modelValue: {
88
- type: StringConstructor;
129
+ type: PropType<string | Text>;
89
130
  default: string;
90
131
  };
132
+ /**
133
+ * Selection
134
+ *
135
+ * @see {@link https://codemirror.net/6/docs/ref/#state.EditorSelection | EditorSelection}
136
+ */
137
+ selection: {
138
+ type: PropType<EditorSelection | {
139
+ anchor: number;
140
+ head?: number | undefined;
141
+ }>;
142
+ default: () => undefined;
143
+ };
91
144
  /**
92
145
  * Theme
93
146
  *
@@ -97,13 +150,40 @@ declare const _default: import("vue-demi").DefineComponent<{
97
150
  type: PropType<{
98
151
  [selector: string]: StyleSpec;
99
152
  }>;
100
- default: () => void;
153
+ default: () => {};
101
154
  };
102
155
  /** Dark Mode */
103
156
  dark: {
104
157
  type: BooleanConstructor;
105
158
  default: boolean;
106
159
  };
160
+ /**
161
+ * Use Basic Setup
162
+ *
163
+ * @see {@link https://codemirror.net/6/docs/ref/#basic-setup | basic-setup}
164
+ */
165
+ basic: {
166
+ type: BooleanConstructor;
167
+ default: boolean;
168
+ };
169
+ /**
170
+ * Line wrapping
171
+ *
172
+ * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5ElineWrapping | LineWrapping}
173
+ */
174
+ wrap: {
175
+ type: BooleanConstructor;
176
+ default: boolean;
177
+ };
178
+ /**
179
+ * Tab handling
180
+ *
181
+ * @see {@link https://codemirror.net/6/examples/tab/ | Tab Handling}
182
+ */
183
+ tab: {
184
+ type: BooleanConstructor;
185
+ default: boolean;
186
+ };
107
187
  /**
108
188
  * Readonly
109
189
  *
@@ -129,7 +209,7 @@ declare const _default: import("vue-demi").DefineComponent<{
129
209
  */
130
210
  extensions: {
131
211
  type: PropType<Extension[]>;
132
- default: undefined;
212
+ default: () => never[];
133
213
  };
134
214
  /**
135
215
  * Language Phreses
@@ -138,7 +218,7 @@ declare const _default: import("vue-demi").DefineComponent<{
138
218
  */
139
219
  phrases: {
140
220
  type: PropType<Record<string, string>>;
141
- default: undefined;
221
+ default: () => undefined;
142
222
  };
143
223
  /**
144
224
  * CodeMirror Language
@@ -147,7 +227,7 @@ declare const _default: import("vue-demi").DefineComponent<{
147
227
  */
148
228
  lang: {
149
229
  type: PropType<LanguageSupport>;
150
- default: undefined;
230
+ default: () => undefined;
151
231
  };
152
232
  /**
153
233
  * CodeMirror Linter
@@ -156,21 +236,27 @@ declare const _default: import("vue-demi").DefineComponent<{
156
236
  */
157
237
  linter: {
158
238
  type: PropType<Diagnostic[]>;
159
- default: undefined;
239
+ default: () => undefined;
160
240
  };
161
241
  }>>, {
162
- readonly: boolean;
163
- modelValue: string;
242
+ modelValue: string | Text;
243
+ selection: EditorSelection | {
244
+ anchor: number;
245
+ head?: number | undefined;
246
+ };
164
247
  theme: {
165
248
  [selector: string]: StyleSpec;
166
249
  };
167
250
  dark: boolean;
251
+ basic: boolean;
252
+ wrap: boolean;
253
+ tab: boolean;
254
+ readonly: boolean;
168
255
  editable: boolean;
169
256
  extensions: Extension[];
170
257
  phrases: Record<string, string>;
171
258
  lang: LanguageSupport;
172
259
  linter: Diagnostic[];
173
260
  }>;
174
- /** CodeMirror Component */
175
261
  export default _default;
176
- //# sourceMappingURL=CodeMirror.vue.d.ts.map
262
+ //# 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;AAUjE,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,wBAiRG"}
@@ -0,0 +1,14 @@
1
+ interface Options {
2
+ domProps?: Record<any, any>;
3
+ on?: Record<any, any>;
4
+ props?: Record<any, any>;
5
+ ref?: string;
6
+ class?: 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,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;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAWD,QAAA,MAAM,CAAC,SACC,MAAM,GAAG,OAAO,GAAG,EAAE,GAAG,CAAC,YACtB,OAAO,YACN,GAAG;;EASd,CAAC;AAEF,QAAA,MAAM,IAAI,iBAAkB,GAAG,QAG9B,CAAC;AACF,OAAO,EAAE,IAAI,EAAE,CAAC,IAAI,OAAO,EAAE,CAAC"}
package/dist/index.d.ts CHANGED
@@ -6,9 +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.vue';
10
- import { type App } from 'vue-demi';
11
- declare const installCodeMirror: (app: App) => void;
12
- export { CodeMirror, installCodeMirror as install };
13
- export default CodeMirror;
9
+ import CodeMirror from './components/CodeMirror';
10
+ declare const installCodeMirror: (app: any) => any;
11
+ export { CodeMirror as default, installCodeMirror as install };
14
12
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,UAAU,MAAM,6BAA6B,CAAC;AACrD,OAAO,EAAW,KAAK,GAAG,EAAE,MAAM,UAAU,CAAC;AAI7C,QAAA,MAAM,iBAAiB,QAAS,GAAG,SAElC,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,iBAAiB,IAAI,OAAO,EAAE,CAAC;AACpD,eAAe,UAAU,CAAC"}
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"}
@@ -0,0 +1,180 @@
1
+ import { isVue2, h as h$1, defineComponent, ref, toRefs, computed, toRaw, watch, onMounted, nextTick, onUnmounted } from "vue-demi";
2
+ import { EditorState } from "@codemirror/state";
3
+ import { EditorView, keymap } from "@codemirror/view";
4
+ import { basicSetup } from "@codemirror/basic-setup";
5
+ import { indentWithTab } from "@codemirror/commands";
6
+ import { merge, compact, trim } from "lodash";
7
+ const adaptOnsV3 = (ons) => {
8
+ if (!ons)
9
+ return null;
10
+ return Object.entries(ons).reduce((ret, [key, handler]) => {
11
+ key = key.charAt(0).toUpperCase() + key.slice(1);
12
+ key = `on${key}`;
13
+ return { ...ret, [key]: handler };
14
+ }, {});
15
+ };
16
+ const h = (type, options = {}, chidren) => {
17
+ if (isVue2)
18
+ return h$1(type, options, chidren);
19
+ const { props, domProps, on, ...extraOptions } = options;
20
+ const ons = on ? adaptOnsV3(on) : {};
21
+ const params = { ...extraOptions, ...props, ...domProps, ...ons };
22
+ return h$1(type, params, chidren);
23
+ };
24
+ const slot = (defaultSlots) => {
25
+ if (typeof defaultSlots == "function")
26
+ return defaultSlots();
27
+ return defaultSlots;
28
+ };
29
+ var CodeMirror = defineComponent({
30
+ name: "CodeMirror",
31
+ model: {
32
+ prop: "modelValue",
33
+ event: "update:modelValue"
34
+ },
35
+ props: {
36
+ modelValue: {
37
+ type: String,
38
+ default: ""
39
+ },
40
+ selection: {
41
+ type: Object,
42
+ default: () => void 0
43
+ },
44
+ theme: {
45
+ type: Object,
46
+ default: () => {
47
+ return {};
48
+ }
49
+ },
50
+ dark: {
51
+ type: Boolean,
52
+ default: false
53
+ },
54
+ basic: {
55
+ type: Boolean,
56
+ default: false
57
+ },
58
+ wrap: {
59
+ type: Boolean,
60
+ default: false
61
+ },
62
+ tab: {
63
+ type: Boolean,
64
+ default: false
65
+ },
66
+ readonly: {
67
+ type: Boolean,
68
+ default: false
69
+ },
70
+ editable: {
71
+ type: Boolean,
72
+ default: true
73
+ },
74
+ extensions: {
75
+ type: Array,
76
+ default: () => {
77
+ return [];
78
+ }
79
+ },
80
+ phrases: {
81
+ type: Object,
82
+ default: () => void 0
83
+ },
84
+ lang: {
85
+ type: Object,
86
+ default: () => void 0
87
+ },
88
+ linter: {
89
+ type: Array,
90
+ default: () => void 0
91
+ }
92
+ },
93
+ emits: ["update:modelValue", "update"],
94
+ setup(props, context) {
95
+ const editor = ref();
96
+ const doc = ref(props.modelValue);
97
+ const { dark } = toRefs(props);
98
+ const emit = context.emit;
99
+ const exts = computed(() => {
100
+ const ext = [
101
+ props.basic ? basicSetup : void 0,
102
+ EditorView.updateListener.of((update) => emit("update", update)),
103
+ EditorView.theme(props.theme, { dark: dark.value }),
104
+ props.wrap ? EditorView.lineWrapping : void 0,
105
+ props.tab ? keymap.of([indentWithTab]) : void 0,
106
+ props.phrases ? EditorState.phrases.of(props.phrases) : void 0,
107
+ props.readonly ? EditorState.readOnly.of(props.readonly) : void 0,
108
+ props.editable ? EditorView.editable.of(props.editable) : void 0,
109
+ props.lang ? toRaw(props.lang) : void 0
110
+ ];
111
+ if (props.linter) {
112
+ merge(ext, props.linter);
113
+ }
114
+ if (props.extensions) {
115
+ merge(ext, props.extensions);
116
+ }
117
+ return compact(ext);
118
+ });
119
+ let view;
120
+ watch(doc, (current) => {
121
+ if (view.composing) {
122
+ return;
123
+ }
124
+ const previous = view.state.selection;
125
+ view.setState(EditorState.create({
126
+ doc: current,
127
+ extensions: exts.value,
128
+ selection: previous
129
+ }));
130
+ });
131
+ watch(dark, () => {
132
+ view.setState(EditorState.create({
133
+ doc: doc.value,
134
+ extensions: exts.value
135
+ }));
136
+ });
137
+ onMounted(async () => {
138
+ if (doc.value == "" && editor.value) {
139
+ doc.value = trim(editor.value.childNodes[0].innerText);
140
+ }
141
+ view = new EditorView({
142
+ state: EditorState.create({
143
+ doc: doc.value,
144
+ selection: props.selection,
145
+ extensions: exts.value
146
+ }),
147
+ parent: editor.value,
148
+ dispatch: (tr) => {
149
+ view.update([tr]);
150
+ if (tr.changes.empty)
151
+ return;
152
+ doc.value = view.state.doc.toString();
153
+ emit("update:modelValue", doc.value);
154
+ }
155
+ });
156
+ await nextTick();
157
+ });
158
+ onUnmounted(() => view.destroy());
159
+ return {
160
+ context,
161
+ editor
162
+ };
163
+ },
164
+ render() {
165
+ return h("div", {
166
+ ref: "editor",
167
+ class: "vue-codemirror"
168
+ }, this.$slots.default ? h("aside", { style: "display: none;" }, slot(this.$slots.default)) : void 0);
169
+ }
170
+ });
171
+ /**
172
+ * Vue CodeMirror6 Component
173
+ *
174
+ * @license MIT
175
+ * @author Logue {@link logue@hotmail.co.jp}
176
+ * @copyright 2022 Masashi Yoshikawa {@link https://logue.dev/} All rights reserved.
177
+ * @see {@link https://github.com/logue/vue-codemirror6}
178
+ */
179
+ const installCodeMirror = (app) => app.component("CodeMirror", CodeMirror);
180
+ 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/basic-setup"),require("@codemirror/commands"),require("lodash")):typeof define=="function"&&define.amd?define(["exports","vue-demi","@codemirror/state","@codemirror/view","@codemirror/basic-setup","@codemirror/commands","lodash"],t):(r=typeof globalThis<"u"?globalThis:r||self,t(r.CodeMirror={},r.VueDemi,r.state,r.view,r.basicSetup,r.commands,r.lodash))})(this,function(r,t,l,s,v,b,f){"use strict";const V=e=>e?Object.entries(e).reduce((i,[o,a])=>(o=o.charAt(0).toUpperCase()+o.slice(1),o=`on${o}`,{...i,[o]:a}),{}):null,y=(e,i={},o)=>{if(t.isVue2)return t.h(e,i,o);const{props:a,domProps:m,on:u,...c}=i,n=u?V(u):{},d={...c,...a,...m,...n};return t.h(e,d,o)},w=e=>typeof e=="function"?e():e;var h=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,i){const o=t.ref(),a=t.ref(e.modelValue),{dark:m}=t.toRefs(e),u=i.emit,c=t.computed(()=>{const d=[e.basic?v.basicSetup:void 0,s.EditorView.updateListener.of(p=>u("update",p)),s.EditorView.theme(e.theme,{dark:m.value}),e.wrap?s.EditorView.lineWrapping:void 0,e.tab?s.keymap.of([b.indentWithTab]):void 0,e.phrases?l.EditorState.phrases.of(e.phrases):void 0,e.readonly?l.EditorState.readOnly.of(e.readonly):void 0,e.editable?s.EditorView.editable.of(e.editable):void 0,e.lang?t.toRaw(e.lang):void 0];return e.linter&&f.merge(d,e.linter),e.extensions&&f.merge(d,e.extensions),f.compact(d)});let n;return t.watch(a,d=>{if(n.composing)return;const p=n.state.selection;n.setState(l.EditorState.create({doc:d,extensions:c.value,selection:p}))}),t.watch(m,()=>{n.setState(l.EditorState.create({doc:a.value,extensions:c.value}))}),t.onMounted(async()=>{a.value==""&&o.value&&(a.value=f.trim(o.value.childNodes[0].innerText)),n=new s.EditorView({state:l.EditorState.create({doc:a.value,selection:e.selection,extensions:c.value}),parent:o.value,dispatch:d=>{n.update([d]),!d.changes.empty&&(a.value=n.state.doc.toString(),u("update:modelValue",a.value))}}),await t.nextTick()}),t.onUnmounted(()=>n.destroy()),{context:i,editor:o}},render(){return y("div",{ref:"editor",class:"vue-codemirror"},this.$slots.default?y("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 x=e=>e.component("CodeMirror",h);r.default=h,r.install=x,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
@@ -2,7 +2,7 @@ import type { ViewUpdate } from '@codemirror/view';
2
2
  /** Emit Interface */
3
3
  export default interface CodeMirrorEmitsInterface {
4
4
  /** Model Update */
5
- (e: 'update:modelValue', value: string): void;
5
+ (e: 'update:modelValue', value: string | Text): void;
6
6
  /** CodeMirror ViewUpdate */
7
7
  (e: 'update', value: ViewUpdate): void;
8
8
  }
@@ -1 +1 @@
1
- {"version":3,"file":"CodeMirrorEmitsInterface.d.ts","sourceRoot":"","sources":["../../src/interfaces/CodeMirrorEmitsInterface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,qBAAqB;AACrB,MAAM,CAAC,OAAO,WAAW,wBAAwB;IAC/C,mBAAmB;IACnB,CAAC,CAAC,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9C,4BAA4B;IAC5B,CAAC,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;CACxC"}
1
+ {"version":3,"file":"CodeMirrorEmitsInterface.d.ts","sourceRoot":"","sources":["../../src/interfaces/CodeMirrorEmitsInterface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,qBAAqB;AACrB,MAAM,CAAC,OAAO,WAAW,wBAAwB;IAC/C,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;CACxC"}
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.3.5",
5
- "description": "CodeMirror6 Component for vue3",
4
+ "version": "0.5.0",
5
+ "description": "CodeMirror6 Component for vue2 and vue3",
6
6
  "keywords": [
7
7
  "vuejs",
8
8
  "vue",
@@ -34,18 +34,19 @@
34
34
  },
35
35
  "engines": {
36
36
  "node": ">=16.15.0",
37
- "yarn": "^1.22.18"
37
+ "yarn": ">=1.22.4"
38
38
  },
39
+ "packageManager": "yarn@3.2.1",
39
40
  "license": "MIT",
40
- "main": "./dist/vue-codemirror6.umd.js",
41
- "module": "./dist/vue-codemirror6.es.js",
41
+ "main": "./dist/index.umd.js",
42
+ "module": "./dist/index.es.js",
42
43
  "browser": {
43
44
  "./sfc": "./src/components/CodeMirror.vue"
44
45
  },
45
46
  "exports": {
46
47
  ".": {
47
- "import": "./dist/vue-codemirror6.es.js",
48
- "require": "./dist/vue-codemirror6.umd.js"
48
+ "import": "./dist/index.es.js",
49
+ "require": "./dist/index.umd.js"
49
50
  }
50
51
  },
51
52
  "types": "./dist/index.d.ts",
@@ -56,27 +57,26 @@
56
57
  "sideEffects": false,
57
58
  "scripts": {
58
59
  "dev": "vite",
59
- "clean": "rimraf node_modules/.vite",
60
+ "clean": "rimraf node_modules/.vite && npx vue-demi-fix",
60
61
  "build": "vite build & vue-tsc --declaration --emitDeclarationOnly",
61
62
  "build:analyze": "vue-tsc --noEmit && vite build --mode analyze",
62
63
  "build:docs": "vite build --config vite.config.docs.ts",
63
64
  "build:clean": "rimraf dist",
64
- "lint": "eslint . --fix --cache --cache-location ./node_modules/.vite/vite-plugin-eslint && prettier . -w -u",
65
+ "lint": "eslint --fix --cache --cache-location ./node_modules/.vite/vite-plugin-eslint && prettier . -w -u",
65
66
  "lint:markup": "vue-tsc --noEmit",
66
- "switch:2": "vue-demi-switch 2 & npx vue-demi-fix",
67
- "switch:3": "vue-demi-switch 3 vue3 & npx vue-demi-fix",
68
67
  "preview": "vite preview --config vite.config.docs.ts",
69
68
  "prepare": "husky install",
70
69
  "postinstall": "npx vue-demi-fix"
71
70
  },
72
71
  "dependencies": {
73
- "vue-demi": "latest"
72
+ "@codemirror/basic-setup": "^0.20.0",
73
+ "@codemirror/state": "^0.20.0",
74
+ "@codemirror/view": "^0.20.6",
75
+ "vue-demi": "^0.12.5"
74
76
  },
75
77
  "peerDependencies": {
76
- "@codemirror/state": "latest",
77
- "@codemirror/view": "latest",
78
- "@vue/composition-api": "latest",
79
- "vue": "^2.6.14 || ^3.2.33"
78
+ "@vue/composition-api": "^1.6.2",
79
+ "vue": "^2.6.14 || ^3.2.36"
80
80
  },
81
81
  "peerDependenciesMeta": {
82
82
  "@vue/composition-api": {
@@ -84,43 +84,38 @@
84
84
  }
85
85
  },
86
86
  "devDependencies": {
87
- "@codemirror/basic-setup": "^0.20.0",
88
- "@codemirror/lang-markdown": "^0.20.0",
89
- "@codemirror/language": "^0.20.0",
90
- "@codemirror/lint": "^0.20.2",
91
- "@modyqyw/vite-plugin-eslint": "^1.3.2",
87
+ "@codemirror/lang-markdown": "^0.20.1",
92
88
  "@types/lodash": "^4.14.182",
93
- "@types/node": "^17.0.32",
94
- "@typescript-eslint/eslint-plugin": "^5.23.0",
95
- "@typescript-eslint/parser": "^5.23.0",
89
+ "@types/node": "^17.0.35",
90
+ "@typescript-eslint/eslint-plugin": "^5.26.0",
91
+ "@typescript-eslint/parser": "^5.26.0",
96
92
  "@vitejs/plugin-vue": "^2.3.3",
97
- "@vue/eslint-config-typescript": "^10.0.0",
98
- "eslint": "^8.15.0",
93
+ "@vue/eslint-config-prettier": "^7.0.0",
94
+ "eslint": "^8.16.0",
99
95
  "eslint-config-google": "^0.14.0",
100
96
  "eslint-config-prettier": "^8.5.0",
101
97
  "eslint-import-resolver-alias": "^1.1.2",
102
98
  "eslint-import-resolver-typescript": "^2.7.1",
103
99
  "eslint-plugin-import": "^2.26.0",
104
- "eslint-plugin-prettier": "^4.0.0",
105
- "eslint-plugin-prettier-vue": "^3.1.0",
100
+ "eslint-plugin-jsdoc": "^39.3.2",
106
101
  "eslint-plugin-tsdoc": "^0.2.16",
107
- "eslint-plugin-vue": "^8.7.1",
102
+ "eslint-plugin-vue": "^9.0.1",
108
103
  "eslint-plugin-vuejs-accessibility": "^1.1.1",
109
104
  "husky": "^8.0.1",
110
- "lint-staged": "^12.4.1",
105
+ "lint-staged": "^12.4.2",
111
106
  "lodash": "^4.17.21",
112
107
  "prettier": "^2.6.2",
113
108
  "prettier-plugin-jsdoc": "^0.3.38",
114
109
  "prettier-plugin-md-nocjsp": "^1.2.0",
115
- "prettier-plugin-sort-imports": "^1.6.0",
110
+ "prettier-plugin-sort-imports": "^1.6.1",
116
111
  "rimraf": "^3.0.2",
117
112
  "rollup-plugin-visualizer": "^5.6.0",
118
- "typescript": "^4.6.4",
113
+ "typescript": "^4.7.2",
119
114
  "vite": "^2.9.9",
120
- "vue": "^3.2.33",
121
- "vue-eslint-parser": "^9.0.1",
122
- "vue-template-compiler": "^2.6.14",
123
- "vue-tsc": "^0.34.13"
115
+ "vite-plugin-checker": "^0.4.6",
116
+ "vue": "^3.2.36",
117
+ "vue-eslint-parser": "^9.0.2",
118
+ "vue-tsc": "^0.34.16"
124
119
  },
125
120
  "husky": {
126
121
  "hooks": {
@@ -1 +0,0 @@
1
- {"version":3,"file":"CodeMirror.vue.d.ts","sourceRoot":"","sources":["../../src/components/CodeMirror.vue.ts"],"names":[],"mappings":"AACA,OAAO,EASL,KAAK,GAAG,EACR,KAAK,QAAQ,EACb,KAAK,YAAY,EAClB,MAAM,UAAU,CAAC;AAOlB,OAAO,KAAK,EAAE,SAAS,EAAe,MAAM,mBAAmB,CAAC;AAChE,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;;IAavC,kBAAkB;;;;;IAKlB;;;;OAIG;;;;;;;IAKH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;;;;;IApEH,kBAAkB;;;;;IAKlB;;;;OAIG;;;;;;;IAKH,gBAAgB;;;;;IAKhB;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;IAKH;;;;OAIG;;;;;;;;;;;;;;;;;;AA/EP,2BAA2B;AAC3B,wBAqNG"}
@@ -1,151 +0,0 @@
1
- import { defineComponent, ref, computed, watch, nextTick, onMounted, onUnmounted, install } from "vue-demi";
2
- import { EditorState } from "@codemirror/state";
3
- import { EditorView } from "@codemirror/view";
4
- import { merge, compact } from "lodash";
5
- import { openBlock, createElementBlock, withDirectives, createElementVNode, renderSlot, vShow } from "vue";
6
- var _export_sfc = (sfc, props) => {
7
- const target = sfc.__vccOpts || sfc;
8
- for (const [key, val] of props) {
9
- target[key] = val;
10
- }
11
- return target;
12
- };
13
- const _sfc_main = defineComponent({
14
- name: "CodeMirror",
15
- model: {
16
- prop: "modelValue",
17
- event: "update:modelValue"
18
- },
19
- props: {
20
- modelValue: {
21
- type: String,
22
- default: ""
23
- },
24
- theme: {
25
- type: Object,
26
- default: () => {
27
- }
28
- },
29
- dark: {
30
- type: Boolean,
31
- default: false
32
- },
33
- readonly: {
34
- type: Boolean,
35
- default: false
36
- },
37
- editable: {
38
- type: Boolean,
39
- default: true
40
- },
41
- extensions: {
42
- type: Array,
43
- default: void 0
44
- },
45
- phrases: {
46
- type: Object,
47
- default: void 0
48
- },
49
- lang: {
50
- type: Object,
51
- default: void 0
52
- },
53
- linter: {
54
- type: Array,
55
- default: void 0
56
- }
57
- },
58
- emits: ["update:modelValue", "update"],
59
- setup(props, context) {
60
- const editor = ref();
61
- const modelValue = ref(props.modelValue);
62
- const dark = ref(props.dark);
63
- const emit = context.emit;
64
- const extensions = computed(() => {
65
- const ext = [
66
- EditorView.updateListener.of((update) => emit("update", update)),
67
- EditorView.theme(props.theme, { dark: dark.value }),
68
- props.phrases ? EditorState.phrases.of(props.phrases) : void 0,
69
- props.lang,
70
- props.readonly ? EditorState.readOnly.of(props.readonly) : void 0,
71
- props.editable ? EditorView.editable.of(props.editable) : void 0
72
- ];
73
- if (props.linter) {
74
- merge(ext, props.linter);
75
- }
76
- if (props.extensions) {
77
- merge(ext, props.extensions);
78
- }
79
- return compact(ext);
80
- });
81
- let view;
82
- watch(modelValue, (current) => {
83
- if (view.composing) {
84
- return;
85
- }
86
- const previous = view.state.selection;
87
- view.setState(EditorState.create({
88
- doc: current,
89
- extensions: extensions.value,
90
- selection: previous
91
- }));
92
- });
93
- watch(extensions, async (value) => {
94
- view.setState(EditorState.create({
95
- doc: modelValue.value,
96
- extensions: value
97
- }));
98
- await nextTick();
99
- }, { deep: true });
100
- onMounted(async () => {
101
- const value = !modelValue.value && editor.value ? editor.value.childNodes[0].innerText : modelValue.value;
102
- view = new EditorView({
103
- state: EditorState.create({
104
- doc: value,
105
- extensions: extensions.value
106
- }),
107
- parent: editor.value,
108
- dispatch: (tr) => {
109
- view.update([tr]);
110
- if (tr.changes.empty)
111
- return;
112
- modelValue.value = view.state.doc.toString();
113
- emit("update:modelValue", modelValue.value);
114
- }
115
- });
116
- await nextTick();
117
- });
118
- onUnmounted(() => view.destroy());
119
- return {
120
- context,
121
- editor
122
- };
123
- }
124
- });
125
- const _hoisted_1 = {
126
- ref: "editor",
127
- class: "vue-codemirror"
128
- };
129
- function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
130
- return openBlock(), createElementBlock("div", _hoisted_1, [
131
- withDirectives(createElementVNode("aside", null, [
132
- renderSlot(_ctx.$slots, "default")
133
- ], 512), [
134
- [vShow, !_ctx.context.slots.default]
135
- ])
136
- ], 512);
137
- }
138
- var CodeMirror = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
139
- /**
140
- * Vue CodeMirror6 Component
141
- *
142
- * @license MIT
143
- * @author Logue {@link logue@hotmail.co.jp}
144
- * @copyright 2022 Masashi Yoshikawa {@link https://logue.dev/} All rights reserved.
145
- * @see {@link https://github.com/logue/vue-codemirror6}
146
- */
147
- install();
148
- const installCodeMirror = (app) => {
149
- app.component("CodeMirror", CodeMirror);
150
- };
151
- export { CodeMirror, CodeMirror as default, installCodeMirror as install };
@@ -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("lodash"),require("vue")):typeof define=="function"&&define.amd?define(["exports","vue-demi","@codemirror/state","@codemirror/view","lodash","vue"],t):(o=typeof globalThis<"u"?globalThis:o||self,t(o.CodeMirror={},o.VueDemi,o.state,o.view,o.lodash,o.Vue))})(this,function(o,t,s,c,m,i){"use strict";var v=(e,u)=>{const a=e.__vccOpts||e;for(const[r,f]of u)a[r]=f;return a};const V=t.defineComponent({name:"CodeMirror",model:{prop:"modelValue",event:"update:modelValue"},props:{modelValue:{type:String,default:""},theme:{type:Object,default:()=>{}},dark:{type:Boolean,default:!1},readonly:{type:Boolean,default:!1},editable:{type:Boolean,default:!0},extensions:{type:Array,default:void 0},phrases:{type:Object,default:void 0},lang:{type:Object,default:void 0},linter:{type:Array,default:void 0}},emits:["update:modelValue","update"],setup(e,u){const a=t.ref(),r=t.ref(e.modelValue),f=t.ref(e.dark),p=u.emit,h=t.computed(()=>{const d=[c.EditorView.updateListener.of(l=>p("update",l)),c.EditorView.theme(e.theme,{dark:f.value}),e.phrases?s.EditorState.phrases.of(e.phrases):void 0,e.lang,e.readonly?s.EditorState.readOnly.of(e.readonly):void 0,e.editable?c.EditorView.editable.of(e.editable):void 0];return e.linter&&m.merge(d,e.linter),e.extensions&&m.merge(d,e.extensions),m.compact(d)});let n;return t.watch(r,d=>{if(n.composing)return;const l=n.state.selection;n.setState(s.EditorState.create({doc:d,extensions:h.value,selection:l}))}),t.watch(h,async d=>{n.setState(s.EditorState.create({doc:r.value,extensions:d})),await t.nextTick()},{deep:!0}),t.onMounted(async()=>{const d=!r.value&&a.value?a.value.childNodes[0].innerText:r.value;n=new c.EditorView({state:s.EditorState.create({doc:d,extensions:h.value}),parent:a.value,dispatch:l=>{n.update([l]),!l.changes.empty&&(r.value=n.state.doc.toString(),p("update:modelValue",r.value))}}),await t.nextTick()}),t.onUnmounted(()=>n.destroy()),{context:u,editor:a}}}),S={ref:"editor",class:"vue-codemirror"};function w(e,u,a,r,f,p){return i.openBlock(),i.createElementBlock("div",S,[i.withDirectives(i.createElementVNode("aside",null,[i.renderSlot(e.$slots,"default")],512),[[i.vShow,!e.context.slots.default]])],512)}var y=v(V,[["render",w]]);/**
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
- */t.install();const _=e=>{e.component("CodeMirror",y)};o.CodeMirror=y,o.default=y,o.install=_,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
@@ -1,248 +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
- ref,
11
- watch,
12
- onMounted,
13
- onUnmounted,
14
- defineComponent,
15
- nextTick,
16
- type ComputedRef,
17
- type Ref,
18
- type PropType,
19
- type SetupContext,
20
- } from 'vue-demi';
21
- import { EditorState } from '@codemirror/state';
22
- import { EditorView } from '@codemirror/view';
23
- import { compact, merge } from 'lodash';
24
-
25
- import type CodeMirrorEmitsInterface from '@/interfaces/CodeMirrorEmitsInterface';
26
-
27
- import type { Extension, Transaction } from '@codemirror/state';
28
- import type { LanguageSupport } from '@codemirror/language';
29
- import type { ViewUpdate } from '@codemirror/view';
30
- import type { Diagnostic } from '@codemirror/lint';
31
- import type { StyleSpec } from 'style-mod';
32
-
33
- /** CodeMirror Component */
34
- export default defineComponent({
35
- /** Component Name */
36
- name: 'CodeMirror',
37
- /** Model Definition */
38
- model: {
39
- prop: 'modelValue',
40
- event: 'update:modelValue',
41
- },
42
- /** Props Definition */
43
- props: {
44
- /** Model value */
45
- modelValue: {
46
- type: String,
47
- default: '',
48
- },
49
- /**
50
- * Theme
51
- *
52
- * @see {@link https://codemirror.net/6/examples/styling/ | Example: Styling}
53
- */
54
- theme: {
55
- type: Object as PropType<{ [selector: string]: StyleSpec }>,
56
- default: () => {},
57
- },
58
- /** Dark Mode */
59
- dark: {
60
- type: Boolean,
61
- default: false,
62
- },
63
- /**
64
- * Readonly
65
- *
66
- * @see {@link https://codemirror.net/6/docs/ref/#state.EditorState%5EreadOnly | readOnly}
67
- */
68
- readonly: {
69
- type: Boolean,
70
- default: false,
71
- },
72
- /**
73
- * Editable
74
- *
75
- * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5Eeditable | editable}
76
- */
77
- editable: {
78
- type: Boolean,
79
- default: true,
80
- },
81
- /**
82
- * Additional Extension
83
- *
84
- * @see {@link https://codemirror.net/6/docs/ref/#state.Extension | Extending Editor State}
85
- */
86
- extensions: {
87
- type: Array as PropType<Extension[]>,
88
- default: undefined,
89
- },
90
- /**
91
- * Language Phreses
92
- *
93
- * @see {@link https://codemirror.net/6/examples/translate/ | Example: Internationalization}
94
- */
95
- phrases: {
96
- type: Object as PropType<Record<string, string>>,
97
- default: undefined,
98
- },
99
- /**
100
- * CodeMirror Language
101
- *
102
- * @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
103
- */
104
- lang: {
105
- type: Object as PropType<LanguageSupport>,
106
- default: undefined,
107
- },
108
- /**
109
- * CodeMirror Linter
110
- *
111
- * @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
112
- */
113
- linter: {
114
- type: Array as PropType<Diagnostic[]>,
115
- default: undefined,
116
- },
117
- },
118
- /** Emits */
119
- emits: ['update:modelValue', 'update'],
120
- /**
121
- * Setup
122
- * @param props - Props
123
- * @param context - Context
124
- */
125
- setup(props, context: SetupContext) {
126
- /** Editor DOM */
127
- const editor: Ref<Element | undefined> = ref();
128
-
129
- /** Model */
130
- const modelValue: Ref<string> = ref(props.modelValue);
131
-
132
- /** Dark mode */
133
- const dark: Ref<boolean | undefined> = ref(props.dark);
134
-
135
- /** Emits */
136
- const emit = context.emit as CodeMirrorEmitsInterface;
137
-
138
- /** CodeMirror Extension */
139
- const extensions: ComputedRef<Extension[]> = computed(() => {
140
- /** Default extension */
141
- const ext = [
142
- // ViewUpdate event listener
143
- EditorView.updateListener.of((update: ViewUpdate) =>
144
- emit('update', update)
145
- ),
146
- // Toggle light/dark mode.
147
- EditorView.theme(props.theme, { dark: dark.value }),
148
- // locale settings
149
- props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
150
- // Parser language setting
151
- props.lang,
152
- // Readonly option
153
- props.readonly ? EditorState.readOnly.of(props.readonly) : undefined,
154
- // Editable option
155
- props.editable ? EditorView.editable.of(props.editable) : undefined,
156
- ];
157
-
158
- if (props.linter) {
159
- // Append Linter settings
160
- merge(ext, props.linter);
161
- }
162
-
163
- if (props.extensions) {
164
- // Append Extensions (such as basic-setup)
165
- merge(ext, props.extensions);
166
- }
167
-
168
- // console.debug('[CodeMirror.vue] Loaded extensions:', ext, compact(ext));
169
- return compact(ext);
170
- });
171
-
172
- /** CodeMirror Editor View */
173
- let view!: EditorView;
174
-
175
- /**
176
- * Input value changed
177
- *
178
- * @see {@link https://codemirror.net/6/docs/migration/#making-changes | Making Changes}
179
- */
180
- watch(modelValue, current => {
181
- if (view.composing) {
182
- // IME fix
183
- return;
184
- }
185
-
186
- /** Previous cursor location */
187
- const previous = view.state.selection;
188
- view.setState(
189
- EditorState.create({
190
- doc: current,
191
- extensions: extensions.value,
192
- selection: previous,
193
- })
194
- );
195
- });
196
-
197
- /** Apply extensions */
198
- watch(
199
- extensions,
200
- async value => {
201
- view.setState(
202
- EditorState.create({
203
- doc: modelValue.value,
204
- extensions: value,
205
- })
206
- );
207
- await nextTick();
208
- },
209
- { deep: true }
210
- );
211
-
212
- /** When loaded */
213
- onMounted(async () => {
214
- /** Initial Value */
215
- const value =
216
- !modelValue.value && editor.value
217
- ? (editor.value.childNodes[0] as HTMLDivElement).innerText
218
- : modelValue.value;
219
-
220
- // Register Codemirror
221
- view = new EditorView({
222
- state: EditorState.create({
223
- doc: value,
224
- extensions: extensions.value,
225
- }),
226
- parent: editor.value,
227
- dispatch: (tr: Transaction) => {
228
- view.update([tr]);
229
-
230
- if (tr.changes.empty) return;
231
- // to parent binding
232
- modelValue.value = view.state.doc.toString();
233
- emit('update:modelValue', modelValue.value);
234
- },
235
- });
236
- await nextTick();
237
- });
238
-
239
- /** Destroy */
240
- onUnmounted(() => view.destroy());
241
-
242
- return {
243
- context,
244
- editor,
245
- };
246
- },
247
- });
248
- </script>