Changed singnature of `handleInsert`, `handleDuplicate`, and `handleRemove`

This commit is contained in:
jos 2016-08-06 14:18:15 +02:00
parent a065fbb09e
commit 608f45c8e7
2 changed files with 36 additions and 43 deletions

View File

@ -303,6 +303,9 @@ export default class JSONNode extends Component {
} }
if (hasParent) { if (hasParent) {
const parentPath = this.props.parent.getPath()
const prop = this.props.prop
if (items.length) { if (items.length) {
// create a separator // create a separator
items.push({ items.push({
@ -316,52 +319,50 @@ export default class JSONNode extends Component {
title: 'Insert a new item with type \'value\' after this item (Ctrl+Ins)', title: 'Insert a new item with type \'value\' after this item (Ctrl+Ins)',
submenuTitle: 'Select the type of the item to be inserted', submenuTitle: 'Select the type of the item to be inserted',
className: 'jsoneditor-insert', className: 'jsoneditor-insert',
click: () => events.onInsert(path, 'value'), click: () => events.onInsert(parentPath, prop, 'value'),
submenu: [ submenu: [
{ {
text: 'Value', text: 'Value',
className: 'jsoneditor-type-value', className: 'jsoneditor-type-value',
title: TYPE_TITLES.value, title: TYPE_TITLES.value,
click: () => events.onInsert(path, 'value') click: () => events.onInsert(parentPath, prop,'value')
}, },
{ {
text: 'Array', text: 'Array',
className: 'jsoneditor-type-array', className: 'jsoneditor-type-array',
title: TYPE_TITLES.array, title: TYPE_TITLES.array,
click: () => events.onInsert(path, 'array') click: () => events.onInsert(parentPath, prop, 'array')
}, },
{ {
text: 'Object', text: 'Object',
className: 'jsoneditor-type-object', className: 'jsoneditor-type-object',
title: TYPE_TITLES.object, title: TYPE_TITLES.object,
click: () => events.onInsert(path, 'object') click: () => events.onInsert(parentPath, prop, 'object')
}, },
{ {
text: 'String', text: 'String',
className: 'jsoneditor-type-string', className: 'jsoneditor-type-string',
title: TYPE_TITLES.string, title: TYPE_TITLES.string,
click: () => events.onInsert(path, 'string') click: () => events.onInsert(parentPath, prop, 'string')
} }
] ]
}); });
if (hasParent) { // create duplicate button
// create duplicate button items.push({
items.push({ text: 'Duplicate',
text: 'Duplicate', title: 'Duplicate this item (Ctrl+D)',
title: 'Duplicate this item (Ctrl+D)', className: 'jsoneditor-duplicate',
className: 'jsoneditor-duplicate', click: () => events.onDuplicate(parentPath, prop)
click: () => events.onDuplicate(path) });
});
// create remove button // create remove button
items.push({ items.push({
text: 'Remove', text: 'Remove',
title: 'Remove this item (Ctrl+Del)', title: 'Remove this item (Ctrl+Del)',
className: 'jsoneditor-remove', className: 'jsoneditor-remove',
click: () => events.onRemove(path) click: () => events.onRemove(parentPath, prop)
}); });
}
} }
// TODO: implement a hook to adjust the context menu // TODO: implement a hook to adjust the context menu

View File

@ -89,13 +89,10 @@ export default class Main extends Component {
}) })
} }
// TODO: change to handleInsert(path, after, type) handleInsert (path, afterProp, type) {
handleInsert (path, type) { console.log('handleInsert', path, afterProp, type)
console.log('handleInsert', path, type)
const afterProp = last(path) const dataPath = toDataPath(this.state.data, path)
const parentPath = path.slice(0, path.length - 1)
const dataPath = toDataPath(this.state.data, parentPath)
const parent = getIn(this.state.data, dataPath) const parent = getIn(this.state.data, dataPath)
if (parent.type === 'array') { if (parent.type === 'array') {
@ -160,16 +157,13 @@ export default class Main extends Component {
} }
} }
// TODO: change to handleDuplicate(path, prop) handleDuplicate (path, prop) {
handleDuplicate (path) {
console.log('handleDuplicate', path) console.log('handleDuplicate', path)
const prop = last(path) const dataPath = toDataPath(this.state.data, path)
const parentPath = path.slice(0, path.length - 1) const object = getIn(this.state.data, dataPath)
const dataPath = toDataPath(this.state.data, parentPath)
const parent = getIn(this.state.data, dataPath)
if (parent.type === 'array') { if (object.type === 'array') {
this.setState({ this.setState({
data: updateIn(this.state.data, dataPath.concat(['items']), (items) => { data: updateIn(this.state.data, dataPath.concat(['items']), (items) => {
const index = parseInt(prop) const index = parseInt(prop)
@ -183,7 +177,7 @@ export default class Main extends Component {
}) })
}) })
} }
else { // parent.type === 'object' else { // object.type === 'object'
this.setState({ this.setState({
data: updateIn(this.state.data, dataPath.concat(['props']), (props) => { data: updateIn(this.state.data, dataPath.concat(['props']), (props) => {
const index = props.findIndex(p => p.name === prop) const index = props.findIndex(p => p.name === prop)
@ -199,22 +193,20 @@ export default class Main extends Component {
} }
} }
// TODO: change to handleRemove(path, prop) handleRemove (path, prop) {
handleRemove (path) {
console.log('handleRemove', path) console.log('handleRemove', path)
const parentPath = path.slice(0, path.length - 1) const object = getIn(this.state.data, toDataPath(this.state.data, path))
const parent = getIn(this.state.data, toDataPath(this.state.data, parentPath))
if (parent.type === 'array') { if (object.type === 'array') {
const dataPath = toDataPath(this.state.data, path) const dataPath = toDataPath(this.state.data, path.concat(prop))
this.setState({ this.setState({
data: deleteIn(this.state.data, dataPath) data: deleteIn(this.state.data, dataPath)
}) })
} }
else { // parent.type === 'object' else { // object.type === 'object'
const dataPath = toDataPath(this.state.data, path) const dataPath = toDataPath(this.state.data, path.concat(prop))
dataPath.pop() // remove the 'value' property, we want to remove the whole object property dataPath.pop() // remove the 'value' property, we want to remove the whole object property
this.setState({ this.setState({