9
9
unref ,
10
10
watch ,
11
11
} from 'vue-demi'
12
- import type { PropType } from 'vue-demi'
12
+ import type { HTMLAttributes , PropType } from 'vue-demi'
13
13
import { JSONEditor } from 'vanilla-jsoneditor'
14
14
import { conclude } from 'vue-global-config'
15
15
import { debounce } from 'lodash-es'
@@ -18,31 +18,32 @@ import { globalAttrs, globalProps } from './install'
18
18
19
19
export type Mode = 'tree' | 'text' | 'table'
20
20
21
- const modelValueProp = isVue3 ? 'modelValue' : 'value'
22
- const updateModelValue = isVue3 ? 'update:modelValue' : 'input'
23
- const boolAttrs = [
21
+ type ModalValue = typeof isVue3 extends true ? 'modelValue' : 'value'
22
+ type UpdateModalValue = typeof isVue3 extends true ? 'update:modelValue' : 'input'
23
+
24
+ const modelValueProp : ModalValue = ( isVue3 ? 'modelValue' : 'value' ) as any
25
+ const updateModelValue : UpdateModalValue = ( isVue3 ? 'update:modelValue' : 'input' ) as any
26
+
27
+ const boolAttributes = [
24
28
'mainMenuBar' ,
25
29
'navigationBar' ,
26
30
'statusBar' ,
27
31
'readOnly' ,
28
32
'escapeControlCharacters' ,
29
33
'escapeUnicodeCharacters' ,
30
34
'flattenColumns' ,
31
- ]
35
+ ] as const
36
+
37
+ type Attributes = ModalValue | UpdateModalValue
38
+ type BoolAttributes = {
39
+ [ K in typeof boolAttributes [ number ] ] : {
40
+ type : Boolean
41
+ default : undefined
42
+ }
43
+ }
32
44
33
- export default defineComponent ( {
45
+ export default defineComponent < HTMLAttributes & { mode : PropType < Mode > } & { [ K in Attributes ] : any } & BoolAttributes , { } , { } , { } , { } , { } , { } , { 'update:mode' : ( mode : Mode ) => void ; 'update:modalValue' : ( value : unknown ) => void ; 'input' : ( value : unknown ) => void } > ( {
34
46
name,
35
- props : {
36
- [ modelValueProp ] : { } ,
37
- mode : {
38
- type : String as PropType < Mode > ,
39
- } ,
40
- ...Object . fromEntries ( Array . from ( boolAttrs , boolAttr => [ boolAttr , {
41
- type : Boolean ,
42
- default : undefined ,
43
- } ] ) ) ,
44
- } ,
45
- emits : [ updateModelValue , 'update:mode' ] ,
46
47
setup ( props , { attrs, emit, expose } ) {
47
48
const currentInstance = getCurrentInstance ( ) ?. proxy
48
49
const jsonEditor = ref ( )
@@ -54,7 +55,7 @@ export default defineComponent({
54
55
type : String as PropType < Mode > ,
55
56
} )
56
57
const initialValue = conclude ( [ props [ modelValueProp ] , globalProps [ modelValueProp ] ] )
57
- const initialBoolAttrs = Object . fromEntries ( Array . from ( boolAttrs , boolAttr =>
58
+ const initialBoolAttrs = Object . fromEntries ( Array . from ( boolAttributes , boolAttr =>
58
59
[ boolAttr , conclude ( [ props [ boolAttr ] , globalProps [ boolAttr ] ] ) ] )
59
60
. filter ( ( [ , v ] ) => v !== undefined ) )
60
61
@@ -64,7 +65,7 @@ export default defineComponent({
64
65
return
65
66
}
66
67
preventUpdate . value = true
67
- emit ( updateModelValue , updatedContent . text === undefined
68
+ emit ( updateModelValue as any , updatedContent . text === undefined
68
69
? updatedContent . json
69
70
: updatedContent . text )
70
71
} , 100 )
@@ -117,9 +118,9 @@ export default defineComponent({
117
118
} )
118
119
} )
119
120
120
- watch ( ( ) => Array . from ( boolAttrs , boolAttr => props [ boolAttr ] ) , ( values ) => {
121
+ watch ( ( ) => Array . from ( boolAttributes , boolAttr => props [ boolAttr ] ) , ( values ) => {
121
122
jsonEditor . value . updateProps ( Object . fromEntries ( Array . from ( values , ( v , i ) =>
122
- [ boolAttrs [ i ] , v ] ) . filter ( ( [ , v ] ) => v !== undefined ) ) )
123
+ [ boolAttributes [ i ] , v ] ) . filter ( ( [ , v ] ) => v !== undefined ) ) )
123
124
} )
124
125
125
126
watch ( ( ) => attrs , ( newAttrs ) => {
0 commit comments