Show More
@@ -9,6 +9,7 b' import io' | |||||
9 | import os |
|
9 | import os | |
10 | import shutil |
|
10 | import shutil | |
11 | from contextlib import contextmanager |
|
11 | from contextlib import contextmanager | |
|
12 | import mimetypes | |||
12 |
|
13 | |||
13 | from tornado import web |
|
14 | from tornado import web | |
14 |
|
15 | |||
@@ -204,6 +205,7 b' class FileContentsManager(ContentsManager):' | |||||
204 | model['created'] = created |
|
205 | model['created'] = created | |
205 | model['content'] = None |
|
206 | model['content'] = None | |
206 | model['format'] = None |
|
207 | model['format'] = None | |
|
208 | model['mimetype'] = None | |||
207 | try: |
|
209 | try: | |
208 | model['writable'] = os.access(os_path, os.W_OK) |
|
210 | model['writable'] = os.access(os_path, os.W_OK) | |
209 | except OSError: |
|
211 | except OSError: | |
@@ -264,8 +266,11 b' class FileContentsManager(ContentsManager):' | |||||
264 | """ |
|
266 | """ | |
265 | model = self._base_model(path) |
|
267 | model = self._base_model(path) | |
266 | model['type'] = 'file' |
|
268 | model['type'] = 'file' | |
|
269 | ||||
|
270 | os_path = self._get_os_path(path) | |||
|
271 | model['mimetype'] = mimetypes.guess_type(os_path)[0] or 'text/plain' | |||
|
272 | ||||
267 | if content: |
|
273 | if content: | |
268 | os_path = self._get_os_path(path) |
|
|||
269 | if not os.path.isfile(os_path): |
|
274 | if not os.path.isfile(os_path): | |
270 | # could be FIFO |
|
275 | # could be FIFO | |
271 | raise web.HTTPError(400, "Cannot get content of non-file %s" % os_path) |
|
276 | raise web.HTTPError(400, "Cannot get content of non-file %s" % os_path) |
@@ -5,19 +5,56 b' require([' | |||||
5 | 'jquery', |
|
5 | 'jquery', | |
6 | 'base/js/utils', |
|
6 | 'base/js/utils', | |
7 | 'base/js/page', |
|
7 | 'base/js/page', | |
|
8 | 'contents', | |||
8 | 'codemirror/lib/codemirror', |
|
9 | 'codemirror/lib/codemirror', | |
|
10 | 'codemirror/mode/meta', | |||
9 | 'custom/custom', |
|
11 | 'custom/custom', | |
10 | ], function( |
|
12 | ], function( | |
11 | $, |
|
13 | $, | |
12 | utils, |
|
14 | utils, | |
13 | page, |
|
15 | page, | |
|
16 | contents, | |||
14 | CodeMirror |
|
17 | CodeMirror | |
15 | ){ |
|
18 | ){ | |
16 | page = new page.Page(); |
|
19 | page = new page.Page(); | |
17 |
|
20 | |||
18 | var base_url = utils.get_body_data('baseUrl'); |
|
21 | var base_url = utils.get_body_data('baseUrl'); | |
19 | var cm_instance = CodeMirror($('#texteditor-container')[0]); |
|
22 | contents = new contents.Contents({base_url: base_url}); | |
20 |
|
||||
21 | page.show(); |
|
|||
22 |
|
23 | |||
|
24 | var file_path = utils.get_body_data('filePath'); | |||
|
25 | var ix = file_path.lastIndexOf("/"); | |||
|
26 | var dir_path, basename; | |||
|
27 | if (ix == -1) { | |||
|
28 | dir_path = ''; | |||
|
29 | basename = file_path; | |||
|
30 | } else { | |||
|
31 | dir_path = file_path.substring(0, ix); | |||
|
32 | basename = file_path.substring(ix); | |||
|
33 | } | |||
|
34 | contents.load(dir_path, basename, { | |||
|
35 | success: function(model) { | |||
|
36 | page.show(); | |||
|
37 | if (model.type === "file" && model.format === "text") { | |||
|
38 | console.log(modeinfo); | |||
|
39 | var cm = CodeMirror($('#texteditor-container')[0], { | |||
|
40 | value: model.content, | |||
|
41 | }); | |||
|
42 | ||||
|
43 | // Find and load the highlighting mode | |||
|
44 | var modeinfo = CodeMirror.findModeByMIME(model.mimetype); | |||
|
45 | if (modeinfo) { | |||
|
46 | utils.requireCodeMirrorMode(modeinfo.mode, function() { | |||
|
47 | cm.setOption('mode', modeinfo.mode); | |||
|
48 | }); | |||
|
49 | } | |||
|
50 | ||||
|
51 | // Attach to document for debugging | |||
|
52 | document.cm_instance = cm; | |||
|
53 | } else { | |||
|
54 | $('#texteditor-container').append( | |||
|
55 | $('<p/>').text(dir_path + " is not a text file") | |||
|
56 | ); | |||
|
57 | } | |||
|
58 | } | |||
|
59 | }); | |||
23 | }); |
|
60 | }); |
@@ -2,13 +2,19 b'' | |||||
2 |
|
2 | |||
3 | {% block title %}{{page_title}}{% endblock %} |
|
3 | {% block title %}{{page_title}}{% endblock %} | |
4 |
|
4 | |||
|
5 | {% block stylesheet %} | |||
|
6 | <link rel="stylesheet" href="{{ static_url('components/codemirror/lib/codemirror.css') }}"> | |||
|
7 | ||||
|
8 | {{super()}} | |||
|
9 | {% endblock %} | |||
|
10 | ||||
5 | {% block params %} |
|
11 | {% block params %} | |
6 |
|
12 | |||
7 | data-base-url="{{base_url}}" |
|
13 | data-base-url="{{base_url}}" | |
|
14 | data-file-path="{{file_path}}" | |||
8 |
|
15 | |||
9 | {% endblock %} |
|
16 | {% endblock %} | |
10 |
|
17 | |||
11 |
|
||||
12 | {% block site %} |
|
18 | {% block site %} | |
13 |
|
19 | |||
14 | <div id="texteditor-container"></div> |
|
20 | <div id="texteditor-container"></div> |
@@ -6,12 +6,21 b'' | |||||
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, file_path_regex | |
|
9 | from ..utils import url_escape | |||
9 |
|
10 | |||
10 | class EditorHandler(IPythonHandler): |
|
11 | class EditorHandler(IPythonHandler): | |
11 | """Render the terminal interface.""" |
|
12 | """Render the terminal interface.""" | |
12 | @web.authenticated |
|
13 | @web.authenticated | |
13 | def get(self, path, name): |
|
14 | def get(self, path, name): | |
14 | self.write(self.render_template('texteditor.html')) |
|
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)) | |||
|
18 | ||||
|
19 | file_path = url_escape(path) + "/" + url_escape(name) | |||
|
20 | self.write(self.render_template('texteditor.html', | |||
|
21 | file_path=file_path, | |||
|
22 | ) | |||
|
23 | ) | |||
15 |
|
24 | |||
16 | default_handlers = [ |
|
25 | default_handlers = [ | |
17 | (r"/texteditor%s" % file_path_regex, EditorHandler), |
|
26 | (r"/texteditor%s" % file_path_regex, EditorHandler), |
General Comments 0
You need to be logged in to leave comments.
Login now