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