The factory function now creates a neat wrapper object instead of returning a TreeMode object

This commit is contained in:
jos 2016-08-19 21:14:09 +02:00
parent 422315dba2
commit 41821ef825
2 changed files with 34 additions and 49 deletions

View File

@ -1,43 +0,0 @@
import { h, Component } from 'preact'
import TreeMode from './TreeMode'
import bindMethods from './utils/bindMethods'
// TODO: implement and use JSONEditor.js after we've integrated Redux.
export default class JSONEditor extends Component {
constructor (props) {
super(props)
bindMethods(this)
this.state = {
options: props.options,
data: null
}
}
render () {
return h(TreeMode, {
options: this.state.options,
data: this.state.data
}, [])
}
/**
* Set JSON object in editor
* @param {Object | Array | string | number | boolean | null} json JSON data
* @param {SetOptions} [options]
*/
set (json, options = {}) {
// TODO: set state via redux action
}
/**
* Get JSON from the editor
* @returns {Object | Array | string | number | boolean | null} json
*/
get () {
// TODO: get state from redux store
}
}

View File

@ -4,17 +4,45 @@ import TreeMode from './TreeMode'
import '!style!css!less!./jsoneditor.less'
/**
* Factory function to create a new JSONEditor
* @param container
* Create a new json editor
* @param {HTMLElement} container
* @param {Options} options
* @return {*}
* @return {Object}
* @constructor
*/
function jsoneditor (container, options) {
// TODO: use JSONEditor instead of TreeMode
const elem = render(h(TreeMode, {options}), container)
return elem._component
const component = elem._component
return {
isJSONEditor: true,
_container: container,
_options: options,
_component: component,
/**
* Set JSON object in editor
* @param {Object | Array | string | number | boolean | null} json JSON data
* @param {SetOptions} [options]
*/
set: function (json, options = {}) {
component.set(json, options)
},
/**
* Get JSON from the editor
* @returns {Object | Array | string | number | boolean | null} json
*/
get: function () {
return component.get()
}
// TODO: implement getText
// TODO: implement setText
// TODO: implement expand
}
}
// TODO: use export default jsoneditor, doesn't work out of the box in webpack
module.exports = jsoneditor;
module.exports = jsoneditor