Update text editor for new contents API

This commit is contained in:
Thomas Kluyver 2014-11-14 15:23:45 -08:00
parent c1d012d703
commit 1b612e178b
2 changed files with 32 additions and 43 deletions

View File

@ -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");
}

View File

@ -5,23 +5,22 @@
# Distributed under the terms of the Modified BSD License.
from tornado import web
from ..base.handlers import IPythonHandler, file_path_regex
from ..base.handlers import IPythonHandler, path_regex
from ..utils import url_escape
class EditorHandler(IPythonHandler):
"""Render the terminal interface."""
"""Render the text editor interface."""
@web.authenticated
def get(self, path, name):
def get(self, path):
path = path.strip('/')
if not self.contents_manager.file_exists(name, path):
raise web.HTTPError(404, u'File does not exist: %s/%s' % (path, name))
if not self.contents_manager.file_exists(path):
raise web.HTTPError(404, u'File does not exist: %s' % path)
file_path = url_escape(path) + "/" + url_escape(name)
self.write(self.render_template('texteditor.html',
file_path=file_path,
file_path=url_escape(path),
)
)
default_handlers = [
(r"/texteditor%s" % file_path_regex, EditorHandler),
(r"/texteditor%s" % path_regex, EditorHandler),
]