vue-codemirror6 0.0.3 → 0.0.6

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.
@@ -1,55 +1,89 @@
1
1
  <template>
2
- <div ref="editor" />
2
+ <div ref="editor">
3
+ <template v-if="!$slots.default"><slot /></template>
4
+ </div>
3
5
  </template>
4
6
 
5
7
  <script lang="ts">
6
8
  import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
7
-
8
- import { EditorState, Extension, Transaction } from '@codemirror/state';
9
- import { EditorView, ViewUpdate } from '@codemirror/view';
10
- import type { Diagnostic } from '@codemirror/lint';
9
+ import type { VNode } from 'vue';
10
+
11
+ import {
12
+ EditorState,
13
+ type Extension,
14
+ type Transaction,
15
+ } from '@codemirror/state';
16
+ import { EditorView, type ViewUpdate } from '@codemirror/view';
11
17
  import type { LanguageSupport } from '@codemirror/language';
12
-
13
- import merge from 'lodash/merge';
18
+ import type { Diagnostic } from '@codemirror/lint';
14
19
  import type { StyleSpec } from 'style-mod';
15
20
 
16
- @Component
21
+ import { compact, merge } from 'lodash';
22
+
23
+ @Component({ name: 'CodeMirror' })
17
24
  /** CodeMirror Component */
18
25
  export default class CodeMirror extends Vue {
19
26
  /** Editor */
20
- editor!: EditorView;
21
-
22
- /** Theme */
23
- @Prop({ type: Object })
27
+ private editor!: EditorView;
28
+
29
+ /**
30
+ * Theme
31
+ *
32
+ * @see {@link https://codemirror.net/6/examples/styling/ | Example: Styling}
33
+ */
34
+ @Prop({ type: Object, default: () => {} })
24
35
  readonly theme!: { [selector: string]: StyleSpec };
25
36
 
26
37
  /** Dark Mode */
27
38
  @Prop({ type: Boolean, default: false })
28
39
  readonly dark!: boolean;
29
40
 
30
- /** Language Phreses */
41
+ /**
42
+ * Language Phreses
43
+ *
44
+ * @see {@link https://codemirror.net/6/examples/translate/ | Example: Internationalization}
45
+ */
31
46
  @Prop({ type: Object })
32
47
  readonly phrases!: Record<string, string>;
33
48
 
34
- /** Additional Extension */
49
+ /**
50
+ * Additional Extension
51
+ *
52
+ * @see {@link https://codemirror.net/6/docs/ref/#state.Extension | Extending Editor State}
53
+ */
35
54
  @Prop({ type: Array, default: () => [] })
36
55
  readonly extensions!: Extension[];
37
56
 
38
- /** CodeMirror Language */
57
+ /**
58
+ * CodeMirror Language
59
+ *
60
+ * @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
61
+ */
39
62
  @Prop({ type: Object })
40
63
  readonly lang!: LanguageSupport;
41
64
 
42
- /** CodeMirror Linter */
65
+ /**
66
+ * CodeMirror Linter
67
+ *
68
+ * @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
69
+ */
43
70
  @Prop({ type: Array, default: () => [] })
44
71
  readonly linter!: Diagnostic[];
45
72
 
46
73
  /** Input */
47
- @Prop({ type: String, required: true })
74
+ @Prop({ type: String, default: '' })
48
75
  readonly value!: string;
49
76
 
50
- /** Input value changed */
77
+ /**
78
+ * Input value changed
79
+ *
80
+ * @see {@link https://codemirror.net/6/docs/migration/#making-changes | Making Changes}
81
+ */
51
82
  @Watch('value')
52
83
  onValueChanged() {
84
+ /** Previous cursor location */
85
+ const previous = this.editor.state.selection;
86
+ /*
53
87
  this.editor.dispatch({
54
88
  changes: {
55
89
  from: 0,
@@ -57,6 +91,14 @@ export default class CodeMirror extends Vue {
57
91
  insert: this.value,
58
92
  },
59
93
  });
94
+ */
95
+ this.editor.setState(
96
+ EditorState.create({
97
+ doc: this.value,
98
+ extensions: this.extension,
99
+ selection: previous,
100
+ })
101
+ );
60
102
  }
61
103
 
62
104
  /** Darkmode */
@@ -73,34 +115,46 @@ export default class CodeMirror extends Vue {
73
115
  /** CodeMirror Extension */
74
116
  get extension(): Extension[] {
75
117
  /** Default extension */
76
- const ext = [
77
- EditorState.phrases.of(this.phrases),
118
+ const ext: Extension[] = compact([
119
+ // ViewUpdate event listener
78
120
  EditorView.updateListener.of((update: ViewUpdate) =>
79
121
  this.$emit('update', update)
80
122
  ),
123
+ // Toggle light/dark mode.
81
124
  EditorView.theme(this.theme, { dark: this.dark }),
82
- ];
83
-
84
- if (this.lang) {
85
- ext.push(this.lang);
86
- }
125
+ // locale settings
126
+ this.phrases ? EditorState.phrases.of(this.phrases) : undefined,
127
+ // Parser language setting
128
+ this.lang ? this.lang : undefined,
129
+ ]);
87
130
 
88
131
  if (this.linter.length !== 0) {
132
+ // Append Linter settings
89
133
  merge(ext, this.linter);
90
134
  }
91
135
 
92
136
  if (this.extensions.length !== 0) {
137
+ // Append Extensions (such as basic-setup)
93
138
  merge(ext, this.extensions);
94
139
  }
140
+
141
+ console.debug('[CodeMirror.vue] Loaded extensions:', ext);
142
+
95
143
  return ext;
96
144
  }
97
145
 
98
146
  /** When loaded */
99
147
  mounted() {
148
+ let value = this.value;
149
+ if (!this.value && this.$slots.default !== undefined) {
150
+ // override text node
151
+ value = this.getChildrenTextContent(this.$slots.default);
152
+ }
153
+
100
154
  // Register Codemirror
101
155
  this.editor = new EditorView({
102
156
  state: EditorState.create({
103
- doc: this.value,
157
+ doc: value,
104
158
  extensions: this.extension,
105
159
  }),
106
160
  parent: this.$refs.editor as Element,
@@ -108,7 +162,7 @@ export default class CodeMirror extends Vue {
108
162
  this.editor.update([tr]);
109
163
 
110
164
  if (tr.changes.empty) return;
111
- // Binding
165
+ // to parent binding
112
166
  this.$emit('input', this.editor.state.doc.toString());
113
167
  },
114
168
  });
@@ -118,5 +172,16 @@ export default class CodeMirror extends Vue {
118
172
  beforeDestroy() {
119
173
  this.editor.destroy();
120
174
  }
175
+
176
+ /** Vue node to plain text */
177
+ private getChildrenTextContent(children: VNode[]): string {
178
+ return children
179
+ .map((node: VNode) =>
180
+ node.children
181
+ ? this.getChildrenTextContent(node.children)
182
+ : node.text || '\n'
183
+ )
184
+ .join('');
185
+ }
121
186
  }
122
187
  </script>
package/types/entry.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import * as namedExports from './entry.esm';
2
- declare const _default: import("vue").VueConstructor<import("vue").default> & import("vue").PluginObject<any> & typeof namedExports;
3
- export default _default;
4
- //# sourceMappingURL=entry.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"entry.d.ts","sourceRoot":"","sources":["../src/entry.ts"],"names":[],"mappings":"AACA,OAAkB,KAAK,YAAY,MAAM,aAAa,CAAC;;AASvD,wBAC0C"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"entry.esm.d.ts","sourceRoot":"","sources":["../src/entry.esm.ts"],"names":[],"mappings":"AAAA,OAAa,EAAE,YAAY,EAAE,MAAM,KAAK,CAAC;AAGzC,OAAO,SAAS,MAAM,6BAA6B,CAAC;AAGpD,aAAK,oBAAoB,GAAG,OAAO,SAAS,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC;;AAKjE,wBASK"}