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