From d755ca9d03853560588fa1fdde4a2498c6b449d2 Mon Sep 17 00:00:00 2001 From: jos Date: Fri, 23 Sep 2016 20:09:49 +0200 Subject: [PATCH] Implemented patch for TextMode --- src/TextMode.js | 19 ++++++++++++++++--- src/TreeMode.js | 10 ++++++++-- src/jsonData.js | 2 +- src/typedef.js | 6 ++++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/TextMode.js b/src/TextMode.js index f232129..f20da4b 100644 --- a/src/TextMode.js +++ b/src/TextMode.js @@ -1,5 +1,6 @@ import { h, Component } from 'preact' import { parseJSON } from './utils/jsonUtils' +import { jsonToData, dataToJson, patchData } from './jsonData' export default class TextMode extends Component { // TODO: define propTypes @@ -109,11 +110,23 @@ export default class TextMode extends Component { /** * Apply a JSONPatch to the current JSON document * @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) { - // TODO: implement patch - throw new Error('Patch not yet implemented') + const json = this.get() + + const data = jsonToData(json) + const result = patchData(data, actions) + + this.set(dataToJson(result.data)) + + return { + patch: actions, + revert: result.revert, + error: result.error + } } /** diff --git a/src/TreeMode.js b/src/TreeMode.js index 1acdc56..bc9b6a7 100644 --- a/src/TreeMode.js +++ b/src/TreeMode.js @@ -236,7 +236,9 @@ export default class TreeMode extends Component { /** * Apply a JSONPatch to the current JSON document * @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) { const result = patchData(this.state.data, actions) @@ -256,7 +258,11 @@ export default class TreeMode extends Component { historyIndex: 0 }) - return result.revert + return { + patch: actions, + revert: result.revert, + error: result.error + } } /** diff --git a/src/jsonData.js b/src/jsonData.js index 76d7b8a..8b186e8 100644 --- a/src/jsonData.js +++ b/src/jsonData.js @@ -90,7 +90,7 @@ export function toDataPath (data, path) { const index = path[0] const item = data.items[index] 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))) diff --git a/src/typedef.js b/src/typedef.js index bab48d2..29a8cfe 100644 --- a/src/typedef.js +++ b/src/typedef.js @@ -25,6 +25,12 @@ * @typedef {Array.<{op: string, path?: string, from?: string, value?: *}>} JSONPatch * * @typedef {{ + * patch: JSONPatch, + * revert: JSONPatch, + * error: null | Error + * }} JSONPatchResult + * + * @typedef {{ * mode: 'tree' | 'text', * indentation: number | string, * onChange: function (patch: JSONPatch, revert: JSONPatch),