Implemented support for type 'string'

This commit is contained in:
jos 2016-07-31 14:41:28 +02:00
parent 38250a38ba
commit 49f0ed3e4f
2 changed files with 30 additions and 22 deletions

View File

@ -336,20 +336,20 @@ export default class JSONNode extends Component {
}
handleChangeValue (event) {
const value = JSONNode._getValueFromEvent(event)
const value = this._getValueFromEvent(event)
this.props.events.onChangeValue(this.props.path, value)
}
handleClickValue (event) {
if (event.ctrlKey && event.button === 0) { // Ctrl+Left click
JSONNode._openLinkIfUrl(event)
this._openLinkIfUrl(event)
}
}
handleKeyDownValue (event) {
if (event.ctrlKey && event.which === 13) { // Ctrl+Enter
JSONNode._openLinkIfUrl(event)
this._openLinkIfUrl(event)
}
}
@ -377,8 +377,8 @@ export default class JSONNode extends Component {
* @param event
* @private
*/
static _openLinkIfUrl (event) {
const value = JSONNode._getValueFromEvent(event)
_openLinkIfUrl (event) {
const value = this._getValueFromEvent(event)
if (isUrl(value)) {
event.preventDefault()
@ -388,11 +388,19 @@ export default class JSONNode extends Component {
}
}
static _getValueFromEvent (event) {
return stringConvert(unescapeHTML(getInnerText(event.target)))
/**
* Get the value of the target of an event, and convert it to it's type
* @param event
* @return {string | number | boolean | null}
* @private
*/
_getValueFromEvent (event) {
const stringValue = unescapeHTML(getInnerText(event.target))
return this.props.data.type === 'string'
? stringValue
: stringConvert(stringValue)
}
/**
* Find the root DOM element of the JSONEditor
* Search is done based on the CSS class 'jsoneditor'

View File

@ -371,7 +371,7 @@ function jsonToData (path, json, expand) {
}
else {
return {
type: 'json',
type: 'value',
value: json
}
}
@ -383,21 +383,21 @@ function jsonToData (path, json, expand) {
* @return {Object | Array | string | number | boolean | null} json
*/
function dataToJson (data) {
if (data.type === 'array') {
return data.items.map(dataToJson)
}
else if (data.type === 'object') {
const object = {}
switch (data.type) {
case 'array':
return data.items.map(dataToJson)
data.props.forEach(prop => {
object[prop.name] = dataToJson(prop.value)
})
case 'object':
const object = {}
return object
}
else {
// type 'value' or 'string'
return data.value
data.props.forEach(prop => {
object[prop.name] = dataToJson(prop.value)
})
return object
default: // type 'string' or 'value'
return data.value
}
}