|
1 | 1 | <svelte:options immutable={true} />
|
2 | 2 |
|
3 | 3 | <script lang="ts">
|
4 |
| - import { getContext } from 'svelte' |
| 4 | + import { getContext, tick } from 'svelte' |
5 | 5 | import Header from './Header.svelte'
|
6 | 6 | import type { JSONPatchDocument, JSONPath } from 'immutable-json-patch'
|
7 | 7 | import { compileJSONPointer, immutableJSONPatch, isJSONArray } from 'immutable-json-patch'
|
|
30 | 30 | import { faCaretLeft } from '@fortawesome/free-solid-svg-icons'
|
31 | 31 | import memoizeOne from 'memoize-one'
|
32 | 32 | import { onEscape } from '$lib/actions/onEscape.js'
|
| 33 | + import { getFocusPath } from '$lib/logic/selection.js' |
33 | 34 |
|
34 | 35 | const debug = createDebug('jsoneditor:JSONEditorModal')
|
35 | 36 |
|
|
64 | 65 | interface ModalState {
|
65 | 66 | mode: Mode
|
66 | 67 | content: Content
|
| 68 | + selection: JSONEditorSelection | null |
67 | 69 | relativePath: JSONPath
|
68 | 70 | }
|
69 | 71 |
|
| 72 | + let refEditor: JSONEditorRoot |
| 73 | +
|
70 | 74 | const rootState: ModalState = {
|
71 | 75 | mode: determineMode(content),
|
72 | 76 | content,
|
| 77 | + selection: null, |
73 | 78 | relativePath: path
|
74 | 79 | }
|
75 | 80 | let stack: ModalState[] = [rootState]
|
76 |
| - let selection: JSONEditorSelection | null = null |
77 | 81 |
|
| 82 | + $: currentState = last(stack) || rootState |
78 | 83 | $: absolutePath = stack.flatMap((state) => state.relativePath)
|
79 | 84 | $: pathDescription = !isEmpty(absolutePath) ? stringifyJSONPath(absolutePath) : '(whole document)'
|
80 | 85 |
|
|
87 | 92 | return isJSONContent(content) && isJSONArray(content.json) ? Mode.table : Mode.tree
|
88 | 93 | }
|
89 | 94 |
|
| 95 | + function scrollToSelection() { |
| 96 | + const selection: JSONPath | null = last(stack)?.selection || null |
| 97 | + if (selection) { |
| 98 | + refEditor.scrollTo(getFocusPath(selection)) |
| 99 | + } |
| 100 | + } |
| 101 | +
|
90 | 102 | function handleApply() {
|
91 | 103 | debug('handleApply')
|
92 | 104 |
|
|
97 | 109 | try {
|
98 | 110 | error = undefined
|
99 | 111 |
|
100 |
| - const path = last(stack).relativePath |
101 |
| - const content = last(stack).content |
| 112 | + const path = currentState.relativePath |
| 113 | + const content = currentState.content |
102 | 114 | const operations: JSONPatchDocument = [
|
103 | 115 | {
|
104 | 116 | op: 'replace',
|
|
114 | 126 | json: immutableJSONPatch(parentJson, operations)
|
115 | 127 | }
|
116 | 128 |
|
117 |
| - // after successfully updated, remove from the stack and apply the change |
118 |
| - stack = initial(stack) |
119 |
| - selection = null // TODO: restore the selection, like selection = createValueSelection(path, false) |
120 |
| - handleChange(updatedParentContent) |
| 129 | + // after successfully updated, remove from the stack and apply the change to the parent |
| 130 | + const parentState = stack[stack.length - 2] || rootState |
| 131 | + const updatedParentState: ModalState = { ...parentState, content: updatedParentContent } |
| 132 | + stack = [...stack.slice(0, stack.length - 2), updatedParentState] |
| 133 | + tick().then(scrollToSelection) |
121 | 134 | } else {
|
122 | 135 | onPatch(operations)
|
123 | 136 |
|
124 | 137 | close()
|
125 | 138 | }
|
126 | 139 | } catch (err) {
|
127 |
| - error = err.toString() |
| 140 | + error = String(err) |
128 | 141 | }
|
129 | 142 | }
|
130 | 143 |
|
|
134 | 147 | if (stack.length > 1) {
|
135 | 148 | // remove the last item from the stack
|
136 | 149 | stack = initial(stack)
|
| 150 | + tick().then(scrollToSelection) |
137 | 151 |
|
138 | 152 | // clear any error from the just closed state
|
139 | 153 | error = undefined
|
|
147 | 161 | debug('handleChange', updatedContent)
|
148 | 162 |
|
149 | 163 | const updatedState = {
|
150 |
| - ...last(stack), |
| 164 | + ...currentState, |
151 | 165 | content: updatedContent
|
152 | 166 | }
|
153 | 167 |
|
154 | 168 | stack = [...initial(stack), updatedState]
|
155 | 169 | }
|
156 | 170 |
|
| 171 | + function handleChangeSelection(newSelection: JSONEditorSelection | null) { |
| 172 | + debug('handleChangeSelection', newSelection) |
| 173 | +
|
| 174 | + const updatedState = { |
| 175 | + ...currentState, |
| 176 | + selection: newSelection |
| 177 | + } |
| 178 | +
|
| 179 | + stack = [...initial(stack), updatedState] |
| 180 | + } |
| 181 | +
|
157 | 182 | function handleChangeMode(newMode: Mode) {
|
158 | 183 | debug('handleChangeMode', newMode)
|
159 | 184 |
|
160 | 185 | const updatedState = {
|
161 |
| - ...last(stack), |
| 186 | + ...currentState, |
162 | 187 | mode: newMode
|
163 | 188 | }
|
164 | 189 |
|
|
176 | 201 | const nestedModalState = {
|
177 | 202 | mode: determineMode(content),
|
178 | 203 | content,
|
| 204 | + selection: null, |
179 | 205 | relativePath: path
|
180 | 206 | }
|
181 | 207 | stack = [...stack, nestedModalState]
|
|
204 | 230 |
|
205 | 231 | <div class="jse-modal-inline-editor">
|
206 | 232 | <JSONEditorRoot
|
207 |
| - mode={last(stack).mode} |
208 |
| - content={last(stack).content} |
209 |
| - {selection} |
| 233 | + bind:this={refEditor} |
| 234 | + mode={currentState.mode} |
| 235 | + content={currentState.content} |
| 236 | + selection={currentState.selection} |
210 | 237 | {readOnly}
|
211 | 238 | {indentation}
|
212 | 239 | {tabSize}
|
|
226 | 253 | onError={handleError}
|
227 | 254 | onChange={handleChange}
|
228 | 255 | onChangeMode={handleChangeMode}
|
229 |
| - onSelect={(newSelection) => { |
230 |
| - selection = newSelection |
231 |
| - }} |
| 256 | + onSelect={handleChangeSelection} |
232 | 257 | {onRenderValue}
|
233 | 258 | {onClassName}
|
234 | 259 | onFocus={noop}
|
|
0 commit comments