##// END OF EJS Templates
Better way of saving through CodeMirror...
Thomas Kluyver -
Show More
@@ -1,72 +1,74
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 // It appears we have to set commands on the CodeMirror class, not the
25 // instance. I'd like to be wrong, but since there should only be one CM
26 // instance on the page, this is good enough for now.
27 CodeMirror.commands.save = $.proxy(this.save, this);
28
24 29 this.save_enabled = false;
25 30 };
26 31
27 32 Editor.prototype.load = function() {
28 33 var that = this;
29 34 var cm = this.codemirror;
30 35 this.contents.get(this.file_path, {type: 'file', format: 'text'})
31 36 .then(function(model) {
32 37 cm.setValue(model.content);
33 38
34 39 // Find and load the highlighting mode
35 40 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
36 41 if (modeinfo) {
37 42 utils.requireCodeMirrorMode(modeinfo.mode, function() {
38 43 cm.setOption('mode', modeinfo.mode);
39 44 });
40 45 }
41 46 that.save_enabled = true;
42 47 },
43 48 function(error) {
44 49 cm.setValue("Error! " + error.message +
45 50 "\nSaving disabled.");
46 51 that.save_enabled = false;
47 52 }
48 53 );
49 cm.setOption("extraKeys", {
50 "Ctrl-S": $.proxy(this.save, this),
51 });
52 54 };
53 55
54 56 Editor.prototype.save = function() {
55 57 if (!this.save_enabled) {
56 58 console.log("Not saving, save disabled");
57 59 return;
58 60 }
59 61 var model = {
60 62 path: this.file_path,
61 63 type: 'file',
62 64 format: 'text',
63 65 content: this.codemirror.getValue(),
64 66 };
65 67 var that = this;
66 68 this.contents.save(this.file_path, model).then(function() {
67 69 that.events.trigger("save_succeeded.TextEditor");
68 70 });
69 71 };
70 72
71 73 return {Editor: Editor};
72 74 });
General Comments 0
You need to be logged in to leave comments. Login now