vue-codemirror6 1.1.11 → 1.1.13

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,509 +0,0 @@
1
- import { computed, defineComponent, nextTick, onMounted, onUnmounted, ref, shallowRef, watch, } from 'vue-demi';
2
- // Helpers
3
- import h, { slot } from '@/helpers/h-demi';
4
- import { compact, trim } from 'lodash';
5
- // CodeMirror
6
- import { basicSetup, minimalSetup } from 'codemirror';
7
- import { EditorSelection, EditorState, StateEffect, } from '@codemirror/state';
8
- import { EditorView, keymap, placeholder, } from '@codemirror/view';
9
- import { indentWithTab } from '@codemirror/commands';
10
- import { forceLinting, linter, lintGutter, } from '@codemirror/lint';
11
- /** CodeMirror Component */
12
- export default defineComponent({
13
- /** Component Name */
14
- name: 'CodeMirror',
15
- /** Model Definition */
16
- model: {
17
- prop: 'modelValue',
18
- event: 'update:modelValue',
19
- },
20
- /** Props Definition */
21
- props: {
22
- /** Model value */
23
- modelValue: {
24
- type: String,
25
- default: '',
26
- },
27
- /**
28
- * Theme
29
- *
30
- * @see {@link https://codemirror.net/docs/ref/#view.EditorView^theme}
31
- */
32
- theme: {
33
- type: Object,
34
- default: () => { },
35
- },
36
- /** Dark Mode */
37
- dark: {
38
- type: Boolean,
39
- default: false,
40
- },
41
- /**
42
- * Use Basic Setup
43
- *
44
- * @see {@link https://codemirror.net/docs/ref/#codemirror.basicSetup}
45
- */
46
- basic: {
47
- type: Boolean,
48
- default: false,
49
- },
50
- /**
51
- * Use Minimal Setup (The basic setting has priority.)
52
- *
53
- * @see {@link https://codemirror.net/docs/ref/#codemirror.minimalSetup}
54
- */
55
- minimal: {
56
- type: Boolean,
57
- default: false,
58
- },
59
- /**
60
- * Placeholder
61
- *
62
- * @see {@link https://codemirror.net/docs/ref/#view.placeholder}
63
- */
64
- placeholder: {
65
- type: String,
66
- default: undefined,
67
- },
68
- /**
69
- * Line wrapping
70
- *
71
- * An extension that enables line wrapping in the editor (by setting CSS white-space to pre-wrap in the content).
72
- *
73
- * @see {@link https://codemirror.net/docs/ref/#view.EditorView%5ElineWrapping}
74
- */
75
- wrap: {
76
- type: Boolean,
77
- default: false,
78
- },
79
- /**
80
- * Allow tab key indent.
81
- *
82
- * @see {@link https://codemirror.net/examples/tab/}
83
- */
84
- tab: {
85
- type: Boolean,
86
- default: false,
87
- },
88
- /**
89
- * Tab size
90
- *
91
- * @see {@link https://codemirror.net/docs/ref/#state.EditorState^tabSize}
92
- */
93
- tabSize: {
94
- type: Number,
95
- default: undefined,
96
- },
97
- /**
98
- * Set line break (separetor) char.
99
- *
100
- * @see {@link https://codemirror.net/docs/ref/#state.EditorState^lineSeparator}
101
- */
102
- lineSeparator: {
103
- type: String,
104
- default: undefined,
105
- },
106
- /**
107
- * Readonly
108
- *
109
- * @see {@link https://codemirror.net/docs/ref/#state.EditorState^readOnly}
110
- */
111
- readonly: {
112
- type: Boolean,
113
- default: false,
114
- },
115
- /**
116
- * Disable input.
117
- *
118
- * This is the reversed value of the CodeMirror editable.
119
- * Similar to `readonly`, but setting this value to true disables dragging.
120
- *
121
- * @see {@link https://codemirror.net/docs/ref/#view.EditorView^editable}
122
- */
123
- disabled: {
124
- type: Boolean,
125
- default: false,
126
- },
127
- /**
128
- * Additional Extension
129
- *
130
- * @see {@link https://codemirror.net/docs/ref/#state.Extension}
131
- */
132
- extensions: {
133
- type: Array,
134
- default: () => {
135
- return [];
136
- },
137
- },
138
- /**
139
- * Language Phreses
140
- *
141
- * @see {@link https://codemirror.net/examples/translate/}
142
- */
143
- phrases: {
144
- type: Object,
145
- default: () => undefined,
146
- },
147
- /**
148
- * CodeMirror Language
149
- *
150
- * @see {@link https://codemirror.net/docs/ref/#language}
151
- */
152
- lang: {
153
- type: Object,
154
- default: () => undefined,
155
- },
156
- /**
157
- * CodeMirror Linter
158
- *
159
- * @see {@link https://codemirror.net/docs/ref/#lint.linter}
160
- */
161
- linter: {
162
- type: Function,
163
- default: undefined,
164
- },
165
- /**
166
- * Linter Config
167
- *
168
- * @see {@link https://codemirror.net/docs/ref/#lint.linter^config}
169
- */
170
- linterConfig: {
171
- type: Object,
172
- default: () => { },
173
- },
174
- /**
175
- * Show Linter Gutter
176
- *
177
- * An area to 🔴 the lines with errors will be displayed.
178
- * This feature is not enabled if `linter` is not specified.
179
- *
180
- * @see {@link https://codemirror.net/docs/ref/#lint.lintGutter}
181
- */
182
- gutter: {
183
- type: Boolean,
184
- defalt: false,
185
- },
186
- /**
187
- * Gutter Config
188
- *
189
- * @see {@link https://codemirror.net/docs/ref/#lint.lintGutter^config}
190
- */
191
- gutterConfig: {
192
- type: Object,
193
- default: () => { },
194
- },
195
- /**
196
- * Using tag
197
- */
198
- tag: {
199
- type: String,
200
- default: 'div',
201
- },
202
- },
203
- /** Emits */
204
- emits: ['update:modelValue', 'update', 'ready', 'focus', 'change', 'destroy'],
205
- /**
206
- * Setup
207
- *
208
- * @param props - Props
209
- * @param context - Context
210
- */
211
- setup(props, context) {
212
- /** Editor DOM */
213
- const editor = ref();
214
- /** Internal value */
215
- const doc = ref(props.modelValue);
216
- /**
217
- * CodeMirror Editor View
218
- *
219
- * @see {@link https://codemirror.net/docs/ref/#view.EditorView}
220
- */
221
- const view = shallowRef(new EditorView());
222
- /**
223
- * Editor Selection
224
- *
225
- * @see {@link https://codemirror.net/docs/ref/#state.EditorSelection}
226
- */
227
- const selection = computed({
228
- get: () => view.value.state.selection,
229
- set: s => view.value.dispatch({ selection: s }),
230
- });
231
- /**
232
- * Editor State
233
- *
234
- * @see {@link https://codemirror.net/docs/ref/#state.EditorState}
235
- */
236
- const state = computed({
237
- get: () => view.value.state,
238
- set: s => view.value.setState(s),
239
- });
240
- /** Cursor Position */
241
- const cursor = computed({
242
- get: () => view.value.state.selection.main.head || 0,
243
- set: a => view.value.dispatch({ selection: { anchor: a } }),
244
- });
245
- /**
246
- * Focus
247
- *
248
- * @see {@link https://codemirror.net/docs/ref/#view.EditorView.hasFocus}
249
- */
250
- const focus = computed({
251
- get: () => view.value.hasFocus,
252
- set: f => {
253
- if (f) {
254
- view.value.focus();
255
- }
256
- },
257
- });
258
- /** Text length */
259
- const length = computed(() => view.value.state.doc.length);
260
- /** Get CodeMirror Extension */
261
- const extensions = computed(() =>
262
- // TODO: Ignore previous prop was not changed.
263
- compact([
264
- // Toggle basic setup
265
- props.basic ? basicSetup : undefined,
266
- // Toggle minimal setup
267
- props.minimal && !props.basic ? minimalSetup : undefined,
268
- // ViewUpdate event listener
269
- EditorView.updateListener.of((update) => context.emit('update', update)),
270
- // Toggle light/dark mode.
271
- EditorView.theme(props.theme, { dark: props.dark }),
272
- // Toggle line wrapping
273
- props.wrap ? EditorView.lineWrapping : undefined,
274
- // Indent with tab
275
- props.tab ? keymap.of([indentWithTab]) : undefined,
276
- // Indent tab size
277
- props.tabSize ? EditorState.tabSize.of(props.tabSize) : undefined,
278
- // locale settings
279
- props.phrases ? EditorState.phrases.of(props.phrases) : undefined,
280
- // Readonly option
281
- EditorState.readOnly.of(props.readonly),
282
- // Editable option
283
- EditorView.editable.of(!props.disabled),
284
- // Set Line break char
285
- props.lineSeparator
286
- ? EditorState.lineSeparator.of(props.lineSeparator)
287
- : undefined,
288
- // Lang
289
- props.lang ? props.lang : undefined,
290
- // Append Linter settings
291
- props.linter ? linter(props.linter, props.linterConfig) : undefined,
292
- // Show 🔴 to error line when linter enabled.
293
- props.linter && props.gutter
294
- ? lintGutter(props.gutterConfig)
295
- : undefined,
296
- // Placeholder
297
- props.placeholder ? placeholder(props.placeholder) : undefined,
298
- // Append Extensions (such as basic-setup)
299
- ...props.extensions,
300
- ]));
301
- // for parent-to-child binding.
302
- watch(() => props.modelValue, value => {
303
- if (view.value.composing) {
304
- // IME fix
305
- return;
306
- }
307
- // Update
308
- view.value.dispatch({
309
- changes: { from: 0, to: view.value.state.doc.length, insert: value },
310
- selection: view.value.state.selection,
311
- scrollIntoView: true,
312
- });
313
- }, { immediate: true });
314
- // Extension (mostly props) Changed
315
- watch(extensions, exts =>
316
- // TODO: Reduce unchanched value
317
- view.value.dispatch({
318
- effects: StateEffect.reconfigure.of(exts),
319
- }));
320
- // focus changed
321
- watch(focus, isFocus => context.emit('focus', isFocus));
322
- /** When loaded */
323
- onMounted(async () => {
324
- /** Initial value */
325
- let value = doc.value;
326
- if (editor.value && editor.value.childNodes[0]) {
327
- // when slot mode, overwrite initial value
328
- if (doc.value !== '') {
329
- console.warn('[CodeMirror.vue] The <code-mirror> tag contains child elements that overwrite the `v-model` values.');
330
- }
331
- value = trim(editor.value.childNodes[0].innerText);
332
- }
333
- // Register Codemirror
334
- view.value = new EditorView({
335
- doc: value,
336
- extensions: extensions.value,
337
- parent: editor.value,
338
- dispatch: tr => {
339
- view.value.update([tr]);
340
- // TODO: Emit lint error event
341
- // console.log(view.state.doc.toString(), tr);
342
- if (tr.changes.empty)
343
- return;
344
- // child-to-parent binding
345
- const v = view.value.state.doc.toString();
346
- context.emit('update:modelValue', v);
347
- context.emit('change', view.value.state);
348
- },
349
- });
350
- await nextTick();
351
- context.emit('ready', {
352
- view: view,
353
- state: view.value.state,
354
- container: editor.value,
355
- });
356
- });
357
- /** Destroy */
358
- onUnmounted(() => {
359
- if (view.value) {
360
- view.value.destroy();
361
- context.emit('destroy');
362
- }
363
- });
364
- /**
365
- * Forces any linters configured to run when the editor is idle to run right away.
366
- *
367
- * @see {@link https://codemirror.net/docs/ref/#lint.forceLinting}
368
- */
369
- const lint = () => {
370
- if (props.linter) {
371
- forceLinting(view.value);
372
- }
373
- };
374
- /**
375
- * Force Reconfigure Extension
376
- *
377
- * @see {@link https://codemirror.net/examples/config/#top-level-reconfiguration}
378
- */
379
- const forceReconfigure = () => {
380
- // Deconfigure all Extensions
381
- view.value.dispatch({
382
- effects: StateEffect.reconfigure.of([]),
383
- });
384
- // Register extensions
385
- view.value.dispatch({
386
- effects: StateEffect.appendConfig.of(extensions.value),
387
- });
388
- };
389
- /* ----- Bellow is experimental. ------ */
390
- /**
391
- * Get the text between the given points in the editor.
392
- *
393
- * @param from - start line number
394
- * @param to - end line number
395
- */
396
- const getRange = (from, to) => view.value.state.sliceDoc(from, to);
397
- /**
398
- * Get the content of line.
399
- *
400
- * @param number - line number
401
- */
402
- const getLine = (number) => view.value.state.doc.line(number + 1).text;
403
- /** Get the number of lines in the editor. */
404
- const lineCount = () => view.value.state.doc.lines;
405
- /** Retrieve one end of the primary selection. */
406
- const getCursor = () => cursor.value;
407
- /** Retrieves a list of all current selections. */
408
- const listSelections = () => view.value.state.selection.ranges;
409
- /** Get the currently selected code. */
410
- const getSelection = () => view.value.state.sliceDoc(view.value.state.selection.main.from, view.value.state.selection.main.to);
411
- /**
412
- * The length of the given array should be the same as the number of active selections.
413
- * Replaces the content of the selections with the strings in the array.
414
- */
415
- const getSelections = () => view.value.state.selection.ranges.map((r) => view.value.state.sliceDoc(r.from, r.to));
416
- /** Return true if any text is selected. */
417
- const somethingSelected = () => view.value.state.selection.ranges.some((r) => !r.empty);
418
- /**
419
- * Replace the part of the document between from and to with the given string.
420
- *
421
- * @param replacement - replacement text
422
- * @param from - start string at position
423
- * @param to - insert the string at position
424
- */
425
- const replaceRange = (replacement, from, to) => view.value.dispatch({
426
- changes: { from, to, insert: replacement },
427
- });
428
- /**
429
- * Replace the selection(s) with the given string.
430
- * By default, the new selection ends up after the inserted text.
431
- *
432
- * @param replacement - replacement text
433
- */
434
- const replaceSelection = (replacement) => view.value.dispatch(view.value.state.replaceSelection(replacement));
435
- /**
436
- * Set the cursor position.
437
- *
438
- * @param position - position.
439
- */
440
- const setCursor = (position) => (cursor.value = position);
441
- /**
442
- * Set a single selection range.
443
- *
444
- * @param anchor - anchor position
445
- * @param head -
446
- */
447
- const setSelection = (anchor, head) => view.value.dispatch({ selection: { anchor, head } });
448
- /**
449
- * Sets a new set of selections. There must be at least one selection in the given array.
450
- *
451
- * @param ranges - Selection range
452
- * @param primary -
453
- */
454
- const setSelections = (ranges, primary) => view.value.dispatch({
455
- selection: EditorSelection.create(ranges, primary),
456
- });
457
- /**
458
- * Applies the given function to all existing selections, and calls extendSelections on the result.
459
- *
460
- * @param f - function
461
- */
462
- const extendSelectionsBy = (f) => view.value.dispatch({
463
- selection: EditorSelection.create(selection.value.ranges.map((r) => r.extend(f(r)))),
464
- });
465
- /** Export properties and functions */
466
- context.expose({
467
- editor,
468
- cursor,
469
- selection,
470
- state,
471
- focus,
472
- length,
473
- lint,
474
- forceReconfigure,
475
- // Bellow is CodeMirror5's function
476
- getRange,
477
- getLine,
478
- lineCount,
479
- getCursor,
480
- listSelections,
481
- getSelection,
482
- getSelections,
483
- somethingSelected,
484
- replaceRange,
485
- replaceSelection,
486
- setCursor,
487
- setSelection,
488
- setSelections,
489
- extendSelectionsBy,
490
- });
491
- return {
492
- editor,
493
- };
494
- },
495
- render() {
496
- // <template>
497
- // <div ref="editor" class="vue-codemirror">
498
- // <aside v-show="!context.slots.default" aria-hidden><slot /></aside>
499
- // </div>
500
- // </template>
501
- return h(this.$props.tag, {
502
- ref: 'editor',
503
- class: 'vue-codemirror',
504
- }, this.$slots.default
505
- ? // Hide original content
506
- h('aside', { style: 'display: none;', 'aria-hidden': 'true' }, slot(this.$slots.default))
507
- : undefined);
508
- },
509
- });
@@ -1,27 +0,0 @@
1
- /**
2
- * h-demi - h function for Vue 2 and 3
3
- *
4
- * @see {@link https://github.com/vueuse/vue-demi/issues/65}
5
- */
6
- import { h as hDemi, isVue2, Vue2 } from 'vue-demi';
7
- const adaptOnsV3 = (ons) => {
8
- if (!ons)
9
- return {};
10
- return Object.entries(ons).reduce((ret, [key, handler]) => {
11
- key = key.charAt(0).toUpperCase() + key.slice(1);
12
- key = `on${key}`;
13
- return { ...ret, [key]: handler };
14
- }, {});
15
- };
16
- const h = (type, options = {}, chidren) => {
17
- if (isVue2 && parseInt(Vue2.version) < 2.7) {
18
- // Makeshift support :(
19
- // Since Vue2.7 includes the Composition API, the functions in vue-demi are not used.
20
- return hDemi(type, options, chidren);
21
- }
22
- const { props, domProps, on, ...extraOptions } = options;
23
- const ons = on ? adaptOnsV3(on) : {};
24
- return hDemi(type, { ...extraOptions, ...props, ...domProps, ...ons }, chidren);
25
- };
26
- const slot = (defaultSlots) => typeof defaultSlots == 'function' ? defaultSlots() : defaultSlots;
27
- export { slot, h as default };
package/dist/index.js DELETED
@@ -1,11 +0,0 @@
1
- import CodeMirror from './components/CodeMirror';
2
- import Meta from './Meta';
3
- // TODO: Move phrases props to option.
4
- const installCodeMirror = (app) => app.component('CodeMirror', CodeMirror);
5
- export { CodeMirror as default, installCodeMirror as install, Meta };
6
- // For CDN. (Maybe Vue2 only)
7
- // @ts-ignore
8
- if (typeof window !== 'undefined' && window.Vue) {
9
- // @ts-ignore
10
- window.Vue.use(CodeMirror);
11
- }
@@ -1 +0,0 @@
1
- export {};