vue-codemirror6 0.1.0 → 0.3.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.
@@ -1,12 +1,26 @@
1
1
  <template>
2
- <pre ref="editor">
3
- <template v-if="!$slots.default"><slot /></template>
4
- </pre>
2
+ <div ref="editor" class="vue-codemirror">
3
+ <div v-show="context.slots.defatlt"><slot /></div>
4
+ </div>
5
5
  </template>
6
6
 
7
7
  <script lang="ts">
8
- import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
9
- import type { VNode } from 'vue';
8
+ import {
9
+ computed,
10
+ type ComputedRef,
11
+ ref,
12
+ watch,
13
+ onMounted,
14
+ onUnmounted,
15
+ type Ref,
16
+ defineComponent,
17
+ type PropType,
18
+ type SetupContext,
19
+ EmitsOptions,
20
+ } from 'vue-demi';
21
+
22
+ import type CodeMirrorPropsInterface from '@/interfaces/CodeMirrorPropsInterface';
23
+ import type CodeMirrorEmitsInterface from '@/interfaces/CodeMirrorEmitsInterface';
10
24
 
11
25
  import {
12
26
  EditorState,
@@ -17,187 +31,215 @@ import { EditorView, type ViewUpdate } from '@codemirror/view';
17
31
  import type { LanguageSupport } from '@codemirror/language';
18
32
  import type { Diagnostic } from '@codemirror/lint';
19
33
  import type { StyleSpec } from 'style-mod';
20
-
21
34
  import { compact, merge } from 'lodash';
22
35
 
23
- @Component({ name: 'CodeMirror' })
24
36
  /** CodeMirror Component */
25
- export default class CodeMirror extends Vue {
26
- /** Editor */
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: () => {} })
35
- readonly theme!: { [selector: string]: StyleSpec };
36
-
37
- /** Dark Mode */
38
- @Prop({ type: Boolean, default: false })
39
- readonly dark!: boolean;
40
-
41
- /**
42
- * Readonly
43
- *
44
- * @see {@link https://codemirror.net/6/docs/ref/#state.EditorState%5EreadOnly | readOnly}
45
- */
46
- @Prop({ type: Boolean, default: false })
47
- readonly readonly!: boolean;
48
-
49
- /**
50
- * Editable
51
- *
52
- * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5Eeditable | editable}
53
- */
54
- @Prop({ type: Boolean, default: true })
55
- readonly editable!: boolean;
56
-
37
+ export default defineComponent({
38
+ /** Component Name */
39
+ name: 'CodeMirror',
40
+ /** Model Definition */
41
+ model: {
42
+ prop: 'modelValue',
43
+ event: 'update:modelValue',
44
+ },
45
+ /** Props Definition */
46
+ props: {
47
+ /** Model value */
48
+ modelValue: {
49
+ type: String,
50
+ default: '',
51
+ },
52
+ /**
53
+ * Theme
54
+ *
55
+ * @see {@link https://codemirror.net/6/examples/styling/ | Example: Styling}
56
+ */
57
+ theme: {
58
+ type: Object as PropType<{ [selector: string]: StyleSpec }>,
59
+ default: undefined,
60
+ },
61
+ /** Dark Mode */
62
+ dark: {
63
+ type: Boolean,
64
+ default: false,
65
+ },
66
+ /**
67
+ * Readonly
68
+ *
69
+ * @see {@link https://codemirror.net/6/docs/ref/#state.EditorState%5EreadOnly | readOnly}
70
+ */
71
+ readonly: {
72
+ type: Boolean,
73
+ default: false,
74
+ },
75
+ /**
76
+ * Editable
77
+ *
78
+ * @see {@link https://codemirror.net/6/docs/ref/#view.EditorView%5Eeditable | editable}
79
+ */
80
+ editable: {
81
+ type: Boolean,
82
+ default: true,
83
+ },
84
+ /**
85
+ * Additional Extension
86
+ *
87
+ * @see {@link https://codemirror.net/6/docs/ref/#state.Extension | Extending Editor State}
88
+ */
89
+ extensions: {
90
+ type: Array as PropType<Extension[]>,
91
+ default: undefined,
92
+ },
93
+ /**
94
+ * Language Phreses
95
+ *
96
+ * @see {@link https://codemirror.net/6/examples/translate/ | Example: Internationalization}
97
+ */
98
+ phrases: {
99
+ type: Object as PropType<Record<string, string>>,
100
+ default: undefined,
101
+ },
102
+ /**
103
+ * CodeMirror Language
104
+ *
105
+ * @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
106
+ */
107
+ lang: {
108
+ type: Object as PropType<LanguageSupport>,
109
+ default: undefined,
110
+ },
111
+ /**
112
+ * CodeMirror Linter
113
+ *
114
+ * @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
115
+ */
116
+ linter: {
117
+ type: Array as PropType<Diagnostic[]>,
118
+ default: undefined,
119
+ },
120
+ },
121
+ /** Emits */
122
+ emits: ['update:modelValue', 'update'],
57
123
  /**
58
- * Language Phreses
59
- *
60
- * @see {@link https://codemirror.net/6/examples/translate/ | Example: Internationalization}
124
+ * Setup
125
+ * @param props - Props
126
+ * @param context - Context
61
127
  */
62
- @Prop({ type: Object })
63
- readonly phrases!: Record<string, string>;
128
+ setup(props: CodeMirrorPropsInterface, context: SetupContext<EmitsOptions>) {
129
+ /** Editor DOM */
130
+ const editor: Ref<Element | undefined> = ref<Element>();
131
+
132
+ /** Model */
133
+ const modelValue: Ref<string> = ref(props.modelValue);
134
+
135
+ /** Dark mode */
136
+ const dark: Ref<boolean | undefined> = ref(props.dark);
137
+
138
+ /** Emits */
139
+ const emit = context.emit as CodeMirrorEmitsInterface;
140
+
141
+ /** CodeMirror Extension */
142
+ const extensions: ComputedRef<Extension[]> = computed(() => {
143
+ /** Default extension */
144
+ const ext = [
145
+ // ViewUpdate event listener
146
+ EditorView.updateListener.of((update: ViewUpdate) => {
147
+ emit('update', update);
148
+ }),
149
+ // Toggle light/dark mode.
150
+ EditorView.theme(props.theme || {}, { dark: props.dark }),
151
+ // locale settings
152
+ props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
153
+ // Parser language setting
154
+ props.lang,
155
+ // Readonly option
156
+ props.readonly ? EditorState.readOnly.of(props.readonly) : undefined,
157
+ // Editable option
158
+ props.editable ? EditorView.editable.of(props.editable) : undefined,
159
+ ];
160
+
161
+ if (props.linter) {
162
+ // Append Linter settings
163
+ merge(ext, props.linter);
164
+ }
165
+
166
+ if (props.extensions) {
167
+ // Append Extensions (such as basic-setup)
168
+ merge(ext, props.extensions);
169
+ }
170
+
171
+ // console.debug('[CodeMirror.vue] Loaded extensions:', ext, compact(ext));
172
+ return compact(ext);
173
+ });
64
174
 
65
- /**
66
- * Additional Extension
67
- *
68
- * @see {@link https://codemirror.net/6/docs/ref/#state.Extension | Extending Editor State}
69
- */
70
- @Prop({ type: Array, default: () => [] })
71
- readonly extensions!: Extension[];
175
+ /** CodeMirror Editor View */
176
+ let view!: EditorView;
177
+
178
+ /**
179
+ * Input value changed
180
+ *
181
+ * @see {@link https://codemirror.net/6/docs/migration/#making-changes | Making Changes}
182
+ */
183
+ watch(modelValue, current => {
184
+ if (view.composing) {
185
+ // IME fix
186
+ return;
187
+ }
188
+
189
+ /** Previous cursor location */
190
+ const previous = view.state.selection;
191
+ view.setState(
192
+ EditorState.create({
193
+ doc: current,
194
+ extensions: extensions.value,
195
+ selection: previous,
196
+ })
197
+ );
198
+ });
72
199
 
73
- /**
74
- * CodeMirror Language
75
- *
76
- * @see {@link https://codemirror.net/6/docs/ref/#language | @codemirror/language}
77
- */
78
- @Prop({ type: Object })
79
- readonly lang!: LanguageSupport;
200
+ /** Toggle Dark mode */
201
+ watch(dark, () => {
202
+ view.setState(
203
+ EditorState.create({
204
+ doc: modelValue.value,
205
+ extensions: extensions.value,
206
+ })
207
+ );
208
+ });
80
209
 
81
- /**
82
- * CodeMirror Linter
83
- *
84
- * @see {@link https://codemirror.net/6/docs/ref/#lint | @codemirror/lint}
85
- */
86
- @Prop({ type: Array, default: () => [] })
87
- readonly linter!: Diagnostic[];
210
+ /** When loaded */
211
+ onMounted(() => {
212
+ /** Value */
213
+ const value =
214
+ !modelValue.value && editor.value
215
+ ? (editor.value.childNodes[0] as HTMLDivElement).innerText
216
+ : modelValue.value;
217
+
218
+ // Register Codemirror
219
+ view = new EditorView({
220
+ state: EditorState.create({
221
+ doc: value,
222
+ extensions: extensions.value,
223
+ }),
224
+ parent: editor.value,
225
+ dispatch: (tr: Transaction) => {
226
+ view.update([tr]);
227
+
228
+ if (tr.changes.empty) return;
229
+ // to parent binding
230
+ modelValue.value = view.state.doc.toString();
231
+ emit('update:modelValue', modelValue.value);
232
+ },
233
+ });
234
+ });
88
235
 
89
- /** Input */
90
- @Prop({ type: String, default: '' })
91
- readonly value!: string;
236
+ /** Destroy */
237
+ onUnmounted(() => view.destroy());
92
238
 
93
- /**
94
- * Input value changed
95
- *
96
- * @see {@link https://codemirror.net/6/docs/migration/#making-changes | Making Changes}
97
- */
98
- @Watch('value')
99
- onValueChanged() {
100
- if (this.editor.composing) {
101
- // IME fix
102
- return;
103
- }
104
- /** Previous cursor location */
105
- const previous = this.editor.state.selection;
106
- this.editor.setState(
107
- EditorState.create({
108
- doc: this.value,
109
- extensions: this.extension,
110
- selection: previous,
111
- })
112
- );
113
- }
114
-
115
- /** Toggle Dark mode */
116
- @Watch('dark')
117
- onToggleDarkMode() {
118
- this.editor.setState(
119
- EditorState.create({
120
- doc: this.value,
121
- extensions: this.extension,
122
- })
123
- );
124
- }
125
-
126
- /** CodeMirror Extension */
127
- get extension(): Extension[] {
128
- /** Default extension */
129
- const ext: Extension[] = compact([
130
- // ViewUpdate event listener
131
- EditorView.updateListener.of((update: ViewUpdate) =>
132
- this.$emit('update', update)
133
- ),
134
- // Toggle light/dark mode.
135
- EditorView.theme(this.theme, { dark: this.dark }),
136
- // locale settings
137
- this.phrases ? EditorState.phrases.of(this.phrases) : undefined,
138
- // Parser language setting
139
- this.lang ? this.lang : undefined,
140
- // Readonly option
141
- EditorState.readOnly.of(this.readonly),
142
- // Editable option
143
- EditorView.editable.of(this.editable),
144
- ]);
145
-
146
- if (this.linter.length !== 0) {
147
- // Append Linter settings
148
- merge(ext, this.linter);
149
- }
150
-
151
- if (this.extensions.length !== 0) {
152
- // Append Extensions (such as basic-setup)
153
- merge(ext, this.extensions);
154
- }
155
-
156
- console.debug('[CodeMirror.vue] Loaded extensions:', ext);
157
-
158
- return ext;
159
- }
160
-
161
- /** When loaded */
162
- mounted() {
163
- let value = this.value;
164
- if (!this.value && this.$slots.default !== undefined) {
165
- // override text node
166
- // console.log(this.$slots.default);
167
- value = this.getChildrenTextContent(this.$slots.default);
168
- }
169
-
170
- // Register Codemirror
171
- this.editor = new EditorView({
172
- state: EditorState.create({
173
- doc: value,
174
- extensions: this.extension,
175
- }),
176
- parent: this.$refs.editor as Element,
177
- dispatch: (tr: Transaction) => {
178
- this.editor.update([tr]);
179
-
180
- if (tr.changes.empty) return;
181
- // to parent binding
182
- this.$emit('input', this.editor.state.doc.toString());
183
- },
184
- });
185
- }
186
-
187
- /** Destroy */
188
- beforeDestroy() {
189
- this.editor.destroy();
190
- }
191
-
192
- /** Vue node to plain text */
193
- private getChildrenTextContent(children: VNode[]): string {
194
- return children
195
- .map((node: VNode) =>
196
- node.children
197
- ? this.getChildrenTextContent(node.children)
198
- : node.text || '\n'
199
- )
200
- .join('');
201
- }
202
- }
239
+ return {
240
+ context,
241
+ editor,
242
+ };
243
+ },
244
+ });
203
245
  </script>