##// END OF EJS Templates
add Mode menu to editor
Min RK -
Show More
@@ -21,7 +21,7 b' function($,'
21 CodeMirror
21 CodeMirror
22 ) {
22 ) {
23 "use strict";
23 "use strict";
24
24
25 var Editor = function(selector, options) {
25 var Editor = function(selector, options) {
26 var that = this;
26 var that = this;
27 this.selector = selector;
27 this.selector = selector;
@@ -74,11 +74,21 b' function($,'
74 // which we don't want.
74 // which we don't want.
75 cm.clearHistory();
75 cm.clearHistory();
76
76
77 // Find and load the highlighting mode
77 // Find and load the highlighting mode,
78 utils.requireCodeMirrorMode(model.mimetype, function(spec) {
78 // first by mime-type, then by file extension
79 var mode = CodeMirror.getMode({}, spec);
79 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
80 cm.setOption('mode', mode);
80 if (modeinfo.mode === "null") {
81 });
81 // find by mime failed, use find by ext
82 var ext_idx = model.name.lastIndexOf('.');
83
84 if (ext_idx > 0) {
85 // CodeMirror.findModeByExtension wants extension without '.'
86 modeinfo = CodeMirror.findModeByExtension(model.name.slice(ext_idx + 1));
87 }
88 }
89 if (modeinfo) {
90 that.set_codemirror_mode(modeinfo);
91 }
82 that.save_enabled = true;
92 that.save_enabled = true;
83 that.generation = cm.changeGeneration();
93 that.generation = cm.changeGeneration();
84 that.events.trigger("file_loaded.Editor", model);
94 that.events.trigger("file_loaded.Editor", model);
@@ -91,10 +101,18 b' function($,'
91 );
101 );
92 };
102 };
93
103
104 Editor.prototype.set_codemirror_mode = function (modeinfo) {
105 /** set the codemirror mode from a modeinfo struct */
106 var that = this;
107 utils.requireCodeMirrorMode(modeinfo, function () {
108 that.codemirror.setOption('mode', modeinfo.mode);
109 that.events.trigger("mode_changed.Editor", modeinfo);
110 });
111 };
112
94 Editor.prototype.get_filename = function () {
113 Editor.prototype.get_filename = function () {
95 return utils.url_path_split(this.file_path)[1];
114 return utils.url_path_split(this.file_path)[1];
96
115 };
97 }
98
116
99 Editor.prototype.rename = function (new_name) {
117 Editor.prototype.rename = function (new_name) {
100 /** rename the file */
118 /** rename the file */
@@ -2,12 +2,14 b''
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 'base/js/namespace',
6 'jquery',
5 'jquery',
6 'base/js/namespace',
7 'base/js/utils',
7 'base/js/utils',
8 'base/js/dialog',
8 'base/js/dialog',
9 'codemirror/lib/codemirror',
10 'codemirror/mode/meta',
9 'bootstrap',
11 'bootstrap',
10 ], function(IPython, $, utils, dialog, bootstrap) {
12 ], function($, IPython, utils, dialog, CodeMirror) {
11 "use strict";
13 "use strict";
12
14
13 var MenuBar = function (selector, options) {
15 var MenuBar = function (selector, options) {
@@ -37,6 +39,7 b' define(['
37 this.element = $(selector);
39 this.element = $(selector);
38 this.bind_events();
40 this.bind_events();
39 }
41 }
42 this._load_mode_menu();
40 Object.seal(this);
43 Object.seal(this);
41 };
44 };
42
45
@@ -120,6 +123,36 b' define(['
120 that.element.find(".selected-keymap").removeClass("selected-keymap");
123 that.element.find(".selected-keymap").removeClass("selected-keymap");
121 that.element.find("#menu-keymap-" + keyMap).addClass("selected-keymap");
124 that.element.find("#menu-keymap-" + keyMap).addClass("selected-keymap");
122 });
125 });
126
127 this.events.on("mode_changed.Editor", function (evt, modeinfo) {
128 that.element.find("#current-mode")
129 .text(modeinfo.name)
130 .attr(
131 'title',
132 "The current highlighting mode is " + modeinfo.name
133 );
134 });
135 };
136
137 MenuBar.prototype._load_mode_menu = function () {
138 var list = this.element.find("#mode-menu");
139 var editor = this.editor;
140 function make_set_mode(info) {
141 return function () {
142 editor.set_codemirror_mode(info);
143 };
144 }
145 for (var i = 0; i < CodeMirror.modeInfo.length; i++) {
146 var info = CodeMirror.modeInfo[i];
147 list.append($("<li>").append(
148 $("<a>").attr("href", "#")
149 .text(info.name)
150 .click(make_set_mode(info))
151 .attr('title',
152 "Set highlighting mode to " + info.name
153 )
154 ));
155 }
123 };
156 };
124
157
125 return {'MenuBar': MenuBar};
158 return {'MenuBar': MenuBar};
@@ -6,3 +6,9 b''
6 content: @fa-var-check;
6 content: @fa-var-check;
7 }
7 }
8 }
8 }
9
10 #mode-menu {
11 // truncate mode-menu, so it doesn't get longer than the screen
12 overflow: auto;
13 max-height: 20em;
14 } No newline at end of file
@@ -8125,6 +8125,10 b' ul#new-notebook-menu {'
8125 .selected-keymap i.fa:before {
8125 .selected-keymap i.fa:before {
8126 content: "\f00c";
8126 content: "\f00c";
8127 }
8127 }
8128 #mode-menu {
8129 overflow: auto;
8130 max-height: 20em;
8131 }
8128 #texteditor-container {
8132 #texteditor-container {
8129 border-bottom: 1px solid #ccc;
8133 border-bottom: 1px solid #ccc;
8130 }
8134 }
@@ -63,7 +63,12 b' data-file-path="{{file_path}}"'
63 <li id="menu-line-numbers"><a href="#">Hide Line Numbers</a></li>
63 <li id="menu-line-numbers"><a href="#">Hide Line Numbers</a></li>
64 </ul>
64 </ul>
65 </li>
65 </li>
66 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Mode</a>
67 <ul id="mode-menu" class="dropdown-menu">
68 </ul>
69 </li>
66 </ul>
70 </ul>
71 <p id="current-mode" class="navbar-text navbar-right">current mode</p>
67 </div>
72 </div>
68 </div>
73 </div>
69 </div>
74 </div>
General Comments 0
You need to be logged in to leave comments. Login now