/usr/lib/python2.7/dist-packages/notebook/static/edit/js/editor.js is in python-notebook 5.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | // Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'jquery',
'base/js/utils',
'codemirror/lib/codemirror',
'codemirror/mode/meta',
'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',
],
function(
$,
utils,
CodeMirror
) {
"use strict";
var Editor = function(selector, options) {
var that = this;
this.selector = selector;
this.clean = false;
this.contents = options.contents;
this.events = options.events;
this.base_url = options.base_url;
this.file_path = options.file_path;
this.config = options.config;
this.file_extension_modes = options.file_extension_modes || {};
this.codemirror = new CodeMirror($(this.selector)[0]);
this.codemirror.on('changes', function(cm, changes){
that._clean_state();
});
this.generation = -1;
// 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);
this.save_enabled = false;
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);
that.events.trigger('config_changed.Editor', {config: that.config});
if (cfg.file_extension_modes) {
// check for file extension in user preferences
var modename = cfg.file_extension_modes[that._get_file_extension()];
if (modename) {
var modeinfo = CodeMirror.findModeByName(modename);
if (modeinfo) {
that.set_codemirror_mode(modeinfo);
}
}
}
that._clean_state();
});
this.clean_sel = $('<div/>');
$('.last_modified').before(this.clean_sel);
this.clean_sel.addClass('dirty-indicator-dirty');
};
// default CodeMirror options
Editor.default_codemirror_options = {
extraKeys: {
"Cmd-Right": "goLineRight",
"End": "goLineRight",
"Cmd-Left": "goLineLeft",
"Tab": "indentMore",
"Shift-Tab" : "indentLess",
"Cmd-/" : "toggleComment",
"Ctrl-/" : "toggleComment",
},
indentUnit: 4,
theme: "ipython",
lineNumbers: true,
lineWrapping: true
};
Editor.prototype.load = function() {
/** load the file */
var that = this;
var cm = this.codemirror;
return this.contents.get(this.file_path, {type: 'file', format: 'text'})
.then(function(model) {
cm.setValue(model.content);
// Setting the file's initial value creates a history entry,
// which we don't want.
cm.clearHistory();
that._set_mode_for_model(model);
that.save_enabled = true;
that.generation = cm.changeGeneration();
that.events.trigger("file_loaded.Editor", model);
that._clean_state();
}).catch(
function(error) {
that.events.trigger("file_load_failed.Editor", error);
console.warn('Error loading: ', error);
cm.setValue("Error! " + error.message +
"\nSaving disabled.\nSee Console for more details.");
cm.setOption('readOnly','nocursor');
that.save_enabled = false;
}
);
};
Editor.prototype._set_mode_for_model = function (model) {
/** Set the CodeMirror mode based on the file model */
// Find and load the highlighting mode,
// first by mime-type, then by file extension
var modeinfo;
var ext = this._get_file_extension();
if (ext) {
// check if a mode has been remembered for this extension
var modename = this.file_extension_modes[ext];
if (modename) {
modeinfo = CodeMirror.findModeByName(modename);
}
}
// prioritize CodeMirror's filename identification
if (!modeinfo || modeinfo.mode === "null") {
modeinfo = CodeMirror.findModeByFileName(model.name);
// codemirror's filename identification is case-sensitive.
// try once more with lowercase extension
if (!modeinfo && ext) {
// CodeMirror wants lowercase ext without leading '.'
modeinfo = CodeMirror.findModeByExtension(ext.slice(1).toLowerCase());
}
}
if (model.mimetype && (!modeinfo || modeinfo.mode === "null")) {
// mimetype is not set on file rename
modeinfo = CodeMirror.findModeByMIME(model.mimetype);
}
if (modeinfo) {
this.set_codemirror_mode(modeinfo);
}
};
Editor.prototype.set_codemirror_mode = function (modeinfo) {
/** set the codemirror mode from a modeinfo struct */
var that = this;
utils.requireCodeMirrorMode(modeinfo, function () {
that.codemirror.setOption('mode', modeinfo.mime);
that.events.trigger("mode_changed.Editor", modeinfo);
}, function(err) {
console.log('Error getting CodeMirror mode: ' + err);
});
};
Editor.prototype.save_codemirror_mode = function (modeinfo) {
/** save the selected codemirror mode for the current extension in config */
var update_mode_map = {};
var ext = this._get_file_extension();
// no extension, nothing to save
// TODO: allow remembering no-extension things like Makefile?
if (!ext) return;
update_mode_map[ext] = modeinfo.name;
return this.config.update({
Editor: {
file_extension_modes: update_mode_map,
}
});
};
Editor.prototype.get_filename = function () {
return utils.url_path_split(this.file_path)[1];
};
Editor.prototype._get_file_extension = function () {
/** return file extension *including* .
Returns undefined if no extension is found.
*/
var filename = this.get_filename();
var ext_idx = filename.lastIndexOf('.');
if (ext_idx < 0) {
return;
} else {
return filename.slice(ext_idx);
}
};
Editor.prototype.rename = function (new_name) {
/** rename the file */
var that = this;
var parent = utils.url_path_split(this.file_path)[0];
var new_path = utils.url_path_join(parent, new_name);
return this.contents.rename(this.file_path, new_path).then(
function (model) {
that.file_path = model.path;
that.events.trigger('file_renamed.Editor', model);
that._set_mode_for_model(model);
that._clean_state();
}
);
};
Editor.prototype.save = function () {
/** save the file */
if (!this.save_enabled) {
console.log("Not saving, save disabled");
return;
}
var model = {
path: this.file_path,
type: 'file',
format: 'text',
content: this.codemirror.getValue(),
};
var that = this;
// record change generation for isClean
this.generation = this.codemirror.changeGeneration();
that.events.trigger("file_saving.Editor");
return this.contents.save(this.file_path, model).then(function(data) {
that.events.trigger("file_saved.Editor", data);
that._clean_state();
});
};
Editor.prototype._clean_state = function(){
var clean = this.codemirror.isClean(this.generation);
if (clean === this.clean){
return;
} else {
this.clean = clean;
}
if(clean){
this.events.trigger("save_status_clean.Editor");
this.clean_sel.attr('class','dirty-indicator-clean').attr('title','No changes to save');
} else {
this.events.trigger("save_status_dirty.Editor");
this.clean_sel.attr('class','dirty-indicator-dirty').attr('title','Unsaved changes');
}
};
Editor.prototype._set_codemirror_options = function (options) {
// update codemirror options from a dict
var codemirror = this.codemirror;
$.map(options, function (value, opt) {
if (value === null) {
value = CodeMirror.defaults[opt];
}
codemirror.setOption(opt, value);
});
var that = this;
};
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})
);
};
return {Editor: Editor};
});
|