diff --git a/src/TextMode.js b/src/TextMode.js index 77b7645..0827bac 100644 --- a/src/TextMode.js +++ b/src/TextMode.js @@ -29,7 +29,11 @@ export default class TextMode extends Component { ]), h('div', {class: 'jsoneditor-contents'}, [ - h('textarea', {class: 'jsoneditor-text', value: this.state.text}) + h('textarea', { + class: 'jsoneditor-text', + value: this.state.text, + onChange: this.handleChange + }) ]) ]) } @@ -43,6 +47,17 @@ export default class TextMode extends Component { return this.props.options && this.props.options.indentation || 2 } + /** + * handle changed text input in the textarea + * @param {Event} event + * @private + */ + handleChange = (event) => { + this.setState({ + text: event.target.value + }) + } + /** * Format the json */ @@ -89,12 +104,18 @@ export default class TextMode extends Component { return parseJSON(this.state.text) } - // TODO: comment + /** + * Set a string containing a JSON document + * @param {string} text + */ setText (text) { this.setState({ text }) } - // TODO: comment + /** + * Get the JSON document as text + * @return {string} text + */ getText () { return this.state.text } diff --git a/src/TreeMode.js b/src/TreeMode.js index 100ec6d..1acdc56 100644 --- a/src/TreeMode.js +++ b/src/TreeMode.js @@ -1,13 +1,12 @@ import { h, Component } from 'preact' import { setIn, updateIn } from './utils/immutabilityHelpers' -import { - expand, jsonToData, dataToJson, toDataPath, patchData, compileJSONPointer -} from './jsonData' +import { expand, jsonToData, dataToJson, toDataPath, patchData } from './jsonData' import { duplicate, insert, append, remove, changeType, changeValue, changeProperty, sort } from './actions' import JSONNode from './JSONNode' +import { parseJSON } from './utils/jsonUtils' const MAX_HISTORY_ITEMS = 1000 // maximum number of undo/redo items to be kept in memory @@ -77,7 +76,7 @@ export default class TreeMode extends Component { ]), h('div', {class: 'jsoneditor-contents jsoneditor-tree-contents', onClick: JSONNode.hideContextMenu}, [ - h('ul', {class: 'jsoneditor-list'}, [ + h('ul', {class: 'jsoneditor-list jsoneditor-root'}, [ h(JSONNode, { data: state.data, events: state.events, @@ -287,6 +286,23 @@ export default class TreeMode extends Component { return dataToJson(this.state.data) } + /** + * Set a string containing a JSON document + * @param {string} text + */ + setText (text) { + this.set(parseJSON(text)) + } + + /** + * Get the JSON document as text + * @return {string} text + */ + getText () { + const indentation = this.props.options && this.props.options.indentation || 2 + return JSON.stringify(this.get(), null, indentation) + } + /** * Expand one or multiple objects or arrays * @param {Path | function (path: Path) : boolean} callback @@ -307,8 +323,6 @@ export default class TreeMode extends Component { }) } - // TODO: implement getText and setText - /** * Default function to determine whether or not to expand a node initially * diff --git a/src/develop.html b/src/develop.html index e59da97..51aed54 100644 --- a/src/develop.html +++ b/src/develop.html @@ -10,7 +10,7 @@ @@ -18,22 +18,29 @@
+ +