vue-codemirror6 0.0.6 → 0.3.1

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