##// END OF EJS Templates
update mode on rename
Min RK -
Show More
@@ -1,178 +1,184 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/comment/comment',
10 10 'codemirror/addon/dialog/dialog',
11 11 'codemirror/addon/edit/closebrackets',
12 12 'codemirror/addon/edit/matchbrackets',
13 13 'codemirror/addon/search/searchcursor',
14 14 'codemirror/addon/search/search',
15 15 'codemirror/keymap/emacs',
16 16 'codemirror/keymap/sublime',
17 17 'codemirror/keymap/vim',
18 18 ],
19 19 function($,
20 20 utils,
21 21 CodeMirror
22 22 ) {
23 23 "use strict";
24 24
25 25 var Editor = function(selector, options) {
26 26 var that = this;
27 27 this.selector = selector;
28 28 this.contents = options.contents;
29 29 this.events = options.events;
30 30 this.base_url = options.base_url;
31 31 this.file_path = options.file_path;
32 32 this.config = options.config;
33 33 this.codemirror = new CodeMirror($(this.selector)[0]);
34 34 this.generation = -1;
35 35
36 36 // It appears we have to set commands on the CodeMirror class, not the
37 37 // instance. I'd like to be wrong, but since there should only be one CM
38 38 // instance on the page, this is good enough for now.
39 39 CodeMirror.commands.save = $.proxy(this.save, this);
40 40
41 41 this.save_enabled = false;
42 42
43 43 this.config.loaded.then(function () {
44 44 // load codemirror config
45 45 var cfg = that.config.data.Editor || {};
46 46 var cmopts = $.extend(true, {}, // true = recursive copy
47 47 Editor.default_codemirror_options,
48 48 cfg.codemirror_options || {}
49 49 );
50 50 that._set_codemirror_options(cmopts);
51 51 that.events.trigger('config_changed.Editor', {config: that.config});
52 52 });
53 53 };
54 54
55 55 // default CodeMirror options
56 56 Editor.default_codemirror_options = {
57 57 extraKeys: {
58 58 "Tab" : "indentMore",
59 59 },
60 60 indentUnit: 4,
61 61 theme: "ipython",
62 62 lineNumbers: true,
63 63 };
64 64
65 65 Editor.prototype.load = function() {
66 66 /** load the file */
67 67 var that = this;
68 68 var cm = this.codemirror;
69 69 return this.contents.get(this.file_path, {type: 'file', format: 'text'})
70 70 .then(function(model) {
71 71 cm.setValue(model.content);
72 72
73 73 // Setting the file's initial value creates a history entry,
74 74 // which we don't want.
75 75 cm.clearHistory();
76 that._set_mode_for_model(model);
77 that.save_enabled = true;
78 that.generation = cm.changeGeneration();
79 that.events.trigger("file_loaded.Editor", model);
80 },
81 function(error) {
82 cm.setValue("Error! " + error.message +
83 "\nSaving disabled.");
84 that.save_enabled = false;
85 }
86 );
87 };
88
89 Editor.prototype._set_mode_for_model = function (model) {
90 /** Set the CodeMirror mode based on the file model */
76 91
77 92 // Find and load the highlighting mode,
78 93 // first by mime-type, then by file extension
79 94 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
80 95 if (modeinfo.mode === "null") {
81 96 // find by mime failed, use find by ext
82 97 var ext_idx = model.name.lastIndexOf('.');
83 98
84 99 if (ext_idx > 0) {
85 100 // CodeMirror.findModeByExtension wants extension without '.'
86 101 modeinfo = CodeMirror.findModeByExtension(model.name.slice(ext_idx + 1));
87 102 }
88 103 }
89 104 if (modeinfo) {
90 that.set_codemirror_mode(modeinfo);
105 this.set_codemirror_mode(modeinfo);
91 106 }
92 that.save_enabled = true;
93 that.generation = cm.changeGeneration();
94 that.events.trigger("file_loaded.Editor", model);
95 },
96 function(error) {
97 cm.setValue("Error! " + error.message +
98 "\nSaving disabled.");
99 that.save_enabled = false;
100 }
101 );
102 107 };
103 108
104 109 Editor.prototype.set_codemirror_mode = function (modeinfo) {
105 110 /** set the codemirror mode from a modeinfo struct */
106 111 var that = this;
107 112 utils.requireCodeMirrorMode(modeinfo, function () {
108 113 that.codemirror.setOption('mode', modeinfo.mode);
109 114 that.events.trigger("mode_changed.Editor", modeinfo);
110 115 });
111 116 };
112 117
113 118 Editor.prototype.get_filename = function () {
114 119 return utils.url_path_split(this.file_path)[1];
115 120 };
116 121
117 122 Editor.prototype.rename = function (new_name) {
118 123 /** rename the file */
119 124 var that = this;
120 125 var parent = utils.url_path_split(this.file_path)[0];
121 126 var new_path = utils.url_path_join(parent, new_name);
122 127 return this.contents.rename(this.file_path, new_path).then(
123 function (json) {
124 that.file_path = json.path;
125 that.events.trigger('file_renamed.Editor', json);
128 function (model) {
129 that.file_path = model.path;
130 that.events.trigger('file_renamed.Editor', model);
131 that._set_mode_for_model(model);
126 132 }
127 133 );
128 134 };
129 135
130 136 Editor.prototype.save = function () {
131 137 /** save the file */
132 138 if (!this.save_enabled) {
133 139 console.log("Not saving, save disabled");
134 140 return;
135 141 }
136 142 var model = {
137 143 path: this.file_path,
138 144 type: 'file',
139 145 format: 'text',
140 146 content: this.codemirror.getValue(),
141 147 };
142 148 var that = this;
143 149 // record change generation for isClean
144 150 this.generation = this.codemirror.changeGeneration();
145 151 return this.contents.save(this.file_path, model).then(function(data) {
146 152 that.events.trigger("file_saved.Editor", data);
147 153 });
148 154 };
149 155
150 156 Editor.prototype._set_codemirror_options = function (options) {
151 157 // update codemirror options from a dict
152 158 for (var opt in options) {
153 159 if (!options.hasOwnProperty(opt)) {
154 160 continue;
155 161 }
156 162 var value = options[opt];
157 163 if (value === null) {
158 164 value = CodeMirror.defaults[opt];
159 165 }
160 166 this.codemirror.setOption(opt, value);
161 167 }
162 168 };
163 169
164 170 Editor.prototype.update_codemirror_options = function (options) {
165 171 /** update codemirror options locally and save changes in config */
166 172 var that = this;
167 173 this._set_codemirror_options(options);
168 174 return this.config.update({
169 175 Editor: {
170 176 codemirror_options: options
171 177 }
172 178 }).then(
173 179 that.events.trigger('config_changed.Editor', {config: that.config})
174 180 );
175 181 };
176 182
177 183 return {Editor: Editor};
178 184 });
General Comments 0
You need to be logged in to leave comments. Login now