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
app.load = function() {
//try {
try {
// notification handler
app.notifications = new Notifications();
@ -109,9 +109,16 @@ app.load = function() {
}
}
// Store whether editor or formatter is last changed
app.lastChanged = undefined;
// formatter
var container = document.getElementById("jsonformatter");
formatter = new JSONFormatter(container);
formatter = new JSONFormatter(container, {
change: function () {
app.lastChanged = formatter;
}
});
formatter.set(json);
formatter.onError = function (err) {
app.notifications.showError(err);
@ -119,7 +126,11 @@ app.load = function() {
// editor
container = document.getElementById("jsoneditor");
editor = new JSONEditor(container);
editor = new JSONEditor(container, {
change: function () {
app.lastChanged = editor;
}
});
editor.set(json);
// splitter
@ -243,9 +254,9 @@ app.load = function() {
// enforce FireFox to not do spell checking on any input field
document.body.spellcheck = false;
//} catch (err) {
// app.notifications.showError(err);
//}
} catch (err) {
app.notifications.showError(err);
}
};
/**
@ -299,7 +310,18 @@ app.openUrl = function (url) {
* Open a file explorer to save the file.
*/
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
var data = formatter.getText();
app.retriever.saveFile(data, function (err) {

View File

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