Make editing properties workable (not fully solved yet)

This commit is contained in:
josdejong 2020-04-27 10:28:09 +02:00
parent 5fe4be081f
commit 80c7b2814f
2 changed files with 46 additions and 21 deletions

View File

@ -59,15 +59,12 @@
$: formattedName = `"${name}"` $: formattedName = `"${name}"`
$: console.log('json.number', json.number)
function loadLargeJson () { function loadLargeJson () {
const count = 500 const count = 500
console.log('create large json', { count }) console.log('create large json', { count })
console.time('create large json') console.time('create large json')
const largeJson = { const largeJson = {}
}
largeJson.numbers = [] largeJson.numbers = []
largeJson.array = [] largeJson.array = []
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
@ -179,15 +176,16 @@
value={json} value={json}
searchResult={searchResult} searchResult={searchResult}
expanded={true} expanded={true}
onChangeKey={handleChangeKey}
onChangeValue={handleChangeValue} onChangeValue={handleChangeValue}
/> />
</div> </div>
<!-- <textarea <textarea
class='code' class='code'
value={JSON.stringify(json, null, 2)} value={JSON.stringify(json, null, 2)}
on:input={handleInputTextArea} on:input={handleInputTextArea}
/> --> />
<p> <p>
<button on:click={handleLoadLargeJson}>load large json</button> <button on:click={handleLoadLargeJson}>load large json</button>

View File

@ -5,6 +5,8 @@
import classnames from 'classnames' import classnames from 'classnames'
import { isUrl, valueType } from './utils/typeUtils' import { isUrl, valueType } from './utils/typeUtils'
import { escapeHTML } from './utils/stringUtils' import { escapeHTML } from './utils/stringUtils'
import uniqueId from 'lodash/uniqueId.js'
import remove from 'lodash/remove.js'
export let key = 'root' export let key = 'root'
export let value export let value
@ -14,15 +16,33 @@
export let expanded = true export let expanded = true
const DEFAULT_LIMIT = 10000 const DEFAULT_LIMIT = 10000
const escapeUnicode = false // TODO: pass via options
let limit = DEFAULT_LIMIT let limit = DEFAULT_LIMIT
$: type = valueType (value) $: type = valueType (value)
function getOrCreateId (childKey) {
if (ids === undefined) {
ids = {}
}
if (ids[childKey] === undefined) {
ids[childKey] = uniqueId()
}
return ids[childKey]
}
// FIXME: this should not be needed, use Svelte notation for looping over an object // FIXME: this should not be needed, use Svelte notation for looping over an object
let ids = undefined
$: props = type === 'object' $: props = type === 'object'
? Object.keys(value).map(prop => { ? Object.keys(value).map(childKey => {
return { key: prop, value: value[prop] } return {
id: getOrCreateId(childKey),
key: childKey
}
}) })
: undefined : undefined
@ -32,7 +52,6 @@
? limited ? value.slice(0, limit) : value ? limited ? value.slice(0, limit) : value
: undefined : undefined
const escapeUnicode = false // TODO: pass via options
$: escapedKey = escapeHTML(key, escapeUnicode) $: escapedKey = escapeHTML(key, escapeUnicode)
$: escapedValue = escapeHTML(value, escapeUnicode) $: escapedValue = escapeHTML(value, escapeUnicode)
@ -60,11 +79,20 @@
function handleChangeKey (newChildKey, oldChildKey) { function handleChangeKey (newChildKey, oldChildKey) {
if (type === 'object') { if (type === 'object') {
const updatedValue = { const updatedValue = {}
...value, Object.keys(value).forEach(childKey => {
[newChildKey]: value[oldChildKey] if (childKey === oldChildKey) {
updatedValue[newChildKey] = value[childKey]
} else {
updatedValue[childKey] = value[childKey]
} }
delete updatedValue[oldChildKey] })
if (ids !== undefined) {
ids[newChildKey] = ids[oldChildKey]
delete ids[oldChildKey]
}
onChangeValue(updatedValue, key) onChangeValue(updatedValue, key)
} }
} }
@ -72,14 +100,12 @@
function handleChangeValue (childValue, childKey) { function handleChangeValue (childValue, childKey) {
// FIXME: use an immutability setIn function here // FIXME: use an immutability setIn function here
if (type === 'array') { if (type === 'array') {
const updatedValue = value.slice(0) // copy the array const updatedValue = [...value]
updatedValue[childKey] = childValue updatedValue[childKey] = childValue
onChangeValue(updatedValue, key) onChangeValue(updatedValue, key)
} else if (type === 'object') { } else if (type === 'object') {
const updatedValue = { const updatedValue = { ...value }
...value, updatedValue[childKey] = childValue
[childKey]: childValue
}
onChangeValue(updatedValue, key) onChangeValue(updatedValue, key)
} }
} }
@ -131,6 +157,7 @@
// FIXME: there is whitespace added around the separator in the HTML // FIXME: there is whitespace added around the separator in the HTML
.separator { .separator {
display: inline;
color: $gray; color: $gray;
} }
@ -242,10 +269,10 @@
{:else if type === 'object'} {:else if type === 'object'}
{#if expanded} {#if expanded}
<div class="props"> <div class="props">
{#each props as prop (prop.key)} {#each props as prop (prop.id)}
<svelte:self <svelte:self
key={prop.key} key={prop.key}
value={prop.value} value={value[prop.key]}
searchResult={searchResult ? searchResult[prop.key] : undefined} searchResult={searchResult ? searchResult[prop.key] : undefined}
onChangeKey={handleChangeKey} onChangeKey={handleChangeKey}
onChangeValue={handleChangeValue} onChangeValue={handleChangeValue}