vue-codemirror6 0.6.6 → 1.0.0

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