Editor contents are now synched to the Formatter before saving to disk

This commit is contained in:
Jos de Jong 2012-11-03 11:18:38 +01:00
parent f64138ea52
commit b6311c8ad4
2 changed files with 41 additions and 9 deletions

View File

@ -64,7 +64,7 @@ app.editorToFormatter = function () {
*/ */
// TODO: split the method load in multiple methods, it is too large // TODO: split the method load in multiple methods, it is too large
app.load = function() { app.load = function() {
//try { try {
// notification handler // notification handler
app.notifications = new Notifications(); app.notifications = new Notifications();
@ -109,9 +109,16 @@ app.load = function() {
} }
} }
// Store whether editor or formatter is last changed
app.lastChanged = undefined;
// formatter // formatter
var container = document.getElementById("jsonformatter"); var container = document.getElementById("jsonformatter");
formatter = new JSONFormatter(container); formatter = new JSONFormatter(container, {
change: function () {
app.lastChanged = formatter;
}
});
formatter.set(json); formatter.set(json);
formatter.onError = function (err) { formatter.onError = function (err) {
app.notifications.showError(err); app.notifications.showError(err);
@ -119,7 +126,11 @@ app.load = function() {
// editor // editor
container = document.getElementById("jsoneditor"); container = document.getElementById("jsoneditor");
editor = new JSONEditor(container); editor = new JSONEditor(container, {
change: function () {
app.lastChanged = editor;
}
});
editor.set(json); editor.set(json);
// splitter // splitter
@ -243,9 +254,9 @@ app.load = function() {
// enforce FireFox to not do spell checking on any input field // enforce FireFox to not do spell checking on any input field
document.body.spellcheck = false; document.body.spellcheck = false;
//} catch (err) { } catch (err) {
// app.notifications.showError(err); app.notifications.showError(err);
//} }
}; };
/** /**
@ -299,7 +310,18 @@ app.openUrl = function (url) {
* Open a file explorer to save the file. * Open a file explorer to save the file.
*/ */
app.saveFile = function () { app.saveFile = function () {
// TODO: get data from the most recently changed editor: formatter or editor // first synchronize the editors and formatters contents
if (app.lastChanged == editor) {
app.editorToFormatter();
}
/* TODO: also sync from formatter to editor? will clear the history ...
if (app.lastChanged == formatter) {
app.formatterToEditor();
}
*/
app.lastChanged = undefined;
// save the text from the formatter
// TODO: show a 'saving...' notification // TODO: show a 'saving...' notification
var data = formatter.getText(); var data = formatter.getText();
app.retriever.saveFile(data, function (err) { app.retriever.saveFile(data, function (err) {

View File

@ -2872,8 +2872,6 @@ JSONEditor.prototype._createFrame = function () {
if (this.options.history) { if (this.options.history) {
// create separator // create separator
var separator = document.createElement('span'); var separator = document.createElement('span');
//separator.style.width = '5px';
//separator.style.display = 'inline';
separator.innerHTML = ' '; separator.innerHTML = ' ';
this.menu.appendChild(separator); this.menu.appendChild(separator);
@ -2882,7 +2880,13 @@ JSONEditor.prototype._createFrame = function () {
undo.className = 'jsoneditor-menu jsoneditor-undo'; undo.className = 'jsoneditor-menu jsoneditor-undo';
undo.title = 'Undo last action'; undo.title = 'Undo last action';
undo.onclick = function () { undo.onclick = function () {
// undo last action
editor.history.undo(); editor.history.undo();
// trigger change callback
if (editor.options.change) {
editor.options.change();
}
}; };
this.menu.appendChild(undo); this.menu.appendChild(undo);
this.dom.undo = undo; this.dom.undo = undo;
@ -2892,7 +2896,13 @@ JSONEditor.prototype._createFrame = function () {
redo.className = 'jsoneditor-menu jsoneditor-redo'; redo.className = 'jsoneditor-menu jsoneditor-redo';
redo.title = 'Redo'; redo.title = 'Redo';
redo.onclick = function () { redo.onclick = function () {
// redo last action
editor.history.redo(); editor.history.redo();
// trigger change callback
if (editor.options.change) {
editor.options.change();
}
}; };
this.menu.appendChild(redo); this.menu.appendChild(redo);
this.dom.redo = redo; this.dom.redo = redo;