vue-codemirror6 0.0.6 → 0.1.3

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