Refactoring in duplicate
This commit is contained in:
parent
eea6e09bd8
commit
0c418ac846
|
@ -51,7 +51,7 @@
|
||||||
{#each items as item}
|
{#each items as item}
|
||||||
<li>
|
<li>
|
||||||
<button
|
<button
|
||||||
on:click={item.onClick}
|
on:click={() => item.onClick()}
|
||||||
title={item.title}
|
title={item.title}
|
||||||
disabled={item.disabled}
|
disabled={item.disabled}
|
||||||
>
|
>
|
||||||
|
|
|
@ -9,16 +9,14 @@
|
||||||
import {
|
import {
|
||||||
STATE_EXPANDED,
|
STATE_EXPANDED,
|
||||||
STATE_LIMIT,
|
STATE_LIMIT,
|
||||||
SCROLL_DURATION,
|
SCROLL_DURATION
|
||||||
STATE_PROPS
|
|
||||||
} from '../constants.js'
|
} from '../constants.js'
|
||||||
import { createHistory } from '../logic/history.js'
|
import { createHistory } from '../logic/history.js'
|
||||||
import JSONNode from './JSONNode.svelte'
|
import JSONNode from './JSONNode.svelte'
|
||||||
import {
|
import {
|
||||||
createPathsMap,
|
createPathsMap,
|
||||||
createSelectionFromOperations,
|
createSelectionFromOperations,
|
||||||
expandSelection,
|
expandSelection
|
||||||
getParentPath
|
|
||||||
} from '../logic/selection.js'
|
} from '../logic/selection.js'
|
||||||
import { isContentEditableDiv } from '../utils/domUtils.js'
|
import { isContentEditableDiv } from '../utils/domUtils.js'
|
||||||
import {
|
import {
|
||||||
|
@ -30,11 +28,11 @@ getParentPath
|
||||||
import { keyComboFromEvent } from '../utils/keyBindings.js'
|
import { keyComboFromEvent } from '../utils/keyBindings.js'
|
||||||
import { search, searchNext, searchPrevious } from '../logic/search.js'
|
import { search, searchNext, searchPrevious } from '../logic/search.js'
|
||||||
import { immutableJSONPatch } from '../utils/immutableJSONPatch'
|
import { immutableJSONPatch } from '../utils/immutableJSONPatch'
|
||||||
import { initial, last, cloneDeep } from 'lodash-es'
|
import { last, cloneDeep } from 'lodash-es'
|
||||||
import jump from '../assets/jump.js/src/jump.js'
|
import jump from '../assets/jump.js/src/jump.js'
|
||||||
import { expandPath, syncState, getNextKeys, patchProps } from '../logic/documentState.js'
|
import { expandPath, syncState, patchProps } from '../logic/documentState.js'
|
||||||
import Menu from './Menu.svelte'
|
import Menu from './Menu.svelte'
|
||||||
import { isObjectOrArray } from '../utils/typeUtils.js';
|
import { isObjectOrArray } from '../utils/typeUtils.js'
|
||||||
|
|
||||||
let divContents
|
let divContents
|
||||||
let domHiddenInput
|
let domHiddenInput
|
||||||
|
@ -162,15 +160,7 @@ import { isObjectOrArray } from '../utils/typeUtils.js';
|
||||||
if (selection && selection.paths) {
|
if (selection && selection.paths) {
|
||||||
console.log('duplicate', { selection })
|
console.log('duplicate', { selection })
|
||||||
|
|
||||||
// TODO: move this logic inside duplicate()
|
const operations = duplicate(doc, state, selection.paths)
|
||||||
|
|
||||||
const lastPath = last(selection.paths) // FIXME: here we assume selection.paths is sorted correctly, that's a dangerous assumption
|
|
||||||
const parentPath = initial(lastPath)
|
|
||||||
const beforeKey = last(lastPath)
|
|
||||||
const props = getIn(state, parentPath.concat(STATE_PROPS))
|
|
||||||
const nextKeys = getNextKeys(props, beforeKey, false)
|
|
||||||
|
|
||||||
const operations = duplicate(doc, selection.paths, nextKeys)
|
|
||||||
const newSelection = createSelectionFromOperations(operations)
|
const newSelection = createSelectionFromOperations(operations)
|
||||||
|
|
||||||
handlePatch(operations, newSelection)
|
handlePatch(operations, newSelection)
|
||||||
|
@ -434,7 +424,6 @@ import { isObjectOrArray } from '../utils/typeUtils.js';
|
||||||
searchResult={searchResult}
|
searchResult={searchResult}
|
||||||
bind:showSearch
|
bind:showSearch
|
||||||
|
|
||||||
doc={doc}
|
|
||||||
selection={selection}
|
selection={selection}
|
||||||
clipboard={clipboard}
|
clipboard={clipboard}
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,10 @@
|
||||||
import { faCut, faClone, faCopy, faPaste, faSearch, faUndo, faRedo, faPlus } from '@fortawesome/free-solid-svg-icons'
|
import { faCut, faClone, faCopy, faPaste, faSearch, faUndo, faRedo, faPlus } from '@fortawesome/free-solid-svg-icons'
|
||||||
import SearchBox from './SearchBox.svelte'
|
import SearchBox from './SearchBox.svelte'
|
||||||
import DropdownMenu from './DropdownMenu.svelte'
|
import DropdownMenu from './DropdownMenu.svelte'
|
||||||
import { getParentPath } from '../logic/selection'
|
|
||||||
import { getIn } from '../utils/immutabilityHelpers'
|
|
||||||
|
|
||||||
export let searchText
|
export let searchText
|
||||||
export let searchResult
|
export let searchResult
|
||||||
export let showSearch = false
|
export let showSearch = false
|
||||||
export let doc
|
|
||||||
export let selection
|
export let selection
|
||||||
export let clipboard
|
export let clipboard
|
||||||
export let historyState
|
export let historyState
|
||||||
|
@ -40,8 +37,8 @@
|
||||||
onSearchText('')
|
onSearchText('')
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleInsertValue () {
|
function handleInsertStructure () {
|
||||||
onInsert('value')
|
onInsert('structure')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {MenuDropdownItem[]} */
|
/** @type {MenuDropdownItem[]} */
|
||||||
|
@ -49,7 +46,7 @@
|
||||||
{
|
{
|
||||||
text: 'Insert value',
|
text: 'Insert value',
|
||||||
title: 'Insert a new value',
|
title: 'Insert a new value',
|
||||||
onClick: handleInsertValue,
|
onClick: () => onInsert('value'),
|
||||||
disabled: !hasSelectionWithoutContents,
|
disabled: !hasSelectionWithoutContents,
|
||||||
default: true
|
default: true
|
||||||
},
|
},
|
||||||
|
@ -69,7 +66,7 @@
|
||||||
text: 'Insert structure',
|
text: 'Insert structure',
|
||||||
title: 'Insert a new item with the same structure as other items. ' +
|
title: 'Insert a new item with the same structure as other items. ' +
|
||||||
'Only applicable inside an array',
|
'Only applicable inside an array',
|
||||||
onClick: () => onInsert('structure'),
|
onClick: handleInsertStructure,
|
||||||
disabled: !hasSelectionWithoutContents
|
disabled: !hasSelectionWithoutContents
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -119,7 +116,7 @@
|
||||||
<button
|
<button
|
||||||
class="button insert"
|
class="button insert"
|
||||||
slot="defaultItem"
|
slot="defaultItem"
|
||||||
on:click={handleInsertValue}
|
on:click={handleInsertStructure}
|
||||||
disabled={!hasSelectionWithoutContents}
|
disabled={!hasSelectionWithoutContents}
|
||||||
>
|
>
|
||||||
<Icon data={faPlus} />
|
<Icon data={faPlus} />
|
||||||
|
|
|
@ -185,16 +185,18 @@ export function replace (json, paths, values, nextKeys) { // TODO: find a bette
|
||||||
* and object property
|
* and object property
|
||||||
*
|
*
|
||||||
* @param {JSON} json
|
* @param {JSON} json
|
||||||
|
* @param {JSON} doc
|
||||||
* @param {Path[]} paths
|
* @param {Path[]} paths
|
||||||
* @param {string[]} nextKeys A list with all keys *after* the renamed key,
|
|
||||||
* these keys will be moved down, so the renamed
|
|
||||||
* key will maintain it's position above these keys
|
|
||||||
* @return {JSONPatchDocument}
|
* @return {JSONPatchDocument}
|
||||||
*/
|
*/
|
||||||
export function duplicate (json, paths, nextKeys) {
|
export function duplicate (doc, state, paths) {
|
||||||
const firstPath = first(paths)
|
// FIXME: here we assume selection.paths is sorted correctly, that's a dangerous assumption
|
||||||
const parentPath = initial(firstPath)
|
const lastPath = last(paths)
|
||||||
const parent = getIn(json, parentPath)
|
const parentPath = initial(lastPath)
|
||||||
|
const beforeKey = last(lastPath)
|
||||||
|
const props = getIn(state, parentPath.concat(STATE_PROPS))
|
||||||
|
const nextKeys = getNextKeys(props, beforeKey, false)
|
||||||
|
const parent = getIn(doc, parentPath)
|
||||||
|
|
||||||
if (Array.isArray(parent)) {
|
if (Array.isArray(parent)) {
|
||||||
const lastPath = last(paths)
|
const lastPath = last(paths)
|
||||||
|
@ -240,6 +242,7 @@ export function insert (doc, state, selection, values) {
|
||||||
const props = getIn(state, parentPath.concat(STATE_PROPS))
|
const props = getIn(state, parentPath.concat(STATE_PROPS))
|
||||||
const nextKeys = getNextKeys(props, beforeKey, true)
|
const nextKeys = getNextKeys(props, beforeKey, true)
|
||||||
const operations = insertBefore(doc, selection.beforePath, values, nextKeys)
|
const operations = insertBefore(doc, selection.beforePath, values, nextKeys)
|
||||||
|
// TODO: move calculation of nextKeys inside insertBefore?
|
||||||
|
|
||||||
return operations
|
return operations
|
||||||
} else if (selection.appendPath) {
|
} else if (selection.appendPath) {
|
||||||
|
@ -253,6 +256,7 @@ export function insert (doc, state, selection, values) {
|
||||||
const props = getIn(state, parentPath.concat(STATE_PROPS))
|
const props = getIn(state, parentPath.concat(STATE_PROPS))
|
||||||
const nextKeys = getNextKeys(props, beforeKey, true)
|
const nextKeys = getNextKeys(props, beforeKey, true)
|
||||||
const operations = replace(doc, selection.paths, values, nextKeys)
|
const operations = replace(doc, selection.paths, values, nextKeys)
|
||||||
|
// TODO: move calculation of nextKeys inside replace?
|
||||||
|
|
||||||
return operations
|
return operations
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue