|
|
|
|
@ -21,56 +21,46 @@ function($,
|
|
|
|
|
|
|
|
|
|
this.codemirror = CodeMirror($(this.selector)[0]);
|
|
|
|
|
|
|
|
|
|
this.save_enabled = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO: Remove this once the contents API is refactored to just use paths
|
|
|
|
|
Editor.prototype._split_path = function() {
|
|
|
|
|
var ix = this.file_path.lastIndexOf("/");
|
|
|
|
|
if (ix === -1) {
|
|
|
|
|
return ['', this.file_path];
|
|
|
|
|
} else {
|
|
|
|
|
return [
|
|
|
|
|
this.file_path.substring(0, ix),
|
|
|
|
|
this.file_path.substring(ix+1)
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
this.save_enabled = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Editor.prototype.load = function() {
|
|
|
|
|
var split_path = this._split_path();
|
|
|
|
|
var that = this;
|
|
|
|
|
var cm = this.codemirror;
|
|
|
|
|
this.contents.load(split_path[0], split_path[1], {
|
|
|
|
|
success: function(model) {
|
|
|
|
|
if (model.type === "file" && model.format === "text") {
|
|
|
|
|
cm.setValue(model.content);
|
|
|
|
|
|
|
|
|
|
// Find and load the highlighting mode
|
|
|
|
|
var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
|
|
|
|
|
if (modeinfo) {
|
|
|
|
|
utils.requireCodeMirrorMode(modeinfo.mode, function() {
|
|
|
|
|
cm.setOption('mode', modeinfo.mode);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.codemirror.setValue("Error! Not a text file. Saving disabled.");
|
|
|
|
|
this.save_enabled = false;
|
|
|
|
|
this.contents.get(this.file_path, {type: 'file', format: 'text'})
|
|
|
|
|
.then(function(model) {
|
|
|
|
|
cm.setValue(model.content);
|
|
|
|
|
|
|
|
|
|
// Find and load the highlighting mode
|
|
|
|
|
var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
|
|
|
|
|
if (modeinfo) {
|
|
|
|
|
utils.requireCodeMirrorMode(modeinfo.mode, function() {
|
|
|
|
|
cm.setOption('mode', modeinfo.mode);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
that.save_enabled = true;
|
|
|
|
|
},
|
|
|
|
|
function(error) {
|
|
|
|
|
cm.setValue("Error! " + error.message +
|
|
|
|
|
"\nSaving disabled.");
|
|
|
|
|
that.save_enabled = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Editor.prototype.save = function() {
|
|
|
|
|
var split_path = this._split_path();
|
|
|
|
|
if (!this.save_enabled) {
|
|
|
|
|
console.log("Not saving, save disabled");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var model = {
|
|
|
|
|
path: split_path[0],
|
|
|
|
|
name: split_path[1],
|
|
|
|
|
path: this.file_path,
|
|
|
|
|
type: 'file',
|
|
|
|
|
format: 'text',
|
|
|
|
|
content: this.codemirror.getValue(),
|
|
|
|
|
};
|
|
|
|
|
var that = this;
|
|
|
|
|
this.contents.save(split_path[0], split_path[1], model, {
|
|
|
|
|
this.contents.save(this.file_path, model, {
|
|
|
|
|
success: function() {
|
|
|
|
|
that.events.trigger("save_succeeded.TextEditor");
|
|
|
|
|
}
|
|
|
|
|
|