Show More
@@ -1,134 +1,138 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/comment/comment', |
|
9 | 'codemirror/addon/comment/comment', | |
10 | 'codemirror/addon/dialog/dialog', |
|
10 | 'codemirror/addon/dialog/dialog', | |
11 | 'codemirror/addon/edit/closebrackets', |
|
11 | 'codemirror/addon/edit/closebrackets', | |
12 | 'codemirror/addon/edit/matchbrackets', |
|
12 | 'codemirror/addon/edit/matchbrackets', | |
13 | 'codemirror/addon/search/searchcursor', |
|
13 | 'codemirror/addon/search/searchcursor', | |
14 | 'codemirror/addon/search/search', |
|
14 | 'codemirror/addon/search/search', | |
15 | 'codemirror/keymap/emacs', |
|
15 | 'codemirror/keymap/emacs', | |
16 | 'codemirror/keymap/sublime', |
|
16 | 'codemirror/keymap/sublime', | |
17 | 'codemirror/keymap/vim', |
|
17 | 'codemirror/keymap/vim', | |
18 | ], |
|
18 | ], | |
19 | function($, |
|
19 | function($, | |
20 | utils, |
|
20 | utils, | |
21 | CodeMirror |
|
21 | CodeMirror | |
22 | ) { |
|
22 | ) { | |
23 | "use strict"; |
|
23 | "use strict"; | |
24 |
|
24 | |||
25 | var Editor = function(selector, options) { |
|
25 | var Editor = function(selector, options) { | |
26 | var that = this; |
|
26 | var that = this; | |
27 | this.selector = selector; |
|
27 | this.selector = selector; | |
28 | this.contents = options.contents; |
|
28 | this.contents = options.contents; | |
29 | this.events = options.events; |
|
29 | this.events = options.events; | |
30 | this.base_url = options.base_url; |
|
30 | this.base_url = options.base_url; | |
31 | this.file_path = options.file_path; |
|
31 | this.file_path = options.file_path; | |
32 | this.config = options.config; |
|
32 | this.config = options.config; | |
33 | this.codemirror = new CodeMirror($(this.selector)[0]); |
|
33 | this.codemirror = new CodeMirror($(this.selector)[0]); | |
34 | this.generation = -1; |
|
34 | this.generation = -1; | |
35 |
|
35 | |||
36 | // It appears we have to set commands on the CodeMirror class, not the |
|
36 | // It appears we have to set commands on the CodeMirror class, not the | |
37 | // instance. I'd like to be wrong, but since there should only be one CM |
|
37 | // instance. I'd like to be wrong, but since there should only be one CM | |
38 | // instance on the page, this is good enough for now. |
|
38 | // instance on the page, this is good enough for now. | |
39 | CodeMirror.commands.save = $.proxy(this.save, this); |
|
39 | CodeMirror.commands.save = $.proxy(this.save, this); | |
40 |
|
40 | |||
41 | this.save_enabled = false; |
|
41 | this.save_enabled = false; | |
42 |
|
42 | |||
43 | this.config.loaded.then(function () { |
|
43 | this.config.loaded.then(function () { | |
44 | // load codemirror config |
|
44 | // load codemirror config | |
45 | var cfg = that.config.data.Editor || {}; |
|
45 | var cfg = that.config.data.Editor || {}; | |
46 | var cmopts = $.extend(true, {}, // true = recursive copy |
|
46 | var cmopts = $.extend(true, {}, // true = recursive copy | |
47 | Editor.default_codemirror_options, |
|
47 | Editor.default_codemirror_options, | |
48 | cfg.codemirror_options || {} |
|
48 | cfg.codemirror_options || {} | |
49 | ); |
|
49 | ); | |
50 |
that.set_codemirror_options(cmopts |
|
50 | that._set_codemirror_options(cmopts); | |
51 | that.events.trigger('config_changed.Editor', {config: that.config}); |
|
51 | that.events.trigger('config_changed.Editor', {config: that.config}); | |
52 | }); |
|
52 | }); | |
53 | }; |
|
53 | }; | |
54 |
|
54 | |||
55 | // default CodeMirror options |
|
55 | // default CodeMirror options | |
56 | Editor.default_codemirror_options = { |
|
56 | Editor.default_codemirror_options = { | |
57 | extraKeys: { |
|
57 | extraKeys: { | |
58 | "Tab" : "indentMore", |
|
58 | "Tab" : "indentMore", | |
59 | }, |
|
59 | }, | |
60 | indentUnit: 4, |
|
60 | indentUnit: 4, | |
61 | theme: "ipython", |
|
61 | theme: "ipython", | |
62 | lineNumbers: true, |
|
62 | lineNumbers: true, | |
63 | }; |
|
63 | }; | |
64 |
|
64 | |||
65 | Editor.prototype.load = function() { |
|
65 | Editor.prototype.load = function() { | |
66 | /** load the file */ |
|
66 | /** load the file */ | |
67 | var that = this; |
|
67 | var that = this; | |
68 | var cm = this.codemirror; |
|
68 | var cm = this.codemirror; | |
69 | return this.contents.get(this.file_path, {type: 'file', format: 'text'}) |
|
69 | return this.contents.get(this.file_path, {type: 'file', format: 'text'}) | |
70 | .then(function(model) { |
|
70 | .then(function(model) { | |
71 | cm.setValue(model.content); |
|
71 | cm.setValue(model.content); | |
72 |
|
72 | |||
73 | // Setting the file's initial value creates a history entry, |
|
73 | // Setting the file's initial value creates a history entry, | |
74 | // which we don't want. |
|
74 | // which we don't want. | |
75 | cm.clearHistory(); |
|
75 | cm.clearHistory(); | |
76 |
|
76 | |||
77 | // Find and load the highlighting mode |
|
77 | // Find and load the highlighting mode | |
78 | utils.requireCodeMirrorMode(model.mimetype, function(spec) { |
|
78 | utils.requireCodeMirrorMode(model.mimetype, function(spec) { | |
79 | var mode = CodeMirror.getMode({}, spec); |
|
79 | var mode = CodeMirror.getMode({}, spec); | |
80 | cm.setOption('mode', mode); |
|
80 | cm.setOption('mode', mode); | |
81 | }); |
|
81 | }); | |
82 | that.save_enabled = true; |
|
82 | that.save_enabled = true; | |
83 | that.generation = cm.changeGeneration(); |
|
83 | that.generation = cm.changeGeneration(); | |
84 | }, |
|
84 | }, | |
85 | function(error) { |
|
85 | function(error) { | |
86 | cm.setValue("Error! " + error.message + |
|
86 | cm.setValue("Error! " + error.message + | |
87 | "\nSaving disabled."); |
|
87 | "\nSaving disabled."); | |
88 | that.save_enabled = false; |
|
88 | that.save_enabled = false; | |
89 | } |
|
89 | } | |
90 | ); |
|
90 | ); | |
91 | }; |
|
91 | }; | |
92 |
|
92 | |||
93 | Editor.prototype.save = function() { |
|
93 | Editor.prototype.save = function() { | |
94 | /** save the file */ |
|
94 | /** save the file */ | |
95 | if (!this.save_enabled) { |
|
95 | if (!this.save_enabled) { | |
96 | console.log("Not saving, save disabled"); |
|
96 | console.log("Not saving, save disabled"); | |
97 | return; |
|
97 | return; | |
98 | } |
|
98 | } | |
99 | var model = { |
|
99 | var model = { | |
100 | path: this.file_path, |
|
100 | path: this.file_path, | |
101 | type: 'file', |
|
101 | type: 'file', | |
102 | format: 'text', |
|
102 | format: 'text', | |
103 | content: this.codemirror.getValue(), |
|
103 | content: this.codemirror.getValue(), | |
104 | }; |
|
104 | }; | |
105 | var that = this; |
|
105 | var that = this; | |
106 | // record change generation for isClean |
|
106 | // record change generation for isClean | |
107 | this.generation = this.codemirror.changeGeneration(); |
|
107 | this.generation = this.codemirror.changeGeneration(); | |
108 | return this.contents.save(this.file_path, model).then(function() { |
|
108 | return this.contents.save(this.file_path, model).then(function() { | |
109 | that.events.trigger("save_succeeded.TextEditor"); |
|
109 | that.events.trigger("save_succeeded.TextEditor"); | |
110 | }); |
|
110 | }); | |
111 | }; |
|
111 | }; | |
112 |
|
112 | |||
113 | Editor.prototype._set_codemirror_options = function (options) { |
|
113 | Editor.prototype._set_codemirror_options = function (options) { | |
114 | // update codemirror options from a dict |
|
114 | // update codemirror options from a dict | |
115 | for (var opt in options) { |
|
115 | for (var opt in options) { | |
|
116 | if (!options.hasOwnProperty(opt)) { | |||
|
117 | continue; | |||
|
118 | } | |||
|
119 | console.log(opt, options[opt]); | |||
116 | this.codemirror.setOption(opt, options[opt]); |
|
120 | this.codemirror.setOption(opt, options[opt]); | |
117 | } |
|
121 | } | |
118 | }; |
|
122 | }; | |
119 |
|
123 | |||
120 | Editor.prototype.update_codemirror_options = function (options) { |
|
124 | Editor.prototype.update_codemirror_options = function (options) { | |
121 | /** update codemirror options locally and save changes in config */ |
|
125 | /** update codemirror options locally and save changes in config */ | |
122 | var that = this; |
|
126 | var that = this; | |
123 | this._set_codemirror_options(options); |
|
127 | this._set_codemirror_options(options); | |
124 | return this.config.update({ |
|
128 | return this.config.update({ | |
125 | Editor: { |
|
129 | Editor: { | |
126 | codemirror_options: options |
|
130 | codemirror_options: options | |
127 | } |
|
131 | } | |
128 | }).then( |
|
132 | }).then( | |
129 | that.events.trigger('config_changed.Editor', {config: that.config}) |
|
133 | that.events.trigger('config_changed.Editor', {config: that.config}) | |
130 | ); |
|
134 | ); | |
131 | }; |
|
135 | }; | |
132 |
|
136 | |||
133 | return {Editor: Editor}; |
|
137 | return {Editor: Editor}; | |
134 | }); |
|
138 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now