##// END OF EJS Templates
Update text editor for new contents API
Thomas Kluyver -
Show More
@@ -21,56 +21,46 b' function($,'
21 21
22 22 this.codemirror = CodeMirror($(this.selector)[0]);
23 23
24 this.save_enabled = true;
25 };
26
27 // TODO: Remove this once the contents API is refactored to just use paths
28 Editor.prototype._split_path = function() {
29 var ix = this.file_path.lastIndexOf("/");
30 if (ix === -1) {
31 return ['', this.file_path];
32 } else {
33 return [
34 this.file_path.substring(0, ix),
35 this.file_path.substring(ix+1)
36 ];
37 }
24 this.save_enabled = false;
38 25 };
39 26
40 27 Editor.prototype.load = function() {
41 var split_path = this._split_path();
28 var that = this;
42 29 var cm = this.codemirror;
43 this.contents.load(split_path[0], split_path[1], {
44 success: function(model) {
45 if (model.type === "file" && model.format === "text") {
46 cm.setValue(model.content);
47
48 // Find and load the highlighting mode
49 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
50 if (modeinfo) {
51 utils.requireCodeMirrorMode(modeinfo.mode, function() {
52 cm.setOption('mode', modeinfo.mode);
53 });
54 }
55 } else {
56 this.codemirror.setValue("Error! Not a text file. Saving disabled.");
57 this.save_enabled = false;
30 this.contents.get(this.file_path, {type: 'file', format: 'text'})
31 .then(function(model) {
32 cm.setValue(model.content);
33
34 // Find and load the highlighting mode
35 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
36 if (modeinfo) {
37 utils.requireCodeMirrorMode(modeinfo.mode, function() {
38 cm.setOption('mode', modeinfo.mode);
39 });
58 40 }
41 that.save_enabled = true;
42 },
43 function(error) {
44 cm.setValue("Error! " + error.message +
45 "\nSaving disabled.");
46 that.save_enabled = false;
59 47 }
60 });
48 );
61 49 };
62 50
63 51 Editor.prototype.save = function() {
64 var split_path = this._split_path();
52 if (!this.save_enabled) {
53 console.log("Not saving, save disabled");
54 return;
55 }
65 56 var model = {
66 path: split_path[0],
67 name: split_path[1],
57 path: this.file_path,
68 58 type: 'file',
69 59 format: 'text',
70 60 content: this.codemirror.getValue(),
71 61 };
72 62 var that = this;
73 this.contents.save(split_path[0], split_path[1], model, {
63 this.contents.save(this.file_path, model, {
74 64 success: function() {
75 65 that.events.trigger("save_succeeded.TextEditor");
76 66 }
@@ -5,23 +5,22 b''
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 from tornado import web
8 from ..base.handlers import IPythonHandler, file_path_regex
8 from ..base.handlers import IPythonHandler, path_regex
9 9 from ..utils import url_escape
10 10
11 11 class EditorHandler(IPythonHandler):
12 """Render the terminal interface."""
12 """Render the text editor interface."""
13 13 @web.authenticated
14 def get(self, path, name):
14 def get(self, path):
15 15 path = path.strip('/')
16 if not self.contents_manager.file_exists(name, path):
17 raise web.HTTPError(404, u'File does not exist: %s/%s' % (path, name))
16 if not self.contents_manager.file_exists(path):
17 raise web.HTTPError(404, u'File does not exist: %s' % path)
18 18
19 file_path = url_escape(path) + "/" + url_escape(name)
20 19 self.write(self.render_template('texteditor.html',
21 file_path=file_path,
20 file_path=url_escape(path),
22 21 )
23 22 )
24 23
25 24 default_handlers = [
26 (r"/texteditor%s" % file_path_regex, EditorHandler),
25 (r"/texteditor%s" % path_regex, EditorHandler),
27 26 ] No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now