Some fixes in undo/redo a transform action (WIP)

This commit is contained in:
jos 2018-06-20 15:18:48 +02:00
parent 866c520a71
commit 86d2c60fb4
3 changed files with 30 additions and 9 deletions

View File

@ -120,7 +120,7 @@ function History (editor) {
} }
}, },
'transform': { 'sort': {
'undo': function (params) { 'undo': function (params) {
var node = params.node; var node = params.node;
node.hideChilds(); node.hideChilds();
@ -135,6 +135,21 @@ function History (editor) {
node.updateDom({updateIndexes: true}); node.updateDom({updateIndexes: true});
node.showChilds(); node.showChilds();
} }
},
'transform': {
'undo': function (params) {
var node = params.node;
node.setValue(params.oldValue);
// TODO: would be nice to restore the state of the node and childs
},
'redo': function (params) {
var node = params.node;
node.setValue(params.newValue);
// TODO: would be nice to restore the state of the node and childs
}
} }
// TODO: restore the original caret position and selection with each undo // TODO: restore the original caret position and selection with each undo

View File

@ -685,10 +685,10 @@ Node.prototype.appendChild = function(node, visible, updateDom) {
if (this.expanded && visible !== false) { if (this.expanded && visible !== false) {
// insert into the DOM, before the appendRow // insert into the DOM, before the appendRow
var newTr = node.getDom(); var newTr = node.getDom();
var appendTr = this.getAppendDom(); var nextTr = this._getNextTr();
var table = appendTr ? appendTr.parentNode : undefined; var table = nextTr ? nextTr.parentNode : undefined;
if (appendTr && table) { if (nextTr && table) {
table.insertBefore(newTr, appendTr); table.insertBefore(newTr, nextTr);
} }
node.showChilds(); node.showChilds();
@ -3119,7 +3119,7 @@ Node.prototype.sort = function (path, direction) {
// update the index numbering // update the index numbering
this._updateDomIndexes(); this._updateDomIndexes();
this.editor._onAction('transform', { this.editor._onAction('sort', {
node: this, node: this,
oldChilds: oldChilds, oldChilds: oldChilds,
newChilds: this.childs newChilds: this.childs
@ -3146,14 +3146,18 @@ Node.prototype.transform = function (query) {
try { try {
// apply the JMESPath query // apply the JMESPath query
var transformed = jmespath.search(this.getValue(), query); var oldValue = this.getValue();
var newValue = jmespath.search(oldValue, query);
this.setValue(transformed); this.setValue(newValue);
this.editor._onAction('transform', { this.editor._onAction('transform', {
node: this, node: this,
oldValue: oldValue,
newValue: newValue,
oldChilds: oldChilds, oldChilds: oldChilds,
newChilds: this.childs newChilds: this.childs
// TODO: use oldChilds/newChilds in history or clean it up
}); });
this.showChilds(); this.showChilds();

View File

@ -25,7 +25,7 @@ function showTransformModal (node, container) {
'<tr>' + '<tr>' +
' <td>' + translate('transformQueryLabel') + ' </td>' + ' <td>' + translate('transformQueryLabel') + ' </td>' +
' <td class="jsoneditor-modal-input">' + ' <td class="jsoneditor-modal-input">' +
' <input id="query" type="text" title="' + translate('transformQueryTitle') + '" value="[*]"/>' + ' <input id="query" type="text" title="' + translate('transformQueryTitle') + '" value=""/>' +
' </td>' + ' </td>' +
'</tr>' + '</tr>' +
'<tr>' + '<tr>' +
@ -59,6 +59,8 @@ function showTransformModal (node, container) {
var query = modal.modalElem().querySelector('#query'); var query = modal.modalElem().querySelector('#query');
var preview = modal.modalElem().querySelector('#preview'); var preview = modal.modalElem().querySelector('#preview');
query.value = Array.isArray(value) ? '[*]' : '@';
function updatePreview() { function updatePreview() {
try { try {
var transformed = jmespath.search(value, query.value); var transformed = jmespath.search(value, query.value);