Show More
@@ -1,81 +1,71 b'' | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | 'jquery', |
|
6 | 6 | 'base/js/utils', |
|
7 | 7 | 'codemirror/lib/codemirror', |
|
8 | 8 | 'codemirror/mode/meta', |
|
9 | 9 | 'codemirror/addon/search/search' |
|
10 | 10 | ], |
|
11 | 11 | function($, |
|
12 | 12 | utils, |
|
13 | 13 | CodeMirror |
|
14 | 14 | ) { |
|
15 | 15 | var Editor = function(selector, options) { |
|
16 | 16 | this.selector = selector; |
|
17 | 17 | this.contents = options.contents; |
|
18 | 18 | this.events = options.events; |
|
19 | 19 | this.base_url = options.base_url; |
|
20 | 20 | this.file_path = options.file_path; |
|
21 | 21 | |
|
22 | 22 | this.codemirror = CodeMirror($(this.selector)[0]); |
|
23 | 23 | |
|
24 |
this.save_enabled = |
|
|
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 |
|
|
28 | var that = this; | |
|
42 | 29 | var cm = this.codemirror; |
|
43 | this.contents.load(split_path[0], split_path[1], { | |
|
44 |
|
|
|
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 |
|
|
|
51 |
|
|
|
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: |
|
|
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( |
|
|
63 | this.contents.save(this.file_path, model, { | |
|
74 | 64 | success: function() { |
|
75 | 65 | that.events.trigger("save_succeeded.TextEditor"); |
|
76 | 66 | } |
|
77 | 67 | }); |
|
78 | 68 | }; |
|
79 | 69 | |
|
80 | 70 | return {Editor: Editor}; |
|
81 | 71 | }); |
@@ -1,27 +1,26 b'' | |||
|
1 | 1 | #encoding: utf-8 |
|
2 | 2 | """Tornado handlers for the terminal emulator.""" |
|
3 | 3 | |
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
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, |
|
|
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 ter |
|
|
12 | """Render the text editor interface.""" | |
|
13 | 13 | @web.authenticated |
|
14 |
def get(self, path |
|
|
14 | def get(self, path): | |
|
15 | 15 | path = path.strip('/') |
|
16 |
if not self.contents_manager.file_exists( |
|
|
17 |
raise web.HTTPError(404, u'File does not exist: %s |
|
|
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= |
|
|
20 | file_path=url_escape(path), | |
|
22 | 21 | ) |
|
23 | 22 | ) |
|
24 | 23 | |
|
25 | 24 | default_handlers = [ |
|
26 |
(r"/texteditor%s" % |
|
|
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