Skip to content

Commit 7def339

Browse files
committedSep 15, 2023
fix: creating an array or object or pasting content in the welcome screen not working
1 parent 5faeb96 commit 7def339

File tree

5 files changed

+42
-40
lines changed

5 files changed

+42
-40
lines changed
 

‎src/lib/components/modes/tablemode/TableMode.svelte

+3-3
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@
11721172
selectInside: false,
11731173
refJsonEditor,
11741174
json,
1175-
documentState,
1175+
selection: documentState.selection,
11761176
readOnly,
11771177
parser,
11781178
onPatch: handlePatch,
@@ -1352,7 +1352,7 @@
13521352
onPaste({
13531353
clipboardText,
13541354
json,
1355-
documentState,
1355+
selection: documentState.selection,
13561356
readOnly,
13571357
parser,
13581358
onPatch: handlePatch,
@@ -1686,6 +1686,7 @@
16861686
class:no-main-menu={!mainMenuBar}
16871687
on:mousedown={handleMouseDown}
16881688
on:keydown={handleKeyDown}
1689+
on:paste={handlePaste}
16891690
on:contextmenu={handleContextMenu}
16901691
bind:this={refJsonEditor}
16911692
>
@@ -1711,7 +1712,6 @@
17111712
tabindex="-1"
17121713
class="jse-hidden-input"
17131714
bind:this={refHiddenInput}
1714-
on:paste={handlePaste}
17151715
/>
17161716
</label>
17171717
{#if containsValidArray}

‎src/lib/components/modes/treemode/TreeMode.svelte

+5-5
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@
722722
723723
documentState = {
724724
...documentState,
725-
selection: createMultiSelection([], [])
725+
selection: createValueSelection([], false)
726726
}
727727
}
728728
@@ -898,7 +898,7 @@
898898
onPaste({
899899
clipboardText,
900900
json,
901-
documentState,
901+
selection: documentState.selection,
902902
readOnly,
903903
parser,
904904
onPatch: handlePatch,
@@ -1014,7 +1014,7 @@
10141014
selectInside: true,
10151015
refJsonEditor,
10161016
json,
1017-
documentState,
1017+
selection: documentState.selection,
10181018
readOnly,
10191019
parser,
10201020
onPatch: handlePatch,
@@ -1112,7 +1112,7 @@
11121112
selectInside: true,
11131113
refJsonEditor,
11141114
json,
1115-
documentState,
1115+
selection: documentState.selection,
11161116
readOnly,
11171117
parser,
11181118
onPatch: handlePatch,
@@ -2095,6 +2095,7 @@
20952095
class:no-main-menu={!mainMenuBar}
20962096
on:keydown={handleKeyDown}
20972097
on:mousedown={handleMouseDown}
2098+
on:paste={handlePaste}
20982099
on:contextmenu={handleContextMenu}
20992100
bind:this={refJsonEditor}
21002101
>
@@ -2135,7 +2136,6 @@
21352136
tabindex="-1"
21362137
class="jse-hidden-input"
21372138
bind:this={refHiddenInput}
2138-
on:paste={handlePaste}
21392139
/>
21402140
</label>
21412141
{#if json === undefined}

‎src/lib/logic/actions.ts

+31-30
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import type {
2828
OnChange,
2929
OnChangeText,
3030
OnPatch,
31-
OnJSONSelect
31+
OnJSONSelect,
32+
JSONSelection
3233
} from '$lib/types'
3334
import { createDebug } from '$lib/utils/debug.js'
3435
import {
@@ -124,7 +125,7 @@ type RepairModalCallback = (text: string, onApply: (repairedText: string) => voi
124125
interface OnPasteAction {
125126
clipboardText: string
126127
json: JSONValue | undefined
127-
documentState: DocumentState
128+
selection: JSONSelection | null
128129
readOnly: boolean
129130
parser: JSONParser
130131
onPatch: OnPatch
@@ -136,7 +137,7 @@ interface OnPasteAction {
136137
export function onPaste({
137138
clipboardText,
138139
json,
139-
documentState,
140+
selection,
140141
readOnly,
141142
parser,
142143
onPatch,
@@ -149,11 +150,11 @@ export function onPaste({
149150

150151
function doPaste(pastedText: string) {
151152
if (json !== undefined) {
152-
const selection = documentState.selection || createMultiSelection([], [])
153+
const selectionNonNull = selection || createValueSelection([], false)
153154

154-
const operations = insert(json, selection, pastedText, parser)
155+
const operations = insert(json, selectionNonNull, pastedText, parser)
155156

156-
debug('paste', { pastedText, operations, selection })
157+
debug('paste', { pastedText, operations, selectionNonNull })
157158

158159
onPatch(operations, (patchedJson, patchedState) => {
159160
let updatedState = patchedState
@@ -486,7 +487,7 @@ export interface OnInsert {
486487
selectInside: boolean
487488
refJsonEditor: HTMLElement
488489
json: JSONValue | undefined
489-
documentState: DocumentState
490+
selection: JSONSelection | null
490491
readOnly: boolean
491492
parser: JSONParser
492493
onPatch: OnPatch
@@ -499,21 +500,21 @@ export function onInsert({
499500
selectInside,
500501
refJsonEditor,
501502
json,
502-
documentState,
503+
selection,
503504
readOnly,
504505
parser,
505506
onPatch,
506507
onReplaceJson
507508
}: OnInsert): void {
508-
if (readOnly || !documentState.selection) {
509+
if (readOnly) {
509510
return
510511
}
511512

512-
const newValue = createNewValue(json, documentState.selection, insertType)
513+
const newValue = createNewValue(json, selection, insertType)
513514

514515
if (json !== undefined) {
515516
const data = parser.stringify(newValue)
516-
const operations = insert(json, documentState.selection, data, parser)
517+
const operations = insert(json, selection, data, parser)
517518
debug('onInsert', { insertType, operations, newValue, data })
518519

519520
const operation = last(
@@ -543,7 +544,7 @@ export function onInsert({
543544
state: expandPath(
544545
patchedJson,
545546
{
546-
...documentState,
547+
...patchedState,
547548
selection: isObject(parent)
548549
? createKeySelection(path, true)
549550
: createValueSelection(path, true)
@@ -584,7 +585,7 @@ export interface OnInsertCharacter {
584585
selectInside: boolean
585586
refJsonEditor: HTMLElement
586587
json: JSONValue | undefined
587-
documentState: DocumentState
588+
selection: JSONSelection | null
588589
readOnly: boolean
589590
parser: JSONParser
590591
onPatch: OnPatch
@@ -598,7 +599,7 @@ export async function onInsertCharacter({
598599
selectInside,
599600
refJsonEditor,
600601
json,
601-
documentState,
602+
selection,
602603
readOnly,
603604
parser,
604605
onPatch,
@@ -607,16 +608,16 @@ export async function onInsertCharacter({
607608
}: OnInsertCharacter) {
608609
// a regular key like a, A, _, etc is entered.
609610
// Replace selected contents with a new value having this first character as text
610-
if (readOnly || !documentState.selection) {
611+
if (readOnly) {
611612
return
612613
}
613614

614-
if (isKeySelection(documentState.selection)) {
615+
if (isKeySelection(selection)) {
615616
// only replace contents when not yet in edit mode (can happen when entering
616617
// multiple characters very quickly after each other due to the async handling)
617-
const replaceContents = !documentState.selection.edit
618+
const replaceContents = !selection.edit
618619

619-
onSelect({ ...documentState.selection, edit: true })
620+
onSelect({ ...selection, edit: true })
620621
tick2(() => insertActiveElementContents(refJsonEditor, char, replaceContents))
621622
return
622623
}
@@ -627,7 +628,7 @@ export async function onInsertCharacter({
627628
selectInside,
628629
refJsonEditor,
629630
json,
630-
documentState,
631+
selection,
631632
readOnly,
632633
parser,
633634
onPatch,
@@ -639,20 +640,20 @@ export async function onInsertCharacter({
639640
selectInside,
640641
refJsonEditor,
641642
json,
642-
documentState,
643+
selection,
643644
readOnly,
644645
parser,
645646
onPatch,
646647
onReplaceJson
647648
})
648649
} 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))) {
651652
// only replace contents when not yet in edit mode (can happen when entering
652653
// multiple characters very quickly after each other due to the async handling)
653-
const replaceContents = !documentState.selection.edit
654+
const replaceContents = !selection.edit
654655

655-
onSelect({ ...documentState.selection, edit: true })
656+
onSelect({ ...selection, edit: true })
656657
tick2(() => insertActiveElementContents(refJsonEditor, char, replaceContents))
657658
} else {
658659
// TODO: replace the object/array with editing a text in edit mode?
@@ -666,7 +667,7 @@ export async function onInsertCharacter({
666667
char,
667668
refJsonEditor,
668669
json,
669-
documentState,
670+
selection,
670671
readOnly,
671672
parser,
672673
onPatch,
@@ -680,7 +681,7 @@ interface OnInsertValueWithCharacter {
680681
char: string
681682
refJsonEditor: HTMLElement
682683
json: JSONValue | undefined
683-
documentState: DocumentState
684+
selection: JSONSelection | null
684685
readOnly: boolean
685686
parser: JSONParser
686687
onPatch: OnPatch
@@ -691,13 +692,13 @@ async function onInsertValueWithCharacter({
691692
char,
692693
refJsonEditor,
693694
json,
694-
documentState,
695+
selection,
695696
readOnly,
696697
parser,
697698
onPatch,
698699
onReplaceJson
699700
}: OnInsertValueWithCharacter) {
700-
if (readOnly || !documentState.selection) {
701+
if (readOnly) {
701702
return
702703
}
703704

@@ -707,7 +708,7 @@ async function onInsertValueWithCharacter({
707708
selectInside: false, // not relevant, we insert a value, not an object or array
708709
refJsonEditor,
709710
json,
710-
documentState,
711+
selection,
711712
readOnly,
712713
parser,
713714
onPatch,
@@ -716,7 +717,7 @@ async function onInsertValueWithCharacter({
716717

717718
// only replace contents when not yet in edit mode (can happen when entering
718719
// multiple characters very quickly after each other due to the async handling)
719-
const replaceContents = !isEditingSelection(documentState.selection)
720+
const replaceContents = !isEditingSelection(selection)
720721

721722
tick2(() => insertActiveElementContents(refJsonEditor, char, replaceContents))
722723
}

‎src/lib/logic/documentState.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import type {
4747
import { CaretType } from '$lib/types.js'
4848
import { int } from '../utils/numberUtils.js'
4949
import { isLargeContent } from '$lib/utils/jsonUtils.js'
50+
import { createValueSelection } from './selection.js'
5051

5152
type OnCreateSelection = (json: JSONValue, documentState: DocumentState) => JSONSelection
5253

@@ -61,7 +62,7 @@ export function createDocumentState(props?: CreateDocumentStateProps): DocumentS
6162
expandedMap: {},
6263
enforceStringMap: {},
6364
visibleSectionsMap: {},
64-
selection: null,
65+
selection: createValueSelection([], false),
6566
sortedColumn: null
6667
}
6768

‎src/lib/logic/selection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ export function getInitialSelection(json: JSONValue, documentState: DocumentStat
471471
}
472472

473473
const path = visiblePaths[index]
474-
return path.length === 0 || Array.isArray(getIn(json, initial(path)))
474+
return path === undefined || path.length === 0 || Array.isArray(getIn(json, initial(path)))
475475
? createValueSelection(path, false) // Array items and root object/array do not have a key, so select value in that case
476476
: createKeySelection(path, false)
477477
}

0 commit comments

Comments
 (0)
Please sign in to comment.