1+ import type { JSONContent , JSONEditorPropsOptional , TextContent } from 'vanilla-jsoneditor'
2+ import type { App , Plugin , PropType } from 'vue-demi'
13import { destr , safeDestr } from 'destr'
24import { debounce } from 'lodash-es'
35import { JSONEditor , Mode } from 'vanilla-jsoneditor'
46import { computed , defineComponent , getCurrentInstance , h , isVue3 , onMounted , onUnmounted , ref , unref , watch , watchEffect } from 'vue-demi'
57import { conclude , resolveConfig } from 'vue-global-config'
6- import type { JSONEditorPropsOptional } from 'vanilla-jsoneditor'
7- import type { App , Plugin , PropType } from 'vue-demi'
88import { PascalCasedName as name } from '../package.json'
99
1010type SFCWithInstall < T > = T & Plugin
11+ type UpdatedContent = JSONContent & Partial < TextContent >
1112interface Parser { parse : typeof destr , stringify : typeof JSON . stringify }
1213
1314const propsGlobal : Record < string , any > = { }
@@ -19,11 +20,11 @@ enum ModelValueProp {
1920}
2021const modelValueProp : ModelValueProp = isVue3 ? ModelValueProp . vue3 : ModelValueProp . vue2
2122
22- enum UpdateModelValue {
23+ enum UpdateModelValueEvent {
2324 vue3 = 'update:modelValue' ,
2425 vue2 = 'input' ,
2526}
26- const updateModelValue = isVue3 ? UpdateModelValue . vue3 : UpdateModelValue . vue2
27+ const updateModelValueEvent = isVue3 ? UpdateModelValueEvent . vue3 : UpdateModelValueEvent . vue2
2728
2829const boolAttrs = [
2930 'mainMenuBar' ,
@@ -80,7 +81,7 @@ const JsonEditorVue = defineComponent({
8081 } ,
8182 props,
8283 emits : {
83- [ updateModelValue ] ( _payload : any ) {
84+ [ updateModelValueEvent ] ( _payload : any ) {
8485 return true
8586 } ,
8687 'update:mode' : function ( _payload : Mode ) {
@@ -118,7 +119,8 @@ const JsonEditorVue = defineComponent({
118119 type : Boolean as PropType < boolean > ,
119120 } ) )
120121 let parse = destr
121- const onChange = debounce ( ( updatedContent : { json ?: any , text ?: string } ) => {
122+
123+ const updateModelValue = ( updatedContent : UpdatedContent ) => {
122124 preventUpdatingContent . value = true
123125 if ( ! stringifiedComputed . value && updatedContent . text ) {
124126 if ( jsonEditor . value && ! jsonEditor . value . validate ( ) ) {
@@ -127,12 +129,22 @@ const JsonEditorVue = defineComponent({
127129 updatedContent . text = undefined
128130 }
129131 emit (
130- updateModelValue ,
132+ updateModelValueEvent ,
131133 updatedContent . text === undefined
132134 ? updatedContent . json
133135 : updatedContent . text ,
134136 )
135- } , debounceComputed . value )
137+ }
138+ const updateModelValueDebounced = debounce ( updateModelValue , debounceComputed . value )
139+
140+ const onChange = ( updatedContent : UpdatedContent ) => {
141+ if ( modeComputed . value === 'text' ) {
142+ updateModelValueDebounced ( updatedContent )
143+ }
144+ else {
145+ updateModelValue ( updatedContent )
146+ }
147+ }
136148
137149 const mergeFunction = ( accumulator : ( ...args : any ) => unknown , currentValue : ( ...args : any ) => unknown ) => ( ...args : any ) => {
138150 accumulator ( ...args )
0 commit comments