From 41821ef825a595b95be6a8693bd13326e8f1ffc1 Mon Sep 17 00:00:00 2001 From: jos Date: Fri, 19 Aug 2016 21:14:09 +0200 Subject: [PATCH] The factory function now creates a neat wrapper object instead of returning a TreeMode object --- src/JSONEditor.js | 43 ------------------------------------------- src/index.js | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 49 deletions(-) delete mode 100644 src/JSONEditor.js diff --git a/src/JSONEditor.js b/src/JSONEditor.js deleted file mode 100644 index 650358f..0000000 --- a/src/JSONEditor.js +++ /dev/null @@ -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 - } -} \ No newline at end of file diff --git a/src/index.js b/src/index.js index d39a2db..59e2f3f 100644 --- a/src/index.js +++ b/src/index.js @@ -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