Show More
@@ -1,74 +1,78 b'' | |||||
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 |
|
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 |
|
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. |
|
26 | // instance on the page, this is good enough for now. | |
27 | CodeMirror.commands.save = $.proxy(this.save, this); |
|
27 | CodeMirror.commands.save = $.proxy(this.save, this); | |
28 |
|
28 | |||
29 | this.save_enabled = false; |
|
29 | this.save_enabled = false; | |
30 | }; |
|
30 | }; | |
31 |
|
31 | |||
32 | Editor.prototype.load = function() { |
|
32 | Editor.prototype.load = function() { | |
33 | var that = this; |
|
33 | var that = this; | |
34 | var cm = this.codemirror; |
|
34 | var cm = this.codemirror; | |
35 | this.contents.get(this.file_path, {type: 'file', format: 'text'}) |
|
35 | this.contents.get(this.file_path, {type: 'file', format: 'text'}) | |
36 | .then(function(model) { |
|
36 | .then(function(model) { | |
37 | cm.setValue(model.content); |
|
37 | cm.setValue(model.content); | |
38 |
|
38 | |||
|
39 | // Setting the file's initial value creates a history entry, | |||
|
40 | // which we don't want. | |||
|
41 | cm.clearHistory(); | |||
|
42 | ||||
39 | // Find and load the highlighting mode |
|
43 | // Find and load the highlighting mode | |
40 | var modeinfo = CodeMirror.findModeByMIME(model.mimetype); |
|
44 | var modeinfo = CodeMirror.findModeByMIME(model.mimetype); | |
41 | if (modeinfo) { |
|
45 | if (modeinfo) { | |
42 | utils.requireCodeMirrorMode(modeinfo.mode, function() { |
|
46 | utils.requireCodeMirrorMode(modeinfo.mode, function() { | |
43 | cm.setOption('mode', modeinfo.mode); |
|
47 | cm.setOption('mode', modeinfo.mode); | |
44 | }); |
|
48 | }); | |
45 | } |
|
49 | } | |
46 | that.save_enabled = true; |
|
50 | that.save_enabled = true; | |
47 | }, |
|
51 | }, | |
48 | function(error) { |
|
52 | function(error) { | |
49 | cm.setValue("Error! " + error.message + |
|
53 | cm.setValue("Error! " + error.message + | |
50 | "\nSaving disabled."); |
|
54 | "\nSaving disabled."); | |
51 | that.save_enabled = false; |
|
55 | that.save_enabled = false; | |
52 | } |
|
56 | } | |
53 | ); |
|
57 | ); | |
54 | }; |
|
58 | }; | |
55 |
|
59 | |||
56 | Editor.prototype.save = function() { |
|
60 | Editor.prototype.save = function() { | |
57 | if (!this.save_enabled) { |
|
61 | if (!this.save_enabled) { | |
58 | console.log("Not saving, save disabled"); |
|
62 | console.log("Not saving, save disabled"); | |
59 | return; |
|
63 | return; | |
60 | } |
|
64 | } | |
61 | var model = { |
|
65 | var model = { | |
62 | path: this.file_path, |
|
66 | path: this.file_path, | |
63 | type: 'file', |
|
67 | type: 'file', | |
64 | format: 'text', |
|
68 | format: 'text', | |
65 | content: this.codemirror.getValue(), |
|
69 | content: this.codemirror.getValue(), | |
66 | }; |
|
70 | }; | |
67 | var that = this; |
|
71 | var that = this; | |
68 | this.contents.save(this.file_path, model).then(function() { |
|
72 | this.contents.save(this.file_path, model).then(function() { | |
69 | that.events.trigger("save_succeeded.TextEditor"); |
|
73 | that.events.trigger("save_succeeded.TextEditor"); | |
70 | }); |
|
74 | }); | |
71 | }; |
|
75 | }; | |
72 |
|
76 | |||
73 | return {Editor: Editor}; |
|
77 | return {Editor: Editor}; | |
74 | }); |
|
78 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now