Show More
@@ -1,80 +1,81 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 | 'codemirror/addon/search/search' | |
|
9 | 10 | ], |
|
10 | 11 | function($, |
|
11 | 12 | utils, |
|
12 | 13 | CodeMirror |
|
13 | 14 | ) { |
|
14 | 15 | var Editor = function(selector, options) { |
|
15 | 16 | this.selector = selector; |
|
16 | 17 | this.contents = options.contents; |
|
17 | 18 | this.events = options.events; |
|
18 | 19 | this.base_url = options.base_url; |
|
19 | 20 | this.file_path = options.file_path; |
|
20 | 21 | |
|
21 | 22 | this.codemirror = CodeMirror($(this.selector)[0]); |
|
22 | 23 | |
|
23 | 24 | this.save_enabled = true; |
|
24 | 25 | }; |
|
25 | 26 | |
|
26 | 27 | // TODO: Remove this once the contents API is refactored to just use paths |
|
27 | 28 | Editor.prototype._split_path = function() { |
|
28 | 29 | var ix = this.file_path.lastIndexOf("/"); |
|
29 | 30 | if (ix === -1) { |
|
30 | 31 | return ['', this.file_path]; |
|
31 | 32 | } else { |
|
32 | 33 | return [ |
|
33 | 34 | this.file_path.substring(0, ix), |
|
34 | 35 | this.file_path.substring(ix+1) |
|
35 | 36 | ]; |
|
36 | 37 | } |
|
37 | 38 | }; |
|
38 | 39 | |
|
39 | 40 | Editor.prototype.load = function() { |
|
40 | 41 | var split_path = this._split_path(); |
|
41 | 42 | var cm = this.codemirror; |
|
42 | 43 | this.contents.load(split_path[0], split_path[1], { |
|
43 | 44 | success: function(model) { |
|
44 | 45 | if (model.type === "file" && model.format === "text") { |
|
45 | 46 | cm.setValue(model.content); |
|
46 | 47 | |
|
47 | 48 | // Find and load the highlighting mode |
|
48 | 49 | var modeinfo = CodeMirror.findModeByMIME(model.mimetype); |
|
49 | 50 | if (modeinfo) { |
|
50 | 51 | utils.requireCodeMirrorMode(modeinfo.mode, function() { |
|
51 | 52 | cm.setOption('mode', modeinfo.mode); |
|
52 | 53 | }); |
|
53 | 54 | } |
|
54 | 55 | } else { |
|
55 | 56 | this.codemirror.setValue("Error! Not a text file. Saving disabled."); |
|
56 | 57 | this.save_enabled = false; |
|
57 | 58 | } |
|
58 | 59 | } |
|
59 | 60 | }); |
|
60 | 61 | }; |
|
61 | 62 | |
|
62 | 63 | Editor.prototype.save = function() { |
|
63 | 64 | var split_path = this._split_path(); |
|
64 | 65 | var model = { |
|
65 | 66 | path: split_path[0], |
|
66 | 67 | name: split_path[1], |
|
67 | 68 | type: 'file', |
|
68 | 69 | format: 'text', |
|
69 | 70 | content: this.codemirror.getValue(), |
|
70 | 71 | }; |
|
71 | 72 | var that = this; |
|
72 | 73 | this.contents.save(split_path[0], split_path[1], model, { |
|
73 | 74 | success: function() { |
|
74 | 75 | that.events.trigger("save_succeeded.TextEditor"); |
|
75 | 76 | } |
|
76 | 77 | }); |
|
77 | 78 | }; |
|
78 | 79 | |
|
79 | 80 | return {Editor: Editor}; |
|
80 | 81 | }); |
@@ -1,60 +1,61 b'' | |||
|
1 | 1 | {% extends "page.html" %} |
|
2 | 2 | |
|
3 | 3 | {% block title %}{{page_title}}{% endblock %} |
|
4 | 4 | |
|
5 | 5 | {% block stylesheet %} |
|
6 | 6 | <link rel="stylesheet" href="{{ static_url('components/codemirror/lib/codemirror.css') }}"> |
|
7 | <link rel="stylesheet" href="{{ static_url('components/codemirror/addon/dialog/dialog.css') }}"> | |
|
7 | 8 | |
|
8 | 9 | {{super()}} |
|
9 | 10 | {% endblock %} |
|
10 | 11 | |
|
11 | 12 | {% block params %} |
|
12 | 13 | |
|
13 | 14 | data-base-url="{{base_url}}" |
|
14 | 15 | data-file-path="{{file_path}}" |
|
15 | 16 | |
|
16 | 17 | {% endblock %} |
|
17 | 18 | |
|
18 | 19 | {% block site %} |
|
19 | 20 | |
|
20 | 21 | <div id="menubar-container" class="container"> |
|
21 | 22 | <div id="menubar"> |
|
22 | 23 | <div id="menus" class="navbar navbar-default" role="navigation"> |
|
23 | 24 | <div class="container-fluid"> |
|
24 | 25 | <button type="button" class="btn btn-default navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> |
|
25 | 26 | <i class="fa fa-bars"></i> |
|
26 | 27 | <span class="navbar-text">Menu</span> |
|
27 | 28 | </button> |
|
28 | 29 | <ul class="nav navbar-nav navbar-right"> |
|
29 | 30 | <li id="kernel_indicator"> |
|
30 | 31 | <i id="kernel_indicator_icon"></i> |
|
31 | 32 | </li> |
|
32 | 33 | <li id="modal_indicator"> |
|
33 | 34 | <i id="modal_indicator_icon"></i> |
|
34 | 35 | </li> |
|
35 | 36 | <li id="notification_area"></li> |
|
36 | 37 | </ul> |
|
37 | 38 | <div class="navbar-collapse collapse"> |
|
38 | 39 | <ul class="nav navbar-nav"> |
|
39 | 40 | <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">File</a> |
|
40 | 41 | <ul id="file_menu" class="dropdown-menu"> |
|
41 | 42 | <li id="save_file"><a href="#">Save</a></li> |
|
42 | 43 | </ul> |
|
43 | 44 | </li> |
|
44 | 45 | </ul> |
|
45 | 46 | </div> |
|
46 | 47 | </div> |
|
47 | 48 | </div> |
|
48 | 49 | </div> |
|
49 | 50 | </div> |
|
50 | 51 | |
|
51 | 52 | <div id="texteditor-container"></div> |
|
52 | 53 | |
|
53 | 54 | {% endblock %} |
|
54 | 55 | |
|
55 | 56 | {% block script %} |
|
56 | 57 | |
|
57 | 58 | {{super()}} |
|
58 | 59 | |
|
59 | 60 | <script src="{{ static_url("texteditor/js/main.js") }}" type="text/javascript" charset="utf-8"></script> |
|
60 | 61 | {% endblock %} |
General Comments 0
You need to be logged in to leave comments.
Login now