Merge pull request #162 from juhasch/codefolding_2x

Update to use relative path CSS loading + cleanup
This commit is contained in:
Juergen Hasch
2015-01-24 11:23:21 +01:00
3 changed files with 22 additions and 68 deletions
+22 -15
View File
@@ -1,14 +1,18 @@
// Allow codefolding in code cells
var codefolding_extension = (function() {
define([
'require',
'components/codemirror/addon/fold/foldgutter',
'components/codemirror/addon/fold/foldcode',
'components/codemirror/addon/fold/brace-fold',
'components/codemirror/addon/fold/indent-fold'
], function(require) {
"use strict";
if (IPython.version[0] != 2) {
console.log("This extension requires IPython 2.x")
return
}
var hotKey = "Alt-F";
function toggleFolding(cm) {
var pos = cm.getCursor();
var opts = cm.state.foldGutter.options;
@@ -26,7 +30,7 @@ var codefolding_extension = (function() {
var ret = {};
var len = arguments.length;
for (var i=0; i<len; i++) {
for (var p in arguments[i]) {
for (p in arguments[i]) {
if (arguments[i].hasOwnProperty(p)) {
ret[p] = arguments[i][p];
}
@@ -99,7 +103,7 @@ var codefolding_extension = (function() {
* Add codefolding to existing cells
*
*/
var initExtension = function(event) {
var initGutter = function(event) {
var cells = IPython.notebook.get_cells();
for(var i in cells){
var cell = cells[i];
@@ -118,7 +122,12 @@ var codefolding_extension = (function() {
$([IPython.events]).on('create.Cell',createCell);
}
/* Load my own CSS file */
/**
* Load my own CSS file
*
* @param name add CSS file
*
*/
var load_css = function (name) {
var link = document.createElement("link");
link.type = "text/css";
@@ -126,13 +135,11 @@ var codefolding_extension = (function() {
link.href = require.toUrl(name);
document.getElementsByTagName("head")[0].appendChild(link);
};
load_css("/nbextensions/usability/codefolding/foldgutter.css");
load_css('components/codemirror/addon/fold/foldgutter.css');
/* change default gutter width */
load_css( './foldgutter.css');
/* additional custom codefolding mode */
require(['./firstline-fold'], initGutter)
/* load codemirror addon */
/* BUG in '/static/components/codemirror/addon/fold/indent-fold.js' will crash extension at reaload, use local copy */
require(['/static/components/codemirror/addon/fold/foldcode.js',
'/static/components/codemirror/addon/fold/foldgutter.js',
'/nbextensions/usability/codefolding/indent-fold.js',
'/static/components/codemirror/addon/fold/brace-fold.js',
'/nbextensions/usability/codefolding/firstline-fold.js'], initExtension)
})();
});
-22
View File
@@ -1,26 +1,4 @@
.CodeMirror-foldmarker {
color: blue;
text-shadow: #b9f 1px 1px 2px, #b9f -1px -1px 2px, #b9f 1px -1px 2px, #b9f -1px 1px 2px;
font-family: arial;
line-height: .3;
cursor: pointer;
}
.CodeMirror-foldgutter {
width: .9em;
}
.CodeMirror-foldgutter-open,
.CodeMirror-foldgutter-folded {
color: #555;
cursor: pointer;
}
.CodeMirror-foldgutter-open:after {
content: "\25BE";
}
.CodeMirror-foldgutter-folded:after {
content: "\25B8";
}
-31
View File
@@ -1,31 +0,0 @@
CodeMirror.registerHelper("fold", "indent", function(cm, start) {
var tabSize = cm.getOption("tabSize"), firstLine = cm.getLine(start.line);
if (!/\S/.test(firstLine)) return;
var getIndent = function(lineNum) {
if (lineNum == undefined) { return 0; }
return CodeMirror.countColumn(lineNum, null, tabSize);
};
var myIndent = getIndent(firstLine);
var lastLineInFold = null;
// Go through lines until we find a line that definitely doesn't belong in
// the block we're folding, or to the end.
for (var i = start.line + 1, end = cm.lastLine(); i <= end; ++i) {
var curLine = cm.getLine(i);
var curIndent = getIndent(curLine);
if (curIndent > myIndent) {
// Lines with a greater indent are considered part of the block.
lastLineInFold = i;
} else if (!/\S/.test(curLine)) {
// Empty lines might be breaks within the block we're trying to fold.
} else {
// A non-empty line at an indent equal to or less than ours marks the
// start of another block.
break;
}
}
if (lastLineInFold) return {
from: CodeMirror.Pos(start.line, firstLine.length),
to: CodeMirror.Pos(lastLineInFold, cm.getLine(lastLineInFold).length)
};
});
CodeMirror.indentRangeFinder = CodeMirror.fold.indent; // deprecated