##// END OF EJS Templates
editor progress...
editor progress - load/store codemirror config - fill out menus a bit: - Edit/find,replace - View/toggle line numbers

File last commit:

r19303:c57e5636
r19303:c57e5636
Show More
editor.js
130 lines | 4.2 KiB | application/javascript | JavascriptLexer
Thomas Kluyver
Refactor editor into Editor class
r19013 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'jquery',
'base/js/utils',
'codemirror/lib/codemirror',
'codemirror/mode/meta',
Min RK
editor progress...
r19303 'codemirror/addon/comment/comment',
'codemirror/addon/dialog/dialog',
'codemirror/addon/edit/closebrackets',
'codemirror/addon/edit/matchbrackets',
'codemirror/addon/search/searchcursor',
'codemirror/addon/search/search',
'codemirror/keymap/emacs',
'codemirror/keymap/sublime',
'codemirror/keymap/vim',
Thomas Kluyver
Refactor editor into Editor class
r19013 ],
function($,
utils,
CodeMirror
) {
Min RK
editor progress...
r19303 "use strict";
Thomas Kluyver
Refactor editor into Editor class
r19013 var Editor = function(selector, options) {
Min RK
editor progress...
r19303 var that = this;
Thomas Kluyver
Refactor editor into Editor class
r19013 this.selector = selector;
this.contents = options.contents;
this.events = options.events;
this.base_url = options.base_url;
this.file_path = options.file_path;
Min RK
editor progress...
r19303 this.config = options.config;
this.codemirror = new CodeMirror($(this.selector)[0]);
Thomas Kluyver
Refactor editor into Editor class
r19013
Thomas Kluyver
Better way of saving through CodeMirror...
r19020 // It appears we have to set commands on the CodeMirror class, not the
// instance. I'd like to be wrong, but since there should only be one CM
// instance on the page, this is good enough for now.
CodeMirror.commands.save = $.proxy(this.save, this);
Thomas Kluyver
Update text editor for new contents API
r19015 this.save_enabled = false;
Min RK
editor progress...
r19303
this.config.loaded.then(function () {
// load codemirror config
var cfg = that.config.data.Editor || {};
var cmopts = $.extend(true, {}, // true = recursive copy
Editor.default_codemirror_options,
cfg.codemirror_options || {}
);
that.set_codemirror_options(cmopts, false);
that.events.trigger('config_changed.Editor', {config: that.config});
});
};
// default CodeMirror options
Editor.default_codemirror_options = {
extraKeys: {
"Tab" : "indentMore",
},
indentUnit: 4,
theme: "ipython",
lineNumbers: true,
Thomas Kluyver
Refactor editor into Editor class
r19013 };
Editor.prototype.load = function() {
Min RK
editor progress...
r19303 /** load the file */
Thomas Kluyver
Update text editor for new contents API
r19015 var that = this;
Thomas Kluyver
Refactor editor into Editor class
r19013 var cm = this.codemirror;
Min RK
editor progress...
r19303 return this.contents.get(this.file_path, {type: 'file', format: 'text'})
Thomas Kluyver
Update text editor for new contents API
r19015 .then(function(model) {
cm.setValue(model.content);
Scott Sanderson
BUG: Prevent users from undoing the initial document load with CTRL-Z....
r19089
// Setting the file's initial value creates a history entry,
// which we don't want.
cm.clearHistory();
Thomas Kluyver
Update text editor for new contents API
r19015 // Find and load the highlighting mode
Nicholas Bollweg (Nick)
more fidgeting before starting over
r19290 utils.requireCodeMirrorMode(model.mimetype, function(spec) {
var mode = CodeMirror.getMode({}, spec);
cm.setOption('mode', mode);
});
Thomas Kluyver
Update text editor for new contents API
r19015 that.save_enabled = true;
},
function(error) {
cm.setValue("Error! " + error.message +
"\nSaving disabled.");
that.save_enabled = false;
Thomas Kluyver
Refactor editor into Editor class
r19013 }
Thomas Kluyver
Update text editor for new contents API
r19015 );
Thomas Kluyver
Refactor editor into Editor class
r19013 };
Editor.prototype.save = function() {
Min RK
editor progress...
r19303 /** save the file */
Thomas Kluyver
Update text editor for new contents API
r19015 if (!this.save_enabled) {
console.log("Not saving, save disabled");
return;
}
Thomas Kluyver
Refactor editor into Editor class
r19013 var model = {
Thomas Kluyver
Update text editor for new contents API
r19015 path: this.file_path,
Thomas Kluyver
Refactor editor into Editor class
r19013 type: 'file',
format: 'text',
content: this.codemirror.getValue(),
};
var that = this;
Min RK
editor progress...
r19303 return this.contents.save(this.file_path, model).then(function() {
Thomas Kluyver
Use NotificationArea in the text editor
r19017 that.events.trigger("save_succeeded.TextEditor");
Thomas Kluyver
Refactor editor into Editor class
r19013 });
};
Min RK
editor progress...
r19303
Editor.prototype._set_codemirror_options = function (options) {
// update codemirror options from a dict
for (var opt in options) {
this.codemirror.setOption(opt, options[opt]);
}
};
Editor.prototype.update_codemirror_options = function (options) {
/** update codemirror options locally and save changes in config */
var that = this;
this._set_codemirror_options(options);
return this.config.update({
Editor: {
codemirror_options: options
}
}).then(
that.events.trigger('config_changed.Editor', {config: that.config})
);
};
Thomas Kluyver
Refactor editor into Editor class
r19013
return {Editor: Editor};
});