Skip to content

Commit fbfa2cb

Browse files
committed
fix: v-model not updating codemirror instance if changed externally
1 parent c7357d0 commit fbfa2cb

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

playground/app.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,8 @@ onMounted(() => {
5757
@on-update="handleUpdate"
5858
/>
5959
<div>{{ code }}</div>
60+
<input
61+
v-model="code"
62+
type="text"
63+
>
6064
</template>

src/runtime/components/NuxtCodeMirror.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const emit = defineEmits<{
3434
onMounted(() => {
3535
useNuxtCodeMirror({
3636
...props,
37-
modelValue: modelValue.value,
37+
modelValue: modelValue,
3838
onChange: (value, viewUpdate) => {
3939
modelValue.value = value
4040
emit('onChange', value, viewUpdate)

src/runtime/composables/useNuxtCodeMirror.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const emptyExtensions: Extension[] = []
1111

1212
export function useNuxtCodeMirror(props: UseCodeMirrorProps) {
1313
const {
14-
modelValue: value = '',
14+
modelValue: value,
1515
selection,
1616
onChange,
1717
onStatistics,
@@ -58,7 +58,7 @@ export function useNuxtCodeMirror(props: UseCodeMirrorProps) {
5858
if (
5959
viewUpdate.docChanged
6060
&& typeof onChange === 'function'
61-
// Fix echoing of the remote changes:
61+
// Fix echoing/ infinite update loops of the remote changes:
6262
// If transaction is market as remote we don't have to call `onChange` handler again
6363
&& !viewUpdate.transactions.some(tr => tr.annotation(External))
6464
) {
@@ -98,7 +98,7 @@ export function useNuxtCodeMirror(props: UseCodeMirrorProps) {
9898
watchEffect(() => {
9999
if (containerRef.value && !stateRef?.value) {
100100
const config = {
101-
doc: value,
101+
doc: value?.value,
102102
selection,
103103
extensions: getExtensions,
104104
}
@@ -158,9 +158,9 @@ export function useNuxtCodeMirror(props: UseCodeMirrorProps) {
158158
}
159159

160160
const currentValue = viewRef.value ? viewRef.value.state.doc.toString() : ''
161-
if (viewRef.value && value !== currentValue) {
161+
if (viewRef.value && value.value !== currentValue) {
162162
viewRef.value.dispatch({
163-
changes: { from: 0, to: currentValue.length, insert: value || '' },
163+
changes: { from: 0, to: currentValue.length, insert: value.value || '' },
164164
annotations: [External.of(true)],
165165
})
166166
}

src/runtime/types/nuxt-codemirror.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { EditorState, EditorStateConfig, Extension, StateField, EditorSelection, SelectionRange, Line } from '@codemirror/state'
22
import type { EditorView, ViewUpdate } from '@codemirror/view'
33
import type { BasicSetupOptions } from '@uiw/codemirror-extensions-basic-setup'
4+
import type { ModelRef } from 'vue'
45
import type { Ref } from '#imports'
56

67
export interface NuxtCodeMirrorProps
78
extends Omit<EditorStateConfig, 'doc' | 'extensions'> {
89
/** value of the auto created model in the editor. Works as underlying logic of a V-Model */
9-
modelValue?: string
10+
modelValue?: ModelRef<string | undefined, string>
1011
/** The height value of the editor. */
1112
height?: string
1213
/** The minimum height value of the editor. */

0 commit comments

Comments
 (0)