@@ -23,7 +23,7 @@ import {
23
23
isValueSelection ,
24
24
singleItemSelected
25
25
} from '$lib/logic/selection'
26
- import type { DocumentState , InsertType , JSONParser } from 'svelte-jsoneditor '
26
+ import type { ConvertType , DocumentState , InsertType , JSONParser } from '$lib/types '
27
27
import { initial , isEmpty } from 'lodash-es'
28
28
import { compileJSONPointer , getIn } from 'immutable-json-patch'
29
29
import { isObjectOrArray , isObject } from '$lib/utils/typeUtils'
@@ -33,8 +33,8 @@ import type { ContextMenuItem } from 'svelte-jsoneditor'
33
33
export default function ( {
34
34
json,
35
35
documentState,
36
+ readOnly,
36
37
parser,
37
-
38
38
onEditKey,
39
39
onEditValue,
40
40
onToggleEnforceString,
@@ -53,8 +53,8 @@ export default function ({
53
53
} : {
54
54
json : unknown
55
55
documentState : DocumentState
56
+ readOnly : boolean
56
57
parser : JSONParser
57
-
58
58
onEditKey : ( ) => void
59
59
onEditValue : ( ) => void
60
60
onToggleEnforceString : ( ) => void
@@ -66,7 +66,7 @@ export default function ({
66
66
onExtract : ( ) => void
67
67
onInsertBefore : ( ) => void
68
68
onInsert : ( type : InsertType ) => void
69
- onConvert : ( type : InsertType ) => void
69
+ onConvert : ( type : ConvertType ) => void
70
70
onInsertAfter : ( ) => void
71
71
onSort : ( ) => void
72
72
onTransform : ( ) => void
@@ -87,36 +87,38 @@ export default function ({
87
87
hasJson &&
88
88
( isMultiSelection ( selection ) || isKeySelection ( selection ) || isValueSelection ( selection ) )
89
89
90
- const canDuplicate = hasJson && hasSelectionContents && ! rootSelected // must not be root
91
- const canExtract =
92
- hasJson &&
93
- selection != null &&
94
- ( isMultiSelection ( selection ) || isValueSelection ( selection ) ) &&
95
- ! rootSelected // must not be root
96
-
97
90
const canEditKey =
91
+ ! readOnly &&
98
92
hasJson &&
99
93
selection != null &&
100
94
singleItemSelected ( selection ) &&
101
95
! rootSelected &&
102
96
! Array . isArray ( getIn ( json , initial ( getFocusPath ( selection ) ) ) )
103
97
104
- const canEditValue = hasJson && selection != null && singleItemSelected ( selection )
98
+ const canEditValue = ! readOnly && hasJson && selection != null && singleItemSelected ( selection )
105
99
const canEnforceString = canEditValue && ! isObjectOrArray ( focusValue )
106
100
101
+ const canCut = ! readOnly && hasSelectionContents
102
+ const canCopy = hasSelectionContents
103
+ const canPaste = ! readOnly && hasSelection
104
+ const canDuplicate = ! readOnly && hasJson && hasSelectionContents && ! rootSelected // must not be root
105
+ const canExtract =
106
+ ! readOnly &&
107
+ hasJson &&
108
+ selection != null &&
109
+ ( isMultiSelection ( selection ) || isValueSelection ( selection ) ) &&
110
+ ! rootSelected // must not be root
111
+
107
112
const convertMode = hasSelectionContents
108
113
const insertOrConvertText = convertMode ? 'Convert to:' : 'Insert:'
109
114
110
- const canInsertOrConvertStructure = convertMode ? false : hasSelection
111
- const canInsertOrConvertObject = convertMode
112
- ? canConvert ( selection ) && ! isObject ( focusValue )
113
- : hasSelection
114
- const canInsertOrConvertArray = convertMode
115
- ? canConvert ( selection ) && ! Array . isArray ( focusValue )
116
- : hasSelection
117
- const canInsertOrConvertValue = convertMode
118
- ? canConvert ( selection ) && isObjectOrArray ( focusValue )
119
- : hasSelection
115
+ const canInsertOrConvertStructure = readOnly || convertMode ? false : hasSelection
116
+ const canInsertOrConvertObject =
117
+ ! readOnly && ( convertMode ? canConvert ( selection ) && ! isObject ( focusValue ) : hasSelection )
118
+ const canInsertOrConvertArray =
119
+ ! readOnly && ( convertMode ? canConvert ( selection ) && ! Array . isArray ( focusValue ) : hasSelection )
120
+ const canInsertOrConvertValue =
121
+ ! readOnly && ( convertMode ? canConvert ( selection ) && isObjectOrArray ( focusValue ) : hasSelection )
120
122
121
123
const enforceString =
122
124
selection != null && focusValue
@@ -130,7 +132,9 @@ export default function ({
130
132
131
133
function handleInsertOrConvert ( type : InsertType ) {
132
134
if ( hasSelectionContents ) {
133
- onConvert ( type )
135
+ if ( type !== 'structure' ) {
136
+ onConvert ( type )
137
+ }
134
138
} else {
135
139
onInsert ( type )
136
140
}
@@ -192,7 +196,7 @@ export default function ({
192
196
icon : faCut ,
193
197
text : 'Cut' ,
194
198
title : 'Cut selected contents, formatted with indentation (Ctrl+X)' ,
195
- disabled : ! hasSelectionContents
199
+ disabled : ! canCut
196
200
} ,
197
201
width : '10em' ,
198
202
items : [
@@ -202,15 +206,15 @@ export default function ({
202
206
text : 'Cut formatted' ,
203
207
title : 'Cut selected contents, formatted with indentation (Ctrl+X)' ,
204
208
onClick : ( ) => onCut ( true ) ,
205
- disabled : ! hasSelectionContents
209
+ disabled : ! canCut
206
210
} ,
207
211
{
208
212
type : 'button' ,
209
213
icon : faCut ,
210
214
text : 'Cut compacted' ,
211
215
title : 'Cut selected contents, without indentation (Ctrl+Shift+X)' ,
212
216
onClick : ( ) => onCut ( false ) ,
213
- disabled : ! hasSelectionContents
217
+ disabled : ! canCut
214
218
}
215
219
]
216
220
} ,
@@ -222,7 +226,7 @@ export default function ({
222
226
icon : faCopy ,
223
227
text : 'Copy' ,
224
228
title : 'Copy selected contents, formatted with indentation (Ctrl+C)' ,
225
- disabled : ! hasSelectionContents
229
+ disabled : ! canCopy
226
230
} ,
227
231
width : '12em' ,
228
232
items : [
@@ -232,15 +236,15 @@ export default function ({
232
236
text : 'Copy formatted' ,
233
237
title : 'Copy selected contents, formatted with indentation (Ctrl+C)' ,
234
238
onClick : ( ) => onCopy ( true ) ,
235
- disabled : ! hasSelectionContents
239
+ disabled : ! canCopy
236
240
} ,
237
241
{
238
242
type : 'button' ,
239
243
icon : faCopy ,
240
244
text : 'Copy compacted' ,
241
245
title : 'Copy selected contents, without indentation (Ctrl+Shift+C)' ,
242
246
onClick : ( ) => onCopy ( false ) ,
243
- disabled : ! hasSelectionContents
247
+ disabled : ! canCopy
244
248
}
245
249
]
246
250
} ,
@@ -250,7 +254,7 @@ export default function ({
250
254
icon : faPaste ,
251
255
text : 'Paste' ,
252
256
title : 'Paste clipboard contents (Ctrl+V)' ,
253
- disabled : ! hasSelection
257
+ disabled : ! canPaste
254
258
}
255
259
]
256
260
} ,
@@ -283,23 +287,23 @@ export default function ({
283
287
icon : faSortAmountDownAlt ,
284
288
text : 'Sort' ,
285
289
title : 'Sort array or object contents' ,
286
- disabled : ! hasSelectionContents
290
+ disabled : readOnly || ! hasSelectionContents
287
291
} ,
288
292
{
289
293
type : 'button' ,
290
294
onClick : ( ) => onTransform ( ) ,
291
295
icon : faFilter ,
292
296
text : 'Transform' ,
293
297
title : 'Transform array or object contents (filter, sort, project)' ,
294
- disabled : ! hasSelectionContents
298
+ disabled : readOnly || ! hasSelectionContents
295
299
} ,
296
300
{
297
301
type : 'button' ,
298
302
onClick : ( ) => onRemove ( ) ,
299
303
icon : faTrashCan ,
300
304
text : 'Remove' ,
301
305
title : 'Remove selected contents (Delete)' ,
302
- disabled : ! hasSelectionContents
306
+ disabled : readOnly || ! hasSelectionContents
303
307
}
304
308
]
305
309
} ,
@@ -355,15 +359,15 @@ export default function ({
355
359
icon : faCaretSquareUp ,
356
360
text : 'Insert before' ,
357
361
title : 'Select area before current entry to insert or paste contents' ,
358
- disabled : ! hasSelectionContents || rootSelected
362
+ disabled : readOnly || ! hasSelectionContents || rootSelected
359
363
} ,
360
364
{
361
365
type : 'button' ,
362
366
onClick : ( ) => onInsertAfter ( ) ,
363
367
icon : faCaretSquareDown ,
364
368
text : 'Insert after' ,
365
369
title : 'Select area after current entry to insert or paste contents' ,
366
- disabled : ! hasSelectionContents || rootSelected
370
+ disabled : readOnly || ! hasSelectionContents || rootSelected
367
371
}
368
372
]
369
373
}
0 commit comments