Show More
@@ -1,72 +1,74 | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
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 | 'jquery', |
|
5 | 'jquery', | |
6 | 'base/js/utils', |
|
6 | 'base/js/utils', | |
7 | 'codemirror/lib/codemirror', |
|
7 | 'codemirror/lib/codemirror', | |
8 | 'codemirror/mode/meta', |
|
8 | 'codemirror/mode/meta', | |
9 | 'codemirror/addon/search/search' |
|
9 | 'codemirror/addon/search/search' | |
10 | ], |
|
10 | ], | |
11 | function($, |
|
11 | function($, | |
12 | utils, |
|
12 | utils, | |
13 | CodeMirror |
|
13 | CodeMirror | |
14 | ) { |
|
14 | ) { | |
15 | var Editor = function(selector, options) { |
|
15 | var Editor = function(selector, options) { | |
16 | this.selector = selector; |
|
16 | this.selector = selector; | |
17 | this.contents = options.contents; |
|
17 | this.contents = options.contents; | |
18 | this.events = options.events; |
|
18 | this.events = options.events; | |
19 | this.base_url = options.base_url; |
|
19 | this.base_url = options.base_url; | |
20 | this.file_path = options.file_path; |
|
20 | this.file_path = options.file_path; | |
21 |
|
21 | |||
22 | this.codemirror = CodeMirror($(this.selector)[0]); |
|
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 | this.save_enabled = false; |
|
29 | this.save_enabled = false; | |
25 | }; |
|
30 | }; | |
26 |
|
31 | |||
27 | Editor.prototype.load = function() { |
|
32 | Editor.prototype.load = function() { | |
28 | var that = this; |
|
33 | var that = this; | |
29 | var cm = this.codemirror; |
|
34 | var cm = this.codemirror; | |
30 | this.contents.get(this.file_path, {type: 'file', format: 'text'}) |
|
35 | this.contents.get(this.file_path, {type: 'file', format: 'text'}) | |
31 | .then(function(model) { |
|
36 | .then(function(model) { | |
32 | cm.setValue(model.content); |
|
37 | cm.setValue(model.content); | |
33 |
|
38 | |||
34 | // Find and load the highlighting mode |
|
39 | // Find and load the highlighting mode | |
35 | var modeinfo = CodeMirror.findModeByMIME(model.mimetype); |
|
40 | var modeinfo = CodeMirror.findModeByMIME(model.mimetype); | |
36 | if (modeinfo) { |
|
41 | if (modeinfo) { | |
37 | utils.requireCodeMirrorMode(modeinfo.mode, function() { |
|
42 | utils.requireCodeMirrorMode(modeinfo.mode, function() { | |
38 | cm.setOption('mode', modeinfo.mode); |
|
43 | cm.setOption('mode', modeinfo.mode); | |
39 | }); |
|
44 | }); | |
40 | } |
|
45 | } | |
41 | that.save_enabled = true; |
|
46 | that.save_enabled = true; | |
42 | }, |
|
47 | }, | |
43 | function(error) { |
|
48 | function(error) { | |
44 | cm.setValue("Error! " + error.message + |
|
49 | cm.setValue("Error! " + error.message + | |
45 | "\nSaving disabled."); |
|
50 | "\nSaving disabled."); | |
46 | that.save_enabled = false; |
|
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 | Editor.prototype.save = function() { |
|
56 | Editor.prototype.save = function() { | |
55 | if (!this.save_enabled) { |
|
57 | if (!this.save_enabled) { | |
56 | console.log("Not saving, save disabled"); |
|
58 | console.log("Not saving, save disabled"); | |
57 | return; |
|
59 | return; | |
58 | } |
|
60 | } | |
59 | var model = { |
|
61 | var model = { | |
60 | path: this.file_path, |
|
62 | path: this.file_path, | |
61 | type: 'file', |
|
63 | type: 'file', | |
62 | format: 'text', |
|
64 | format: 'text', | |
63 | content: this.codemirror.getValue(), |
|
65 | content: this.codemirror.getValue(), | |
64 | }; |
|
66 | }; | |
65 | var that = this; |
|
67 | var that = this; | |
66 | this.contents.save(this.file_path, model).then(function() { |
|
68 | this.contents.save(this.file_path, model).then(function() { | |
67 | that.events.trigger("save_succeeded.TextEditor"); |
|
69 | that.events.trigger("save_succeeded.TextEditor"); | |
68 | }); |
|
70 | }); | |
69 | }; |
|
71 | }; | |
70 |
|
72 | |||
71 | return {Editor: Editor}; |
|
73 | return {Editor: Editor}; | |
72 | }); |
|
74 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now