Implemented patch for TextMode

This commit is contained in:
jos 2016-09-23 20:09:49 +02:00
parent 0c3faa03ea
commit d755ca9d03
4 changed files with 31 additions and 6 deletions

View File

@ -1,5 +1,6 @@
import { h, Component } from 'preact' import { h, Component } from 'preact'
import { parseJSON } from './utils/jsonUtils' import { parseJSON } from './utils/jsonUtils'
import { jsonToData, dataToJson, patchData } from './jsonData'
export default class TextMode extends Component { export default class TextMode extends Component {
// TODO: define propTypes // TODO: define propTypes
@ -109,11 +110,23 @@ export default class TextMode extends Component {
/** /**
* Apply a JSONPatch to the current JSON document * Apply a JSONPatch to the current JSON document
* @param {JSONPatch} actions JSONPatch actions * @param {JSONPatch} actions JSONPatch actions
* @return {JSONPatch} Returns a JSONPatch to revert the applied patch * @return {JSONPatchResult} Returns a JSONPatch result containing the
* patch, a patch to revert the action, and
* an error object which is null when successful
*/ */
patch (actions) { patch (actions) {
// TODO: implement patch const json = this.get()
throw new Error('Patch not yet implemented')
const data = jsonToData(json)
const result = patchData(data, actions)
this.set(dataToJson(result.data))
return {
patch: actions,
revert: result.revert,
error: result.error
}
} }
/** /**

View File

@ -236,7 +236,9 @@ export default class TreeMode extends Component {
/** /**
* Apply a JSONPatch to the current JSON document * Apply a JSONPatch to the current JSON document
* @param {JSONPatch} actions JSONPatch actions * @param {JSONPatch} actions JSONPatch actions
* @return {JSONPatch} Returns a JSONPatch to revert the applied patch * @return {JSONPatchResult} Returns a JSONPatch result containing the
* patch, a patch to revert the action, and
* an error object which is null when successful
*/ */
patch (actions) { patch (actions) {
const result = patchData(this.state.data, actions) const result = patchData(this.state.data, actions)
@ -256,7 +258,11 @@ export default class TreeMode extends Component {
historyIndex: 0 historyIndex: 0
}) })
return result.revert return {
patch: actions,
revert: result.revert,
error: result.error
}
} }
/** /**

View File

@ -90,7 +90,7 @@ export function toDataPath (data, path) {
const index = path[0] const index = path[0]
const item = data.items[index] const item = data.items[index]
if (!item) { if (!item) {
throw new Error('Array item ' + index + ' not found') throw new Error('Array item "' + index + '" not found')
} }
return ['items', index].concat(toDataPath(item, path.slice(1))) return ['items', index].concat(toDataPath(item, path.slice(1)))

View File

@ -25,6 +25,12 @@
* @typedef {Array.<{op: string, path?: string, from?: string, value?: *}>} JSONPatch * @typedef {Array.<{op: string, path?: string, from?: string, value?: *}>} JSONPatch
* *
* @typedef {{ * @typedef {{
* patch: JSONPatch,
* revert: JSONPatch,
* error: null | Error
* }} JSONPatchResult
*
* @typedef {{
* mode: 'tree' | 'text', * mode: 'tree' | 'text',
* indentation: number | string, * indentation: number | string,
* onChange: function (patch: JSONPatch, revert: JSONPatch), * onChange: function (patch: JSONPatch, revert: JSONPatch),