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