-
Notifications
You must be signed in to change notification settings - Fork 109
/
types.ts
774 lines (663 loc) · 19 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
import type { JSONPatchDocument, JSONPath } from 'immutable-json-patch'
import type { SvelteComponent } from 'svelte'
import type { IconDefinition } from '@fortawesome/free-solid-svg-icons'
import type { Action } from 'svelte/action'
export type TextContent = { text: string }
export type JSONContent = { json: unknown }
export type Content = JSONContent | TextContent
// The `JSONParser` interface is compatible with `JSON`,
// except that JSON.stringify is wrongly defined to return a string whilst it can return a string or undefined
// see: https://stackoverflow.com/questions/74461780/is-the-official-type-definition-for-json-stringify-wrong
export interface JSONParser {
parse(
text: string,
reviver?: ((this: unknown, key: string, value: unknown) => unknown) | null
): unknown
stringify(
value: unknown,
replacer?:
| ((this: unknown, key: string, value: unknown) => unknown)
| Array<number | string>
| null,
space?: string | number
): string | undefined
}
export interface JSONPathParser {
parse: (pathStr: string) => JSONPath
stringify: (path: JSONPath) => string
}
export interface VisibleSection {
start: number
end: number
}
export enum Mode {
text = 'text',
tree = 'tree',
table = 'table'
}
export enum SelectionType {
after = 'after',
inside = 'inside',
key = 'key',
value = 'value',
multi = 'multi',
text = 'text' // in text mode
}
export enum CaretType {
after = 'after',
key = 'key',
value = 'value',
inside = 'inside'
}
export interface PathOption {
value: JSONPath
label: string
}
export interface NumberOption {
value: 1 | -1
label: string
}
export interface CaretPosition {
path: JSONPath
type: CaretType // TODO: refactor this to use SelectionType here, then we can simplify the util functions to turn this into a selection
}
export interface ObjectRecursiveState {
type: 'object'
properties: Record<string, RecursiveState | undefined>
}
export interface ArrayRecursiveState {
type: 'array'
items: Array<RecursiveState | undefined>
}
export interface ValueRecursiveState {
type: 'value'
}
export type RecursiveState = ObjectRecursiveState | ArrayRecursiveState | ValueRecursiveState
export interface RecursiveStateFactory {
createObjectDocumentState: () => ObjectRecursiveState
createArrayDocumentState: () => ArrayRecursiveState
createValueDocumentState: () => ValueRecursiveState
}
export interface ObjectDocumentState extends ObjectRecursiveState {
type: 'object'
properties: Record<string, DocumentState | undefined>
expanded: boolean
}
export interface ArrayDocumentState extends ArrayRecursiveState {
type: 'array'
items: Array<DocumentState | undefined>
expanded: boolean
visibleSections: VisibleSection[]
}
export interface ValueDocumentState extends ValueRecursiveState {
type: 'value'
enforceString?: boolean
}
export type DocumentState = ObjectDocumentState | ArrayDocumentState | ValueDocumentState
export interface ObjectSearchResults extends ObjectRecursiveState {
type: 'object'
properties: Record<string, SearchResults | undefined>
searchResults?: ExtendedSearchResultItem[]
}
export interface ArraySearchResults extends ArrayRecursiveState {
type: 'array'
items: Array<SearchResults | undefined>
searchResults?: ExtendedSearchResultItem[]
}
export interface ValueSearchResults extends ValueRecursiveState {
type: 'value'
searchResults?: ExtendedSearchResultItem[]
}
export type SearchResults = ObjectSearchResults | ArraySearchResults | ValueSearchResults
export type WithSearchResults = SearchResults & {
searchResults: ExtendedSearchResultItem[]
}
export interface ObjectValidationErrors extends ObjectRecursiveState {
type: 'object'
properties: Record<string, ValidationErrors | undefined>
validationError?: NestedValidationError
}
export interface ArrayValidationErrors extends ArrayRecursiveState {
type: 'array'
items: Array<ValidationErrors | undefined>
validationError?: NestedValidationError
}
export interface ValueValidationErrors extends ValueRecursiveState {
type: 'value'
validationError?: NestedValidationError
}
export type ValidationErrors =
| ObjectValidationErrors
| ArrayValidationErrors
| ValueValidationErrors
export interface JSONPatchResult {
json: unknown
previousJson: unknown
undo: JSONPatchDocument
redo: JSONPatchDocument
}
export type AfterPatchCallback = (
patchedJson: unknown,
patchedState: DocumentState | undefined,
patchedSelection: JSONSelection | undefined
) =>
| {
json?: unknown
state?: DocumentState | undefined
selection?: JSONSelection | undefined
sortedColumn?: SortedColumn | undefined
}
| undefined
export interface MultiSelection {
type: SelectionType.multi
anchorPath: JSONPath
focusPath: JSONPath
}
export interface AfterSelection {
type: SelectionType.after
path: JSONPath
}
export interface InsideSelection {
type: SelectionType.inside
path: JSONPath
}
export interface KeySelection {
type: SelectionType.key
path: JSONPath
}
export interface EditKeySelection extends KeySelection {
type: SelectionType.key
path: JSONPath
edit: true
initialValue?: string
}
export type ValueSelection = {
type: SelectionType.value
path: JSONPath
}
export interface EditValueSelection extends ValueSelection {
type: SelectionType.value
path: JSONPath
edit: true
initialValue?: string
}
export type JSONSelection =
| MultiSelection
| AfterSelection
| InsideSelection
| KeySelection
| EditKeySelection
| ValueSelection
| EditValueSelection
// TextSelection is the result of EditorSelection.toJSON() from CodeMirror,
// with an additional `type` property
export interface TextSelection {
type: SelectionType.text
ranges: { anchor: number; head: number }[]
main: number
}
export type JSONEditorSelection = JSONSelection | TextSelection
export type ClipboardValues = Array<{ key: string; value: unknown }>
export interface MenuButton {
type: 'button'
onClick: (event: MouseEvent) => void
icon?: IconDefinition
text?: string
title?: string
className?: string
disabled?: boolean
}
export interface MenuDropDownButton {
type: 'dropdown-button'
main: MenuButton
width?: string
items: MenuButton[]
}
export interface MenuLabel {
type: 'label'
text: string
}
export interface MenuSeparator {
type: 'separator'
}
export interface MenuSpace {
type: 'space'
}
export type MenuItem = MenuButton | MenuSeparator | MenuSpace
export interface ContextMenuColumn {
type: 'column'
items: Array<MenuButton | MenuDropDownButton | MenuLabel | MenuSeparator>
}
export interface ContextMenuRow {
type: 'row'
items: Array<MenuButton | MenuDropDownButton | ContextMenuColumn>
}
export type ContextMenuItem = MenuButton | MenuDropDownButton | MenuSeparator | ContextMenuRow
export interface MessageAction {
text: string
title: string
icon?: IconDefinition
onClick?: () => void
onMouseDown?: () => void
disabled?: boolean
}
export enum ValidationSeverity {
info = 'info',
warning = 'warning',
error = 'error'
}
export interface ValidationError {
path: JSONPath
message: string
severity: ValidationSeverity
}
export interface NestedValidationError extends ValidationError {
isChildError?: boolean
}
export type Validator = (json: unknown) => ValidationError[]
export interface ParseError {
position: number | undefined
line: number | undefined
column: number | undefined
message: string
}
export interface ContentParseError {
parseError: ParseError
isRepairable: boolean
}
export interface ContentValidationErrors {
validationErrors: ValidationError[]
}
export type ContentErrors = ContentParseError | ContentValidationErrors
export interface RichValidationError extends ValidationError {
line: number | undefined
column: number | undefined
from: number | undefined
to: number | undefined
actions: Array<{ name: string; apply: () => void }> | undefined
}
export interface TextLocation {
path: JSONPath
line: number
column: number
from: number
to: number
}
export interface Section {
start: number // start included
end: number // end excluded
}
export interface QueryLanguage {
id: string
name: string
description: string
createQuery: (json: unknown, queryOptions: QueryLanguageOptions) => string
executeQuery: (json: unknown, query: string, parser: JSONParser) => unknown
}
export interface QueryLanguageOptions {
filter?: {
path?: JSONPath
relation?: '==' | '!=' | '<' | '<=' | '>' | '>='
value?: string
}
sort?: {
path?: JSONPath
direction?: 'asc' | 'desc'
}
projection?: {
paths?: JSONPath[]
}
}
export type OnChangeQueryLanguage = (queryLanguageId: string) => void
export interface OnChangeStatus {
contentErrors: ContentErrors | undefined
patchResult: JSONPatchResult | undefined
}
export type OnChange =
| ((content: Content, previousContent: Content, status: OnChangeStatus) => void)
| undefined
export type OnJSONSelect = (selection: JSONSelection) => void
export type OnSelect = (selection: JSONEditorSelection | undefined) => void
export type OnPatch = (
operations: JSONPatchDocument,
afterPatch?: AfterPatchCallback
) => JSONPatchResult
export type OnChangeText = (updatedText: string, afterPatch?: AfterPatchCallback) => void
export type OnSort = (params: {
operations: JSONPatchDocument
rootPath: JSONPath
itemPath: JSONPath
direction: 1 | -1
}) => void
export type OnFind = (findAndReplace: boolean) => void
export type OnPaste = (pastedText: string) => void
export type OnPasteJson = (pastedJson: PastedJson) => void
export type OnExpand = (relativePath: JSONPath) => boolean
export type OnRenderValue = (props: RenderValueProps) => RenderValueComponentDescription[]
export type OnClassName = (path: JSONPath, value: unknown) => string | undefined
export type OnChangeMode = (mode: Mode) => void
export type OnContextMenu = (contextMenuProps: AbsolutePopupOptions) => void
export type RenderMenuContext = {
mode: Mode
modal: boolean
readOnly: boolean
}
export type OnRenderMenu = (items: MenuItem[], context: RenderMenuContext) => MenuItem[] | undefined
export type OnRenderMenuInternal = (items: MenuItem[]) => MenuItem[] | undefined
export type RenderContextMenuContext = RenderMenuContext & {
selection: JSONEditorSelection | undefined
}
export type OnRenderContextMenu = (
items: ContextMenuItem[],
context: RenderContextMenuContext
) => ContextMenuItem[] | false | undefined
export type OnRenderContextMenuInternal = (
items: ContextMenuItem[]
) => ContextMenuItem[] | false | undefined
export type OnError = (error: Error) => void
export type OnFocus = () => void
export type OnBlur = () => void
export type OnSortModal = (props: SortModalCallback) => void
export type OnTransformModal = (props: TransformModalCallback) => void
export type OnJSONEditorModal = (props: JSONEditorModalCallback) => void
export type FindNextInside = (path: JSONPath) => JSONSelection | undefined
export interface SearchResultDetails {
items: ExtendedSearchResultItem[]
activeItem: ExtendedSearchResultItem | undefined
activeIndex: number | -1
}
export enum SearchField {
key = 'key',
value = 'value'
}
export interface SearchOptions {
maxResults?: number
columns?: JSONPath[]
}
export interface SearchResultItem {
path: JSONPath
field: SearchField
fieldIndex: number
start: number
end: number
}
export interface ExtendedSearchResultItem extends SearchResultItem {
active: boolean
}
export type EscapeValue = (value: unknown) => string
export type UnescapeValue = (escapedValue: string) => string
export interface ValueNormalization {
escapeValue: EscapeValue
unescapeValue: UnescapeValue
}
export type PastedJson = {
path: JSONPath
contents: unknown
onPasteAsJson: () => void
}
export interface DragInsideProps {
json: unknown
selection: JSONSelection
deltaY: number
items: Array<{ path: JSONPath; height: number }>
}
export type DragInsideAction =
| { beforePath: JSONPath; offset: number }
| { append: true; offset: number }
export interface RenderedItem {
path: JSONPath
height: number
}
export interface HistoryItem {
undo: {
patch: JSONPatchDocument | undefined
json: unknown | undefined
text: string | undefined
documentState: DocumentState | undefined
selection: JSONSelection | undefined
sortedColumn: SortedColumn | undefined
textIsRepaired: boolean
}
redo: {
patch: JSONPatchDocument | undefined
json: unknown | undefined
text: string | undefined
documentState: DocumentState | undefined
selection: JSONSelection | undefined
sortedColumn: SortedColumn | undefined
textIsRepaired: boolean
}
}
export type ConvertType = 'value' | 'object' | 'array'
export type InsertType = ConvertType | 'structure'
export interface PopupEntry {
id: number
component: typeof SvelteComponent<Record<string, unknown>>
props: Record<string, unknown>
options: AbsolutePopupOptions
}
export interface AbsolutePopupOptions {
anchor?: Element
position?: 'top' | 'left'
left?: number
top?: number
width?: number
height?: number
offsetTop?: number
offsetLeft?: number
showTip?: boolean
closeOnOuterClick?: boolean
onClose?: () => void
}
export interface AbsolutePopupContext {
openAbsolutePopup: (
component: typeof SvelteComponent<Record<string, unknown>>,
props: Record<string, unknown>,
options: AbsolutePopupOptions
) => number
closeAbsolutePopup: (popupId: number | undefined) => void
}
export interface JSONEditorPropsOptional {
content?: Content
readOnly?: boolean
indentation?: number | string
tabSize?: number
mode?: Mode
mainMenuBar?: boolean
navigationBar?: boolean
statusBar?: boolean
askToFormat?: boolean
escapeControlCharacters?: boolean
escapeUnicodeCharacters?: boolean
flattenColumns?: boolean
parser?: JSONParser
validator?: Validator | undefined
validationParser?: JSONParser
pathParser?: JSONPathParser
queryLanguages?: QueryLanguage[]
queryLanguageId?: string
onChangeQueryLanguage?: OnChangeQueryLanguage
onChange?: OnChange
onRenderValue?: OnRenderValue
onClassName?: OnClassName
onRenderMenu?: OnRenderMenu
onRenderContextMenu?: OnRenderContextMenu
onChangeMode?: OnChangeMode
onSelect?: OnSelect
onError?: OnError
onFocus?: OnFocus
onBlur?: OnBlur
}
export interface JSONEditorModalProps {
content: Content
path: JSONPath
onPatch: OnPatch
readOnly: boolean
indentation: number | string
tabSize: number
mainMenuBar: boolean
navigationBar: boolean
statusBar: boolean
askToFormat: boolean
escapeControlCharacters: boolean
escapeUnicodeCharacters: boolean
flattenColumns: boolean
parser: JSONParser
validator: Validator | undefined
validationParser: JSONParser
pathParser: JSONPathParser
onRenderValue: OnRenderValue
onClassName: OnClassName
onRenderMenu: OnRenderMenu
onRenderContextMenu: OnRenderContextMenu
onSortModal: (props: SortModalCallback) => void
onTransformModal: (props: TransformModalCallback) => void
onClose: () => void
}
export interface JSONEditorContext {
mode: Mode
readOnly: boolean
parser: JSONParser
normalization: ValueNormalization
getJson: () => unknown | undefined
getDocumentState: () => DocumentState | undefined
findElement: (path: JSONPath) => Element | undefined
findNextInside: FindNextInside
focus: () => void
onPatch: OnPatch
onSelect: OnJSONSelect
onFind: OnFind
onPasteJson: (newPastedJson: PastedJson) => void
onRenderValue: OnRenderValue
}
export interface TreeModeContext extends JSONEditorContext {
getJson: () => unknown | undefined
getDocumentState: () => DocumentState | undefined
getSelection: () => JSONSelection | undefined
findElement: (path: JSONPath) => Element | undefined
onInsert: (type: InsertType) => void
onExpand: (path: JSONPath, expanded: boolean, recursive?: boolean) => void
onExpandSection: (path: JSONPath, section: Section) => void
onContextMenu: OnContextMenu
onClassName: OnClassName
onDrag: (event: MouseEvent) => void
onDragEnd: () => void
}
export interface RenderValueProps {
path: JSONPath
value: unknown
mode: Mode
readOnly: boolean
enforceString: boolean
selection: JSONSelection | undefined
searchResultItems: SearchResultItem[] | undefined
isEditing: boolean
parser: JSONParser
normalization: ValueNormalization
onPatch: OnPatch
onPasteJson: OnPasteJson
onSelect: OnJSONSelect
onFind: OnFind
findNextInside: FindNextInside
focus: () => void
}
export type RenderValuePropsOptional = Partial<RenderValueProps>
export interface DraggingState {
initialTarget: Element
initialClientY: number
initialContentTop: number
selectionStartIndex: number
selectionItemsCount: number
items: RenderedItem[]
offset: number
didMoveItems: boolean
}
export type RenderValueComponentDescription = SvelteComponentRenderer | SvelteActionRenderer
export interface SvelteComponentRenderer {
component: typeof SvelteComponent<RenderValuePropsOptional>
props: Record<string, unknown>
}
export interface SvelteActionRenderer {
action: Action<HTMLElement, Record<string, unknown>>
props: Record<string, unknown>
}
export interface TransformModalOptions {
id?: string
rootPath?: JSONPath
onTransform?: (state: {
operations: JSONPatchDocument
json: unknown
transformedJson: unknown
}) => void
onClose?: () => void
}
export interface TransformModalCallback {
id: string
json: unknown
rootPath: JSONPath
onTransform: (operations: JSONPatchDocument) => void
onClose: () => void
}
export interface TransformModalProps extends TransformModalCallback {
id: string
json: unknown
rootPath: JSONPath
indentation: number | string
escapeControlCharacters: boolean
escapeUnicodeCharacters: boolean
parser: JSONParser
parseMemoizeOne: JSONParser['parse']
validationParser: JSONParser
pathParser: JSONPathParser
queryLanguages: QueryLanguage[]
queryLanguageId: string
onChangeQueryLanguage: OnChangeQueryLanguage
onRenderValue: OnRenderValue
onRenderMenu: OnRenderMenuInternal
onRenderContextMenu: OnRenderContextMenuInternal
onClassName: OnClassName
onTransform: (operations: JSONPatchDocument) => void
onClose: () => void
}
export interface SortModalCallback {
id: string
json: unknown
rootPath: JSONPath
onSort: OnSort
onClose: () => void
}
export interface JSONRepairModalProps {
text: string
onParse: (text: string) => void
onRepair: (text: string) => string
onApply: (repairedText: string) => void
onClose: () => void
}
export interface JSONEditorModalCallback {
content: Content
path: JSONPath
onPatch: OnPatch
onClose: () => void
}
export enum SortDirection {
asc = 'asc',
desc = 'desc'
}
export enum UpdateSelectionAfterChange {
no = 'no',
self = 'self',
nextInside = 'nextInside'
}
export interface TableCellIndex {
rowIndex: number
columnIndex: number
}
export interface SortedColumn {
path: JSONPath
sortDirection: SortDirection
}
// TODO: work out the JSONSchema type in detail.
// Ideally, we use use Schema from Ajv, but this interface isn't worked out either
export type JSONSchema = Record<string, unknown>
export type JSONSchemaDefinitions = Record<string, JSONSchema>
export type JSONSchemaEnum = Array<unknown>