##// END OF EJS Templates
Update text editor for new contents API
Update text editor for new contents API

File last commit:

r19015:86aafe6d
r19015:86aafe6d
Show More
editor.js
71 lines | 2.1 KiB | application/javascript | JavascriptLexer
Thomas Kluyver
Refactor editor into Editor class
r19013 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'jquery',
'base/js/utils',
'codemirror/lib/codemirror',
'codemirror/mode/meta',
Thomas Kluyver
Enable codemirror search/replace shortcuts
r19014 'codemirror/addon/search/search'
Thomas Kluyver
Refactor editor into Editor class
r19013 ],
function($,
utils,
CodeMirror
) {
var Editor = function(selector, options) {
this.selector = selector;
this.contents = options.contents;
this.events = options.events;
this.base_url = options.base_url;
this.file_path = options.file_path;
this.codemirror = CodeMirror($(this.selector)[0]);
Thomas Kluyver
Update text editor for new contents API
r19015 this.save_enabled = false;
Thomas Kluyver
Refactor editor into Editor class
r19013 };
Editor.prototype.load = function() {
Thomas Kluyver
Update text editor for new contents API
r19015 var that = this;
Thomas Kluyver
Refactor editor into Editor class
r19013 var cm = this.codemirror;
Thomas Kluyver
Update text editor for new contents API
r19015 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);
});
Thomas Kluyver
Refactor editor into Editor class
r19013 }
Thomas Kluyver
Update text editor for new contents API
r19015 that.save_enabled = true;
},
function(error) {
cm.setValue("Error! " + error.message +
"\nSaving disabled.");
that.save_enabled = false;
Thomas Kluyver
Refactor editor into Editor class
r19013 }
Thomas Kluyver
Update text editor for new contents API
r19015 );
Thomas Kluyver
Refactor editor into Editor class
r19013 };
Editor.prototype.save = function() {
Thomas Kluyver
Update text editor for new contents API
r19015 if (!this.save_enabled) {
console.log("Not saving, save disabled");
return;
}
Thomas Kluyver
Refactor editor into Editor class
r19013 var model = {
Thomas Kluyver
Update text editor for new contents API
r19015 path: this.file_path,
Thomas Kluyver
Refactor editor into Editor class
r19013 type: 'file',
format: 'text',
content: this.codemirror.getValue(),
};
var that = this;
Thomas Kluyver
Update text editor for new contents API
r19015 this.contents.save(this.file_path, model, {
Thomas Kluyver
Refactor editor into Editor class
r19013 success: function() {
that.events.trigger("save_succeeded.TextEditor");
}
});
};
return {Editor: Editor};
});