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) {
const parentPath = this.props.parent.getPath()
const prop = this.props.prop
if (items.length) {
// create a separator
items.push({
@ -316,42 +319,41 @@ export default class JSONNode extends Component {
title: 'Insert a new item with type \'value\' after this item (Ctrl+Ins)',
submenuTitle: 'Select the type of the item to be inserted',
className: 'jsoneditor-insert',
click: () => events.onInsert(path, 'value'),
click: () => events.onInsert(parentPath, prop, 'value'),
submenu: [
{
text: 'Value',
className: 'jsoneditor-type-value',
title: TYPE_TITLES.value,
click: () => events.onInsert(path, 'value')
click: () => events.onInsert(parentPath, prop,'value')
},
{
text: 'Array',
className: 'jsoneditor-type-array',
title: TYPE_TITLES.array,
click: () => events.onInsert(path, 'array')
click: () => events.onInsert(parentPath, prop, 'array')
},
{
text: 'Object',
className: 'jsoneditor-type-object',
title: TYPE_TITLES.object,
click: () => events.onInsert(path, 'object')
click: () => events.onInsert(parentPath, prop, 'object')
},
{
text: 'String',
className: 'jsoneditor-type-string',
title: TYPE_TITLES.string,
click: () => events.onInsert(path, 'string')
click: () => events.onInsert(parentPath, prop, 'string')
}
]
});
if (hasParent) {
// create duplicate button
items.push({
text: 'Duplicate',
title: 'Duplicate this item (Ctrl+D)',
className: 'jsoneditor-duplicate',
click: () => events.onDuplicate(path)
click: () => events.onDuplicate(parentPath, prop)
});
// create remove button
@ -359,10 +361,9 @@ export default class JSONNode extends Component {
text: 'Remove',
title: 'Remove this item (Ctrl+Del)',
className: 'jsoneditor-remove',
click: () => events.onRemove(path)
click: () => events.onRemove(parentPath, prop)
});
}
}
// 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, type) {
console.log('handleInsert', path, type)
handleInsert (path, afterProp, type) {
console.log('handleInsert', path, afterProp, type)
const afterProp = last(path)
const parentPath = path.slice(0, path.length - 1)
const dataPath = toDataPath(this.state.data, parentPath)
const dataPath = toDataPath(this.state.data, path)
const parent = getIn(this.state.data, dataPath)
if (parent.type === 'array') {
@ -160,16 +157,13 @@ export default class Main extends Component {
}
}
// TODO: change to handleDuplicate(path, prop)
handleDuplicate (path) {
handleDuplicate (path, prop) {
console.log('handleDuplicate', path)
const prop = last(path)
const parentPath = path.slice(0, path.length - 1)
const dataPath = toDataPath(this.state.data, parentPath)
const parent = getIn(this.state.data, dataPath)
const dataPath = toDataPath(this.state.data, path)
const object = getIn(this.state.data, dataPath)
if (parent.type === 'array') {
if (object.type === 'array') {
this.setState({
data: updateIn(this.state.data, dataPath.concat(['items']), (items) => {
const index = parseInt(prop)
@ -183,7 +177,7 @@ export default class Main extends Component {
})
})
}
else { // parent.type === 'object'
else { // object.type === 'object'
this.setState({
data: updateIn(this.state.data, dataPath.concat(['props']), (props) => {
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) {
handleRemove (path, prop) {
console.log('handleRemove', path)
const parentPath = path.slice(0, path.length - 1)
const parent = getIn(this.state.data, toDataPath(this.state.data, parentPath))
const object = getIn(this.state.data, toDataPath(this.state.data, path))
if (parent.type === 'array') {
const dataPath = toDataPath(this.state.data, path)
if (object.type === 'array') {
const dataPath = toDataPath(this.state.data, path.concat(prop))
this.setState({
data: deleteIn(this.state.data, dataPath)
})
}
else { // parent.type === 'object'
const dataPath = toDataPath(this.state.data, path)
else { // object.type === 'object'
const dataPath = toDataPath(this.state.data, path.concat(prop))
dataPath.pop() // remove the 'value' property, we want to remove the whole object property
this.setState({