@@ -28,7 +28,8 @@ import type {
28
28
OnChange ,
29
29
OnChangeText ,
30
30
OnPatch ,
31
- OnJSONSelect
31
+ OnJSONSelect ,
32
+ JSONSelection
32
33
} from '$lib/types'
33
34
import { createDebug } from '$lib/utils/debug.js'
34
35
import {
@@ -124,7 +125,7 @@ type RepairModalCallback = (text: string, onApply: (repairedText: string) => voi
124
125
interface OnPasteAction {
125
126
clipboardText : string
126
127
json : JSONValue | undefined
127
- documentState : DocumentState
128
+ selection : JSONSelection | null
128
129
readOnly : boolean
129
130
parser : JSONParser
130
131
onPatch : OnPatch
@@ -136,7 +137,7 @@ interface OnPasteAction {
136
137
export function onPaste ( {
137
138
clipboardText,
138
139
json,
139
- documentState ,
140
+ selection ,
140
141
readOnly,
141
142
parser,
142
143
onPatch,
@@ -149,11 +150,11 @@ export function onPaste({
149
150
150
151
function doPaste ( pastedText : string ) {
151
152
if ( json !== undefined ) {
152
- const selection = documentState . selection || createMultiSelection ( [ ] , [ ] )
153
+ const selectionNonNull = selection || createValueSelection ( [ ] , false )
153
154
154
- const operations = insert ( json , selection , pastedText , parser )
155
+ const operations = insert ( json , selectionNonNull , pastedText , parser )
155
156
156
- debug ( 'paste' , { pastedText, operations, selection } )
157
+ debug ( 'paste' , { pastedText, operations, selectionNonNull } )
157
158
158
159
onPatch ( operations , ( patchedJson , patchedState ) => {
159
160
let updatedState = patchedState
@@ -486,7 +487,7 @@ export interface OnInsert {
486
487
selectInside : boolean
487
488
refJsonEditor : HTMLElement
488
489
json : JSONValue | undefined
489
- documentState : DocumentState
490
+ selection : JSONSelection | null
490
491
readOnly : boolean
491
492
parser : JSONParser
492
493
onPatch : OnPatch
@@ -499,21 +500,21 @@ export function onInsert({
499
500
selectInside,
500
501
refJsonEditor,
501
502
json,
502
- documentState ,
503
+ selection ,
503
504
readOnly,
504
505
parser,
505
506
onPatch,
506
507
onReplaceJson
507
508
} : OnInsert ) : void {
508
- if ( readOnly || ! documentState . selection ) {
509
+ if ( readOnly ) {
509
510
return
510
511
}
511
512
512
- const newValue = createNewValue ( json , documentState . selection , insertType )
513
+ const newValue = createNewValue ( json , selection , insertType )
513
514
514
515
if ( json !== undefined ) {
515
516
const data = parser . stringify ( newValue )
516
- const operations = insert ( json , documentState . selection , data , parser )
517
+ const operations = insert ( json , selection , data , parser )
517
518
debug ( 'onInsert' , { insertType, operations, newValue, data } )
518
519
519
520
const operation = last (
@@ -543,7 +544,7 @@ export function onInsert({
543
544
state : expandPath (
544
545
patchedJson ,
545
546
{
546
- ...documentState ,
547
+ ...patchedState ,
547
548
selection : isObject ( parent )
548
549
? createKeySelection ( path , true )
549
550
: createValueSelection ( path , true )
@@ -584,7 +585,7 @@ export interface OnInsertCharacter {
584
585
selectInside : boolean
585
586
refJsonEditor : HTMLElement
586
587
json : JSONValue | undefined
587
- documentState : DocumentState
588
+ selection : JSONSelection | null
588
589
readOnly : boolean
589
590
parser : JSONParser
590
591
onPatch : OnPatch
@@ -598,7 +599,7 @@ export async function onInsertCharacter({
598
599
selectInside,
599
600
refJsonEditor,
600
601
json,
601
- documentState ,
602
+ selection ,
602
603
readOnly,
603
604
parser,
604
605
onPatch,
@@ -607,16 +608,16 @@ export async function onInsertCharacter({
607
608
} : OnInsertCharacter ) {
608
609
// a regular key like a, A, _, etc is entered.
609
610
// Replace selected contents with a new value having this first character as text
610
- if ( readOnly || ! documentState . selection ) {
611
+ if ( readOnly ) {
611
612
return
612
613
}
613
614
614
- if ( isKeySelection ( documentState . selection ) ) {
615
+ if ( isKeySelection ( selection ) ) {
615
616
// only replace contents when not yet in edit mode (can happen when entering
616
617
// multiple characters very quickly after each other due to the async handling)
617
- const replaceContents = ! documentState . selection . edit
618
+ const replaceContents = ! selection . edit
618
619
619
- onSelect ( { ...documentState . selection , edit : true } )
620
+ onSelect ( { ...selection , edit : true } )
620
621
tick2 ( ( ) => insertActiveElementContents ( refJsonEditor , char , replaceContents ) )
621
622
return
622
623
}
@@ -627,7 +628,7 @@ export async function onInsertCharacter({
627
628
selectInside,
628
629
refJsonEditor,
629
630
json,
630
- documentState ,
631
+ selection ,
631
632
readOnly,
632
633
parser,
633
634
onPatch,
@@ -639,20 +640,20 @@ export async function onInsertCharacter({
639
640
selectInside,
640
641
refJsonEditor,
641
642
json,
642
- documentState ,
643
+ selection ,
643
644
readOnly,
644
645
parser,
645
646
onPatch,
646
647
onReplaceJson
647
648
} )
648
649
} else {
649
- if ( isValueSelection ( documentState . selection ) && json !== undefined ) {
650
- if ( ! isObjectOrArray ( getIn ( json , documentState . selection . path ) ) ) {
650
+ if ( isValueSelection ( selection ) && json !== undefined ) {
651
+ if ( ! isObjectOrArray ( getIn ( json , selection . path ) ) ) {
651
652
// only replace contents when not yet in edit mode (can happen when entering
652
653
// multiple characters very quickly after each other due to the async handling)
653
- const replaceContents = ! documentState . selection . edit
654
+ const replaceContents = ! selection . edit
654
655
655
- onSelect ( { ...documentState . selection , edit : true } )
656
+ onSelect ( { ...selection , edit : true } )
656
657
tick2 ( ( ) => insertActiveElementContents ( refJsonEditor , char , replaceContents ) )
657
658
} else {
658
659
// TODO: replace the object/array with editing a text in edit mode?
@@ -666,7 +667,7 @@ export async function onInsertCharacter({
666
667
char,
667
668
refJsonEditor,
668
669
json,
669
- documentState ,
670
+ selection ,
670
671
readOnly,
671
672
parser,
672
673
onPatch,
@@ -680,7 +681,7 @@ interface OnInsertValueWithCharacter {
680
681
char : string
681
682
refJsonEditor : HTMLElement
682
683
json : JSONValue | undefined
683
- documentState : DocumentState
684
+ selection : JSONSelection | null
684
685
readOnly : boolean
685
686
parser : JSONParser
686
687
onPatch : OnPatch
@@ -691,13 +692,13 @@ async function onInsertValueWithCharacter({
691
692
char,
692
693
refJsonEditor,
693
694
json,
694
- documentState ,
695
+ selection ,
695
696
readOnly,
696
697
parser,
697
698
onPatch,
698
699
onReplaceJson
699
700
} : OnInsertValueWithCharacter ) {
700
- if ( readOnly || ! documentState . selection ) {
701
+ if ( readOnly ) {
701
702
return
702
703
}
703
704
@@ -707,7 +708,7 @@ async function onInsertValueWithCharacter({
707
708
selectInside : false , // not relevant, we insert a value, not an object or array
708
709
refJsonEditor,
709
710
json,
710
- documentState ,
711
+ selection ,
711
712
readOnly,
712
713
parser,
713
714
onPatch,
@@ -716,7 +717,7 @@ async function onInsertValueWithCharacter({
716
717
717
718
// only replace contents when not yet in edit mode (can happen when entering
718
719
// multiple characters very quickly after each other due to the async handling)
719
- const replaceContents = ! isEditingSelection ( documentState . selection )
720
+ const replaceContents = ! isEditingSelection ( selection )
720
721
721
722
tick2 ( ( ) => insertActiveElementContents ( refJsonEditor , char , replaceContents ) )
722
723
}
0 commit comments